Skip to content

Commit

Permalink
adjust scheduled tasks for folia 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Feb 26, 2024
1 parent 3d6efe8 commit e3c8c8c
Show file tree
Hide file tree
Showing 42 changed files with 419 additions and 514 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onChunkLoad(ChunkLoadEvent event) {
if (!alsoCheckNewChunks && event.isNewChunk()) return;
final World world = event.getWorld();
if (exemptedWorlds.contains(world.getName())) return;
if (world.getEnvironment() != World.Environment.NETHER) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return;

Chunk chunk = event.getChunk();

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(),
fixBedrock -> ChunkUtil.createBedrockLayer(chunk, ceilingY));
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, ceilingY);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onChunkLoad(ChunkLoadEvent event) {
if (!alsoCheckNewChunks && event.isNewChunk()) return;
final World world = event.getWorld();
if (exemptedWorlds.contains(world.getName())) return;
if (world.getEnvironment() != World.Environment.NETHER) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return;

Chunk chunk = event.getChunk();

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(),
fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight()));
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,18 @@ public void disable() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onChunkLoad(ChunkLoadEvent event) {
if (!alsoCheckNewChunks && event.isNewChunk()) return;
final World world = event.getWorld();
if (exemptedWorlds.contains(world.getName())) return;
if (world.getEnvironment() != World.Environment.NETHER) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(event) <= pauseTPS)) return;

Chunk chunk = event.getChunk();

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(),
fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight()));
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class PeriodicallyFillNetherCeiling implements AnarchyExploitFixesModule
private final long checkPeriod;
private final double pauseTPS;
private final int ceilingY;
private final boolean checkShouldPauseOnLowTPS;
private final boolean pauseOnLowTPS;

public PeriodicallyFillNetherCeiling() {
shouldEnable();
Expand All @@ -29,7 +29,7 @@ public PeriodicallyFillNetherCeiling() {
this.ceilingY = config.nether_ceiling_max_y;
this.checkPeriod = config.getInt("bedrock.fill-in-bedrock.nether-ceiling.periodically-check-and-fill.check-period-in-seconds", 10) * 20L;
this.exemptedWorlds = new HashSet<>(config.getList("bedrock.fill-in-bedrock.nether-ceiling.exempted-worlds", List.of("exampleworld", "exampleworld2")));
this.checkShouldPauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.nether-ceiling.periodically-check-and-fill.pause-on-low-tps", true);
this.pauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.nether-ceiling.periodically-check-and-fill.pause-on-low-tps", true);
this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.nether-ceiling.periodically-check-and-fill.pause-tps", 16.0);
}

Expand Down Expand Up @@ -64,11 +64,11 @@ private void run() {
if (exemptedWorlds.contains(world.getName())) continue;

for (Chunk chunk : world.getLoadedChunks()) {
if (!chunk.isEntitiesLoaded()) continue;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue;

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(),
fixBedrock -> ChunkUtil.createBedrockLayer(chunk, ceilingY));
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixChunk -> {
if (!chunk.isEntitiesLoaded()) return;
if (pauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, ceilingY);
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ private void run() {
if (exemptedWorlds.contains(world.getName())) continue;

for (Chunk chunk : world.getLoadedChunks()) {
if (!chunk.isEntitiesLoaded()) continue;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue;

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(),
fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight()));
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (!chunk.isEntitiesLoaded()) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PeriodicallyFillOverworldFloor implements AnarchyExploitFixesModule
private final Set<String> exemptedWorlds;
private final long checkPeriod;
private final double pauseTPS;
private final boolean checkShouldPauseOnLowTPS;
private final boolean pauseOnLowTPS;

public PeriodicallyFillOverworldFloor() {
shouldEnable();
Expand All @@ -28,7 +28,7 @@ public PeriodicallyFillOverworldFloor() {
config.addComment("bedrock.fill-in-bedrock.overworld-floor.periodically-check-and-fill.enable","only checks loaded chunks");
this.checkPeriod = config.getInt("bedrock.fill-in-bedrock.overworld-floor.periodically-check-and-fill.check-period-in-seconds", 10) * 20L;
this.exemptedWorlds = new HashSet<>(config.getList("bedrock.fill-in-bedrock.overworld-floor.exempted-worlds", List.of("exampleworld", "exampleworld2")));
this.checkShouldPauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.overworld-floor.periodically-check-and-fill.pause-on-low-tps", true);
this.pauseOnLowTPS = config.getBoolean("bedrock.fill-in-bedrock.overworld-floor.periodically-check-and-fill.pause-on-low-tps", true);
this.pauseTPS = config.getDouble("bedrock.fill-in-bedrock.overworld-floor.periodically-check-and-fill.pause-tps", 16.0);
}

Expand Down Expand Up @@ -63,11 +63,11 @@ private void run() {
if (exemptedWorlds.contains(world.getName())) continue;

for (Chunk chunk : world.getLoadedChunks()) {
if (!chunk.isEntitiesLoaded()) continue;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS(world, chunk.getX(), chunk.getZ()) <= pauseTPS)) continue;

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(),
fixBedrock -> ChunkUtil.createBedrockLayer(chunk, world.getMinHeight()));
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (!chunk.isEntitiesLoaded()) return;
if (pauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ private void onSpawn(EntitySpawnEvent event) {
private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
if (!forceLoadEntities && !chunk.isEntitiesLoaded()) continue;
if (enableChunkAgeSkip && chunk.getInhabitedTime() < minChunkAge) continue;
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), checkChunk -> {
if (!forceLoadEntities && !chunk.isEntitiesLoaded()) return;
if (enableChunkAgeSkip && chunk.getInhabitedTime() < minChunkAge) return;

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), checkEntities -> {
Entity[] chunkEntities = chunk.getEntities();

for (Map.Entry<EntityType, Integer> limit : entityLimits.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.moomoo.anarchyexploitfixes.modules.chunklimits;

import com.destroystokyo.paper.MaterialTags;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
Expand All @@ -20,14 +21,16 @@
import org.bukkit.event.world.ChunkLoadEvent;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class DroppedItemLimit implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final Set<Material> whitelistedItems = new HashSet<>();
private final Set<Material> whitelistedItems;
private ScheduledTask scheduledTask;
private final long checkPeriod;
private final int maxDroppedItemsPerChunk;
Expand All @@ -45,18 +48,20 @@ public DroppedItemLimit() {
this.checkPeriod = config.getInt("chunk-limits.entity-limits.dropped-item-limit.check-period-in-ticks", 800,
"20 ticks = 1 second");
this.usingWhitelist = config.getBoolean("chunk-limits.entity-limits.dropped-item-limit.whitelist-specific-item-types", false);
config.getList("chunk-limits.entity-limits.dropped-item-limit.whitelisted-types", List.of(
"SHULKER_BOX", "BLACK_SHULKER_BOX", "BLUE_SHULKER_BOX", "BROWN_SHULKER_BOX", "CYAN_SHULKER_BOX", "GRAY_SHULKER_BOX",
"GREEN_SHULKER_BOX", "LIGHT_BLUE_SHULKER_BOX", "LIGHT_GRAY_SHULKER_BOX", "LIME_SHULKER_BOX", "MAGENTA_SHULKER_BOX",
"ORANGE_SHULKER_BOX", "PINK_SHULKER_BOX", "PURPLE_SHULKER_BOX", "RED_SHULKER_BOX", "WHITE_SHULKER_BOX", "YELLOW_SHULKER_BOX"
), "You need to use correct Material enums for your minecraft version here.").forEach(configuredWhitelistedType -> {
try {
Material whitelistedType = Material.valueOf(configuredWhitelistedType);
this.whitelistedItems.add(whitelistedType);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), configuredWhitelistedType);
}
});
this.whitelistedItems = config.getList("chunk-limits.entity-limits.dropped-item-limit.whitelisted-types",
MaterialTags.SHULKER_BOXES.getValues().stream().map(Enum::name).sorted().toList(),
"You need to use correct Material enums for your minecraft version here.")
.stream()
.map(configuredType -> {
try {
return Material.valueOf(configuredType);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized(Level.WARNING, name(), configuredType);
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toCollection(HashSet::new));
}

@Override
Expand Down Expand Up @@ -129,19 +134,17 @@ private void onItemDrop(ItemSpawnEvent event) {
private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
if (!chunk.isEntitiesLoaded()) continue;

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), chunkCheck -> {
int droppedItemCount = 0;
if (!chunk.isEntitiesLoaded()) return;

for (Entity entity : chunk.getEntities()) {
if (entity.getType() != EntityType.DROPPED_ITEM) continue;
AtomicInteger droppedItemCount = new AtomicInteger();

droppedItemCount++;
if (droppedItemCount <= maxDroppedItemsPerChunk) continue;
if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) continue;
for (Entity entity : chunk.getEntities()) {
entity.getScheduler().run(plugin, checkEntity -> {
if (entity.getType() != EntityType.DROPPED_ITEM) return;
if (droppedItemCount.incrementAndGet() <= maxDroppedItemsPerChunk) return;
if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) return;

entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at " +
LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxDroppedItemsPerChunk);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package me.moomoo.anarchyexploitfixes.modules.chunklimits;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
Expand All @@ -13,11 +16,14 @@
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ExpBottleEvent;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;

public class ExpBottleLimit implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private ScheduledTask scheduledTask;
private final long checkPeriod;
private final int maxExpBottlePerChunk;
private final boolean logIsEnabled;

Expand All @@ -30,6 +36,8 @@ public ExpBottleLimit() {
this.logIsEnabled = config.getBoolean("chunk-limits.exp-bottle-limit.log", false);
this.maxExpBottlePerChunk = config.getInt("chunk-limits.exp-bottle-limit.max-exp-bottle-per-chunk", 25,
"Max in a chunk, doesn't limit the actual xp orbs.");
this.checkPeriod = config.getInt("chunk-limits.exp-bottle-limit.check-period-in-ticks", 800,
"20 ticks = 1 second");
}

@Override
Expand All @@ -45,6 +53,7 @@ public String category() {
@Override
public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
this.scheduledTask = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, task -> run(), checkPeriod, checkPeriod);
}

@Override
Expand All @@ -55,6 +64,7 @@ public boolean shouldEnable() {
@Override
public void disable() {
HandlerList.unregisterAll(this);
if (scheduledTask != null) scheduledTask.cancel();
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand All @@ -75,4 +85,27 @@ private void onExpBottle(ExpBottleEvent event) {
}
}
}

private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), chunkCheck -> {
if (!chunk.isEntitiesLoaded()) return;

AtomicInteger droppedItemCount = new AtomicInteger();

for (Entity entity : chunk.getEntities()) {
entity.getScheduler().run(plugin, checkEntity -> {
if (entity.getType() != EntityType.THROWN_EXP_BOTTLE) return;
if (droppedItemCount.incrementAndGet() <= maxExpBottlePerChunk) return;

entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed XP-Bottle at " +
LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxExpBottlePerChunk);
}, null);
}
});
}
}
}
}
Loading

0 comments on commit e3c8c8c

Please sign in to comment.