Skip to content

Commit

Permalink
Changed it so entities will not be counted when chunks are unloaded/l…
Browse files Browse the repository at this point in the history
…oaded (#1862)
  • Loading branch information
OmerBenGera committed Sep 1, 2023
1 parent 91a2e0c commit 9417807
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Expand Up @@ -5,9 +5,12 @@
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.core.Singleton;
import com.bgsoftware.superiorskyblock.core.key.Keys;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.module.BuiltinModules;
import com.bgsoftware.superiorskyblock.module.upgrades.type.UpgradeTypeEntityLimits;
import com.bgsoftware.superiorskyblock.world.BukkitEntities;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -107,7 +110,16 @@ private class PaperDeathListener implements Listener {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onEntityRemove(com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent e) {
onEntityDespawn(e.getEntity());
Location entityLocation = e.getEntity().getLocation();

BukkitExecutor.sync(() -> {
World world = entityLocation.getWorld();
int chunkX = entityLocation.getBlockX() >> 4;
int chunkZ = entityLocation.getBlockZ() >> 4;
// We don't want to track entities that are removed due to chunk being unloaded.
if (world.isChunkLoaded(chunkX, chunkZ))
onEntityDespawn(e.getEntity());
}, 1L);
}

}
Expand Down
Expand Up @@ -92,12 +92,6 @@ public void loadChunk(Chunk chunk) {
plugin.getStackedBlocks().getStackedBlockAmount(entity.getLocation().subtract(0, 1, 0)) > 1) {
entity.remove();
}

// We simulate the spawning of the entities in the newly loaded chunk.
// We do it only if we do not need to recalculate the entities.
if (entityLimitsEnabled && !recalculateEntities.getValue()) {
entityTrackingListener.get().onEntitySpawn(entity);
}
}

if (recalculateEntities.getValue()) {
Expand Down

0 comments on commit 9417807

Please sign in to comment.