Skip to content

Commit

Permalink
improve chunklimit modules
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 26, 2023
1 parent 10491c0 commit 2d0685a
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,91 @@ public CustomEntityLimit() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("chunk-limits.entity-limits.custom-limit.enable", "Limit specific entity types per chunk.");
config.addComment("chunk-limits.entity-limits.custom-limit.enable",
"Limit specific entity types per chunk.");
this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.custom-limit.log-removals", true);
this.checkPeriod = config.getInt("chunk-limits.entity-limits.custom-limit.check-period-in-ticks", 1200, "Check all chunks every x ticks.");
this.checkPeriod = config.getInt("chunk-limits.entity-limits.custom-limit.check-period-in-ticks", 1200,
"Check all chunks every x ticks.");
this.enableChunkAgeSkip = config.getBoolean("chunk-limits.entity-limits.custom-limit.chunk-age-skip.enable", true);
this.minChunkAge = config.getInt("chunk-limits.entity-limits.custom-limit.chunk-age-skip.min-age-in-ticks", 800, "How long a chunk has to have been inhabited for it to be checked");
this.forceLoadEntities = config.getBoolean("chunk-limits.entity-limits.custom-limit.forceload-entities", false, "When a chunk is loaded, entities inside of it are not necessarily loaded as well.\nForce loading is worse for performance but could help in some scenarios.");
this.minChunkAge = config.getInt("chunk-limits.entity-limits.custom-limit.chunk-age-skip.min-age-in-ticks", 800,
"How long a chunk has to have been inhabited for it to be checked");
this.forceLoadEntities = config.getBoolean("chunk-limits.entity-limits.custom-limit.forceload-entities", false,
"When a chunk is loaded, entities inside of it are not necessarily loaded as well.\n" +
"Force loading is worse for performance but could help in some scenarios.");

Map<String, Object> defaults = new HashMap<>();
defaults.put("WITHER", 16);
defaults.put("WITHER_SKULL", 10);
defaults.put("COW", 10);
defaults.put("BLAZE", 10);
defaults.put("CREEPER", 10);
defaults.put("ENDERMAN", 10);
defaults.put("MAGMA_CUBE", 10);
defaults.put("PHANTOM", 10);
defaults.put("SLIME", 10);
defaults.put("SKELETON", 10);
defaults.put("STRAY", 10);
defaults.put("WITHER_SKELETON", 10);
defaults.put("SPIDER", 10);
defaults.put("CAVE_SPIDER", 10);
defaults.put("ZOMBIE", 10);
defaults.put("DROWNED", 10);
defaults.put("HUSK", 10);
defaults.put("PIGLIN_BRUTE", 10);
defaults.put("ZOMBIFIED_PIGLIN", 10);
defaults.put("HOGLIN", 10);
defaults.put("ZOGLIN", 10);
defaults.put("CHICKEN", 10);
defaults.put("WOLF", 15);
defaults.put("HORSE", 5);
defaults.put("SKELETON_HORSE", 5);
defaults.put("DONKEY", 5);
ConfigSection section = config.getConfigSection("chunk-limits.entity-limits.custom-limit.limited-types", defaults, "Google EntityType Spigot for the correct names. They have to match your minecraft version.");
defaults.put("PIG", 10);
defaults.put("SHEEP", 10);
defaults.put("COW", 10);
defaults.put("MUSHROOM_COW", 10);
defaults.put("WOLF", 10);
defaults.put("DONKEY", 10);
defaults.put("HORSE", 10);
defaults.put("MULE", 10);
defaults.put("SKELETON_HORSE", 10);
defaults.put("ZOMBIE_HORSE", 10);
defaults.put("GOAT", 10);
defaults.put("LLAMA", 10);
defaults.put("TRADER_LLAMA", 10);
defaults.put("BAT", 3);
defaults.put("CAT", 10);
defaults.put("OCELOT", 3);
defaults.put("DOLPHIN", 4);
defaults.put("ENDERMITE", 3);
defaults.put("FOX", 10);
defaults.put("PANDA", 5);
defaults.put("PARROT", 10);
defaults.put("POLAR_BEAR", 5);
defaults.put("RABBIT", 5);
defaults.put("SILVERFISH", 3);
defaults.put("STRIDER", 3);
defaults.put("EVOKER", 15);
defaults.put("VEX", 15);
defaults.put("PILLAGER", 15);
defaults.put("VINDICATOR", 15);
defaults.put("WITCH", 15);
defaults.put("RAVAGER", 15);
defaults.put("AXOLOTL", 10);
defaults.put("COD", 6);
defaults.put("SALMON", 6);
defaults.put("TROPICAL_FISH", 6);
defaults.put("PUFFERFISH", 3);
defaults.put("SQUID", 20);
defaults.put("GLOW_SQUID", 20);
defaults.put("FROG", 20);
defaults.put("TADPOLE", 20);
defaults.put("ALLAY", 20);
defaults.put("BEE", 15);
defaults.put("TURTLE", 20);
defaults.put("GUARDIAN", 20);
defaults.put("PIGLIN", 25);
defaults.put("IRON_GOLEM", 15);
defaults.put("ZOMBIE_VILLAGER", 25);
defaults.put("WANDERING_TRADER", 10);

ConfigSection section = config.getConfigSection("chunk-limits.entity-limits.custom-limit.limited-types", defaults,
"Google EntityType Spigot for the correct names. They have to match your minecraft version.");
for (String configuredEntity : section.getKeys(false)) {
try {
Integer maxAmountPerChunk = Integer.valueOf(section.getString(configuredEntity));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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;
import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet;
import org.bukkit.Chunk;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -23,7 +22,7 @@
public class FallingBlockLimit implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final Cache<Chunk, Boolean> checkedChunks;
private final ExpiringSet<Chunk> checkedChunks;
private final int maxFallingGravityBlockPerChunk;
private final boolean logIsEnabled;

Expand All @@ -36,9 +35,10 @@ public FallingBlockLimit() {
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.");
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();
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.\s
Prevents overchecking, because a physics event can be called multiple times for the same chunk.""") * 50L;
this.checkedChunks = new ExpiringSet<>(Duration.ofMillis(chunkCheckDelay));
}

@Override
Expand Down Expand Up @@ -69,7 +69,7 @@ public void disable() {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onBlockPhysics(BlockPhysicsEvent event) {
Chunk chunk = event.getBlock().getChunk();
if (checkedChunks.getIfPresent(chunk) != null) return;
if (checkedChunks.contains(chunk)) return;

int count = 0;
boolean removed_falling = false;
Expand All @@ -89,14 +89,14 @@ private void onBlockPhysics(BlockPhysicsEvent event) {
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);

checkedChunks.put(chunk, true);
checkedChunks.add(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();
if (checkedChunks.getIfPresent(chunk) != null) return;
if (checkedChunks.contains(chunk)) return;

int count = 0;
boolean removed_falling = false;
Expand All @@ -116,6 +116,6 @@ private void onChangeBlock(EntityChangeBlockEvent event) {
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);

checkedChunks.put(chunk, true);
checkedChunks.add(chunk);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -67,16 +66,19 @@ public void disable() {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onSpawn(EntitySpawnEvent event) {
if (event.getEntity() instanceof LivingEntity || event.getEntityType().equals(EntityType.DROPPED_ITEM)) return;
if (event.getEntityType().equals(EntityType.DROPPED_ITEM) || event.getEntity() instanceof LivingEntity) return;

int count = 0;
for (Entity entity : event.getEntity().getChunk().getEntities()) {
if (entity instanceof Item) continue;
if (!(entity instanceof LivingEntity)) {
if (
!entity.getType().equals(EntityType.DROPPED_ITEM)
&& !entity.getType().equals(EntityType.ENDER_PEARL)
&& !(entity instanceof LivingEntity)
) {
count++;
}
if (count > maxNonLivingEntities) {
entity.getScheduler().run(plugin, kill -> entity.remove(), null);
if (count > maxNonLivingEntities) {
entity.getScheduler().run(plugin, kill -> entity.remove(), null);
}
}
}
}
Expand All @@ -90,7 +92,11 @@ private void run() {

int nonLiving = 0;
for (Entity entity : chunk.getEntities()) {
if (!(entity instanceof LivingEntity) && !(entity instanceof Item)) {
if (
!entity.getType().equals(EntityType.DROPPED_ITEM)
&& !entity.getType().equals(EntityType.ENDER_PEARL)
&& !(entity instanceof LivingEntity)
) {
nonLiving++;
if (nonLiving > maxNonLivingEntities) {
entity.getScheduler().run(plugin, kill -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,85 @@ public CustomEntityLimit() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("chunk-limits.entity-limits.custom-limit.enable", "Limit specific entity types per chunk.");
config.addComment("chunk-limits.entity-limits.custom-limit.enable",
"Limit specific entity types per chunk.");
this.logIsEnabled = config.getBoolean("chunk-limits.entity-limits.custom-limit.log-removals", true);
this.checkPeriod = config.getInt("chunk-limits.entity-limits.custom-limit.check-period-in-ticks", 1200, "check all chunks every x ticks.");
this.checkPeriod = config.getInt("chunk-limits.entity-limits.custom-limit.check-period-in-ticks", 1200,
"Check all chunks every x ticks.");

Map<String, Object> defaults = new HashMap<>();
defaults.put("WITHER", 16);
defaults.put("WITHER_SKULL", 10);
defaults.put("COW", 10);
defaults.put("BLAZE", 10);
defaults.put("CREEPER", 10);
defaults.put("ENDERMAN", 10);
defaults.put("MAGMA_CUBE", 10);
defaults.put("PHANTOM", 10);
defaults.put("SLIME", 10);
defaults.put("SKELETON", 10);
defaults.put("STRAY", 10);
defaults.put("WITHER_SKELETON", 10);
defaults.put("SPIDER", 10);
defaults.put("CAVE_SPIDER", 10);
defaults.put("ZOMBIE", 10);
defaults.put("DROWNED", 10);
defaults.put("HUSK", 10);
defaults.put("PIGLIN_BRUTE", 10);
defaults.put("ZOMBIFIED_PIGLIN", 20);
defaults.put("HOGLIN", 10);
defaults.put("ZOGLIN", 10);
defaults.put("CHICKEN", 10);
defaults.put("WOLF", 15);
defaults.put("HORSE", 5);
defaults.put("SKELETON_HORSE", 5);
defaults.put("DONKEY", 5);
ConfigSection section = config.getConfigSection("chunk-limits.entity-limits.custom-limit.limited-types", defaults, "Google EntityType Spigot for the correct names. They have to match your minecraft version.");
defaults.put("PIG", 10);
defaults.put("SHEEP", 10);
defaults.put("COW", 10);
defaults.put("MUSHROOM_COW", 10);
defaults.put("WOLF", 10);
defaults.put("DONKEY", 10);
defaults.put("HORSE", 10);
defaults.put("MULE", 10);
defaults.put("SKELETON_HORSE", 10);
defaults.put("ZOMBIE_HORSE", 10);
defaults.put("GOAT", 10);
defaults.put("LLAMA", 10);
defaults.put("TRADER_LLAMA", 10);
defaults.put("BAT", 3);
defaults.put("CAT", 10);
defaults.put("OCELOT", 3);
defaults.put("DOLPHIN", 4);
defaults.put("ENDERMITE", 3);
defaults.put("FOX", 10);
defaults.put("PANDA", 5);
defaults.put("PARROT", 10);
defaults.put("POLAR_BEAR", 5);
defaults.put("RABBIT", 5);
defaults.put("SILVERFISH", 3);
defaults.put("STRIDER", 3);
defaults.put("EVOKER", 15);
defaults.put("VEX", 15);
defaults.put("PILLAGER", 15);
defaults.put("VINDICATOR", 15);
defaults.put("WITCH", 15);
defaults.put("RAVAGER", 15);
defaults.put("AXOLOTL", 10);
defaults.put("COD", 6);
defaults.put("SALMON", 6);
defaults.put("TROPICAL_FISH", 6);
defaults.put("PUFFERFISH", 3);
defaults.put("SQUID", 20);
defaults.put("GLOW_SQUID", 20);
defaults.put("FROG", 20);
defaults.put("TADPOLE", 20);
defaults.put("ALLAY", 20);
defaults.put("BEE", 15);
defaults.put("TURTLE", 20);
defaults.put("GUARDIAN", 20);
defaults.put("PIGLIN", 25);
defaults.put("IRON_GOLEM", 15);
defaults.put("ZOMBIE_VILLAGER", 25);
defaults.put("WANDERING_TRADER", 10);

ConfigSection section = config.getConfigSection("chunk-limits.entity-limits.custom-limit.limited-types", defaults,
"Google EntityType Spigot for the correct names. They have to match your minecraft version.");
for (String configuredEntity : section.getKeys(false)) {
try {
EntityType limitedEntity = EntityType.valueOf(configuredEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
public class DroppedItemLimit implements AnarchyExploitFixesModule, Listener, Runnable {

private final AnarchyExploitFixes plugin;
private final boolean logIsEnabled, usingWhitelist;
private final int maxDroppedItemsPerChunk;
private final long checkPeriod;
private final HashSet<Material> whitelistedItems = new HashSet<>();
private final long checkPeriod;
private final int maxDroppedItemsPerChunk;
private final boolean logIsEnabled, usingWhitelist;

public DroppedItemLimit() {
shouldEnable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
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;
import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet;
import org.bukkit.Chunk;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -21,7 +20,7 @@

public class FallingBlockLimit implements AnarchyExploitFixesModule, Listener {

private final Cache<Chunk, Boolean> checkedChunks;
private final ExpiringSet<Chunk> checkedChunks;
private final int maxFallingGravityBlockPerChunk;
private final boolean logIsEnabled;

Expand All @@ -34,8 +33,9 @@ public FallingBlockLimit() {
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();
"Delay in ticks until the same chunk can be checked again.\n" +
"Prevents overchecking because a physics event can be called multiple times for the same chunk.") * 50L;
this.checkedChunks = new ExpiringSet<>(Duration.ofMillis(chunkCheckDelay));
}

@Override
Expand All @@ -62,7 +62,7 @@ public boolean shouldEnable() {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onBlockPhysics(BlockPhysicsEvent event) {
Chunk chunk = event.getBlock().getChunk();
if (checkedChunks.getIfPresent(chunk) != null) return;
if (checkedChunks.contains(chunk)) return;

int count = 0;
boolean removed_falling = false;
Expand All @@ -77,7 +77,7 @@ private void onBlockPhysics(BlockPhysicsEvent event) {
}
}

checkedChunks.put(chunk, true);
checkedChunks.add(chunk);

if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getSourceBlock().getLocation()
Expand All @@ -89,7 +89,7 @@ private void onBlockPhysics(BlockPhysicsEvent event) {
private void onChangeBlock(EntityChangeBlockEvent event) {
if (!event.getEntityType().equals(EntityType.FALLING_BLOCK)) return;
Chunk chunk = event.getBlock().getChunk();
if (checkedChunks.getIfPresent(chunk) != null) return;
if (checkedChunks.contains(chunk)) return;

int count = 0;
boolean removed_falling = false;
Expand All @@ -104,7 +104,7 @@ private void onChangeBlock(EntityChangeBlockEvent event) {
}
}

checkedChunks.put(chunk, true);
checkedChunks.add(chunk);

if (logIsEnabled && removed_falling) moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getBlock().getLocation()
Expand Down
Loading

0 comments on commit 2d0685a

Please sign in to comment.