Help me with modifying player inventory slot

Started by Ziadod on

Topic category: Help with Minecraft modding (Java Edition)

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Help me with modifying player inventory slot

So I have been 5 hours trying to figure out how to get a random slot in the player's inventory each time he breaks a block and apply the sharpness I enchantment on that randomly chosen slot if it has an item and not empty. It just won't work; no errors, but it won't work.
Can anyone tell me what is wrong with my code and how to fix it? Here is my code:

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
so, in mcreator when you set…
Tue, 10/14/2025 - 21:40

so, in mcreator when you set an item to a variable, it actually copies it.

so you're not modifying the item by modifying the variable,

try using this block at the end,

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Thanks! This works, but I…
Wed, 10/15/2025 - 10:27

Thanks! This works, but I need help with something else, so here is my current code:
sThe issue right now is that if a player has a stack of items, like 64 of them, it gets replaced with just one enchanted item. I want the amount to stay the same and just get enchanted.

I also don’t want it to apply only Sharpness. I want it to pick from any enchantment available in Minecraft 1.21.1 and earlier. How can I make that work?

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
you can set OriginalAmount…
Wed, 10/15/2025 - 13:59

you can set OriginalAmount to 

 

maybe you can use this to get a random enchantment,

if not you can use a custom snippet,

if(!world.isClientSide()) {
			Optional<Holder.Reference<Enchantment>> enchantmentHolder = world.registryAccess()
					.lookupOrThrow(Registries.ENCHANTMENT)
					.getRandom(world.getRandom());
			if(enchantmentHolder.isPresent()) {
				ChosenItem.enchant(enchantmentHolder.get(), EnchantmentLevel);
			}
		}

 

Joined Oct 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Alright, everything is…
Wed, 10/15/2025 - 20:29

Alright, everything is working perfectly. Thank you for your help! I still have a few more questions about something else, but I’ll create a new post since this isn’t the right place for them.