Started by
kajto359
on
Topic category: Help with Minecraft modding (Java Edition)
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
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
i know this thread is probably long dead, but i tried to follow the steps provided along with the image as a guide and can't seem to get the code to work when i try to trigger it with the event trigger, of *when player colides with this entity
it seems it is working but is reading the inventory of the mob instead of the players
If it's reading off the wrong entity, then you're probably using the wrong entity to begin with.
The solution is to use 'Source entity' instead of 'Event/target entity' in the for loop BUT I reccomend reading my explanation so you can understand why it works :)
Most of the time, when coding procedures with a provided entity dependency, you'll have trained yourself to use the 'Event/target entity' dependency to refer to 'the entity im thinking about'. It works a lot of the time, but unfortunately code can't read our minds.
The 'Event/target entity' refers to the entity that the action happened to. For example, let's say a player gets shot by a skeleton. The 'Event/target entity' is the player; they were the one who got shot. But you'd also be given the 'Source entity' and the 'Immediate source entity' dependency as you can see in the provided dependencies list.
The 'Source entity' is quite literally the source of the interaction, or better put what caused it, which in the mockup example is the Skeleton. The 'Immediate source entity' is the source by technicality, what directly caused it. Think, the arrow entity that hit the player instead of the skeleton. It's probably not what you're looking for, but at least you get a reference to it. (when/if you do line coding these names and concepts become arbitrary but they're nice to know for mcreator)
So, when a mob touches your player, if you're providing the dependencies from the mob's POV (e.g. when the mob collides with a player), the Event/target entity will be the mob, and the Source entity (the one that 'caused it) will be the player. In fact, as a good rule of thumb, when linking a procedure to a custom mob, the Event/target entity will always be the custom mob in question.
That being said, iirc the procedure would get called every tick that you are colliding with the entity. It might not, but if it does then a full inventory would get drained in about a second and a half, so you might need to add preventions to the mob so that it only does it once. If that's the case, it could be a good exercise for you!
Hope this helps! :)
thank you, this really dose help, i am now working on trying to add the preventions so that the mob stops draining the entire inventory of the player.
if i could get pointers on where to begin with stopping it from happening every tick that would be greatly appreciated.
i have figured it out, basically by adding an item to the offhand of our little stealer when it takes an item, we can stop it from doing it again
Nicely done :)
But what if you touch the mob again?
as long as the item is in there offhand it can't take again from you, they also begin running away from you. After a set amount of time they are then reset to steal things again. If you managed to catch them during the chase, you can get the item back.