From 7485470b73c27802f3ac020542349bd98f675df0 Mon Sep 17 00:00:00 2001 From: xGinko Date: Thu, 8 Feb 2024 16:13:14 +0100 Subject: [PATCH] fix issues with end portal frame interactions in banned-blocks --- .../PreventPlacingBannedBlocks.java | 44 ++++++------- .../modules/patches/WorldChangeCrash.java | 4 +- .../StructureGrowPermBlockRemoval.java | 2 +- .../anarchyexploitfixes/utils/EntityUtil.java | 8 +-- .../PreventPlacingBannedBlocks.java | 64 ++++++++----------- .../RedstoneOnTrapdoorCrash.java | 4 +- .../crashexploits/WorldChangeCrash.java | 2 +- .../StructureGrowPermBlockRemoval.java | 2 +- 8 files changed, 58 insertions(+), 72 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java index 14f24940c..7297ace61 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java @@ -8,31 +8,33 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockCanBuildEvent; -import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.player.PlayerInteractEvent; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Level; +import java.util.stream.Collectors; public class PreventPlacingBannedBlocks implements AnarchyExploitFixesModule, Listener { - private final Set bannedItems = new HashSet<>(); + private final Set bannedMaterial; public PreventPlacingBannedBlocks() { shouldEnable(); - AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", List.of( + this.bannedMaterial = AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", List.of( "CHAIN_COMMAND_BLOCK", "COMMAND_BLOCK", "COMMAND_BLOCK_MINECART", "REPEATING_COMMAND_BLOCK", "BEDROCK", "BARRIER", "STRUCTURE_BLOCK", "STRUCTURE_VOID", "END_PORTAL_FRAME", "END_PORTAL", "NETHER_PORTAL", "LIGHT" - )).forEach(configuredBlock -> { - try { - Material bannedMaterial = Material.valueOf(configuredBlock); - this.bannedItems.add(bannedMaterial); - } catch (IllegalArgumentException e) { - LogUtil.materialNotRecognized(Level.WARNING, name(), configuredBlock); - } - }); + )).stream() + .map(confMaterial -> { + try { + return Material.valueOf(confMaterial); + } catch (IllegalArgumentException e) { + LogUtil.materialNotRecognized(Level.WARNING, name(), confMaterial); + return null; + } + }) + .collect(Collectors.toCollection(HashSet::new)); } @Override @@ -62,17 +64,15 @@ public void disable() { } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onBlockPlace(BlockPlaceEvent event) { - if (bannedItems.contains(event.getBlock().getType())) { - event.setCancelled(true); - event.setBuild(false); - } - } + private void onInteract(PlayerInteractEvent event) { + if (!bannedMaterial.contains(event.getMaterial())) return; - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onBlockPlace(BlockCanBuildEvent event) { - if (bannedItems.contains(event.getMaterial())) { - event.setBuildable(false); + // This is a workaround because BlockPlaceEvent reports that players were placing end_portal_frames + // when they were actually just putting eyes into them. + if (event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.END_PORTAL_FRAME) { + if (event.getItem() != null && event.getItem().getType() == Material.ENDER_EYE) return; } + + event.setCancelled(true); } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/WorldChangeCrash.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/WorldChangeCrash.java index 8295cfb4a..18427e3a3 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/WorldChangeCrash.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/WorldChangeCrash.java @@ -49,7 +49,7 @@ public void enable() { @Override public boolean shouldEnable() { - return AnarchyExploitFixes.getConfiguration().getBoolean("patches.crash-exploits.prevent-fast-world-teleport-crash.enable", true); + return AnarchyExploitFixes.getConfiguration().getBoolean("patches.crash-exploits.prevent-fast-world-teleport-crash.enable", false); } @Override @@ -65,7 +65,7 @@ private void onTeleport(EntityTeleportEvent event) { if (recentWorldChangers.contains(event.getEntity().getUniqueId())) { event.setCancelled(true); if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Cancelled too fast world teleport of entity: " + - event.getEntityType().name() + " at \n" + + event.getEntityType().name() + " at " + "x: "+event.getEntity().getLocation().getX() + ", " + "y: "+event.getEntity().getLocation().getY() + ", " + "z: "+event.getEntity().getLocation().getZ() + ", " + diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java index effc0de6f..4b02e3426 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java @@ -54,7 +54,7 @@ public void onStructureGrow(StructureGrowEvent event) { for (final BlockState blockState : event.getBlocks()) { if (MaterialUtil.isIndestructible(world.getBlockAt(blockState.getLocation()).getType())) { event.setCancelled(true); - LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at\n" + + LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at " + "x: "+blockState.getLocation().getX() + ", " + "y: "+blockState.getLocation().getY() + ", " + "z: "+blockState.getLocation().getZ() + ", " + diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/EntityUtil.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/EntityUtil.java index 7b1d9f4c3..d0cf2749c 100644 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/EntityUtil.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/utils/EntityUtil.java @@ -11,11 +11,11 @@ public class EntityUtil { public static final Set BOATS = Stream.of( EntityType.BOAT, - EntityType.CHEST_BOAT - ).collect(Collectors.toCollection(HashSet::new)); + EntityType.CHEST_BOAT) + .collect(Collectors.toCollection(HashSet::new)); public static final Set ITEM_FRAMES = Stream.of( EntityType.ITEM_FRAME, - EntityType.GLOW_ITEM_FRAME - ).collect(Collectors.toCollection(HashSet::new)); + EntityType.GLOW_ITEM_FRAME) + .collect(Collectors.toCollection(HashSet::new)); } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java index 344f66b15..a814623ac 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/illegals/items/bannedblocks/PreventPlacingBannedBlocks.java @@ -4,29 +4,26 @@ import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; import me.moomoo.anarchyexploitfixes.utils.LogUtil; -import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.PlayerInventory; -import java.time.Duration; -import java.util.*; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.logging.Level; +import java.util.stream.Collectors; public class PreventPlacingBannedBlocks implements AnarchyExploitFixesModule, Listener { - private final ExpiringSet playersPuttingEyesInFrames; - private final Set bannedMaterial = new HashSet<>(); + private final Set bannedMaterial; private final Material ENDER_EYE, END_PORTAL_FRAME; public PreventPlacingBannedBlocks() { shouldEnable(); - this.playersPuttingEyesInFrames = new ExpiringSet<>(Duration.ofSeconds(1)); // default list entries based on version List defaults; @@ -39,14 +36,17 @@ public PreventPlacingBannedBlocks() { defaults = Arrays.asList("BEDROCK", "BARRIER", "COMMAND", "STRUCTURE_BLOCK", "ENDER_PORTAL_FRAME"); } - for (String configuredBlock : AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", defaults)) { - try { - Material bannedMaterial = Material.valueOf(configuredBlock); - this.bannedMaterial.add(bannedMaterial); - } catch (IllegalArgumentException e) { - LogUtil.materialNotRecognized(Level.WARNING, name(), configuredBlock); - } - } + this.bannedMaterial = AnarchyExploitFixes.getConfiguration().getList("illegals.ban-specific-blocks.banned-blocks", defaults) + .stream() + .map(confMaterial -> { + try { + return Material.valueOf(confMaterial); + } catch (IllegalArgumentException e) { + LogUtil.materialNotRecognized(Level.WARNING, name(), confMaterial); + return null; + } + }) + .collect(Collectors.toCollection(HashSet::new)); this.ENDER_EYE = XMaterial.ENDER_EYE.parseMaterial(); this.END_PORTAL_FRAME = XMaterial.END_PORTAL_FRAME.parseMaterial(); @@ -73,30 +73,16 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("illegals.ban-specific-blocks.prevent-placing", true); } - // Try to work around a bug in older versions, where it looks like the player placed - // an END_PORTAL_FRAME for the plugin when listening to BlockPlaceEvent. - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - private void onPlaceEyeInEndFrame(PlayerInteractEvent event) { - if (!bannedMaterial.contains(END_PORTAL_FRAME)) return; // If Frame is not banned, no need to continue - if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return; - if (event.getClickedBlock().getType() != END_PORTAL_FRAME) return; + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + private void onInteract(PlayerInteractEvent event) { + if (!bannedMaterial.contains(event.getMaterial())) return; - final PlayerInventory playerInventory = event.getPlayer().getInventory(); - if ( - playerInventory.getItemInMainHand().getType() == ENDER_EYE - || playerInventory.getItemInOffHand().getType() == ENDER_EYE - ) { - playersPuttingEyesInFrames.add(event.getPlayer().getUniqueId()); + // This is a workaround because BlockPlaceEvent reports that players were placing end_portal_frames + // when they were actually just putting eyes into them. + if (event.getClickedBlock() != null && event.getClickedBlock().getType() == END_PORTAL_FRAME) { + if (event.getItem() != null && event.getItem().getType() == ENDER_EYE) return; } - } - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onBlockPlace(BlockPlaceEvent event) { - if ( - bannedMaterial.contains(event.getBlock().getType()) - && !playersPuttingEyesInFrames.contains(event.getPlayer().getUniqueId()) - ) { - event.setCancelled(true); - } + event.setCancelled(true); } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/RedstoneOnTrapdoorCrash.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/RedstoneOnTrapdoorCrash.java index 1c475d251..3f8ee1e32 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/RedstoneOnTrapdoorCrash.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/RedstoneOnTrapdoorCrash.java @@ -78,7 +78,7 @@ private void onRedstonePowerTrapdoor(BlockRedstoneEvent event) { trapdoorActivationCache.put(trapdoorLoc, activationCount); if (activationCount > trapdoorActivationLimit) { block.setType(AIR); - if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at\n" + + if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at " + "x: "+trapdoorLoc.getX() + ", " + "y: "+trapdoorLoc.getY() + ", " + "z: "+trapdoorLoc.getZ() + ", " + @@ -95,7 +95,7 @@ private void onBlockPlace(BlockPlaceEvent event) { if (MaterialUtil.TRAPDOORS.contains(block.getRelative(BlockFace.DOWN).getType())) { event.setCancelled(true); - if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at\n" + + if (logIsEnabled) LogUtil.moduleLog(Level.WARNING, name(), "Prevented possible trapdoor crash at " + "x: "+block.getLocation().getX() + ", " + "y: "+block.getLocation().getY() + ", " + "z: "+block.getLocation().getZ() + ", " + diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/WorldChangeCrash.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/WorldChangeCrash.java index 98bb6c8ac..682b15b7b 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/WorldChangeCrash.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/crashexploits/WorldChangeCrash.java @@ -59,7 +59,7 @@ private void onTeleport(EntityTeleportEvent event) { if (recentWorldChangers.contains(event.getEntity().getUniqueId())) { event.setCancelled(true); if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Cancelled too fast world teleport of entity: " + - event.getEntityType().name() + " at \n" + + event.getEntityType().name() + " at " + "x: "+event.getEntity().getLocation().getX() + ", " + "y: "+event.getEntity().getLocation().getY() + ", " + "z: "+event.getEntity().getLocation().getZ() + ", " + diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java index 90572716a..03a8b11a0 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/preventions/blockbreak/StructureGrowPermBlockRemoval.java @@ -48,7 +48,7 @@ public void onStructureGrow(StructureGrowEvent event) { for (final BlockState blockState : event.getBlocks()) { if (MaterialUtil.INDESTRUCTIBLES.contains(world.getBlockAt(blockState.getLocation()).getType())) { event.setCancelled(true); - LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at\n" + + LogUtil.moduleLog(Level.INFO, name(), "Prevented permanent block break by growing a structure at " + "x: "+blockState.getLocation().getX() + ", " + "y: "+blockState.getLocation().getY() + ", " + "z: "+blockState.getLocation().getZ() + ", " +