Simulate Java chunk unloading by sending empty chunks

Closes #2725
This commit is contained in:
RednedEpic 2021-12-28 23:25:33 -06:00
parent 6742865bc5
commit 61636f44aa
2 changed files with 23 additions and 16 deletions

View file

@ -30,6 +30,7 @@ import com.nukkitx.math.vector.Vector3i;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.ChunkUtils;
import java.util.Iterator;
@ -60,5 +61,7 @@ public class JavaForgetLevelChunkTranslator extends PacketTranslator<Clientbound
}
}
}
ChunkUtils.sendEmptyChunk(session, packet.getX(), packet.getZ(), false);
}
}

View file

@ -204,27 +204,31 @@ public class ChunkUtils {
session.getChunkCache().updateBlock(position.getX(), position.getY(), position.getZ(), blockState);
}
public static void sendEmptyChunk(GeyserSession session, int chunkX, int chunkZ, boolean forceUpdate) {
LevelChunkPacket data = new LevelChunkPacket();
data.setChunkX(chunkX);
data.setChunkZ(chunkZ);
data.setSubChunksLength(0);
data.setData(EMPTY_CHUNK_DATA);
data.setCachingEnabled(false);
session.sendUpstreamPacket(data);
if (forceUpdate) {
Vector3i pos = Vector3i.from(chunkX << 4, 80, chunkZ << 4);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setBlockPosition(pos);
blockPacket.setDataLayer(0);
blockPacket.setRuntimeId(1);
session.sendUpstreamPacket(blockPacket);
}
}
public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) {
int chunkX = position.getX() >> 4;
int chunkZ = position.getZ() >> 4;
for (int x = -radius; x <= radius; x++) {
for (int z = -radius; z <= radius; z++) {
LevelChunkPacket data = new LevelChunkPacket();
data.setChunkX(chunkX + x);
data.setChunkZ(chunkZ + z);
data.setSubChunksLength(0);
data.setData(EMPTY_CHUNK_DATA);
data.setCachingEnabled(false);
session.sendUpstreamPacket(data);
if (forceUpdate) {
Vector3i pos = Vector3i.from(chunkX + x << 4, 80, chunkZ + z << 4);
UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setBlockPosition(pos);
blockPacket.setDataLayer(0);
blockPacket.setRuntimeId(1);
session.sendUpstreamPacket(blockPacket);
}
sendEmptyChunk(session, chunkX + x, chunkZ + z, forceUpdate);
}
}
}