Keep certain items upon death

Started by Ayreseraph on

Topic category: Help with modding (Java Edition)

Last seen on 21:23, 29. Mar 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Keep certain items upon death

So, I want to make some special items given to the players when they step on a pressure plate in the beginning of the game. They’ll keep these items throughout their time playing and can’t remove them from their inventory.

Of course because of this I also don’t want them to drop these items when they die either. 

I’m quite new to using MCreator, so terms like variable, and NBT are very new to me. So any step by step explanations are very appreciated.

Thanks - Ayres

P.S. depending on which pressure plate they step on, they’ll get a different assortment of items. Like a warrior with his spear, and a mage with his staff (example)

Last seen on 18:01, 13. Jun 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
There's a couple ways you…
Mon, 04/08/2024 - 11:13

There's a couple ways you could do this, sort of depends on what you're going for, and if you want the player to get more items like this later on! I did something kind of similar for a smithing mod. All of these will require you to use variables, which you can access in the left-hand tab of your workspace. Variables let you store information permanently, and can be altered using procedures- you want to make sure all your variables are player-based, not global. (Global variables are for the entire world, player variables are specifically tracked for each player.)

The first (and probably easiest) method would be to just assign players a 'class' variable, and then whenever they respawn, give them the items associated with that class. (A Knight just always gets a sword/shield and whatnot whenever they respawn.) You would save this as a player-persistent string variable. (It's important that it's player persistent, player-lifetime variables reset on death.)

The second, (and probably better) method would be to make variables to specifically track which items the player retains on death. You would first want to make a tag for 'permanent' items, and then a couple player persistent itemstack variables. Then you would make a procedure that, on update tick, checks each slot in the player's inventory, and, if the item is permanent, changes the first available itemstack variable to that item. (You can check every slot by making a repeat procedure that repeats 27 times, and increments a local variable by one each time to determine which slot to check.) This could get somewhat tedious depending on how many items you want the player to be able to retain- if you really want to do this properly, I recommend looking into the arraylist plugin, which lets you save list variables instead of just single itemstacks. This is definitely the better approach, as it means you can arbitrarily give new permanent items to the player over the course of the game. It's a little complicated for a beginner though, so I could send some pics of my procedures if that'd help. :)

To ensure these items aren't dropped on death, you can make a procedure that runs when the player dies, and then runs an entity iterator bracket that targets all entities in a large radius. If the entity iterator is an item, and the item is tagged as permanent, despawn it. There are some edge cases, (say if a player drops the item, and another player dies nearby), but this should work in most cases.