diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 918803f13..3e4296886 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -246,7 +246,14 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { if (shouldRemove()) { remove(); return; - } else if (shouldUpdate(time)) { + } + + if (ChunkUtils.isChunkUnloaded(getInstance(), getPosition().getX(), getPosition().getZ())) { + // No update for entities in unloaded chunk + return; + } + + if (shouldUpdate(time)) { this.lastUpdate = time; // Velocity diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index b9061645c..1563ad197 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -12,7 +12,10 @@ import net.minestom.server.entity.property.Attribute; import net.minestom.server.entity.vehicle.PlayerVehicleInformation; import net.minestom.server.event.item.ItemDropEvent; import net.minestom.server.event.item.PickupExperienceEvent; -import net.minestom.server.event.player.*; +import net.minestom.server.event.player.PlayerDisconnectEvent; +import net.minestom.server.event.player.PlayerRespawnEvent; +import net.minestom.server.event.player.PlayerSpawnEvent; +import net.minestom.server.event.player.PlayerTickEvent; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.block.CustomBlock; @@ -660,9 +663,6 @@ public class Player extends LivingEntity { Chunk chunk = instance.getChunk(chunkPos[0], chunkPos[1]); if (chunk != null) chunk.removeViewer(this); - - PlayerChunkUnloadEvent playerChunkUnloadEvent = new PlayerChunkUnloadEvent(this, chunk); - callEvent(PlayerChunkUnloadEvent.class, playerChunkUnloadEvent); } updateViewPosition(newChunk); diff --git a/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java b/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java new file mode 100644 index 000000000..78cfae97a --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerChunkLoadEvent.java @@ -0,0 +1,25 @@ +package net.minestom.server.event.player; + +import net.minestom.server.entity.Player; +import net.minestom.server.event.Event; +import net.minestom.server.instance.Chunk; + +public class PlayerChunkLoadEvent extends Event { + + private Player player; + private Chunk chunk; + + public PlayerChunkLoadEvent(Player player, Chunk chunk) { + this.player = player; + this.chunk = chunk; + } + + public Player getPlayer() { + return player; + } + + public Chunk getChunk() { + return chunk; + } + +} diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 93d97ead1..5a94f4f16 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -7,6 +7,8 @@ import net.minestom.server.Viewable; import net.minestom.server.data.Data; import net.minestom.server.data.SerializableData; import net.minestom.server.entity.Player; +import net.minestom.server.event.player.PlayerChunkLoadEvent; +import net.minestom.server.event.player.PlayerChunkUnloadEvent; import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.CustomBlock; @@ -363,12 +365,18 @@ public class Chunk implements Viewable { @Override public void addViewer(Player player) { this.viewers.add(player); + + PlayerChunkLoadEvent playerChunkLoadEvent = new PlayerChunkLoadEvent(player, this); + player.callEvent(PlayerChunkLoadEvent.class, playerChunkLoadEvent); } // UNSAFE @Override public void removeViewer(Player player) { this.viewers.remove(player); + + PlayerChunkUnloadEvent playerChunkUnloadEvent = new PlayerChunkUnloadEvent(player, this); + player.callEvent(PlayerChunkUnloadEvent.class, playerChunkUnloadEvent); } @Override diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index fa2f136d4..2d7e33796 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -5,7 +5,6 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.data.Data; import net.minestom.server.entity.Player; import net.minestom.server.event.player.PlayerBlockBreakEvent; -import net.minestom.server.event.player.PlayerChunkUnloadEvent; import net.minestom.server.instance.batch.BlockBatch; import net.minestom.server.instance.batch.ChunkBatch; import net.minestom.server.instance.block.Block; @@ -285,9 +284,6 @@ public class InstanceContainer extends Instance { for (Player viewer : chunk.getViewers()) { chunk.removeViewer(viewer); - - PlayerChunkUnloadEvent playerChunkUnloadEvent = new PlayerChunkUnloadEvent(viewer, chunk); - viewer.callEvent(PlayerChunkUnloadEvent.class, playerChunkUnloadEvent); } this.chunks.remove(index);