Add Potion Add and Remove events

This commit is contained in:
ThatCreeper 2021-01-02 16:59:17 -06:00
parent dad642a6a2
commit 30be7b5b81
5 changed files with 83 additions and 5 deletions

View File

@ -10,10 +10,7 @@ import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer;
import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback;
import net.minestom.server.event.entity.EntityDeathEvent;
import net.minestom.server.event.entity.EntitySpawnEvent;
import net.minestom.server.event.entity.EntityTickEvent;
import net.minestom.server.event.entity.EntityVelocityEvent;
import net.minestom.server.event.entity.*;
import net.minestom.server.event.handler.EventHandler;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
@ -407,7 +404,16 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
this.effects.removeIf(timedPotion -> {
final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS;
// Remove if the potion should be expired
return timedPotion.getStartingTime() + potionTime >= time;
if (time >= timedPotion.getStartingTime() + potionTime) {
// Send the packet that the potion should no longer be applied
timedPotion.getPotion().sendRemovePacket(this);
this.callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent(
this,
timedPotion.getPotion()
));
return true;
}
return false;
});
}
@ -1476,6 +1482,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
this.effects.removeIf(timedPotion -> {
if (timedPotion.getPotion().getEffect() == effect) {
timedPotion.getPotion().sendRemovePacket(this);
this.callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent(
this,
timedPotion.getPotion()
));
return true;
}
return false;
@ -1491,6 +1501,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
removeEffect(potion.getEffect());
this.effects.add(new TimedPotion(potion, System.currentTimeMillis()));
potion.sendAddPacket(this);
this.callEvent(EntityPotionAddEvent.class, new EntityPotionAddEvent(
this,
potion
));
}
protected boolean shouldRemove() {

View File

@ -0,0 +1,24 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.potion.Potion;
import org.jetbrains.annotations.NotNull;
public class EntityPotionAddEvent extends EntityEvent {
private final Potion potion;
public EntityPotionAddEvent(@NotNull Entity entity, @NotNull Potion potion) {
super(entity);
this.potion = potion;
}
/**
* Returns the potion that was added.
*
* @return the added potion.
*/
public Potion getPotion() {
return potion;
}
}

View File

@ -0,0 +1,24 @@
package net.minestom.server.event.entity;
import net.minestom.server.entity.Entity;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.potion.Potion;
import org.jetbrains.annotations.NotNull;
public class EntityPotionRemoveEvent extends EntityEvent {
private final Potion potion;
public EntityPotionRemoveEvent(@NotNull Entity entity, @NotNull Potion potion) {
super(entity);
this.potion = potion;
}
/**
* Returns the potion that was removed.
*
* @return the removed potion.
*/
public Potion getPotion() {
return potion;
}
}

View File

@ -1,5 +1,6 @@
package net.minestom.server.potion;
import net.minestom.server.entity.Entity;
import org.jetbrains.annotations.NotNull;
public class TimedPotion {

View File

@ -12,6 +12,8 @@ import net.minestom.server.entity.type.other.EntityEndCrystal;
import net.minestom.server.event.EntityEvent;
import net.minestom.server.event.GlobalEventHandler;
import net.minestom.server.event.entity.EntityAttackEvent;
import net.minestom.server.event.entity.EntityPotionAddEvent;
import net.minestom.server.event.entity.EntityPotionRemoveEvent;
import net.minestom.server.event.item.ItemDropEvent;
import net.minestom.server.event.item.PickupItemEvent;
import net.minestom.server.event.player.*;
@ -29,6 +31,7 @@ import net.minestom.server.item.Material;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.server.play.PlayerListHeaderAndFooterPacket;
import net.minestom.server.ping.ResponseDataConsumer;
import net.minestom.server.potion.Potion;
import net.minestom.server.timer.TaskBuilder;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.Position;
@ -271,6 +274,18 @@ public class PlayerInit {
//player.getInstance().unloadChunk(chunk);
}
});
globalEventHandler.addEventCallback(EntityPotionAddEvent.class, event -> {
if (event.getEntity() instanceof Player) {
((Player) event.getEntity()).sendMessage("Potion added: " + event.getPotion().getEffect());
}
});
globalEventHandler.addEventCallback(EntityPotionRemoveEvent.class, event -> {
if (event.getEntity() instanceof Player) {
((Player) event.getEntity()).sendMessage("Potion removed: " + event.getPotion().getEffect());
}
});
}
public static ResponseDataConsumer getResponseDataConsumer() {