/
minecraftbackup
/
AnarchyExploitFixes
Обзор
Документация
Войти
/
minecraftbackup
/
AnarchyExploitFixes
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/NonLivingEntityLimit.java
106 строк
4 KB
xGinko
rework comments in folia build
06 июн 2024, 23:22
06 июн 2024, 23:22
d3bbe93
Код
Авторство
О чём код?
package me.xginko.aef.modules.chunklimits; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import me.xginko.aef.modules.AEFModule; import me.xginko.aef.utils.EntityUtil; import me.xginko.aef.utils.LocationUtil; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntitySpawnEvent; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; public class NonLivingEntityLimit extends AEFModule implements Consumer<ScheduledTask>, Listener { private ScheduledTask scheduledTask; private final long checkPeriod; private final int maxNonLivingEntities; private final boolean logIsEnabled; public NonLivingEntityLimit() { super("chunk-limits.entity-limits.non-living-limit"); config.addComment(configPath + ".enable", """ Limit the amount of non living entities in a chunk to prevent lag.\s Ignores dropped items."""); this.logIsEnabled = config.getBoolean(configPath + ".log-removals", true); this.maxNonLivingEntities = config.getInt(configPath + ".max-non-living-per-chunk", 100); this.checkPeriod = config.getInt(configPath + ".check-period-in-ticks", 20); } @Override public void enable() { plugin.getServer().getPluginManager().registerEvents(this, plugin); scheduledTask = plugin.getServer().getGlobalRegionScheduler() .runAtFixedRate(plugin, this, checkPeriod, checkPeriod); } @Override public boolean shouldEnable() { return config.getBoolean(configPath + ".enable", false); } @Override public void disable() { if (scheduledTask != null) scheduledTask.cancel(); HandlerList.unregisterAll(this); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onSpawn(EntitySpawnEvent event) { if (event.getEntityType().equals(EntityType.DROPPED_ITEM) || EntityUtil.isLivingEntity(event.getEntity())) return; int nonLivingCount = 0; for (Entity entity : event.getEntity().getChunk().getEntities()) { if (entity.getType().equals(EntityType.DROPPED_ITEM)) continue; if (EntityUtil.isLivingEntity(entity)) continue; nonLivingCount++; if (nonLivingCount <= maxNonLivingEntities) continue; event.setCancelled(true); entity.getScheduler().execute(plugin, () -> { entity.remove(); if (logIsEnabled) info( "Removed non-living entity " + entity.getType() + " at " + LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxNonLivingEntities); }, null, 1L); } } @Override public void accept(ScheduledTask task) { for (World world : plugin.getServer().getWorlds()) { for (Chunk chunk : world.getLoadedChunks()) { plugin.getServer().getRegionScheduler().execute(plugin, world, chunk.getX(), chunk.getZ(), () -> { if (!chunk.isEntitiesLoaded()) return; AtomicInteger nonLivingCount = new AtomicInteger(); for (Entity entity : chunk.getEntities()) { entity.getScheduler().execute(plugin, () -> { if (entity.getType().equals(EntityType.DROPPED_ITEM)) return; if (EntityUtil.isLivingEntity(entity)) return; if (nonLivingCount.incrementAndGet() <= maxNonLivingEntities) return; entity.remove(); if (logIsEnabled) info( "Removed non-living entity " + entity.getType() + " at " + LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxNonLivingEntities); }, null, 1L); } }); } } } }