WIP entities in unloaded chunk + PlayerChunkLoadEvent

This commit is contained in:
Felix Cravic 2020-05-09 18:24:14 +02:00
parent 39f0053ded
commit f7badc021b
5 changed files with 45 additions and 9 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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

View File

@ -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);