From 534fd675b33b7fc3133502c33b1b51dc17281bed Mon Sep 17 00:00:00 2001 From: TreemanKing <67459602+TreemanKing@users.noreply.github.com> Date: Tue, 25 Jun 2024 00:31:53 +1000 Subject: [PATCH] feat: new flag - handle tamed animals (#2412) --- .../flags/protection/HurtingListener.java | 25 ++++++++++++++++--- .../world/bentobox/bentobox/lists/Flags.java | 1 + .../world/bentobox/bentobox/util/Util.java | 19 ++++---------- src/main/resources/locales/en-US.yml | 4 +++ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListener.java index 942cb7edb..3553d61f8 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/HurtingListener.java @@ -51,7 +51,10 @@ public class HurtingListener extends FlagListener { public void onEntityDamage(final EntityDamageByEntityEvent e) { // Mobs being hurt - if (Util.isPassiveEntity(e.getEntity())) + if (Util.isTamableEntity(e.getEntity())) { + this.respond(e, e.getDamager(), Flags.HURT_TAMED_ANIMALS); + } + else if (Util.isPassiveEntity(e.getEntity())) { this.respond(e, e.getDamager(), Flags.HURT_ANIMALS); } @@ -92,7 +95,8 @@ public class HurtingListener extends FlagListener { return; } - if ((Util.isPassiveEntity(e.getCaught()) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_ANIMALS)) + if ((Util.isTamableEntity(e.getCaught()) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_TAMED_ANIMALS)) + || (Util.isPassiveEntity(e.getCaught()) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_ANIMALS)) || (Util.isHostileEntity(e.getCaught()) && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_MONSTERS)) || (e.getCaught() instanceof AbstractVillager && checkIsland(e, e.getPlayer(), e.getCaught().getLocation(), Flags.HURT_VILLAGERS))) { e.getHook().remove(); @@ -113,7 +117,11 @@ public class HurtingListener extends FlagListener { if (e.getRightClicked() instanceof Parrot && (e.getHand().equals(EquipmentSlot.HAND) && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.COOKIE)) || (e.getHand().equals(EquipmentSlot.OFF_HAND) && e.getPlayer().getInventory().getItemInOffHand().getType().equals(Material.COOKIE))) { - checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.HURT_ANIMALS); + if (((Parrot) e.getRightClicked()).isTamed()) { + checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.HURT_TAMED_ANIMALS); + } else { + checkIsland(e, e.getPlayer(), e.getRightClicked().getLocation(), Flags.HURT_ANIMALS); + } } } @@ -139,6 +147,13 @@ public class HurtingListener extends FlagListener { } } + // Tamed animals being hurt + if (Util.isTamableEntity(entity) && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_TAMED_ANIMALS)) { + for (PotionEffect effect : e.getPotion().getEffects()) { + entity.removePotionEffect(effect.getType()); + } + } + // Mobs being hurt if (Util.isPassiveEntity(entity) && !checkIsland(e, attacker, entity.getLocation(), Flags.HURT_ANIMALS)) { for (PotionEffect effect : e.getPotion().getEffects()) { @@ -189,6 +204,10 @@ public class HurtingListener extends FlagListener { if (Util.isHostileEntity(entity)) { checkIsland(e, attacker, entity.getLocation(), Flags.HURT_MONSTERS); } + // Tamed animals being hurt + if (Util.isTamableEntity(entity)) { + checkIsland(e, attacker, entity.getLocation(), Flags.HURT_TAMED_ANIMALS); + } // Mobs being hurt if (Util.isPassiveEntity(entity)) { checkIsland(e, attacker, entity.getLocation(), Flags.HURT_ANIMALS); diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 9fc685b24..33d4bbf16 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -283,6 +283,7 @@ public final class Flags { public static final Flag HURT_ANIMALS = new Flag.Builder("HURT_ANIMALS", Material.STONE_SWORD).listener(new HurtingListener()).mode(Flag.Mode.ADVANCED).build(); public static final Flag HURT_MONSTERS = new Flag.Builder("HURT_MONSTERS", Material.WOODEN_SWORD).mode(Flag.Mode.BASIC).build(); public static final Flag HURT_VILLAGERS = new Flag.Builder("HURT_VILLAGERS", Material.GOLDEN_SWORD).mode(Flag.Mode.ADVANCED).build(); + public static final Flag HURT_TAMED_ANIMALS = new Flag.Builder("HURT_TAMABLE_ENTITIES", Material.DIAMOND_SWORD).mode(Flag.Mode.ADVANCED).build(); // Leashes public static final Flag LEASH = new Flag.Builder("LEASH", Material.LEAD).listener(new LeashListener()).build(); diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index c4c4bc945..7679b3bcf 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -28,20 +28,7 @@ import org.bukkit.World.Environment; import org.bukkit.attribute.Attribute; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.entity.Allay; -import org.bukkit.entity.Animals; -import org.bukkit.entity.Bat; -import org.bukkit.entity.EnderDragon; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Flying; -import org.bukkit.entity.IronGolem; -import org.bukkit.entity.Monster; -import org.bukkit.entity.Player; -import org.bukkit.entity.PufferFish; -import org.bukkit.entity.Shulker; -import org.bukkit.entity.Slime; -import org.bukkit.entity.Snowman; -import org.bukkit.entity.WaterMob; +import org.bukkit.entity.*; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.NonNull; @@ -370,6 +357,10 @@ public class Util { entity instanceof Allay; } + public static boolean isTamableEntity(Entity entity) { + return entity instanceof Tameable && ((Tameable) entity).isTamed(); + } + /* * PaperLib methods for addons to call */ diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 1eb99735d..7989f41ed 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -1231,6 +1231,10 @@ protection: description: '&a Toggle hive harvesting.' name: Hive harvesting hint: Harvesting disabled + HURT_TAMED_ANIMALS: + description: Toggle hurting. Enabled means that tamed animals can take damage. Disabled means they are invincible. + name: Hurt tamed animals + hint: Tamed animal hurting disabled HURT_ANIMALS: description: Toggle hurting name: Hurt animals