Remove chunk unloaded check

This commit is contained in:
Felix Cravic 2020-05-26 21:30:12 +02:00
parent 0240cc2d80
commit fc773044e8
7 changed files with 14 additions and 59 deletions

View File

@ -187,20 +187,16 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
refreshPosition(position.getX(), position.getY(), position.getZ());
refreshView(position.getYaw(), position.getPitch());
sendSynchronization();
if (callback != null)
callback.run();
};
if (instance.hasEnabledAutoChunkLoad()) {
instance.loadChunk(position, chunk -> {
runnable.run();
if (callback != null)
callback.run();
});
} else {
if (ChunkUtils.isChunkUnloaded(instance, position.getX(), position.getZ()))
return;
runnable.run();
if (callback != null)
callback.run();
}
}
@ -303,10 +299,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
Position newPosition = new Position(newX, newY, newZ);
chunkUnloaded = ChunkUtils.isChunkUnloaded(instance, newX, newZ);
if (chunkUnloaded)
return;
//if (!PlayerUtils.isNettyClient(this) && !noGravity) { // players handle gravity by themselves
if (!noGravity) {
velocity.setY(velocity.getY() - gravityDragPerTick * tps);

View File

@ -91,7 +91,6 @@ public class Chunk implements Viewable {
}
public void UNSAFE_removeCustomBlock(int x, int y, int z) {
unloadCheck();
this.customBlocksId[getBlockIndex(x, y, z)] = 0; // Set to none
int index = SerializerUtils.coordToChunkIndex(x, y, z);
this.blocksData.remove(index);
@ -103,7 +102,6 @@ public class Chunk implements Viewable {
}
private void setBlock(int x, int y, int z, short blockId, short customId, Data data, UpdateConsumer updateConsumer) {
unloadCheck();
int index = SerializerUtils.coordToChunkIndex(x, y, z);
if (blockId != 0
|| (blockId == 0 && customId != 0 && updateConsumer != null)) { // Allow custom air block for update purpose, refused if no update consumer has been found
@ -151,7 +149,6 @@ public class Chunk implements Viewable {
}
public void setBlockData(int x, int y, int z, Data data) {
unloadCheck();
int index = SerializerUtils.coordToChunkIndex(x, y, z);
if (data != null) {
this.blocksData.put(index, data);
@ -161,7 +158,6 @@ public class Chunk implements Viewable {
}
public short getBlockId(int x, int y, int z) {
unloadCheck();
int index = getBlockIndex(x, y, z);
if (!MathUtils.isBetween(index, 0, blocksId.length)) {
return 0; // TODO: custom invalid block
@ -171,7 +167,6 @@ public class Chunk implements Viewable {
}
public short getCustomBlockId(int x, int y, int z) {
unloadCheck();
int index = getBlockIndex(x, y, z);
if (!MathUtils.isBetween(index, 0, blocksId.length)) {
return 0; // TODO: custom invalid block
@ -181,7 +176,6 @@ public class Chunk implements Viewable {
}
public CustomBlock getCustomBlock(int x, int y, int z) {
unloadCheck();
int index = getBlockIndex(x, y, z);
if (!MathUtils.isBetween(index, 0, blocksId.length)) {
return null; // TODO: custom invalid block
@ -196,7 +190,6 @@ public class Chunk implements Viewable {
}
protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) {
unloadCheck();
int blockIndex = getBlockIndex(x, y, z);
if (!MathUtils.isBetween(blockIndex, 0, blocksId.length)) {
return;
@ -207,7 +200,6 @@ public class Chunk implements Viewable {
}
protected void refreshBlockId(int x, int y, int z, short blockId) {
unloadCheck();
int blockIndex = getBlockIndex(x, y, z);
if (!MathUtils.isBetween(blockIndex, 0, blocksId.length)) {
return;
@ -228,12 +220,10 @@ public class Chunk implements Viewable {
}
protected Data getData(int index) {
unloadCheck();
return blocksData.get(index);
}
public void updateBlocks(long time, Instance instance) {
unloadCheck();
if (updatableBlocks.isEmpty())
return;
@ -296,7 +286,6 @@ public class Chunk implements Viewable {
}
protected byte[] getSerializedData() throws IOException {
unloadCheck();
ByteArrayOutputStream output = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(output);
@ -318,14 +307,16 @@ public class Chunk implements Viewable {
Data data = blocksData.get(index);
boolean hasData = data != null;
// Chunk coord
// Chunk coordinates
dos.writeInt(x);
dos.writeInt(y);
dos.writeInt(z);
// Id
dos.writeShort(blockId);
dos.writeShort(customBlockId);
// Data
hasData = (data != null && (data instanceof SerializableData)) && hasData;
dos.writeBoolean(hasData);
if (hasData) {
@ -363,7 +354,6 @@ public class Chunk implements Viewable {
// Write the packet in the writer thread pools
public void refreshDataPacket(Runnable runnable) {
unloadCheck();
PacketWriterUtils.writeCallbackPacket(getFreshFullDataPacket(), buf -> {
setFullDataPacket(buf);
runnable.run();
@ -388,7 +378,6 @@ public class Chunk implements Viewable {
// UNSAFE
@Override
public boolean addViewer(Player player) {
unloadCheck();
boolean result = this.viewers.add(player);
PlayerChunkLoadEvent playerChunkLoadEvent = new PlayerChunkLoadEvent(player, chunkX, chunkZ);
@ -399,7 +388,6 @@ public class Chunk implements Viewable {
// UNSAFE
@Override
public boolean removeViewer(Player player) {
unloadCheck();
boolean result = this.viewers.remove(player);
PlayerChunkUnloadEvent playerChunkUnloadEvent = new PlayerChunkUnloadEvent(player, chunkX, chunkZ);
@ -413,26 +401,10 @@ public class Chunk implements Viewable {
}
/**
* Used to prevent major memory leak when unloading chunks
* Set the chunk as "unloaded"
*/
protected void unload() {
this.loaded = false;
this.biomes = null;
this.blocksId = null;
this.customBlocksId = null;
this.blocksData = null;
this.updatableBlocks = null;
this.updatableBlocksLastUpdate = null;
this.blockEntities = null;
this.viewers = null;
}
/**
* Ensure that the check is loaded before interaction
*/
private void unloadCheck() {
Check.stateCondition(!isLoaded(), "The chunk " + toString() + " is unloaded, you cannot interact with it");
}
private int getBlockIndex(int x, int y, int z) {

View File

@ -118,9 +118,6 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
//
protected void sendChunkUpdate(Collection<Player> players, Chunk chunk) {
if (ChunkUtils.isChunkUnloaded(this, chunk))
return;
ByteBuf chunkData = chunk.getFullDataPacket();
players.forEach(player -> {
if (!PlayerUtils.isNettyClient(player))
@ -131,17 +128,12 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
}
protected void sendChunkSectionUpdate(Chunk chunk, int section, Collection<Player> players) {
if (ChunkUtils.isChunkUnloaded(this, chunk))
return;
PacketWriterUtils.writeAndSend(players, getChunkSectionUpdatePacket(chunk, section));
}
public void sendChunkSectionUpdate(Chunk chunk, int section, Player player) {
if (!PlayerUtils.isNettyClient(player))
return;
if (ChunkUtils.isChunkUnloaded(this, chunk))
return;
PacketWriterUtils.writeAndSend(player, getChunkSectionUpdatePacket(chunk, section));
}

View File

@ -283,10 +283,8 @@ public class InstanceContainer extends Instance {
unloadChunkPacket.chunkZ = chunkZ;
chunk.sendPacketToViewers(unloadChunkPacket);
if (!ChunkUtils.isChunkUnloaded(this, chunk)) {
for (Player viewer : chunk.getViewers()) {
chunk.removeViewer(viewer);
}
for (Player viewer : chunk.getViewers()) {
chunk.removeViewer(viewer);
}
this.chunks.remove(index);

View File

@ -61,8 +61,8 @@ public class PlayerPositionListener {
private static void processMovement(Player player, float x, float y, float z,
float yaw, float pitch, Consumer<Position> consumer) {
boolean chunkTest = ChunkUtils.isChunkUnloaded(player.getInstance(), x, z);
if (chunkTest) {
// Try to move in an unloaded chunk, prevent it
if (ChunkUtils.isChunkUnloaded(player.getInstance(), x, z)) {
player.teleport(player.getPosition());
return;
}

View File

@ -73,8 +73,9 @@ public class ConnectionManager {
* Used in {@link net.minestom.server.network.packet.client.login.LoginStartPacket} in order
* to give the player the right UUID
*
* @param playerConnection
* @return
* @param playerConnection the player connection
* @return the uuid based on {@code playerConnection}
* return a random UUID if no UUID provider is defined see {@link #setUuidProvider(UuidProvider)}
*/
public UUID getPlayerConnectionUuid(PlayerConnection playerConnection) {
if (uuidProvider == null)

View File

@ -103,7 +103,7 @@ public class ChunkDataPacket implements ServerPacket {
blockEntity.put("z", new DoubleTag(blockPosition.getZ() + 16 * chunk.getChunkZ()));
CustomBlock customBlock = chunk.getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
if (customBlock != null) {
Data data = chunk.getData(blockPosition.getX(), (byte)blockPosition.getY(), blockPosition.getZ());
Data data = chunk.getData(blockPosition.getX(), (byte) blockPosition.getY(), blockPosition.getZ());
customBlock.writeBlockEntity(blockPosition, data, blockEntity);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();