Skip to content

Commit

Permalink
check for class instead of version
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Nov 29, 2023
1 parent 0e2b312 commit 1077de5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 40 deletions.
4 changes: 4 additions & 0 deletions AnarchyExploitFixesFolia/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<finalName>${project.name}-${project.parent.version}</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>me.moomoo.anarchyexploitfixes.caffeine</shadedPattern>
</relocation>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>me.moomoo.anarchyexploitfixes.paperlib</shadedPattern>
Expand Down
4 changes: 4 additions & 0 deletions AnarchyExploitFixesLegacy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<finalName>${project.name}-${project.parent.version}</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>me.moomoo.anarchyexploitfixes.caffeine</shadedPattern>
</relocation>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>me.moomoo.anarchyexploitfixes.paperlib</shadedPattern>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class BeehiveCoordinates implements AnarchyExploitFixesModule, Listener {
private final AnarchyExploitFixes plugin;
private final HashSet<Material> beehiveTypes = new HashSet<>(2);
private final boolean logIsEnabled;
private boolean serverHasBeehives;

public BeehiveCoordinates() {
this.plugin = AnarchyExploitFixes.getInstance();
Expand All @@ -41,14 +42,19 @@ public BeehiveCoordinates() {
List<String> 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;
}
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, Long> playersClickingCraftingRecipes = new HashMap<>();
private final long craftingRecipeDelayInMillis;
private final Cache<UUID, Boolean> 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());
}
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
<!-- Fast caching -->
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.8</version>
<scope>compile</scope>
</dependency>
<!-- FastMath -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down

0 comments on commit 1077de5

Please sign in to comment.