Skip to content

Commit

Permalink
improve anti bed trap
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 27, 2023
1 parent 3b4b159 commit 8b184df
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.moomoo.anarchyexploitfixes.modules.preventions;

import com.destroystokyo.paper.event.player.PlayerPostRespawnEvent;
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 @@ -10,27 +12,28 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.time.Duration;
import java.util.UUID;
import java.util.logging.Level;

public class BedTrap implements AnarchyExploitFixesModule, Listener {

private final HashMap<UUID, Long> cooldowns = new HashMap<>();
private final HashMap<UUID, Integer> deathCounts = new HashMap<>();
private final boolean logIsEnabled;
private final Cache<UUID, Integer> playerDeathNearBedCount;
private final int maxDeathsPerTime;
private final long deathCooldownInMillis;
private final boolean logIsEnabled;

public BedTrap() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("preventions.anti-bed-trap.enable", "Resets Bed Respawn when a player dies too many times within a certain timeframe.");
config.addComment("preventions.anti-bed-trap.enable",
"Resets Bed Respawn when a player dies too many times within a certain timeframe.");
this.logIsEnabled = config.getBoolean("preventions.anti-bed-trap.log", false);
this.maxDeathsPerTime = config.getInt("preventions.anti-bed-trap.max-deaths-per-time", 8, "Amount of times player can die until he is determined as bed-trapped.");
this.deathCooldownInMillis = config.getInt("preventions.anti-bed-trap.time-in-seconds", 6, "Time until death counter will be reset again") * 1000L;
this.maxDeathsPerTime = config.getInt("preventions.anti-bed-trap.max-deaths-per-time", 7,
"Amount of times player can die until he is determined as bed-trapped.");
this.playerDeathNearBedCount = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(
config.getInt("preventions.anti-bed-trap.time-in-seconds", 5,
"Time until death counter will be reset again"))).build();
}

@Override
Expand Down Expand Up @@ -65,36 +68,19 @@ private void onRespawn(PlayerPostRespawnEvent event) {
if (!event.isBedSpawn()) return;

final UUID playerUniqueId = player.getUniqueId();
final long currentTime = System.currentTimeMillis();

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

int playerDeathCount = deathCounts.get(playerUniqueId);

if (playerDeathCount >= maxDeathsPerTime) {
if (currentTime - cooldowns.get(playerUniqueId) < deathCooldownInMillis) {
player.getScheduler().run(AnarchyExploitFixes.getInstance(), resetBed -> player.setBedSpawnLocation(null, true), null);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Reset bed respawn of potentially bed-trapped player '"+ player.getName() + "'");
return;
Integer nearBedDeaths = playerDeathNearBedCount.getIfPresent(playerUniqueId);

if (nearBedDeaths == null) {
playerDeathNearBedCount.put(playerUniqueId, 1);
} else {
nearBedDeaths++;
if (nearBedDeaths > maxDeathsPerTime) {
player.getScheduler().run(AnarchyExploitFixes.getInstance(), resetBed -> {
player.setBedSpawnLocation(null, true);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(),
"Reset bed respawn of potentially bed-trapped player '" + player.getName() + "'");
}, null);
}

playerDeathCount = 1;
}

playerDeathCount++;

deathCounts.put(playerUniqueId, playerDeathCount);
cooldowns.put(playerUniqueId, currentTime);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onLeave(PlayerQuitEvent event) {
final UUID playerUniqueId = event.getPlayer().getUniqueId();
deathCounts.remove(playerUniqueId);
cooldowns.remove(playerUniqueId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.moomoo.anarchyexploitfixes.modules.preventions;

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 @@ -10,29 +12,30 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.time.Duration;
import java.util.UUID;
import java.util.logging.Level;

public class BedTrap implements AnarchyExploitFixesModule, Listener {

private final HashMap<UUID, Long> cooldowns = new HashMap<>();
private final HashMap<UUID, Integer> deathCounts = new HashMap<>();
private final boolean logIsEnabled;
private final int maxDeathsPerTime;
private final long deathCooldownInMillis;
private final Cache<UUID, Integer> playerDeathNearBedCount;
private final double maxBedDistance;
private final int maxDeathsPerTime;
private final boolean logIsEnabled;

public BedTrap() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("preventions.anti-bed-trap.enable", "Resets Bed Respawn when a player dies too many times within a certain timeframe.");
config.addComment("preventions.anti-bed-trap.enable",
"Resets Bed Respawn when a player dies too many times within a certain timeframe.");
this.logIsEnabled = config.getBoolean("preventions.anti-bed-trap.log", false);
this.maxDeathsPerTime = config.getInt("preventions.anti-bed-trap.max-deaths-per-time", 7, "Amount of times player can die until he is determined as bed-trapped.");
this.deathCooldownInMillis = config.getInt("preventions.anti-bed-trap.time-in-seconds", 5, "Time until death counter will be reset again") * 1000L;
this.maxBedDistance = config.getDouble("preventions.anti-bed-trap.max-distance-to-bed", 6.0);
this.maxDeathsPerTime = config.getInt("preventions.anti-bed-trap.max-deaths-per-time", 7,
"Amount of times player can die until he is determined as bed-trapped.");
this.playerDeathNearBedCount = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(
config.getInt("preventions.anti-bed-trap.time-in-seconds", 5,
"Time until death counter will be reset again"))).build();
this.maxBedDistance = config.getDouble("preventions.anti-bed-trap.max-distance-from-bed", 6.0);
}

@Override
Expand All @@ -56,43 +59,24 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("preventions.anti-bed-trap.enable", false);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onDeath(PlayerDeathEvent event) {
Player player = event.getEntity();
if (!isNearBedSpawn(player)) return;

final UUID playerUniqueId = player.getUniqueId();
final long currentTime = System.currentTimeMillis();

if (!deathCounts.containsKey(playerUniqueId) || !cooldowns.containsKey(playerUniqueId)) {
deathCounts.put(playerUniqueId, 1);
cooldowns.put(playerUniqueId, currentTime);
return;
}
Integer nearBedDeaths = playerDeathNearBedCount.getIfPresent(playerUniqueId);

int playerDeathCount = deathCounts.get(playerUniqueId);

if (playerDeathCount >= maxDeathsPerTime) {
if (currentTime - cooldowns.get(playerUniqueId) < deathCooldownInMillis) {
if (nearBedDeaths == null) {
playerDeathNearBedCount.put(playerUniqueId, 1);
} else {
nearBedDeaths++;
if (nearBedDeaths > maxDeathsPerTime) {
player.setBedSpawnLocation(null, true);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(), "Reset bed respawn of potentially bed-trapped player '"+ player.getName() + "'");
return;
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(),
"Reset bed respawn of potentially bed-trapped player '" + player.getName() + "'");
}

playerDeathCount = 1;
}

playerDeathCount++;

deathCounts.put(playerUniqueId, playerDeathCount);
cooldowns.put(playerUniqueId, currentTime);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onLeave(PlayerQuitEvent event) {
final UUID playerUniqueId = event.getPlayer().getUniqueId();
deathCounts.remove(playerUniqueId);
cooldowns.remove(playerUniqueId);
}

private boolean isNearBedSpawn(Player player) {
Expand All @@ -101,4 +85,4 @@ private boolean isNearBedSpawn(Player player) {
if (!player.getWorld().getUID().equals(bedSpawn.getWorld().getUID())) return false;
return player.getLocation().distance(bedSpawn) > maxBedDistance;
}
}
}

0 comments on commit 8b184df

Please sign in to comment.