Skip to content

Commit

Permalink
try to be extra safe with some entity calls on folia
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 11, 2024
1 parent 2bf5ce9 commit 2c32575
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ private void onSpawn(EntitySpawnEvent event) {
entityCount++;
if (entityCount <= maxAllowedPerChunk) continue;

entity.getScheduler().run(plugin, kill -> entity.remove(), null);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType()
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk);
entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType()
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk);
}, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class VehicleLimit implements AnarchyExploitFixesModule, Listener {

private final AnarchyExploitFixes plugin;
private final Set<EntityType> VEHICLE_TYPES;
private final Set<EntityType> VEHICLE_TYPE_CACHE;
private ScheduledTask scheduledTask;
private final long checkPeriod;
private final int maxVehiclesPerChunk;
Expand All @@ -34,11 +34,11 @@ public class VehicleLimit implements AnarchyExploitFixesModule, Listener {
public VehicleLimit() {
shouldEnable();
this.plugin = AnarchyExploitFixes.getInstance();
this.VEHICLE_TYPES = new HashSet<>(); // Used to cache entity types of vehicles for faster operations
this.VEHICLE_TYPE_CACHE = new HashSet<>(); // Used to cache entity types of vehicles for faster operations
// Prefill with known types;
this.VEHICLE_TYPES.addAll(EntityUtil.BOATS);
this.VEHICLE_TYPES.addAll(EntityTags.HORSES.getValues());
this.VEHICLE_TYPES.addAll(EntityTags.MINECARTS.getValues());
this.VEHICLE_TYPE_CACHE.addAll(EntityUtil.BOATS);
this.VEHICLE_TYPE_CACHE.addAll(EntityTags.HORSES.getValues());
this.VEHICLE_TYPE_CACHE.addAll(EntityTags.MINECARTS.getValues());
Config config = AnarchyExploitFixes.getConfiguration();
config.addComment("chunk-limits.vehicle-limit.enable",
"Limit the amount of vehicles to prevent some lag machines involving boats and a dispenser.");
Expand Down Expand Up @@ -82,7 +82,7 @@ private void onCreate(VehicleCreateEvent event) {
for (Entity entity : event.getVehicle().getChunk().getEntities()) {
final EntityType type = entity.getType();

if (VEHICLE_TYPES.contains(type)) {
if (VEHICLE_TYPE_CACHE.contains(type)) {
vehicleCount++;
if (vehicleCount <= maxVehiclesPerChunk) continue;

Expand All @@ -97,7 +97,7 @@ private void onCreate(VehicleCreateEvent event) {
}

if (entity instanceof Vehicle) {
VEHICLE_TYPES.add(type); // Add type of vehicle to HashSet for faster operation in the future
VEHICLE_TYPE_CACHE.add(type); // Add type of vehicle to HashSet for faster operation in the future
vehicleCount++;
if (vehicleCount <= maxVehiclesPerChunk) continue;

Expand All @@ -115,15 +115,14 @@ private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), task -> {
Chunk.LoadLevel level = chunk.getLoadLevel();
if (!level.equals(Chunk.LoadLevel.ENTITY_TICKING) && !level.equals(Chunk.LoadLevel.TICKING)) return;
if (!chunk.isEntitiesLoaded()) return;

int vehicleCount = 0;

for (Entity entity : chunk.getEntities()) {
final EntityType type = entity.getType();

if (VEHICLE_TYPES.contains(type)) {
if (VEHICLE_TYPE_CACHE.contains(type)) {
vehicleCount++;
if (vehicleCount <= maxVehiclesPerChunk) continue;

Expand All @@ -139,7 +138,7 @@ private void run() {
}

if (entity instanceof Vehicle) {
VEHICLE_TYPES.add(type); // Add type of vehicle to HashSet for faster operation in the future
VEHICLE_TYPE_CACHE.add(type); // Add type of vehicle to HashSet for faster operation in the future
vehicleCount++;
if (vehicleCount <= maxVehiclesPerChunk) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ private void checkVillagersInChunk(Chunk chunk) {
// Remove prioritized villagers that are too many
for (int i = 0; i < amount_over_the_limit; i++) {
Villager villager = villagers_in_chunk.get(i);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(),
villager.getScheduler().run(plugin, kill -> {
villager.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(),
"Removing villager of profession type '"+villager.getProfession()+"' at "+villager.getLocation());
villager.getScheduler().run(plugin, kill -> villager.remove(), null);
}, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public FallingBlockStasis() {
this.logIsEnabled = config.getBoolean("lag-preventions.prevent-falling-block-stasis-exploit.log", false);
this.max_alive_time = config.getInt("lag-preventions.prevent-falling-block-stasis-exploit.falling-blocks-max-alive-time-in-ticks", 300,
"(20 ticks = 1 second)");
this.check_period_in_ticks = config.getInt("lag-preventions.prevent-falling-block-stasis-exploit.check-period-in-seconds", 120,
this.check_period_in_ticks = config.getInt("lag-preventions.prevent-falling-block-stasis-exploit.check-period-in-seconds", 12,
"How frequently we should check for all projectile's alive time") * 20L;
}

Expand Down Expand Up @@ -59,14 +59,14 @@ public void disable() {
private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (FallingBlock fallingBlock : world.getEntitiesByClass(FallingBlock.class)) {
if (fallingBlock.getTicksLived() > max_alive_time) {
fallingBlock.getScheduler().run(plugin, kill -> {
fallingBlock.getScheduler().run(plugin, killIfOld -> {
if (fallingBlock.getTicksLived() > max_alive_time) {
fallingBlock.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed falling block at x:"
+ fallingBlock.getLocation().getX() + " y:" + fallingBlock.getLocation().getY() + " z:" + fallingBlock.getLocation().getZ()
+ " in "+fallingBlock.getWorld().getName()+" because it was alive for more than " + max_alive_time + " ticks.");
}, null);
}
+ " in " + fallingBlock.getWorld().getName() + " because it was alive for more than " + max_alive_time + " ticks.");
}
}, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ private void onLiquidSpread(BlockFromToEvent event) {

if (liquidSpreadCount == null) {
liquidSpreadEventCountCache.put(chunk, 1);
} else {
liquidSpreadCount++;
liquidSpreadEventCountCache.put(chunk, liquidSpreadCount);
if (liquidSpreadCount > maxLiquidSpreadEventsPerChunk) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(),
"Cancelled liquid events for chunk x:" + chunk.getX() + ", z:" + chunk.getZ() +
"in world: " + chunk.getWorld().getName());
return;
}
return;
}

liquidSpreadCount++;
liquidSpreadEventCountCache.put(chunk, liquidSpreadCount);

if (liquidSpreadCount > maxLiquidSpreadEventsPerChunk) {
event.setCancelled(true);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(),
"Cancelled liquid events for chunk x:" + chunk.getX() + ", z:" + chunk.getZ() +
"in world: " + chunk.getWorld().getName());
return;
}

if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), " Recorded " + liquidSpreadCount +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ public boolean shouldEnable() {
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onPistonExtend(BlockPistonExtendEvent event) {
for (Block block : event.getBlocks()) {
if (
block.getBlockData() instanceof Waterlogged waterlogged
&& waterlogged.isWaterlogged()
) {
if (block.getBlockData() instanceof Waterlogged waterlogged && waterlogged.isWaterlogged()) {
if (shouldDeleteWaterlogged) block.setType(Material.AIR);
event.setCancelled(true);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public void disable() {
private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (Projectile projectile : world.getEntitiesByClass(Projectile.class)) {
final EntityType projectileType = projectile.getType();
if (
projectileType == EntityType.ENDER_PEARL
|| projectileType == EntityType.WITHER_SKULL
|| projectileType == EntityType.FISHING_HOOK
|| projectileType == EntityType.ENDER_SIGNAL
) continue;

if (projectile.getTicksLived() > max_alive_time) {
projectile.getScheduler().run(plugin, removeOld -> projectile.remove(), null);
switch (projectile.getType()) {
case ENDER_PEARL, WITHER_SKULL, FISHING_HOOK, ENDER_SIGNAL -> {
continue;
}
}

projectile.getScheduler().run(plugin, removeIfOld -> {
if (projectile.getTicksLived() > max_alive_time) {
projectile.remove();
}
}, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void disable() {
private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (WitherSkull witherSkull : world.getEntitiesByClass(WitherSkull.class)) {
witherSkull.getScheduler().run(plugin, remove -> witherSkull.remove(), null);
witherSkull.getScheduler().run(plugin, kill -> witherSkull.remove(), null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ public void disable() {
private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (WitherSkull witherSkull : world.getEntitiesByClass(WitherSkull.class)) {
if (witherSkull.getTicksLived() > maxAgeInTicks) {
witherSkull.getScheduler().run(plugin, kill -> witherSkull.remove(), null);
}
witherSkull.getScheduler().run(plugin, killIfOld -> {
if (witherSkull.getTicksLived() > maxAgeInTicks) {
witherSkull.remove();
}
}, null);
}
}
}
Expand Down

0 comments on commit 2c32575

Please sign in to comment.