Skip to content

Commit

Permalink
improve anti trapdoor crash
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 27, 2023
1 parent 2d0685a commit 8c66f2a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@
import org.bukkit.event.block.BlockPlaceEvent;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;
import java.util.stream.Collectors;

public class BedAuraDelay implements AnarchyExploitFixesModule, Listener {

private final ExpiringSet<UUID> breakCooldowns;
private final ExpiringSet<UUID> placeCooldowns;
private final HashSet<Material> beds = new HashSet<>();
private final HashSet<Material> beds;
private final long breakDelayMillis, placeDelayMillis;

public BedAuraDelay() {
shouldEnable();
this.beds.addAll(Arrays.stream(Material.values()).filter(MaterialTags.BEDS::isTagged).collect(Collectors.toSet()));
this.beds = new HashSet<>(MaterialTags.BEDS.getValues());
Config config = AnarchyExploitFixes.getConfiguration();
this.breakDelayMillis = config.getInt("combat.bed-aura-delay.break-delay-in-ticks", 5,
"Set to -1 to disable.") * 50L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class PreventProjectilesInPortals implements AnarchyExploitFixesModule, L

public PreventProjectilesInPortals() {
shouldEnable();
AnarchyExploitFixes.getConfiguration().addComment("preventions.portals.prevent-projectiles-in-portals", "Prevents a lag exploit. Might disable some chunk loader designs.");
AnarchyExploitFixes.getConfiguration().addComment("preventions.portals.prevent-projectiles-in-portals",
"Prevents a lag exploit. Might disable some chunk loader designs.");
}

@Override
Expand Down Expand Up @@ -47,7 +48,7 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onEntityPortal(EntityPortalEvent event) {
if (event.getEntity() instanceof Projectile) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.bukkit.event.player.PlayerInteractEvent;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;
import java.util.stream.Collectors;
Expand All @@ -26,15 +25,15 @@ public class BedAuraDelay implements AnarchyExploitFixesModule, Listener {

private final ExpiringSet<UUID> breakCooldowns;
private final ExpiringSet<UUID> placeCooldowns;
private final HashSet<Material> beds = new HashSet<>();
private final HashSet<Material> beds;
private final long breakDelayMillis, placeDelayMillis;

public BedAuraDelay() {
shouldEnable();
this.beds.addAll(Arrays.stream(XMaterial.values())
.filter(xMaterial -> xMaterial.isSupported() && XTag.BEDS.isTagged(xMaterial))
this.beds = XTag.BEDS.getValues().stream()
.filter(XMaterial::isSupported)
.map(XMaterial::parseMaterial)
.collect(Collectors.toSet()));
.collect(Collectors.toCollection(HashSet::new));
Config config = AnarchyExploitFixes.getConfiguration();
this.breakDelayMillis = config.getInt("combat.bed-aura-delay.break-delay-in-ticks", 5,
"Set to -1 to disable.") * 50L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class DispenserCrash implements AnarchyExploitFixesModule, Listener {
public DispenserCrash() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("patches.crash-exploits.prevent-dispenser-crash.enable", "Prevents dispensers from crashing the server when dispensing items out of bounds: https://www.youtube.com/watch?v=XL17P87O6xA");
config.addComment("patches.crash-exploits.prevent-dispenser-crash.enable",
"Prevents dispensers from crashing the server when dispensing items out of bounds: https://www.youtube.com/watch?v=XL17P87O6xA");
this.logIsEnabled = config.getBoolean("patches.crash-exploits.prevent-dispenser-crash.log", true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class EndGatewayCrash implements AnarchyExploitFixesModule, Listener {
public EndGatewayCrash() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("patches.crash-exploits.prevent-end-gateway-crash.enable", "Prevents a crash exploit involving boats and end gateways: https://www.youtube.com/watch?v=c5nVBQeYo-I");
config.addComment("patches.crash-exploits.prevent-end-gateway-crash.enable",
"Prevents a crash exploit involving boats and end gateways: https://www.youtube.com/watch?v=c5nVBQeYo-I");
this.logIsEnabled = config.getBoolean("patches.crash-exploits.prevent-end-gateway-crash.log", true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package me.moomoo.anarchyexploitfixes.modules.patches.crashexploits;

import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XTag;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
Expand All @@ -13,26 +16,34 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockRedstoneEvent;

import java.util.HashMap;
import java.time.Duration;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class RedstoneOnTrapdoorCrash implements AnarchyExploitFixesModule, Listener {

private final HashMap<Location, Long> cooldowns = new HashMap<>();
private final HashMap<Location, Integer> trapdoorActivationByRedstoneCounts = new HashMap<>();
private final boolean logIsEnabled;
private final Cache<Location, Integer> trapdoorActivationCache;
private final HashSet<Material> TRAPDOORS;
private final Material AIR;
private final int trapdoorActivationLimit;
private final long trapdoorRedstoneCooldownMillis;
private final Material air;
private final boolean logIsEnabled;

public RedstoneOnTrapdoorCrash() {
shouldEnable();
this.AIR = XMaterial.AIR.parseMaterial();
this.TRAPDOORS = XTag.TRAPDOORS.getValues().stream()
.filter(XMaterial::isSupported)
.map(XMaterial::parseMaterial)
.collect(Collectors.toCollection(HashSet::new));
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.enable", "prevents a powerful crash exploit present in 1.13 - 1.19.3");
config.addComment("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.enable",
"prevents a powerful crash exploit present in 1.13 - 1.19.3");
this.logIsEnabled = config.getBoolean("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.log", true);
this.trapdoorActivationLimit = config.getInt("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.max-trapdoor-activations-by-redstone-per-time", 20);
this.trapdoorRedstoneCooldownMillis = config.getInt("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.time-in-ticks", 60) * 50L;
this.air = XMaterial.AIR.parseMaterial();
this.trapdoorActivationLimit = config.getInt("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.max-trapdoor-activations-by-redstone-per-time", 10);
this.trapdoorActivationCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(
config.getInt("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.time-in-ticks", 30, "1 sec = 20 ticks") * 50L
)).build();
}

@Override
Expand All @@ -49,46 +60,31 @@ public String category() {
public void enable() {
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> {
trapdoorActivationByRedstoneCounts.clear();
cooldowns.clear();
}, 6000L, 6000L);
}

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("patches.crash-exploits.prevent-redstone-on-trapdoor-crash.enable", false);
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onRedstonePowerTrapdoor(BlockRedstoneEvent event) {
Block block = event.getBlock();
if (!block.getType().name().contains("TRAPDOOR")) return;
if (!TRAPDOORS.contains(block.getType())) return;

final Location trapdoorLoc = block.getLocation();
final long currentTime = System.currentTimeMillis();

if (!trapdoorActivationByRedstoneCounts.containsKey(trapdoorLoc) || !cooldowns.containsKey(trapdoorLoc)) {
trapdoorActivationByRedstoneCounts.put(trapdoorLoc, 1);
cooldowns.put(trapdoorLoc, currentTime);
return;
}

int trapdoorOpenByRedstoneCount = trapdoorActivationByRedstoneCounts.get(trapdoorLoc);

if (trapdoorOpenByRedstoneCount >= trapdoorActivationLimit) {
if (currentTime - cooldowns.get(trapdoorLoc) < trapdoorRedstoneCooldownMillis) {
block.setType(air);
if (logIsEnabled) LogUtils.moduleLog(Level.WARNING, name(), "Someone tried to crash the server using trapdoors and redstone at: " + trapdoorLoc);
return;
Integer activationCount = trapdoorActivationCache.getIfPresent(trapdoorLoc);

if (activationCount == null) {
// Trapdoor has not been cached yet
trapdoorActivationCache.put(trapdoorLoc, 1);
} else {
activationCount++;
trapdoorActivationCache.put(trapdoorLoc, activationCount);
if (activationCount > trapdoorActivationLimit) {
block.setType(AIR);
if (logIsEnabled) LogUtils.moduleLog(Level.WARNING, name(), "Prevented trapdoor crash at: " + trapdoorLoc);
}

trapdoorOpenByRedstoneCount = 1;
}

trapdoorOpenByRedstoneCount++;

trapdoorActivationByRedstoneCounts.put(trapdoorLoc, trapdoorOpenByRedstoneCount);
cooldowns.put(trapdoorLoc, currentTime);
}
}
}

0 comments on commit 8c66f2a

Please sign in to comment.