Started by
Mister_Pavel
on
Topic category: Help with Minecraft modding (Java Edition)
I don't understand how to make sure that when a player kills any entity and he has a certain item in the inventory, the particles I need appear in place of the deceased entity and some percentage of the health of the deceased entity is added to the player. Please help me how to do this? I didn't find a procedure-"when a player kills an entity".
Hey there! I might be a new modder, but I did want something similar to this. I'll see if I can edit my code to generate one that helps you as well, but it might take me a bit.
Kind Regards,
Primeval
P.S: By a bit I mean less than an hour hopefully, I'm just loading up a very slow mcreator now. I can give you some basic info to what I did with my code when I get in there, but it wont require too much coding. I will send you a screenshot of the codes I used and tested when it's done.
Thank you, I'm glad someone answered me, (I thought they wouldn't answer me at all)
I'm in now, and I'll give ya the rundown of what you need to do to start with.
To explain a little bit about what I did which may help you as it is also item related, here's a code I used.
Hopefully the picture has loaded for you, but this is the code I've been using. As a precaution I will also display the code I used here, since this code can be a bit of a puzzle and it also relates to killing entities. I'm also going to explain the code and what I did, as I don't know if you understand source entity stuff yet.
Note: The first line of code about the subtype of player is to allow this to only happen to players.
One of the first things you will want to do is to get out the 'If do' block from Flow Control, which will be a very basic starting point to create a procedure. You might know about this already, but I don't know what you know and what you don't, so I'd rather cover all bases.
The next thing you'll want to do is go into 'Player Procedures' and drag 'Has [entity] in inventory [item]'. You'll then want to change the 'Event/target entity' part of that to 'Source entity' via the minecraft component section. The source entity in this case is your player. It should look something like what I've displayed.
Note: Was going to make this into one lengthy post but this you'll need to do generally for what you want, and I'll test some stuff out in the meantime.
Damn... pictures didn't work. But that isn't a big problem.
Anyways, I think I've done what you've asked with a very basic procedure. I hope this helps, but I can't show due to the darn picture thing not working. Feel free to tweak this to your liking - duh - and enjoy your time modding. Back to the tutorial!
For my example, I've used a block to hold in my inventory rather than an item. We'll get to why I did this later, but this can be any object; block, item, armour and so on. This leads onto the next step; making particles appear with code.
Note: There are two ways to do this. The one I did was simply to place lightning in a location.
If you want more information on the other way, let me know, but we'll go through what I did. Now, we're going to be focusing on the first 'do' section of our if do block. You'll now want to drag 'Strike lightning at x: [] y: [] z: []' block from world management and put it next to the 'do' part of our block. We're telling Minecraft we want to strike lightning in a specific location, and our location? Our event entity, which we are attacking. We'll dive into how we're going to tell MC what entity we're attacking later.
So now we have lightning in the procedure, the effect is optional. Basically, the effect means if it damages the entities around or not. But that begs the question; how are we supposed to subtract health from the deceased animal? That part requires a line more of coding, and math.
This time, we're going to be adding the exact same thing from the start; another 'if do' block. This'll be put under our lightning program, so we now have something that has two if blocks. The next step - and certainly the easiest way, possibly the only way - is a little complicated.
We first need to stop by 'Logic', where we'll grab a '[] = []' block. You then want to go into 'Entity Data', where we will also get a 'Current health of []' block. Put your 'Current health' block in the first part of the '[] = []' block, so you get something like this;
Then, you'll want to stop by the 'Math' segment. Grab the block that only says a number (It most likely will say 0) and put it on the other end of our block. Now it should look like this;
If you've made it this far, you've only got two bits to go. Well done!
Our penultimate part will be to set a potion effect up, and you can do this in two ways. The way I prefer - so the user doesn't particularly know and the effect lasts less - is to put the 'Add potion [] with level [] for [] ticks ambient [false] particles [false]', which is directed to our source entity. In other words, instead of our event/target entity, change the end to source entity once more. You can then set the potion effect, how long it lasts in ticks and the strength of the potion.
All in all, it should look something like this;
But there is one thing we need to do before we finish up, and that's set up the trigger; what causes the actual event to happen. Make sure to save and name the procedure before you do, then go to the 'Triggers' tab of your tool/item. Click on the 'When entity is hit with item' trigger, then select your procedure. You're now good to test it and it should work, hopefully as well as mine did!
sorry, but I meant a little wrong, but it also helped. I didn't mean lightning, but the fact that when a player has a "certain" item in his inventory and he kills an entity, then a particle appeared at the place where the entity died and the percentage of health of the deceased entity was added to the player. like if a player kills someone who has 100 health, having this "certain" item in the inventory, then the particle I need will appear at the place of death of the entity and the player will be treated for 15% of the full health of the already deceased entity, that is, for +15 heath, and if he kills the entity with 10 health, then the player will get +1.5 heath.
and I also wanted to ask, since the command says-(if a player has an "item" in the inventory, then-"strike lightning at x: [x] y: [y] z: [z]"), does this mean that lightning will always strike while the item is in the inventory?
This is how I did:
Event trigger - trigged by external call
or when (global trigger): No additional trigger
IF Has [Source entity] in inventory [item]
DO : IF Current health [Event/target entity] = [0]
DO Spawn single particle at x: [X position of [Event/target entity]]
y: [Y position of [Event/target entity]]
z: [Z position of [Event/target entity]]
with vx: [0]
vy: [0]
vz: [0]
type: SCULK_SOUL
Set health of [Source entity] to [{Current health of[Source entity]} + [2]]
but this command is inserted only into the trigger-"When living entity is hit with item", but I don't want when a player kills an entity, the item was in his hand, I want the item to just be somewhere in the inventory and the player get 15% health from dead entities
Oof- but at least it partially helped. I was using lightning as an example effect since I was just quite quickly putting it together, but as for actually taking away health? I think it requires actual coding knowledge. I don't know if it can be done in mcreator. Also, the code should work with the object anywhere in the inventory; or maybe I got that wrong? I'll have a look into it either way.
As for the lightning strikes, only when the entity is hit with the object.
the option with the effect of the treatment potion is also good, but I checked my team and it doesn't seem to work, even when the item is in my hand or in the inventory, I didn't notice particles at the place of death of entities
also, does lightning strike the player?
yes, I want the item to be not in my hand, but somewhere in the inventory, and when an entity is killed at the place of its death, a particle is spawned and the player simply gets the same amount of health that I will indicate in the team (you can not take percentages, so they are not needed). Also, does lightning strike the player or in entity?
particles do not appear ):