From 1077de5226c332b78929f4eb377549e2e5bb6202 Mon Sep 17 00:00:00 2001 From: xGinko Date: Wed, 29 Nov 2023 16:17:19 +0100 Subject: [PATCH] check for class instead of version --- AnarchyExploitFixesFolia/pom.xml | 4 ++ AnarchyExploitFixesLegacy/pom.xml | 4 ++ .../modules/patches/BeehiveCoordinates.java | 22 +++++++---- .../RedstoneOnTrapdoorCrash.java | 8 ++-- .../craftrecipe/AutoRecipePacketListener.java | 39 ++++++------------- pom.xml | 7 ++++ 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/AnarchyExploitFixesFolia/pom.xml b/AnarchyExploitFixesFolia/pom.xml index b2b760126..c938f47c9 100644 --- a/AnarchyExploitFixesFolia/pom.xml +++ b/AnarchyExploitFixesFolia/pom.xml @@ -43,6 +43,10 @@ ${project.name}-${project.parent.version} false + + com.github.benmanes.caffeine + me.moomoo.anarchyexploitfixes.caffeine + io.papermc.lib me.moomoo.anarchyexploitfixes.paperlib diff --git a/AnarchyExploitFixesLegacy/pom.xml b/AnarchyExploitFixesLegacy/pom.xml index da0229abc..66caa80df 100644 --- a/AnarchyExploitFixesLegacy/pom.xml +++ b/AnarchyExploitFixesLegacy/pom.xml @@ -43,6 +43,10 @@ ${project.name}-${project.parent.version} false + + com.github.benmanes.caffeine + me.moomoo.anarchyexploitfixes.caffeine + io.papermc.lib me.moomoo.anarchyexploitfixes.paperlib diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BeehiveCoordinates.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BeehiveCoordinates.java index 9c6c26906..db639dbc0 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BeehiveCoordinates.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/patches/BeehiveCoordinates.java @@ -32,6 +32,7 @@ public class BeehiveCoordinates implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; private final HashSet beehiveTypes = new HashSet<>(2); private final boolean logIsEnabled; + private boolean serverHasBeehives; public BeehiveCoordinates() { this.plugin = AnarchyExploitFixes.getInstance(); @@ -41,14 +42,19 @@ public BeehiveCoordinates() { List configuredStorageTypes = config.getList("patches.remove-beehive-coordinates.beehive-types-to-check", Arrays.asList( "BEEHIVE", "BEE_NEST" )); - if (AnarchyExploitFixes.getMCVersion() < 15) return; - for (String configuredType : configuredStorageTypes) { - try { - Material typeToCheck = Material.valueOf(configuredType); - beehiveTypes.add(typeToCheck); - } catch (IllegalArgumentException e) { - materialNotRecognized(Level.WARNING, name(), configuredType); + try { + Class.forName("org.bukkit.block.Beehive"); + this.serverHasBeehives = true; + for (String configuredType : configuredStorageTypes) { + try { + Material typeToCheck = Material.valueOf(configuredType); + beehiveTypes.add(typeToCheck); + } catch (IllegalArgumentException e) { + materialNotRecognized(Level.WARNING, name(), configuredType); + } } + } catch (ClassNotFoundException e) { + this.serverHasBeehives = false; } } @@ -70,7 +76,7 @@ public void enable() { @Override public boolean shouldEnable() { - return AnarchyExploitFixes.getConfiguration().getBoolean("patches.remove-beehive-coordinates.enable", true) && AnarchyExploitFixes.getMCVersion() >= 15; + return AnarchyExploitFixes.getConfiguration().getBoolean("patches.remove-beehive-coordinates.enable", true) && serverHasBeehives; } private void handleBeehiveIfPresent(ItemStack item) { 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 3d5c1fc0d..ff90d3916 100644 --- 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 @@ -62,10 +62,10 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) private void onRedstonePowerTrapdoor(BlockRedstoneEvent event) { - Block trapdoor = event.getBlock(); - if (!trapdoor.getType().name().contains("TRAPDOOR")) return; + Block block = event.getBlock(); + if (!block.getType().name().contains("TRAPDOOR")) return; - final Location trapdoorLoc = trapdoor.getLocation(); + final Location trapdoorLoc = block.getLocation(); final long currentTime = System.currentTimeMillis(); if (!trapdoorActivationByRedstoneCounts.containsKey(trapdoorLoc) || !cooldowns.containsKey(trapdoorLoc)) { @@ -78,7 +78,7 @@ private void onRedstonePowerTrapdoor(BlockRedstoneEvent event) { if (trapdoorOpenByRedstoneCount >= trapdoorActivationLimit) { if (currentTime - cooldowns.get(trapdoorLoc) < trapdoorRedstoneCooldownMillis) { - trapdoor.setType(air); + block.setType(air); if (logIsEnabled) LogUtils.moduleLog(Level.WARNING, name(), "Someone tried to crash the server using trapdoors and redstone at: " + trapdoorLoc); return; } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/craftrecipe/AutoRecipePacketListener.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/craftrecipe/AutoRecipePacketListener.java index c1e4f2c16..96a591180 100644 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/craftrecipe/AutoRecipePacketListener.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/protocollib/craftrecipe/AutoRecipePacketListener.java @@ -5,53 +5,36 @@ import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketEvent; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerQuitEvent; -import java.util.HashMap; +import java.time.Duration; import java.util.UUID; -public class AutoRecipePacketListener extends PacketAdapter implements Listener { +public class AutoRecipePacketListener extends PacketAdapter { - private final HashMap playersClickingCraftingRecipes = new HashMap<>(); - private final long craftingRecipeDelayInMillis; + private final Cache recipeCooldowns; protected AutoRecipePacketListener(AnarchyExploitFixes plugin, long craftingRecipeDelayInMillis) { super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.AUTO_RECIPE); - this.craftingRecipeDelayInMillis = craftingRecipeDelayInMillis; + this.recipeCooldowns = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(craftingRecipeDelayInMillis)).build(); } protected void register() { ProtocolLibrary.getProtocolManager().addPacketListener(this); - plugin.getServer().getPluginManager().registerEvents(this, plugin); } @Override public void onPacketReceiving(PacketEvent event) { if (event.isPlayerTemporary() || event.getPlayer() == null) return; - UUID playerUniqueID = event.getPlayer().getUniqueId(); - if ( - playersClickingCraftingRecipes.containsKey(playerUniqueID) - && playersClickingCraftingRecipes.get(playerUniqueID) > System.currentTimeMillis() - ) { + final UUID playerUniqueID = event.getPlayer().getUniqueId(); + + if (recipeCooldowns.getIfPresent(playerUniqueID) != null) { event.setCancelled(true); } else { - playersClickingCraftingRecipes.put(playerUniqueID, System.currentTimeMillis() + craftingRecipeDelayInMillis); + recipeCooldowns.put(playerUniqueID, true); } } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - private void onPlayerQuit(PlayerQuitEvent event) { - playersClickingCraftingRecipes.remove(event.getPlayer().getUniqueId()); - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - private void onPlayerKick(PlayerKickEvent event) { - playersClickingCraftingRecipes.remove(event.getPlayer().getUniqueId()); - } -} +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index ddbf5100a..0249acaee 100644 --- a/pom.xml +++ b/pom.xml @@ -155,6 +155,13 @@ 3.0.2 compile + + + com.github.ben-manes.caffeine + caffeine + 3.1.8 + compile + org.apache.commons