From 52ca80b79386f2f6ef684834452d827b1ea5090f Mon Sep 17 00:00:00 2001 From: chacho4 Date: Sun, 15 May 2022 21:36:46 +0900 Subject: [PATCH] entity-armor-stand-destroy flag is added. --- .../listener/EventAbstractionListener.java | 17 ++-------- .../listener/WorldGuardEntityListener.java | 32 ++++++++++++++++--- .../listener/WorldGuardHangingListener.java | 14 ++++---- .../worldguard/protection/flags/Flags.java | 1 + 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java index c2d44990d..ffee15edd 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/EventAbstractionListener.java @@ -66,20 +66,7 @@ import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Waterlogged; import org.bukkit.block.data.type.Dispenser; -import org.bukkit.entity.AreaEffectCloud; -import org.bukkit.entity.Creeper; -import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.FallingBlock; -import org.bukkit.entity.Firework; -import org.bukkit.entity.Item; -import org.bukkit.entity.ItemFrame; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Mob; -import org.bukkit.entity.Painting; -import org.bukkit.entity.Player; -import org.bukkit.entity.Tameable; -import org.bukkit.entity.ThrownPotion; +import org.bukkit.entity.*; import org.bukkit.entity.minecart.HopperMinecart; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; @@ -762,6 +749,8 @@ public void onHangingBreak(HangingBreakEvent event) { destroyEntityEvent.getRelevantFlags().add(Flags.ENTITY_ITEM_FRAME_DESTROY); } else if (event.getEntity() instanceof Painting) { destroyEntityEvent.getRelevantFlags().add(Flags.ENTITY_PAINTING_DESTROY); + } else if (event.getEntity() instanceof ArmorStand) { + destroyEntityEvent.getRelevantFlags().add(Flags.ENTITY_ARMOR_STAND_DESTROY); } Events.fireToCancel(event, destroyEntityEvent); } diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java index 74a77c59f..0011de711 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardEntityListener.java @@ -208,8 +208,8 @@ private void onEntityDamageByEntity(EntityDamageByEntityEvent event) { event.setCancelled(true); return; } - } else if (defender instanceof ArmorStand && !(attacker instanceof Player)) { - if (wcfg.blockEntityArmorStandDestroy) { + } else if (defender instanceof ArmorStand) { + if (checkArmorStandProtection(attacker, (ArmorStand) defender)) { event.setCancelled(true); return; } @@ -334,9 +334,10 @@ private void onEntityDamageByProjectile(EntityDamageByEntityEvent event) { event.setCancelled(true); return; } - } else if (defender instanceof ArmorStand && Entities.isNonPlayerCreature(attacker)) { - if (wcfg.blockEntityArmorStandDestroy) { + } else if (defender instanceof ArmorStand) { + if (checkArmorStandProtection(attacker, (ArmorStand) defender)) { event.setCancelled(true); + return; } } @@ -851,4 +852,27 @@ private boolean checkItemFrameProtection(Entity attacker, ItemFrame defender) { return false; } + /** + * Checks regions and config settings to protect items from being knocked + * out of armor stand. + * @param attacker attacking entity + * @param defender armor stand being damaged + * @return true if the event should be cancelled + */ + private boolean checkArmorStandProtection(Entity attacker, ArmorStand defender) { + World world = defender.getWorld(); + WorldConfiguration wcfg = getWorldConfig(world); + if (wcfg.useRegions) { + // bukkit throws this event when a player attempts to remove an item from a frame + if (!(attacker instanceof Player)) { + if (!StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(defender.getLocation()), (RegionAssociable) null, Flags.ENTITY_ARMOR_STAND_DESTROY))) { + return true; + } + } + } + if (wcfg.blockEntityArmorStandDestroy && !(attacker instanceof Player)) { + return true; + } + return false; + } } diff --git a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardHangingListener.java b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardHangingListener.java index 9d93a5191..225480141 100644 --- a/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardHangingListener.java +++ b/worldguard-bukkit/src/main/java/com/sk89q/worldguard/bukkit/listener/WorldGuardHangingListener.java @@ -27,14 +27,7 @@ import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.flags.StateFlag; import org.bukkit.World; -import org.bukkit.entity.Creeper; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Hanging; -import org.bukkit.entity.ItemFrame; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Painting; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; +import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.hanging.HangingBreakByEntityEvent; @@ -90,6 +83,11 @@ public void onHangingBreak(HangingBreakEvent event) { || (wcfg.useRegions && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(hanging.getLocation()), (RegionAssociable) null, Flags.ENTITY_ITEM_FRAME_DESTROY))))) { event.setCancelled(true); + } else if (hanging instanceof ArmorStand + && (wcfg.blockEntityArmorStandDestroy + || (wcfg.useRegions + && !StateFlag.test(WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().queryState(BukkitAdapter.adapt(hanging.getLocation()), (RegionAssociable) null, Flags.ENTITY_ARMOR_STAND_DESTROY))))) { + event.setCancelled(true); } } } else { diff --git a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java index 218637b53..025e194a3 100644 --- a/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java +++ b/worldguard-core/src/main/java/com/sk89q/worldguard/protection/flags/Flags.java @@ -99,6 +99,7 @@ public final class Flags { public static final StateFlag RAVAGER_RAVAGE = register(new StateFlag("ravager-grief", true)); public static final StateFlag ENTITY_PAINTING_DESTROY = register(new StateFlag("entity-painting-destroy", true)); public static final StateFlag ENTITY_ITEM_FRAME_DESTROY = register(new StateFlag("entity-item-frame-destroy", true)); + public static final StateFlag ENTITY_ARMOR_STAND_DESTROY = register(new StateFlag("entity-armor-stand-destroy", true)); // mob spawning related public static final StateFlag MOB_SPAWNING = register(new StateFlag("mob-spawning", true));