From a25713722bf271dc962803708eb6440864a358de Mon Sep 17 00:00:00 2001 From: Freddie Date: Thu, 1 Jun 2023 14:32:30 +0100 Subject: [PATCH] add enderpearl, equipment and potion effect events --- .../vera/spigot/event/EquipmentSetEvent.java | 30 +++++++ .../spigot/event/PlayerPearlRefundEvent.java | 28 ++++++ .../event/potion/PotionEffectAddEvent.java | 88 +++++++++++++++++++ .../event/potion/PotionEffectEvent.java | 23 +++++ .../event/potion/PotionEffectExpireEvent.java | 43 +++++++++ .../event/potion/PotionEffectExtendEvent.java | 23 +++++ .../event/potion/PotionEffectRemoveEvent.java | 33 +++++++ 7 files changed, 268 insertions(+) create mode 100644 src/main/java/lol/vera/spigot/event/EquipmentSetEvent.java create mode 100644 src/main/java/lol/vera/spigot/event/PlayerPearlRefundEvent.java create mode 100644 src/main/java/lol/vera/spigot/event/potion/PotionEffectAddEvent.java create mode 100644 src/main/java/lol/vera/spigot/event/potion/PotionEffectEvent.java create mode 100644 src/main/java/lol/vera/spigot/event/potion/PotionEffectExpireEvent.java create mode 100644 src/main/java/lol/vera/spigot/event/potion/PotionEffectExtendEvent.java create mode 100644 src/main/java/lol/vera/spigot/event/potion/PotionEffectRemoveEvent.java diff --git a/src/main/java/lol/vera/spigot/event/EquipmentSetEvent.java b/src/main/java/lol/vera/spigot/event/EquipmentSetEvent.java new file mode 100644 index 0000000..69c83e4 --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/EquipmentSetEvent.java @@ -0,0 +1,30 @@ +package lol.vera.spigot.event; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; + +@Getter +@RequiredArgsConstructor +public class EquipmentSetEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + private final HumanEntity humanEntity; + private final int slot; + private final ItemStack previousItem; + private final ItemStack newItem; + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +} \ No newline at end of file diff --git a/src/main/java/lol/vera/spigot/event/PlayerPearlRefundEvent.java b/src/main/java/lol/vera/spigot/event/PlayerPearlRefundEvent.java new file mode 100644 index 0000000..eca99bd --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/PlayerPearlRefundEvent.java @@ -0,0 +1,28 @@ +package lol.vera.spigot.event; + +import lombok.*; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +@Data +@RequiredArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class PlayerPearlRefundEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private final Player player; + private boolean cancelled; + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +} \ No newline at end of file diff --git a/src/main/java/lol/vera/spigot/event/potion/PotionEffectAddEvent.java b/src/main/java/lol/vera/spigot/event/potion/PotionEffectAddEvent.java new file mode 100644 index 0000000..2536cd8 --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/potion/PotionEffectAddEvent.java @@ -0,0 +1,88 @@ +package lol.vera.spigot.event.potion; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.potion.PotionEffect; + +/** + * Called when a potion effect is applied to an entity, or an existing effect is extended or upgraded + */ + +@Getter +@Setter +public class PotionEffectAddEvent extends PotionEffectEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private boolean cancelled; + private final EffectAddReason reason; + + public PotionEffectAddEvent(LivingEntity what, PotionEffect effect, EffectAddReason reason) { + super(what, effect); + this.reason = reason; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public enum EffectAddReason { + /** + * Added using the /effect command + */ + COMMAND, + /** + * Added by eating a golden apple + */ + GOLDEN_APPLE, + /** + * Added by being near a beacon + */ + BEACON, + /** + * Added by being hit with a wither skull + */ + WITHER_SKULL, + /** + * Added by being hit by a wither skeleton + */ + WITHER_SKELETON, + /** + * Added to villagers when they are cured + */ + VILLAGER_CURE, + /** + * Added to villagers when they unlock a new set of trades + */ + VILLAGER_LEVELUP, + /** + * Added to spiders on higher difficulty + */ + SPIDER_POWERUP, + /** + * Added when a potion is splashed + */ + POTION_SPLASH, + /** + * Added when a potion is consumed + */ + POTION_DRINK, + /** + * Added by a plugin + */ + CUSTOM, + /** + * Added by a part of the vanilla code not covered in the above cases + */ + UNKNOWN + } + +} \ No newline at end of file diff --git a/src/main/java/lol/vera/spigot/event/potion/PotionEffectEvent.java b/src/main/java/lol/vera/spigot/event/potion/PotionEffectEvent.java new file mode 100644 index 0000000..82386b4 --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/potion/PotionEffectEvent.java @@ -0,0 +1,23 @@ +package lol.vera.spigot.event.potion; + +import lombok.Getter; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.EntityEvent; +import org.bukkit.potion.PotionEffect; + +@Getter +public abstract class PotionEffectEvent extends EntityEvent { + + private final PotionEffect effect; + + public PotionEffectEvent(LivingEntity what, PotionEffect effect) { + super(what); + this.effect = effect; + } + + @Override + public LivingEntity getEntity() { + return (LivingEntity) super.getEntity(); + } + +} \ No newline at end of file diff --git a/src/main/java/lol/vera/spigot/event/potion/PotionEffectExpireEvent.java b/src/main/java/lol/vera/spigot/event/potion/PotionEffectExpireEvent.java new file mode 100644 index 0000000..1784445 --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/potion/PotionEffectExpireEvent.java @@ -0,0 +1,43 @@ +package lol.vera.spigot.event.potion; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.LivingEntity; +import org.bukkit.potion.PotionEffect; + +/** + * Called when a potion effect on an entity runs out. Cancelling the event extends + * the effect with a practically infinite duration. The new duration can also be set + * explicitly by calling {@link #setDuration}. + * + * Handlers of {@link PotionEffectRemoveEvent} will also receive this event. + */ + +@Getter @Setter +public class PotionEffectExpireEvent extends PotionEffectRemoveEvent { + + private int duration = 0; + + public PotionEffectExpireEvent(LivingEntity entity, PotionEffect effect) { + super(entity, effect); + } + + /** + * Set a new duration for the potion effect. Passing 0 to this method un-cancels + * the event, and passing anything above 0 cancels it. + */ + public void setDuration(int duration) { + this.duration = Math.max(0, duration); + } + + @Override + public boolean isCancelled() { + return duration > 0; + } + + @Override + public void setCancelled(boolean cancel) { + this.duration = cancel ? Integer.MAX_VALUE : 0; + } + +} \ No newline at end of file diff --git a/src/main/java/lol/vera/spigot/event/potion/PotionEffectExtendEvent.java b/src/main/java/lol/vera/spigot/event/potion/PotionEffectExtendEvent.java new file mode 100644 index 0000000..b9793df --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/potion/PotionEffectExtendEvent.java @@ -0,0 +1,23 @@ +package lol.vera.spigot.event.potion; + +import lombok.Getter; +import org.bukkit.entity.LivingEntity; +import org.bukkit.potion.PotionEffect; + +/** + * Called when an entity's active potion effect is extended or upgraded. + * + * Handlers of {@link PotionEffectAddEvent} will also receive this event. + */ + +@Getter +public class PotionEffectExtendEvent extends PotionEffectAddEvent { + + private final PotionEffect oldEffect; + + public PotionEffectExtendEvent(LivingEntity what, PotionEffect effect, EffectAddReason reason, PotionEffect oldEffect) { + super(what, effect, reason); + this.oldEffect = oldEffect; + } + +} \ No newline at end of file diff --git a/src/main/java/lol/vera/spigot/event/potion/PotionEffectRemoveEvent.java b/src/main/java/lol/vera/spigot/event/potion/PotionEffectRemoveEvent.java new file mode 100644 index 0000000..145e55c --- /dev/null +++ b/src/main/java/lol/vera/spigot/event/potion/PotionEffectRemoveEvent.java @@ -0,0 +1,33 @@ +package lol.vera.spigot.event.potion; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.potion.PotionEffect; + +/** + * Called when a potion effect is removed from an entity for whatever reason + */ + +@Getter @Setter +public class PotionEffectRemoveEvent extends PotionEffectEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + + public PotionEffectRemoveEvent(LivingEntity entity, PotionEffect effect) { + super(entity, effect); + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +} \ No newline at end of file