From 814c742554e40554a30cee1915b42457081000c6 Mon Sep 17 00:00:00 2001 From: kaenganxt Date: Fri, 20 Jul 2018 16:04:37 +1000 Subject: SPIGOT-840, SPIGOT-2522: [Draft] Add EntityPotionEffectChangeEvent Discussion ongoing in PR #449 --- .../craftbukkit/entity/CraftLivingEntity.java | 5 ++-- .../craftbukkit/event/CraftEventFactory.java | 35 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java index 382b8028..13100e5d 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java @@ -68,6 +68,7 @@ import org.bukkit.entity.ThrownPotion; import org.bukkit.entity.TippedArrow; import org.bukkit.entity.Trident; import org.bukkit.entity.WitherSkull; +import org.bukkit.event.entity.EntityPotionEffectEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.ItemStack; @@ -262,7 +263,7 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { } removePotionEffect(effect.getType()); } - getHandle().addEffect(new MobEffect(MobEffectList.fromId(effect.getType().getId()), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles())); + getHandle().addEffect(new MobEffect(MobEffectList.fromId(effect.getType().getId()), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.hasParticles()), EntityPotionEffectEvent.Cause.PLUGIN); return true; } @@ -285,7 +286,7 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity { } public void removePotionEffect(PotionEffectType type) { - getHandle().removeEffect(MobEffectList.fromId(type.getId())); + getHandle().removeEffect(MobEffectList.fromId(type.getId()), EntityPotionEffectEvent.Cause.PLUGIN); } public Collection getActivePotionEffects() { diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 5793bdf4..72b5dfd7 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -31,6 +31,7 @@ import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.inventory.CraftInventoryCrafting; import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.inventory.CraftMetaBook; +import org.bukkit.craftbukkit.potion.CraftPotionUtil; import org.bukkit.craftbukkit.util.CraftDamageSource; import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.entity.AreaEffectCloud; @@ -67,6 +68,7 @@ import org.bukkit.inventory.meta.BookMeta; import org.bukkit.entity.AbstractHorse; import org.bukkit.entity.Vehicle; import org.bukkit.event.vehicle.VehicleCreateEvent; +import org.bukkit.potion.PotionEffect; public class CraftEventFactory { public static final DamageSource MELTING = CraftDamageSource.copyOf(DamageSource.BURN); @@ -1054,6 +1056,39 @@ public class CraftEventFactory { return handleBlockFormEvent(world, pos, block, 3); } + public static EntityPotionEffectEvent callEntityPotionEffectChangeEvent(EntityLiving entity, @Nullable MobEffect oldEffect, @Nullable MobEffect newEffect, EntityPotionEffectEvent.Cause cause) { + return callEntityPotionEffectChangeEvent(entity, oldEffect, newEffect, cause, true); + } + + public static EntityPotionEffectEvent callEntityPotionEffectChangeEvent(EntityLiving entity, @Nullable MobEffect oldEffect, @Nullable MobEffect newEffect, EntityPotionEffectEvent.Cause cause, EntityPotionEffectEvent.Action action) { + return callEntityPotionEffectChangeEvent(entity, oldEffect, newEffect, cause, action, true); + } + + public static EntityPotionEffectEvent callEntityPotionEffectChangeEvent(EntityLiving entity, @Nullable MobEffect oldEffect, @Nullable MobEffect newEffect, EntityPotionEffectEvent.Cause cause, boolean willOverride) { + EntityPotionEffectEvent.Action action = EntityPotionEffectEvent.Action.CHANGED; + if (oldEffect == null) { + action = EntityPotionEffectEvent.Action.ADDED; + } else if (newEffect == null) { + action = EntityPotionEffectEvent.Action.REMOVED; + } + + return callEntityPotionEffectChangeEvent(entity, oldEffect, newEffect, cause, action, willOverride); + } + + public static EntityPotionEffectEvent callEntityPotionEffectChangeEvent(EntityLiving entity, @Nullable MobEffect oldEffect, @Nullable MobEffect newEffect, EntityPotionEffectEvent.Cause cause, EntityPotionEffectEvent.Action action, boolean willOverride) { + PotionEffect bukkitOldEffect = (oldEffect == null) ? null : CraftPotionUtil.toBukkit(oldEffect); + PotionEffect bukkitNewEffect = (newEffect == null) ? null : CraftPotionUtil.toBukkit(newEffect); + + if (bukkitOldEffect == null && bukkitNewEffect == null) { + throw new IllegalStateException("Old and new potion effect are both null"); + } + + EntityPotionEffectEvent event = new EntityPotionEffectEvent((LivingEntity) entity.getBukkitEntity(), bukkitOldEffect, bukkitNewEffect, cause, action, willOverride); + Bukkit.getPluginManager().callEvent(event); + + return event; + } + public static boolean handleBlockFormEvent(World world, BlockPosition pos, IBlockData block, @Nullable Entity entity) { return handleBlockFormEvent(world, pos, block, 3, entity); } -- cgit v1.2.3