How to make it that if bob (entity i made) is near a player bob will replace existing items to corrupt items?

Started by ILoveNuggets on

Topic category: Help with modding (Java Edition)

Last seen on 16:20, 29. Jan 2024
Joined Nov 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to make it that if bob (entity i made) is near a player bob will replace existing items to corrupt items?

im making a annoying mod soo yeah i need help please

Last seen on 21:55, 20. Feb 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You'll need to use an…
Tue, 01/02/2024 - 12:52

You'll need to use an itemstack iterator to target things in the player's inventory. It would look something like this:

You should be able to do this using just one procedure, though you may also want to make a player-persistent number variable to act as a cooldown timer, so that the effect doesn't constantly replace items in the player's inventory, or use a fairly small random chance of this occurring.

The procedure should run on the update tick of your entity. You should first check that a player exists within a radius of your entity, (this is important, it will crash the game if you try to use 'nearest player' without a player in the given radius). Normally you would use an itemstack iterator to target every item in the player's inventory, but you also need to know which slot number to replace. Instead, use a 'repeat' bracket set to repeat 35 times, and add a local 'slot number' variable to keep track of what slot you're currently targeting. 

Inside this bracket, you should check that the item in the current slot number is replaceable, (I'd recommend just making a tag of which items can be replaced), and also run another random chance check. (A percentage chance that the item is replaced- the 'do with 70% chance' procedure template should work fine.) If the item is replaceable and the random chance passes, replace the item in the current slot number of the player's inventory with the corrupted variant. (If there are different corrupted variants of an item, you may want to make a separate procedure to return the correct corrupted variant.) Then set the 'slot number' variable to itself +1 so when it repeats, it does the same thing on the next slot.

This won't target armor slots and the offhand slot by default, but that's the basic idea.