Chunk fixes

This commit is contained in:
AJ Ferguson 2020-02-05 19:32:33 -09:00
parent e0a1435d51
commit e4ab4b336c
9 changed files with 161 additions and 70 deletions

View file

@ -38,7 +38,6 @@ import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.tcp.TcpSessionFactory; import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
import com.nukkitx.math.vector.Vector2f; import com.nukkitx.math.vector.Vector2f;
import com.nukkitx.math.vector.Vector2i;
import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i; import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.nbt.tag.CompoundTag; import com.nukkitx.nbt.tag.CompoundTag;
@ -84,10 +83,8 @@ public class GeyserSession implements Player {
private DataCache<Packet> javaPacketCache; private DataCache<Packet> javaPacketCache;
@Setter
private Vector2i lastChunkPosition = null;
@Setter
private int renderDistance; private int renderDistance;
private int chunkPublisherRadius;
private boolean loggedIn; private boolean loggedIn;
private boolean loggingIn; private boolean loggingIn;
@ -269,6 +266,17 @@ public class GeyserSession implements Player {
windowCache.showWindow(window, id); windowCache.showWindow(window, id);
} }
public void setRenderDistance(int renderDistance) {
if (renderDistance > 32) renderDistance = 32; // <3 u ViaVersion but I don't like crashing clients x)
this.renderDistance = renderDistance;
chunkPublisherRadius = renderDistance * 3/2 << 4; //some chunks are ignored if this isn't increased
ChunkRadiusUpdatedPacket chunkRadiusUpdatedPacket = new ChunkRadiusUpdatedPacket();
chunkRadiusUpdatedPacket.setRadius(renderDistance);
upstream.sendPacket(chunkRadiusUpdatedPacket);
}
@Override @Override
public InetSocketAddress getSocketAddress() { public InetSocketAddress getSocketAddress() {
return this.upstream.getAddress(); return this.upstream.getAddress();

View file

@ -146,6 +146,9 @@ public class TranslatorsInit {
Registry.registerJava(ServerMultiBlockChangePacket.class, new JavaMultiBlockChangeTranslator()); Registry.registerJava(ServerMultiBlockChangePacket.class, new JavaMultiBlockChangeTranslator());
Registry.registerJava(ServerUnloadChunkPacket.class, new JavaUnloadChunkTranslator()); Registry.registerJava(ServerUnloadChunkPacket.class, new JavaUnloadChunkTranslator());
Registry.registerJava(ServerUpdateViewPositionPacket.class, new JavaUpdateViewPositionTranslator());
Registry.registerJava(ServerUpdateViewDistancePacket.class, new JavaUpdateViewDistanceTranslator());
Registry.registerJava(ServerOpenWindowPacket.class, new OpenWindowPacketTranslator()); Registry.registerJava(ServerOpenWindowPacket.class, new OpenWindowPacketTranslator());
Registry.registerBedrock(AnimatePacket.class, new BedrockAnimateTranslator()); Registry.registerBedrock(AnimatePacket.class, new BedrockAnimateTranslator());

View file

@ -28,18 +28,12 @@ package org.geysermc.connector.network.translators.block;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState; import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import org.geysermc.connector.utils.Toolbox; import org.geysermc.connector.utils.Toolbox;
import java.util.HashMap;
import java.util.Map;
public class BlockTranslator { public class BlockTranslator {
private final Map<String, BlockEntry> javaIdentifierMap = new HashMap<>();
public BlockEntry getBlockEntry(BlockState state) { public BlockEntry getBlockEntry(BlockState state) {
return Toolbox.BLOCK_ENTRIES.get(state.getId()); return Toolbox.BLOCK_ENTRIES.get(state.getId());
} }
public BlockEntry getBlockEntry(String javaIdentifier) { public BlockEntry getBlockEntry(String javaIdentifier) {
return javaIdentifierMap.computeIfAbsent(javaIdentifier, key -> Toolbox.BLOCK_ENTRIES.values() return Toolbox.JAVA_IDENTIFIER_TO_ENTRY.get(javaIdentifier);
.stream().filter(blockEntry -> blockEntry.getJavaIdentifier().equals(key)).findFirst().orElse(null));
} }
} }

View file

@ -60,12 +60,7 @@ public class JavaJoinGameTranslator extends PacketTranslator<ServerJoinGamePacke
entityDataPacket.getMetadata().putAll(entity.getMetadata()); entityDataPacket.getMetadata().putAll(entity.getMetadata());
session.getUpstream().sendPacket(entityDataPacket); session.getUpstream().sendPacket(entityDataPacket);
session.setRenderDistance(packet.getViewDistance() + 1); // +1 to be sure it includes every chunk session.setRenderDistance(packet.getViewDistance());
if (session.getRenderDistance() > 32) session.setRenderDistance(32); // <3 u ViaVersion but I don't like crashing clients x)
ChunkRadiusUpdatedPacket chunkRadiusPacket = new ChunkRadiusUpdatedPacket();
chunkRadiusPacket.setRadius(session.getRenderDistance());
session.getUpstream().sendPacket(chunkRadiusPacket);
if (DimensionUtils.javaToBedrock(packet.getDimension()) != entity.getDimension()) { if (DimensionUtils.javaToBedrock(packet.getDimension()) != entity.getDimension()) {
ChunkUtils.sendEmptyChunks(session, entity.getPosition().toInt(), 3, true); ChunkUtils.sendEmptyChunks(session, entity.getPosition().toInt(), 3, true);

View file

@ -25,12 +25,12 @@
package org.geysermc.connector.network.translators.java.world; package org.geysermc.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerChunkDataPacket; import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerChunkDataPacket;
import com.nukkitx.math.vector.Vector2i; import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.network.VarInts; import com.nukkitx.network.VarInts;
import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket; import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket;
import com.nukkitx.protocol.bedrock.packet.NetworkChunkPublisherUpdatePacket;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.geysermc.api.Geyser; import org.geysermc.api.Geyser;
@ -45,49 +45,59 @@ public class JavaChunkDataTranslator extends PacketTranslator<ServerChunkDataPac
public void translate(ServerChunkDataPacket packet, GeyserSession session) { public void translate(ServerChunkDataPacket packet, GeyserSession session) {
// Not sure if this is safe or not, however without this the client usually times out // Not sure if this is safe or not, however without this the client usually times out
Geyser.getConnector().getGeneralThreadPool().execute(() -> { Geyser.getConnector().getGeneralThreadPool().execute(() -> {
Vector2i chunkPos = session.getLastChunkPosition();
Vector3f position = session.getPlayerEntity().getPosition();
Vector2i newChunkPos = Vector2i.from(position.getFloorX() >> 4, position.getFloorZ() >> 4);
if (chunkPos == null || !chunkPos.equals(newChunkPos)) {
NetworkChunkPublisherUpdatePacket chunkPublisherUpdatePacket = new NetworkChunkPublisherUpdatePacket();
chunkPublisherUpdatePacket.setPosition(position.toInt());
chunkPublisherUpdatePacket.setRadius(session.getRenderDistance() << 4);
session.getUpstream().sendPacket(chunkPublisherUpdatePacket);
session.setLastChunkPosition(newChunkPos);
}
try { try {
ChunkUtils.ChunkData chunkData = ChunkUtils.translateToBedrock(packet.getColumn()); if (packet.getColumn().getBiomeData() != null) { //Full chunk
ByteBuf byteBuf = Unpooled.buffer(32); ChunkUtils.ChunkData chunkData = ChunkUtils.translateToBedrock(packet.getColumn());
ChunkSection[] sections = chunkData.sections; ByteBuf byteBuf = Unpooled.buffer(32);
ChunkSection[] sections = chunkData.sections;
int sectionCount = sections.length - 1; int sectionCount = sections.length - 1;
while (sectionCount >= 0 && sections[sectionCount].isEmpty()) { while (sectionCount >= 0 && sections[sectionCount].isEmpty()) {
sectionCount--; sectionCount--;
}
sectionCount++;
for (int i = 0; i < sectionCount; i++) {
ChunkSection section = chunkData.sections[i];
section.writeToNetwork(byteBuf);
}
byteBuf.writeBytes(chunkData.biomes); // Biomes - 256 bytes
byteBuf.writeByte(0); // Border blocks - Edu edition only
VarInts.writeUnsignedInt(byteBuf, 0); // extra data length, 0 for now
byte[] payload = new byte[byteBuf.writerIndex()];
byteBuf.readBytes(payload);
LevelChunkPacket levelChunkPacket = new LevelChunkPacket();
levelChunkPacket.setSubChunksLength(sectionCount);
levelChunkPacket.setCachingEnabled(false);
levelChunkPacket.setChunkX(packet.getColumn().getX());
levelChunkPacket.setChunkZ(packet.getColumn().getZ());
levelChunkPacket.setData(payload);
session.getUpstream().sendPacket(levelChunkPacket);
} else {
Chunk[] chunks = packet.getColumn().getChunks();
for (int i = 0; i < chunks.length; i++) {
Chunk chunk = chunks[i];
if (chunk == null) continue;
final int xOffset = packet.getColumn().getX() << 4;
final int yOffset = i * 16;
final int zOffset = packet.getColumn().getZ() << 4;
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
BlockState blockState = chunk.get(x, y, z);
Vector3i pos = Vector3i.from(
x + xOffset,
y + yOffset,
z + zOffset);
ChunkUtils.updateBlock(session, blockState, pos);
}
}
}
}
} }
sectionCount++;
for (int i = 0; i < sectionCount; i++) {
ChunkSection section = chunkData.sections[i];
section.writeToNetwork(byteBuf);
}
byteBuf.writeBytes(chunkData.biomes); // Biomes - 256 bytes
byteBuf.writeByte(0); // Border blocks - Edu edition only
VarInts.writeUnsignedInt(byteBuf, 0); // extra data length, 0 for now
byte[] payload = new byte[byteBuf.writerIndex()];
byteBuf.readBytes(payload);
LevelChunkPacket levelChunkPacket = new LevelChunkPacket();
levelChunkPacket.setSubChunksLength(sectionCount);
levelChunkPacket.setCachingEnabled(false);
levelChunkPacket.setChunkX(packet.getColumn().getX());
levelChunkPacket.setChunkZ(packet.getColumn().getZ());
levelChunkPacket.setData(payload);
session.getUpstream().sendPacket(levelChunkPacket);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerUpdateViewDistancePacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
public class JavaUpdateViewDistanceTranslator extends PacketTranslator<ServerUpdateViewDistancePacket> {
@Override
public void translate(ServerUpdateViewDistancePacket packet, GeyserSession session) {
session.setRenderDistance(packet.getViewDistance());
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerUpdateViewPositionPacket;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.NetworkChunkPublisherUpdatePacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
public class JavaUpdateViewPositionTranslator extends PacketTranslator<ServerUpdateViewPositionPacket> {
@Override
public void translate(ServerUpdateViewPositionPacket packet, GeyserSession session) {
NetworkChunkPublisherUpdatePacket chunkPublisherUpdatePacket = new NetworkChunkPublisherUpdatePacket();
chunkPublisherUpdatePacket.setPosition(Vector3i.from(packet.getChunkX() << 4, 0, packet.getChunkZ() << 4));
chunkPublisherUpdatePacket.setRadius(session.getChunkPublisherRadius());
session.getUpstream().sendPacket(chunkPublisherUpdatePacket);
}
}

View file

@ -31,7 +31,6 @@ import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState; import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.nukkitx.math.vector.Vector3i; import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket; import com.nukkitx.protocol.bedrock.packet.LevelChunkPacket;
import com.nukkitx.protocol.bedrock.packet.NetworkChunkPublisherUpdatePacket;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket; import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit; import org.geysermc.connector.network.translators.TranslatorsInit;
@ -76,19 +75,23 @@ public class ChunkUtils {
} }
public static void updateBlock(GeyserSession session, BlockState blockState, Position position) { public static void updateBlock(GeyserSession session, BlockState blockState, Position position) {
BlockEntry blockEntry = TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ()); Vector3i pos = Vector3i.from(position.getX(), position.getY(), position.getZ());
updateBlock(session, blockState, pos);
}
public static void updateBlock(GeyserSession session, BlockState blockState, Vector3i position) {
BlockEntry blockEntry = TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket(); UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setDataLayer(0); updateBlockPacket.setDataLayer(0);
updateBlockPacket.setBlockPosition(pos); updateBlockPacket.setBlockPosition(position);
updateBlockPacket.setRuntimeId(blockEntry.getBedrockRuntimeId()); updateBlockPacket.setRuntimeId(blockEntry.getBedrockRuntimeId());
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS); updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
session.getUpstream().sendPacket(updateBlockPacket); session.getUpstream().sendPacket(updateBlockPacket);
UpdateBlockPacket waterPacket = new UpdateBlockPacket(); UpdateBlockPacket waterPacket = new UpdateBlockPacket();
waterPacket.setDataLayer(1); waterPacket.setDataLayer(1);
waterPacket.setBlockPosition(pos); waterPacket.setBlockPosition(position);
if (blockEntry.isWaterlogged()) { if (blockEntry.isWaterlogged()) {
BlockEntry water = TranslatorsInit.getBlockTranslator().getBlockEntry("minecraft:water[level=0]"); BlockEntry water = TranslatorsInit.getBlockTranslator().getBlockEntry("minecraft:water[level=0]");
waterPacket.setRuntimeId(water.getBedrockRuntimeId()); waterPacket.setRuntimeId(water.getBedrockRuntimeId());
@ -101,11 +104,6 @@ public class ChunkUtils {
public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) { public static void sendEmptyChunks(GeyserSession session, Vector3i position, int radius, boolean forceUpdate) {
int chunkX = position.getX() >> 4; int chunkX = position.getX() >> 4;
int chunkZ = position.getZ() >> 4; int chunkZ = position.getZ() >> 4;
NetworkChunkPublisherUpdatePacket chunkPublisherUpdatePacket = new NetworkChunkPublisherUpdatePacket();
chunkPublisherUpdatePacket.setPosition(position);
chunkPublisherUpdatePacket.setRadius(radius + 1 << 4);
session.getUpstream().sendPacket(chunkPublisherUpdatePacket);
session.setLastChunkPosition(null);
for (int x = -radius; x <= radius; x++) { for (int x = -radius; x <= radius; x++) {
for (int z = -radius; z <= radius; z++) { for (int z = -radius; z <= radius; z++) {
LevelChunkPacket data = new LevelChunkPacket(); LevelChunkPacket data = new LevelChunkPacket();
@ -120,7 +118,7 @@ public class ChunkUtils {
Vector3i pos = Vector3i.from(chunkX + x << 4, 80, chunkZ + z << 4); Vector3i pos = Vector3i.from(chunkX + x << 4, 80, chunkZ + z << 4);
UpdateBlockPacket blockPacket = new UpdateBlockPacket(); UpdateBlockPacket blockPacket = new UpdateBlockPacket();
blockPacket.setBlockPosition(pos); blockPacket.setBlockPosition(pos);
blockPacket.setDataLayer(1); blockPacket.setDataLayer(0);
blockPacket.setRuntimeId(1); blockPacket.setRuntimeId(1);
session.getUpstream().sendPacket(blockPacket); session.getUpstream().sendPacket(blockPacket);
} }

View file

@ -50,6 +50,7 @@ public class Toolbox {
public static final Int2ObjectMap<ItemEntry> ITEM_ENTRIES = new Int2ObjectOpenHashMap<>(); public static final Int2ObjectMap<ItemEntry> ITEM_ENTRIES = new Int2ObjectOpenHashMap<>();
public static final Int2ObjectMap<BlockEntry> BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>(); public static final Int2ObjectMap<BlockEntry> BLOCK_ENTRIES = new Int2ObjectOpenHashMap<>();
public static final Map<String, BlockEntry> JAVA_IDENTIFIER_TO_ENTRY = new HashMap<>();
public static void init() { public static void init() {
InputStream stream = GeyserConnector.class.getClassLoader().getResourceAsStream("bedrock/runtime_block_states.dat"); InputStream stream = GeyserConnector.class.getClassLoader().getResourceAsStream("bedrock/runtime_block_states.dat");
@ -140,6 +141,7 @@ public class Toolbox {
} }
BlockEntry blockEntry = new BlockEntry(javaEntry.getKey(), javaIndex, bedrockIndex); BlockEntry blockEntry = new BlockEntry(javaEntry.getKey(), javaIndex, bedrockIndex);
BLOCK_ENTRIES.put(javaIndex, blockEntry); BLOCK_ENTRIES.put(javaIndex, blockEntry);
JAVA_IDENTIFIER_TO_ENTRY.put(javaEntry.getKey(), blockEntry);
continue javaLoop; continue javaLoop;
} }
} }