How do I remove the Item in a target's main hand and add it to my inventory.

Started by DoYmN on

Topic category: Help with modding (Java Edition)

Last seen on 17:09, 25. Dec 2023
Joined Nov 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How do I remove the Item in a target's main hand and add it to my inventory.

I'm working on a procedure for an enchantment which can pull specific items out of a players main hand and add them to your inventory.

Does someone have an idea how I could do that?

Last seen on 05:40, 28. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Shouldn't be too difficult…
Fri, 12/08/2023 - 12:35

Shouldn't be too difficult. I assume this goes on a weapon? The basic idea would be as follows:

  • Make a procedure with the global trigger  'before entity is hurt'
  • Make a local itemstack variable for the procedure. (I'll call it 'stolen_item")
  • Then use an 'if' bracket to check that the item in the main hand of the event/target entity is not air, and the item in the main hand of the source-entity, (the one attacking), has your custom enchantment.
  • Use the 'do with 70% chance' procedure template, and configure it to whatever you want the chance of this happening to be. 
  • If the first check and the random chance succeed, do the following, in this order:
    • Set 'stolen-item' to the item in the main slot of the target entity.
    • Use the 'set item in main hand' function to set the item in the main hand of the target entity to air. Alternatively, use the 'shrink-itemstack' function to decrease the number of items in the entity's main hand by one, if you want this to work for stacks of items. (You would need an additional number variable to steal full stacks without changing the number of items in those stacks.)
    • Use the 'set item in inventory' function to set one 'stolen-item' in the source entity's inventory.
Last seen on 18:13, 28. Dec 2023
Joined Oct 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can I use this with my …
Sat, 12/09/2023 - 02:18

Can I use this with my /steal command? If so, would it work with multiple entities such as a horde of zombies all with iron shovels and the command would steal all of them?

Last seen on 05:40, 28. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
It would be more difficult…
Sat, 12/09/2023 - 03:21

It would be more difficult to get this working with a command, as you would need to provide multiple entities as dependencies. (The target and the source entity.) You could still make a more general steal function that you could reference from other functions, if you don't want to rewrite it constantly.