forked from BLOCKFANTASY/LOOHP-Limbo
fix: don't spam empty chunk packets
This commit is contained in:
parent
a0c68a8b87
commit
f236a9b01e
|
|
@ -30,6 +30,7 @@ import com.loohp.limbo.network.protocol.packets.PacketPlayOutEntityDestroy;
|
||||||
import com.loohp.limbo.network.protocol.packets.PacketPlayOutEntityMetadata;
|
import com.loohp.limbo.network.protocol.packets.PacketPlayOutEntityMetadata;
|
||||||
import com.loohp.limbo.network.protocol.packets.PacketPlayOutSpawnEntity;
|
import com.loohp.limbo.network.protocol.packets.PacketPlayOutSpawnEntity;
|
||||||
import com.loohp.limbo.network.protocol.packets.PacketPlayOutUnloadChunk;
|
import com.loohp.limbo.network.protocol.packets.PacketPlayOutUnloadChunk;
|
||||||
|
import com.loohp.limbo.utils.GameMode;
|
||||||
import com.loohp.limbo.world.ChunkPosition;
|
import com.loohp.limbo.world.ChunkPosition;
|
||||||
import com.loohp.limbo.world.World;
|
import com.loohp.limbo.world.World;
|
||||||
import net.querz.mca.Chunk;
|
import net.querz.mca.Chunk;
|
||||||
|
|
@ -47,117 +48,124 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class PlayerInteractManager {
|
public class PlayerInteractManager {
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
|
|
||||||
private Set<Entity> entities;
|
private Set<Entity> entities;
|
||||||
private Map<ChunkPosition, Chunk> currentViewing;
|
private Map<ChunkPosition, Chunk> currentViewing;
|
||||||
|
|
||||||
public PlayerInteractManager() {
|
public PlayerInteractManager() {
|
||||||
this.player = null;
|
this.player = null;
|
||||||
this.entities = new HashSet<>();
|
this.entities = new HashSet<>();
|
||||||
this.currentViewing = new HashMap<>();
|
this.currentViewing = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setPlayer(Player player) {
|
protected void setPlayer(Player player) {
|
||||||
if (this.player == null) {
|
if (this.player == null) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Player in PlayerInteractManager cannot be changed once created");
|
throw new RuntimeException("Player in PlayerInteractManager cannot be changed once created");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getPlayer() {
|
public Player getPlayer() {
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() throws IOException {
|
public void update() throws IOException {
|
||||||
if (player.clientConnection.getClientState() != ClientConnection.ClientState.PLAY) {
|
if (player.clientConnection.getClientState() != ClientConnection.ClientState.PLAY && !player.clientConnection.isReady()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int viewDistanceChunks = Limbo.getInstance().getServerProperties().getViewDistance();
|
int viewDistanceChunks = Limbo.getInstance().getServerProperties().getViewDistance();
|
||||||
int viewDistanceBlocks = viewDistanceChunks << 4;
|
int viewDistanceBlocks = viewDistanceChunks << 4;
|
||||||
Location location = player.getLocation();
|
Location location = player.getLocation();
|
||||||
Set<Entity> entitiesInRange = player.getWorld().getEntities().stream().filter(each -> each.getLocation().distanceSquared(location) < viewDistanceBlocks * viewDistanceBlocks).collect(Collectors.toSet());
|
Set<Entity> entitiesInRange = player.getWorld().getEntities().stream().filter(each -> each.getLocation().distanceSquared(location) < viewDistanceBlocks * viewDistanceBlocks).collect(Collectors.toSet());
|
||||||
for (Entity entity : entitiesInRange) {
|
for (Entity entity : entitiesInRange) {
|
||||||
if (!entities.contains(entity)) {
|
if (!entities.contains(entity)) {
|
||||||
PacketPlayOutSpawnEntity packet = new PacketPlayOutSpawnEntity(entity.getEntityId(), entity.getUniqueId(), entity.getType(), entity.getX(), entity.getY(), entity.getZ(), entity.getYaw(), entity.getPitch(), entity.getPitch(), 0, (short) 0, (short) 0, (short) 0);
|
PacketPlayOutSpawnEntity packet = new PacketPlayOutSpawnEntity(entity.getEntityId(), entity.getUniqueId(), entity.getType(), entity.getX(), entity.getY(), entity.getZ(), entity.getYaw(), entity.getPitch(), entity.getPitch(), 0, (short) 0, (short) 0, (short) 0);
|
||||||
player.clientConnection.sendPacket(packet);
|
player.clientConnection.sendPacket(packet);
|
||||||
|
|
||||||
PacketPlayOutEntityMetadata meta = new PacketPlayOutEntityMetadata(entity);
|
PacketPlayOutEntityMetadata meta = new PacketPlayOutEntityMetadata(entity);
|
||||||
player.clientConnection.sendPacket(meta);
|
player.clientConnection.sendPacket(meta);
|
||||||
}
|
|
||||||
}
|
|
||||||
List<Integer> ids = new ArrayList<>();
|
|
||||||
for (Entity entity : entities) {
|
|
||||||
if (!entitiesInRange.contains(entity)) {
|
|
||||||
ids.add(entity.getEntityId());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int id : ids) {
|
|
||||||
PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(id);
|
|
||||||
player.clientConnection.sendPacket(packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
entities = entitiesInRange;
|
|
||||||
|
|
||||||
int playerChunkX = (int) location.getX() >> 4;
|
|
||||||
int playerChunkZ = (int) location.getZ() >> 4;
|
|
||||||
World world = location.getWorld();
|
|
||||||
|
|
||||||
Map<ChunkPosition, Chunk> chunksInRange = new HashMap<>();
|
|
||||||
|
|
||||||
for (int x = playerChunkX - viewDistanceChunks; x < playerChunkX + viewDistanceChunks; x++) {
|
|
||||||
for (int z = playerChunkZ - viewDistanceChunks; z < playerChunkZ + viewDistanceChunks; z++) {
|
|
||||||
Chunk chunk = world.getChunkAt(x, z);
|
|
||||||
if (chunk != null) {
|
|
||||||
chunksInRange.put(new ChunkPosition(world, x, z), chunk);
|
|
||||||
} else {
|
|
||||||
chunksInRange.put(new ChunkPosition(world, x, z), World.EMPTY_CHUNK);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Entry<ChunkPosition, Chunk> entry : currentViewing.entrySet()) {
|
|
||||||
ChunkPosition chunkPos = entry.getKey();
|
|
||||||
if (!chunksInRange.containsKey(chunkPos)) {
|
|
||||||
PacketPlayOutUnloadChunk packet = new PacketPlayOutUnloadChunk(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
|
||||||
player.clientConnection.sendPacket(packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int counter = 0;
|
|
||||||
ClientboundChunkBatchStartPacket chunkBatchStartPacket = new ClientboundChunkBatchStartPacket();
|
|
||||||
player.clientConnection.sendPacket(chunkBatchStartPacket);
|
|
||||||
for (Entry<ChunkPosition, Chunk> entry : chunksInRange.entrySet()) {
|
|
||||||
ChunkPosition chunkPos = entry.getKey();
|
|
||||||
if (!currentViewing.containsKey(chunkPos)) {
|
|
||||||
Chunk chunk = chunkPos.getWorld().getChunkAt(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
|
||||||
if (chunk == null) {
|
|
||||||
ClientboundLevelChunkWithLightPacket chunkdata = new ClientboundLevelChunkWithLightPacket(chunkPos.getChunkX(), chunkPos.getChunkZ(), entry.getValue(), world.getEnvironment(), Collections.emptyList(), Collections.emptyList());
|
|
||||||
player.clientConnection.sendPacket(chunkdata);
|
|
||||||
} else {
|
|
||||||
List<Byte[]> blockChunk = world.getLightEngineBlock().getBlockLightBitMask(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
|
||||||
if (blockChunk == null) {
|
|
||||||
blockChunk = new ArrayList<>();
|
|
||||||
}
|
|
||||||
List<Byte[]> skyChunk = null;
|
|
||||||
if (world.hasSkyLight()) {
|
|
||||||
skyChunk = world.getLightEngineSky().getSkyLightBitMask(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
|
||||||
}
|
|
||||||
if (skyChunk == null) {
|
|
||||||
skyChunk = new ArrayList<>();
|
|
||||||
}
|
|
||||||
ClientboundLevelChunkWithLightPacket chunkdata = new ClientboundLevelChunkWithLightPacket(chunkPos.getChunkX(), chunkPos.getChunkZ(), chunk, world.getEnvironment(), skyChunk, blockChunk);
|
|
||||||
player.clientConnection.sendPacket(chunkdata);
|
|
||||||
}
|
|
||||||
counter++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ClientboundChunkBatchFinishedPacket chunkBatchFinishedPacket = new ClientboundChunkBatchFinishedPacket(counter);
|
List<Integer> ids = new ArrayList<>();
|
||||||
player.clientConnection.sendPacket(chunkBatchFinishedPacket);
|
for (Entity entity : entities) {
|
||||||
|
if (!entitiesInRange.contains(entity)) {
|
||||||
|
ids.add(entity.getEntityId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int id : ids) {
|
||||||
|
PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(id);
|
||||||
|
player.clientConnection.sendPacket(packet);
|
||||||
|
}
|
||||||
|
|
||||||
currentViewing = chunksInRange;
|
entities = entitiesInRange;
|
||||||
}
|
|
||||||
|
int playerChunkX = (int) location.getX() >> 4;
|
||||||
|
int playerChunkZ = (int) location.getZ() >> 4;
|
||||||
|
World world = location.getWorld();
|
||||||
|
|
||||||
|
Map<ChunkPosition, Chunk> chunksInRange = new HashMap<>();
|
||||||
|
|
||||||
|
for (int x = playerChunkX - viewDistanceChunks; x < playerChunkX + viewDistanceChunks; x++) {
|
||||||
|
for (int z = playerChunkZ - viewDistanceChunks; z < playerChunkZ + viewDistanceChunks; z++) {
|
||||||
|
Chunk chunk = world.getChunkAt(x, z);
|
||||||
|
if (chunk != null) {
|
||||||
|
chunksInRange.put(new ChunkPosition(world, x, z), chunk);
|
||||||
|
} else {
|
||||||
|
chunksInRange.put(new ChunkPosition(world, x, z), World.EMPTY_CHUNK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ChunkPosition chunkPos : currentViewing.keySet()) {
|
||||||
|
if (!chunksInRange.containsKey(chunkPos)) {
|
||||||
|
PacketPlayOutUnloadChunk packet = new PacketPlayOutUnloadChunk(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
||||||
|
player.clientConnection.sendPacket(packet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// blocks cannot be broken, so don't send chunks to the client
|
||||||
|
if (getPlayer().getGamemode() == GameMode.ADVENTURE) {
|
||||||
|
for (ChunkPosition chunkPos : chunksInRange.keySet()) {
|
||||||
|
currentViewing.remove(chunkPos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we don't have any chunk updates, don't send any packets
|
||||||
|
if (chunksInRange.isEmpty()) return;
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
ClientboundChunkBatchStartPacket chunkBatchStartPacket = new ClientboundChunkBatchStartPacket();
|
||||||
|
player.clientConnection.sendPacket(chunkBatchStartPacket);
|
||||||
|
for (Entry<ChunkPosition, Chunk> entry : chunksInRange.entrySet()) {
|
||||||
|
ChunkPosition chunkPos = entry.getKey();
|
||||||
|
Chunk chunk = chunkPos.getWorld().getChunkAt(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
||||||
|
if (chunk == null) {
|
||||||
|
ClientboundLevelChunkWithLightPacket chunkdata = new ClientboundLevelChunkWithLightPacket(chunkPos.getChunkX(), chunkPos.getChunkZ(), entry.getValue(), world.getEnvironment(), Collections.emptyList(), Collections.emptyList());
|
||||||
|
player.clientConnection.sendPacket(chunkdata);
|
||||||
|
} else {
|
||||||
|
List<Byte[]> blockChunk = world.getLightEngineBlock().getBlockLightBitMask(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
||||||
|
if (blockChunk == null) {
|
||||||
|
blockChunk = new ArrayList<>();
|
||||||
|
}
|
||||||
|
List<Byte[]> skyChunk = null;
|
||||||
|
if (world.hasSkyLight()) {
|
||||||
|
skyChunk = world.getLightEngineSky().getSkyLightBitMask(chunkPos.getChunkX(), chunkPos.getChunkZ());
|
||||||
|
}
|
||||||
|
if (skyChunk == null) {
|
||||||
|
skyChunk = new ArrayList<>();
|
||||||
|
}
|
||||||
|
ClientboundLevelChunkWithLightPacket chunkdata = new ClientboundLevelChunkWithLightPacket(chunkPos.getChunkX(), chunkPos.getChunkZ(), chunk, world.getEnvironment(), skyChunk, blockChunk);
|
||||||
|
player.clientConnection.sendPacket(chunkdata);
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
ClientboundChunkBatchFinishedPacket chunkBatchFinishedPacket = new ClientboundChunkBatchFinishedPacket(counter);
|
||||||
|
player.clientConnection.sendPacket(chunkBatchFinishedPacket);
|
||||||
|
|
||||||
|
currentViewing = chunksInRange;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue