Skip to content

Commit

Permalink
improvements to fallingblocklimit and a few more things
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Nov 29, 2023
1 parent 1077de5 commit 64df9d1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.moomoo.anarchyexploitfixes.modules.chunklimits;

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 @@ -12,29 +14,31 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.world.ChunkUnloadEvent;

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

import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog;

public class FallingBlockLimit implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final HashMap<String, Long> checkedChunks = new HashMap<>();
private final Cache<Chunk, Boolean> checkedChunks;
private final int maxFallingGravityBlockPerChunk;
private final boolean logIsEnabled;
private final long chunkCheckDelay;

public FallingBlockLimit() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("chunk-limits.falling-block-limit.enable", "Prevent players from placing massive sand chunks and killing the server.");
config.addComment("chunk-limits.falling-block-limit.enable",
"Prevent players from placing massive sand chunks and killing the server.");
this.logIsEnabled = config.getBoolean("chunk-limits.falling-block-limit.log", false);
this.maxFallingGravityBlockPerChunk = config.getInt("chunk-limits.falling-block-limit.max-falling-gravitiy-blocks-per-chunk", 60, "Removes any falling block if there is more than x blocks (actively) falling in a chunk.");
this.chunkCheckDelay = config.getInt("chunk-limits.falling-block-limit.chunk-check-delay-in-ticks", 20, "Delay in ticks until the same chunk can be checked again (avoids overchecking because a physics event can be called multiple times for the same chunk)") * 50L;
this.maxFallingGravityBlockPerChunk = config.getInt("chunk-limits.falling-block-limit.max-falling-gravitiy-blocks-per-chunk", 60,
"Removes any falling block if there is more than x blocks (actively) falling in a chunk.");
final long chunkCheckDelay = config.getInt("chunk-limits.falling-block-limit.chunk-check-delay-in-ticks", 20,
"Delay in ticks until the same chunk can be checked again (prevents overchecking because a physics event can be called multiple times for the same chunk)") * 50L;
this.checkedChunks = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(chunkCheckDelay)).build();
}

@Override
Expand Down Expand Up @@ -65,12 +69,7 @@ public void disable() {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onBlockPhysics(BlockPhysicsEvent event) {
Chunk chunk = event.getBlock().getChunk();
String chunkUID = chunk.getWorld().getName() + chunk.getChunkKey();

if (
!checkedChunks.containsKey(chunkUID)
|| checkedChunks.get(chunkUID) <= System.currentTimeMillis()
) {
if (checkedChunks.getIfPresent(chunk) == null) {
int count = 0;
boolean removed_falling = false;

Expand All @@ -89,7 +88,7 @@ private void onBlockPhysics(BlockPhysicsEvent event) {
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);

checkedChunks.put(chunkUID, System.currentTimeMillis() + chunkCheckDelay);
checkedChunks.put(chunk, true);
}
}

Expand All @@ -98,12 +97,8 @@ private void onChangeBlock(EntityChangeBlockEvent event) {
if (!event.getEntityType().equals(EntityType.FALLING_BLOCK)) return;

Chunk chunk = event.getBlock().getChunk();
String chunkId = chunk.getWorld().getName() + chunk.getChunkKey();

if (
!checkedChunks.containsKey(chunkId)
|| checkedChunks.get(chunkId) <= System.currentTimeMillis()
) {
if (checkedChunks.getIfPresent(chunk) == null) {
int count = 0;
boolean removed_falling = false;

Expand All @@ -122,12 +117,7 @@ private void onChangeBlock(EntityChangeBlockEvent event) {
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);

checkedChunks.put(chunkId, System.currentTimeMillis() + chunkCheckDelay);
checkedChunks.put(chunk, true);
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onChunkUnload(ChunkUnloadEvent event) {
checkedChunks.remove(String.valueOf(event.getChunk().getWorld().getUID()) + event.getChunk().getChunkKey());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityTargetEvent;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -76,8 +77,8 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onTargetAcquire(EntityPathfindEvent event) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPathfind(EntityPathfindEvent event) {
final double targetDistance = event.getEntity().getLocation().distance(event.getLoc());

if (globalDistanceEnabled) {
Expand All @@ -104,4 +105,36 @@ private void onTargetAcquire(EntityPathfindEvent event) {
}
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onTargetAcquire(EntityTargetEvent event) {
if (event.getTarget() == null) return;
final double targetDistance = event.getEntity().getLocation().distance(event.getTarget().getLocation());

if (globalDistanceEnabled) {
if (targetDistance > globalMaxDistance) {
event.setCancelled(true);
event.setTarget(null);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(),
"Cancelled entity "+event.getEntityType()+" from targeting entity at "
+event.getTarget().getLocation().toBlockLocation()
+" because target is further than the global limit. Distance: "+targetDistance
);
return;
}
}

if (perTypeDistanceEnabled) {
final EntityType pathfindingType = event.getEntityType();
if (limitedTypes.containsKey(pathfindingType) && targetDistance > limitedTypes.get(pathfindingType)) {
event.setCancelled(true);
event.setTarget(null);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(),
"Cancelled entity "+event.getEntityType()+" from targeting entity at "
+event.getTarget().getLocation().toBlockLocation()
+" because target is further than the global limit. Distance: "+targetDistance
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.moomoo.anarchyexploitfixes.modules.chunklimits;

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 @@ -11,27 +13,29 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.world.ChunkUnloadEvent;

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

import static me.moomoo.anarchyexploitfixes.utils.LogUtils.moduleLog;

public class FallingBlockLimit implements AnarchyExploitFixesModule, Listener {

private final HashMap<String, Long> checkedChunks = new HashMap<>();
private final Cache<Chunk, Boolean> checkedChunks;
private final int maxFallingGravityBlockPerChunk;
private final boolean logIsEnabled;
private final long chunkCheckDelay;

public FallingBlockLimit() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("chunk-limits.falling-block-limit.enable", "Prevent players from placing massive sand chunks and killing the server.");
config.addComment("chunk-limits.falling-block-limit.enable",
"Prevent players from placing massive sand chunks and killing the server.");
this.logIsEnabled = config.getBoolean("chunk-limits.falling-block-limit.log", false);
this.maxFallingGravityBlockPerChunk = config.getInt("chunk-limits.falling-block-limit.max-falling-gravitiy-blocks-per-chunk", 60, "Removes any falling block if there is more than x blocks (actively) falling in a chunk.");
this.chunkCheckDelay = config.getInt("chunk-limits.falling-block-limit.chunk-check-delay-in-ticks", 20, "Delay in ticks until the same chunk can be checked again (avoids overchecking because a physics event can be called multiple times for the same chunk)") * 50L;
this.maxFallingGravityBlockPerChunk = config.getInt("chunk-limits.falling-block-limit.max-falling-gravitiy-blocks-per-chunk", 60,
"Removes any falling block if there is more than x blocks (actively) falling in a chunk.");
final long chunkCheckDelay = config.getInt("chunk-limits.falling-block-limit.chunk-check-delay-in-ticks", 20,
"Delay in ticks until the same chunk can be checked again (prevents overchecking because a physics event can be called multiple times for the same chunk)") * 50L;
this.checkedChunks = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(chunkCheckDelay)).build();
}

@Override
Expand All @@ -58,69 +62,53 @@ public boolean shouldEnable() {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onBlockPhysics(BlockPhysicsEvent event) {
Chunk chunk = event.getBlock().getChunk();
String chunkId = String.valueOf(chunk.getWorld().getUID()) + chunk.getChunkKey();

if (
!checkedChunks.containsKey(chunkId)
|| checkedChunks.get(chunkId) <= System.currentTimeMillis()
) {
int count = 0;
boolean removed_falling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType().equals(EntityType.FALLING_BLOCK)) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
if (checkedChunks.getIfPresent(chunk) != null) return;

int count = 0;
boolean removed_falling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType().equals(EntityType.FALLING_BLOCK)) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
}
}

if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getSourceBlock().getLocation()
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);
checkedChunks.put(chunk, true);

checkedChunks.put(chunkId, System.currentTimeMillis() + chunkCheckDelay);
}
if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getSourceBlock().getLocation()
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onChangeBlock(EntityChangeBlockEvent event) {
if (!event.getEntityType().equals(EntityType.FALLING_BLOCK)) return;

Chunk chunk = event.getBlock().getChunk();
String chunkId = String.valueOf(chunk.getWorld().getUID()) + chunk.getChunkKey();

if (
!checkedChunks.containsKey(chunkId)
|| checkedChunks.get(chunkId) <= System.currentTimeMillis()
) {
int count = 0;
boolean removed_falling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType().equals(EntityType.FALLING_BLOCK)) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
}
}
if (checkedChunks.getIfPresent(chunk) != null) return;

if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getBlock().getLocation()
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);
int count = 0;
boolean removed_falling = false;

checkedChunks.put(chunkId, System.currentTimeMillis() + chunkCheckDelay);
for (Entity entity : chunk.getEntities()) {
if (entity.getType().equals(EntityType.FALLING_BLOCK)) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
}
}
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onChunkUnload(ChunkUnloadEvent event) {
checkedChunks.remove(String.valueOf(event.getChunk().getWorld().getUID()) + event.getChunk().getChunkKey());
checkedChunks.put(chunk, true);

if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getBlock().getLocation()
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityTargetEvent;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -70,8 +71,8 @@ public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("lag-preventions.pathfinding-limits.enable", false);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onTargetAcquire(EntityPathfindEvent event) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPathfind(EntityPathfindEvent event) {
final double targetDistance = event.getEntity().getLocation().distance(event.getLoc());

if (globalDistanceEnabled) {
Expand All @@ -98,4 +99,36 @@ private void onTargetAcquire(EntityPathfindEvent event) {
}
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onTargetAcquire(EntityTargetEvent event) {
if (event.getTarget() == null) return;
final double targetDistance = event.getEntity().getLocation().distance(event.getTarget().getLocation());

if (globalDistanceEnabled) {
if (targetDistance > globalMaxDistance) {
event.setCancelled(true);
event.setTarget(null);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(),
"Cancelled entity "+event.getEntityType()+" from targeting entity at "
+event.getTarget().getLocation().toBlockLocation()
+" because target is further than the global limit. Distance: "+targetDistance
);
return;
}
}

if (perTypeDistanceEnabled) {
final EntityType pathfindingType = event.getEntityType();
if (limitedTypes.containsKey(pathfindingType) && targetDistance > limitedTypes.get(pathfindingType)) {
event.setCancelled(true);
event.setTarget(null);
if (logIsEnabled) LogUtils.moduleLog(Level.INFO, name(),
"Cancelled entity "+event.getEntityType()+" from targeting entity at "
+event.getTarget().getLocation().toBlockLocation()
+" because target is further than the global limit. Distance: "+targetDistance
);
}
}
}
}

0 comments on commit 64df9d1

Please sign in to comment.