I made a post a while ago on the same issue but could not find it. I reworked my code and found time to test it.
I want the game to see if a player with a custom potion effect is within a certain distance of a player without it, and if they are, spread the effect to the second player. When tested, I get no errors, it just doesn't spread. None of the comments I placed in my code show up in the console either, so that is a problem.
Anyways, I have a couple of improvements you could make:
1. Do your "if(player1.getActivePotionEffect(EpidemicCraft.potionFlu) != null)" check before you search for the second player. There's no point in looking for the second player if the first doesn't have the effect. Reduce the unnecessary processing that needs to be done.
2. You're using "player1.worldObj.getClosestPlayerToEntity(player1, 2)" to get the closest player, however if you have more than 1 player near you, it would still only get the nearest. You may want to consider using something like gettting the players within a small AABB and looping through those.
You've created a custom potion effect, so why don't you just override the Potion#performEffect() method? Also check if the distance is less than the spread radius or EVERY player on the server will become affected by your potion (mabye just use an AABB around your player, like Bright_Spark mentioned).
You've created a custom potion effect, so why don't you just override the Potion#performEffect() method?
I've seen quite a few come here using tick events instead of that, but I don't know custom potion effects well enough to make suggestions (I've only made 1 and I don't know if I ever did it "correct").
I've seen quite a few come here using tick events instead of that, but I don't know custom potion effects well enough to make suggestions (I've only made 1 and I don't know if I ever did it "correct").
You've created a custom potion effect, so why don't you just override the Potion#performEffect() method? Also check if the distance is less than the spread radius or EVERY player on the server will become affected by your potion (mabye just use an AABB around your player, like Bright_Spark mentioned).
Ok, I got some of it to work! I gave myself the effect and it transmitted when I walked near another player.
One final issue though: the effect does not go away. It sticks at 49 seconds remaining and doesn't count down if I am outside of the 2 block area I gave, and sticks at 50 if I move back inside the area. Why would it not count down like a normal potion does?
You're not checking the entities against the player, so the player applies the effect to himself.
use World#getEntitiesInAABBExcluding().
It actually isn't changing the initial player's values. His count down like normal, but the secondary player has the locked value. He isn't applying the effect to himself, as far as I can tell.
EDIT: I think the issue may be that the event keeps getting triggered and keeps resetting the value back to the 1000 tick mark. I'll mess with that and see what I can do.
Side note: you can use player.getEntityBoundingBox().expand(int x, int y, int z) or player.getEntityBoundingBox().expandXyz(int) to easily get a resized bounding box.
Rollback Post to RevisionRollBack
Click this banner for a list of illegal mod distributors -- only download from legal sites!
So the issue is that the potion effect added to the second player does not count down or go away. It hangs at 50 seconds if he's inside the bounding box and 49 if he's outside. It appears that the 50 second number is coming from the amount of ticks I have set in the middle when the potion is added, but why it doesn't count down is beyond me.
I don't actually know why you're not doing all this in the potion's update method, but meh... I'll leave that to you unless you ask about it. EDIT: I also realise now that I've re-read the thread that it was mentioned before as well XD Sorry, I read a lot of threads.
The effect isn't counting down when players are nearby because you're not checking if they already have the effect. So what's going on, is say we have 2 players with the effect. They don't give themselves the effects constantly, because you're checking for that player. However, the players are still constantly giving each other the effect regardless of whether they already have it or not. So the effect just keeps getting set back to 50 seconds.
That makes sense, but the original player's effect counts down like normal from whatever it was at, even if a new player comes in and gets updated. Also, it only ticks down to 49 on the second player. It hangs at that point and doesn't decrease further. So it isn't updating the first player or the second one once they move away from each other.
So long as you've coded your potions correctly, the only thing I can think of that might cause this strange behaviour is that it might be only happening client side. Try only run the code on the server side and see if that fixes it.
Hello
I made a post a while ago on the same issue but could not find it. I reworked my code and found time to test it.
I want the game to see if a player with a custom potion effect is within a certain distance of a player without it, and if they are, spread the effect to the second player. When tested, I get no errors, it just doesn't spread. None of the comments I placed in my code show up in the console either, so that is a problem.
Here is the code:
http://pastebin.com/ckgTBeWF
If you have any idea what I did poorly, please let me know. I can give more information if you need it.
Thanks,
SchrodingersSpy
Have you registered your event handler?
Anyways, I have a couple of improvements you could make:
1. Do your "if(player1.getActivePotionEffect(EpidemicCraft.potionFlu) != null)" check before you search for the second player. There's no point in looking for the second player if the first doesn't have the effect. Reduce the unnecessary processing that needs to be done.
2. You're using "player1.worldObj.getClosestPlayerToEntity(player1, 2)" to get the closest player, however if you have more than 1 player near you, it would still only get the nearest. You may want to consider using something like gettting the players within a small AABB and looping through those.
You've created a custom potion effect, so why don't you just override the Potion#performEffect() method? Also check if the distance is less than the spread radius or EVERY player on the server will become affected by your potion (mabye just use an AABB around your player, like Bright_Spark mentioned).
I've seen quite a few come here using tick events instead of that, but I don't know custom potion effects well enough to make suggestions (I've only made 1 and I don't know if I ever did it "correct").
If this is hard-coded to only support players, why aren't you using PlayerTickEvent ?
It seems a little redundant (and possibly unreliable) to fire this for every EntityLivingBase when you only support players.
Ok, I got some of it to work! I gave myself the effect and it transmitted when I walked near another player.
One final issue though: the effect does not go away. It sticks at 49 seconds remaining and doesn't count down if I am outside of the 2 block area I gave, and sticks at 50 if I move back inside the area. Why would it not count down like a normal potion does?
Here is the new code piece:
http://pastebin.com/jfcq9hMu
Thank you so much for your help thus far!
SchrodingersSpy
You're not checking the entities against the player, so the player applies the effect to himself.
use World#getEntitiesInAABBExcluding().
It actually isn't changing the initial player's values. His count down like normal, but the secondary player has the locked value. He isn't applying the effect to himself, as far as I can tell.
EDIT: I think the issue may be that the event keeps getting triggered and keeps resetting the value back to the 1000 tick mark. I'll mess with that and see what I can do.
Side note: you can use player.getEntityBoundingBox().expand(int x, int y, int z) or player.getEntityBoundingBox().expandXyz(int) to easily get a resized bounding box.
Cool! I'll keep that in mind if I need to rework this chunk of code.
Bumping for a little help with my secondary problem.
Can you repeat the question and post updated code?
The only issue I'm seeing you might be talking about is the fact that the effect doesn't count down?
So the issue is that the potion effect added to the second player does not count down or go away. It hangs at 50 seconds if he's inside the bounding box and 49 if he's outside. It appears that the 50 second number is coming from the amount of ticks I have set in the middle when the potion is added, but why it doesn't count down is beyond me.
Here is the code link:
http://pastebin.com/iQh72NUg
Thanks,
SchrodingersSpy
I don't actually know why you're not doing all this in the potion's update method, but meh... I'll leave that to you unless you ask about it. EDIT: I also realise now that I've re-read the thread that it was mentioned before as well XD Sorry, I read a lot of threads.
The effect isn't counting down when players are nearby because you're not checking if they already have the effect. So what's going on, is say we have 2 players with the effect. They don't give themselves the effects constantly, because you're checking for that player. However, the players are still constantly giving each other the effect regardless of whether they already have it or not. So the effect just keeps getting set back to 50 seconds.
That makes sense, but the original player's effect counts down like normal from whatever it was at, even if a new player comes in and gets updated. Also, it only ticks down to 49 on the second player. It hangs at that point and doesn't decrease further. So it isn't updating the first player or the second one once they move away from each other.
So long as you've coded your potions correctly, the only thing I can think of that might cause this strange behaviour is that it might be only happening client side. Try only run the code on the server side and see if that fixes it.