How to remove random item from a players inventory

Started by kajto359 on

Topic category: Help with modding (Java Edition)

Last seen on 15:45, 1. May 2024
Joined Mar 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to remove random item from a players inventory

How do I remove a random item from a players inventory? i have a mob thats supposted to remove your items upon touch, and i have no idea how to set the remove block to remove a random item

Last seen on 08:23, 17. May 2024
Joined Feb 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
In the Entity procedures…
Thu, 05/02/2024 - 09:35

In the Entity procedures/Actions tab, you can find a loop block called 'For each slot of inventory of [Entity] get [Item stack Iterator].

First create a local number variable, and call it 'itemIndex'. Create another one called 'chosenItem'.

Next, use the each slot of inventory block and put an if block in the loop, and set it's condition to determine, 'If [itemstack iterator] = NOT air', and put a "Set number 'itemIndex' to (get 'itemIndex' + 1)". This will add 1 to the 'itemIndex' for every non-empty slots in your inventory. Make sure that in the if statement, you are using itemstack iterator, instead of provided itemstack (you can find the block in the 'Minecraft components' tab).

After this, we need to determine which itemstack we wish to remove. Use "Set number 'chosenItem'", and set it to 'Random integer between min [1] and max [itemIndex] included'. This will choose a random number between 1 through to however many actual items are in your inventory.

Next, using the set number block, set 'itemIndex' to 0. This is just to reset our counting variable.

Lastly, we are going to use the action we used at the start, the each slot of inventory loop. Duplicate the whole if block we used at the start (the one that checks for air), with the block changing 'itemIndex'. Below, Put an if block, and set it's condition to "if 'itemIndex' = 'chosenItem'". This will check if the current item it is currently looping through, is our randomly chosen item.

Inside this if block, place a 'shrink [itemstack iterator] by [1]' (making sure that it is itemstack iterator and not provided itemstack). This way, it reduces the randomly chosen item by 1. You can customize this to your liking, if you want it to shrink by more items, change the shrink value by more. If you want to completely make the itemstack dissapear (like, turning 64 diamonds into air, rather than 63 diamonds), replace it with a 'Set number of items in [itemstack iterator] to 0'

 

Here is a picture of what the finished product should look like:

If that doesn't load, try this link:

https://imgur.com/a/C07IgZD