From 885d2449d9b2a9fb916cd6fafcbcc6a7b90056ed Mon Sep 17 00:00:00 2001 From: BONNe Date: Fri, 24 May 2024 17:22:20 +0300 Subject: [PATCH] Fixes creeper ignation by visitors (#2375) This fixes a long-standing bug which was introduced with a code that prevented hostile entities from targeting visitors. As player was not a target for creeper it allowed it to explode. This code change prevents visitors from igniting creepers as I do not see a reason why we should allow them to ignite them, while still protecting from griefing. Addresses issue reported in #2372 --- .../flags/worldsettings/CreeperListener.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListener.java index bbe835457..7adf0d6d6 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListener.java @@ -1,5 +1,8 @@ package world.bentobox.bentobox.listeners.flags.worldsettings; + +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.entity.Creeper; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -8,6 +11,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.localization.TextVariables; @@ -68,4 +72,33 @@ public class CreeperListener extends FlagListener { e.setCancelled(true); } } + + + /** + * Prevent creepers from igniting if they are not allowed to grief + * @param e - event + * @since 2.4.0 + */ + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPlayerInteractEntity(PlayerInteractEntityEvent e) + { + Player player = e.getPlayer(); + Location location = e.getRightClicked().getLocation(); + + if (!Flags.CREEPER_GRIEFING.isSetForWorld(location.getWorld()) && + e.getRightClicked() instanceof Creeper && + !this.getIslandsManager().locationIsOnIsland(player, location)) + { + Material mainHand = player.getInventory().getItemInMainHand().getType(); + + if (Material.FIRE_CHARGE.equals(mainHand) || + Material.FLINT_AND_STEEL.equals(mainHand)) + { + // Creeper igniting + User user = User.getInstance(player); + user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation(Flags.CREEPER_GRIEFING.getHintReference())); + e.setCancelled(true); + } + } + } }