Block states and fix waterlogged blocks

This commit is contained in:
AJ Ferguson 2019-12-20 20:05:20 -09:00
parent dae4bbbd44
commit d06f963b67
15 changed files with 98 additions and 149 deletions

View file

@ -72,7 +72,7 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
ResourcePackStackPacket stack = new ResourcePackStackPacket();
stack.setExperimental(false);
stack.setForcedToAccept(false);
stack.setGameVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion());
stack.setGameVersion("*");
session.getUpstream().sendPacket(stack);
break;
default:

View file

@ -322,7 +322,7 @@ public class GeyserSession implements Player {
startGamePacket.setMultiplayerCorrelationId("");
startGamePacket.setBlockPalette(Toolbox.BLOCKS);
startGamePacket.setItemEntries(Toolbox.ITEMS);
startGamePacket.setVanillaVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getMinecraftVersion());
startGamePacket.setVanillaVersion("*");
// startGamePacket.setMovementServerAuthoritative(true);
upstream.sendPacket(startGamePacket);
}

View file

@ -79,7 +79,7 @@ public class ChunkCache {
Position blockPosition = chunkPosition.getChunkBlock(position.getX(), position.getY(), position.getZ());
if (chunk != null) {
BlockState blockState = chunk.get(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
return TranslatorsInit.getBlockTranslator().getBedrockBlock(blockState);
return TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
}
return BlockEntry.AIR;

View file

@ -32,16 +32,15 @@ import lombok.Getter;
@AllArgsConstructor
public class BlockEntry {
public static BlockEntry AIR = new BlockEntry("minecraft:air", 0, 0, 0);
public static BlockEntry AIR = new BlockEntry("minecraft:air", 0, 0);
private String javaIdentifier;
private int javaId;
private int bedrockId;
private int bedrockData;
private int bedrockRuntimeId;
@Override
public boolean equals(Object obj) {
return obj == this || (obj instanceof BlockEntry && ((BlockEntry) obj).getBedrockId() == this.getBedrockId() && ((BlockEntry) obj).getJavaIdentifier().equals(this.getJavaIdentifier()));
return obj == this || (obj instanceof BlockEntry && ((BlockEntry) obj).getBedrockRuntimeId() == this.getBedrockRuntimeId() && ((BlockEntry) obj).getJavaIdentifier().equals(this.getJavaIdentifier()));
}
}

View file

@ -3,9 +3,22 @@ package org.geysermc.connector.network.translators.block;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import org.geysermc.connector.utils.Toolbox;
public class BlockTranslator {
import java.util.HashMap;
import java.util.Map;
public BlockEntry getBedrockBlock(BlockState state) {
public class BlockTranslator {
private final Map<String, BlockEntry> javaIdentifierMap = new HashMap<>();
public BlockEntry getBlockEntry(BlockState state) {
return Toolbox.BLOCK_ENTRIES.get(state.getId());
}
public BlockEntry getBlockEntry(String javaIdentifier) {
return javaIdentifierMap.computeIfAbsent(javaIdentifier, key -> Toolbox.BLOCK_ENTRIES.values()
.stream().filter(blockEntry -> blockEntry.getJavaIdentifier().equals(key)).findFirst().orElse(null));
}
public boolean isWaterLogged(BlockEntry entry) {
return (entry.getJavaIdentifier().contains("waterlogged=true") || entry.getJavaIdentifier().startsWith("minecraft:kelp") || entry.getJavaIdentifier().contains("seagrass"));
}
}

View file

@ -26,13 +26,9 @@
package org.geysermc.connector.network.translators.java.entity.player;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerActionAckPacket;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
import org.geysermc.connector.utils.ChunkUtils;
public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayerActionAckPacket> {
@ -40,18 +36,7 @@ public class JavaPlayerActionAckTranslator extends PacketTranslator<ServerPlayer
public void translate(ServerPlayerActionAckPacket packet, GeyserSession session) {
switch (packet.getAction()) {
case FINISH_DIGGING:
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setDataLayer(0);
updateBlockPacket.setBlockPosition(Vector3i.from(
packet.getPosition().getX(),
packet.getPosition().getY(),
packet.getPosition().getZ()));
BlockEntry itemEntry = TranslatorsInit.getBlockTranslator().getBedrockBlock(packet.getNewState());
updateBlockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(itemEntry.getBedrockId() << 4 | itemEntry.getBedrockData()));
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
session.getUpstream().sendPacket(updateBlockPacket);
ChunkUtils.updateBlock(session, packet.getNewState(), packet.getPosition());
break;
}
}

View file

@ -1,31 +1,14 @@
package org.geysermc.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerBlockChangePacket;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
import org.geysermc.connector.utils.ChunkUtils;
public class JavaBlockChangeTranslator extends PacketTranslator<ServerBlockChangePacket> {
@Override
public void translate(ServerBlockChangePacket packet, GeyserSession session) {
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
BlockChangeRecord record = packet.getRecord();
updateBlockPacket.setDataLayer(0);
updateBlockPacket.setBlockPosition(Vector3i.from(
record.getPosition().getX(),
record.getPosition().getY(),
record.getPosition().getZ()
));
BlockEntry itemEntry = TranslatorsInit.getBlockTranslator().getBedrockBlock(record.getBlock());
updateBlockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(itemEntry.getBedrockId() << 4 | itemEntry.getBedrockData()));
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
session.getUpstream().sendPacket(updateBlockPacket);
ChunkUtils.updateBlock(session, packet.getRecord().getBlock(), packet.getRecord().getPosition());
}
}

View file

@ -27,32 +27,16 @@ package org.geysermc.connector.network.translators.java.world;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
import com.github.steveice10.mc.protocol.packet.ingame.server.world.ServerMultiBlockChangePacket;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
import org.geysermc.connector.utils.ChunkUtils;
public class JavaMultiBlockChangeTranslator extends PacketTranslator<ServerMultiBlockChangePacket> {
@Override
public void translate(ServerMultiBlockChangePacket packet, GeyserSession session) {
for (BlockChangeRecord record : packet.getRecords()) {
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setDataLayer(0);
updateBlockPacket.setBlockPosition(Vector3i.from(
record.getPosition().getX(),
record.getPosition().getY(),
record.getPosition().getZ()
));
BlockEntry blockEntry = TranslatorsInit.getBlockTranslator().getBedrockBlock(record.getBlock());
updateBlockPacket.setRuntimeId(GlobalBlockPalette.getOrCreateRuntimeId(blockEntry.getBedrockId() << 4 | blockEntry.getBedrockData()));
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
session.getUpstream().sendPacket(updateBlockPacket);
ChunkUtils.updateBlock(session, record.getBlock(), record.getPosition());
}
}
}

View file

@ -2,13 +2,17 @@ package org.geysermc.connector.utils;
import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
import com.github.steveice10.mc.protocol.data.game.chunk.Column;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.TranslatorsInit;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.world.chunk.ChunkSection;
public class ChunkUtils {
public static ChunkData translateToBedrock(Column column) {
ChunkData chunkData = new ChunkData();
@ -29,14 +33,14 @@ public class ChunkUtils {
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
BlockState blockState = chunk.get(x, y, z);
BlockEntry block = TranslatorsInit.getBlockTranslator().getBedrockBlock(blockState);
BlockEntry block = TranslatorsInit.getBlockTranslator().getBlockEntry(blockState);
section.getBlockStorageArray()[0].setFullBlock(ChunkSection.blockPosition(x, y, z),
block.getBedrockId() << 4 | block.getBedrockData());
block.getBedrockRuntimeId());
if (block.getJavaIdentifier().contains("waterlogged=true")) {
section.getBlockStorageArray()[1].setFullBlock(ChunkSection.blockPosition(x, y, z),
9 << 4); // water id
if (TranslatorsInit.getBlockTranslator().isWaterLogged(block)) {
BlockEntry water = TranslatorsInit.getBlockTranslator().getBlockEntry("minecraft:water[level=0]");
section.getBlockStorageArray()[1].setFullBlock(ChunkSection.blockPosition(x, y, z), water.getBedrockRuntimeId());
}
}
}
@ -45,6 +49,29 @@ public class ChunkUtils {
return chunkData;
}
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());
UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
updateBlockPacket.setDataLayer(0);
updateBlockPacket.setBlockPosition(pos);
updateBlockPacket.setRuntimeId(blockEntry.getBedrockRuntimeId());
updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
session.getUpstream().sendPacket(updateBlockPacket);
UpdateBlockPacket waterPacket = new UpdateBlockPacket();
waterPacket.setDataLayer(1);
waterPacket.setBlockPosition(pos);
if (TranslatorsInit.getBlockTranslator().isWaterLogged(blockEntry)) {
BlockEntry water = TranslatorsInit.getBlockTranslator().getBlockEntry("minecraft:water[level=0]");
waterPacket.setRuntimeId(water.getBedrockRuntimeId());
} else {
waterPacket.setRuntimeId(0);
}
session.getUpstream().sendPacket(waterPacket);
}
public static final class ChunkData {
public ChunkSection[] sections;

View file

@ -5,6 +5,7 @@ import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.nbt.stream.NBTInputStream;
import com.nukkitx.nbt.tag.CompoundTag;
import com.nukkitx.nbt.tag.ListTag;
import com.nukkitx.nbt.tag.Tag;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
@ -13,7 +14,6 @@ import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.console.GeyserLogger;
import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
import java.io.InputStream;
import java.util.*;
@ -32,7 +32,6 @@ public class Toolbox {
throw new AssertionError("Unable to find bedrock/runtime_block_states.dat");
}
Map<String, Integer> blockIdToIdentifier = new HashMap<>();
ListTag<CompoundTag> blocksTag;
NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(stream);
@ -44,15 +43,6 @@ public class Toolbox {
throw new AssertionError(ex);
}
for (CompoundTag entry : blocksTag.getValue()) {
String name = entry.getAsCompound("block").getAsString("name");
int id = entry.getAsShort("id");
int data = entry.getAsShort("meta");
blockIdToIdentifier.put(name, id);
GlobalBlockPalette.registerMapping(id << 4 | data);
}
BLOCKS = blocksTag;
InputStream stream2 = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/items.json");
if (stream2 == null) {
@ -97,16 +87,38 @@ public class Toolbox {
ex.printStackTrace();
}
int blockIndex = 0;
for (Map.Entry<String, Map<String, Object>> itemEntry : blocks.entrySet()) {
if (!blockIdToIdentifier.containsKey(itemEntry.getValue().get("bedrock_identifier"))) {
GeyserLogger.DEFAULT.debug("Mapping " + itemEntry.getValue().get("bedrock_identifier") + " was not found for bedrock edition!");
BLOCK_ENTRIES.put(blockIndex, new BlockEntry(itemEntry.getKey(), blockIndex, 248, 0)); // update block
} else {
BLOCK_ENTRIES.put(blockIndex, new BlockEntry(itemEntry.getKey(), blockIndex, blockIdToIdentifier.get(itemEntry.getValue().get("bedrock_identifier")), (int) itemEntry.getValue().get("bedrock_data")));
}
int javaIndex = -1;
javaLoop:
for (Map.Entry<String, Map<String, Object>> javaEntry : blocks.entrySet()) {
javaIndex++;
String wantedIdentifier = (String) javaEntry.getValue().get("bedrock_identifier");
Map<String, Object> wantedStates = (Map<String, Object>) javaEntry.getValue().get("bedrock_states");
blockIndex++;
int bedrockIndex = -1;
bedrockLoop:
for (CompoundTag bedrockEntry : BLOCKS.getValue()) {
bedrockIndex++;
CompoundTag blockTag = bedrockEntry.getAsCompound("block");
if (blockTag.getAsString("name").equals(wantedIdentifier)) {
if (wantedStates != null) {
Map<String, Tag<?>> bedrockStates = blockTag.getAsCompound("states").getValue();
for (Map.Entry<String, Object> stateEntry : wantedStates.entrySet()) {
Tag<?> bedrockStateTag = bedrockStates.get(stateEntry.getKey());
if (bedrockStateTag == null)
continue bedrockLoop;
Object bedrockStateValue = bedrockStateTag.getValue();
if (bedrockStateValue instanceof Byte)
bedrockStateValue = ((Byte) bedrockStateValue).intValue();
if (!stateEntry.getValue().equals(bedrockStateValue))
continue bedrockLoop;
}
}
BlockEntry blockEntry = new BlockEntry(javaEntry.getKey(), javaIndex, bedrockIndex);
BLOCK_ENTRIES.put(javaIndex, blockEntry);
continue javaLoop;
}
}
GeyserLogger.DEFAULT.debug("Mapping " + javaEntry.getKey() + " was not found for bedrock edition!");
}
}
}

View file

@ -1,47 +0,0 @@
package org.geysermc.connector.world;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Adapted from NukkitX: https://github.com/NukkitX/Nukkit
*/
public class GlobalBlockPalette {
private static final Int2IntMap legacyToRuntimeId = new Int2IntOpenHashMap();
private static final Int2IntMap runtimeIdToLegacy = new Int2IntOpenHashMap();
private static final AtomicInteger runtimeIdAllocator = new AtomicInteger(0);
static {
legacyToRuntimeId.defaultReturnValue(-1);
runtimeIdToLegacy.defaultReturnValue(-1);
}
public static int getOrCreateRuntimeId(int id, int meta) {
return getOrCreateRuntimeId((id << 4) | meta);
}
public static int getOrCreateRuntimeId(int legacyId) throws NoSuchElementException {
int runtimeId = legacyToRuntimeId.get(legacyId);
if (!legacyToRuntimeId.containsKey(legacyId) || runtimeId == -1) {
//runtimeId = registerMapping(runtimeIdAllocator.incrementAndGet(), legacyId);
// throw new NoSuchElementException("Unmapped block registered id:" + (legacyId >>> 4) + " meta:" + (legacyId & 0xf));
return 0;
}
return runtimeId;
}
public static int registerMapping(int legacyId) {
int runtimeId = runtimeIdAllocator.getAndIncrement();
runtimeIdToLegacy.put(runtimeId, legacyId);
legacyToRuntimeId.put(legacyId, runtimeId);
return runtimeId;
}
public static int getLegacyId(int runtimeId) {
return runtimeIdToLegacy.get(runtimeId);
}
}

View file

@ -4,7 +4,6 @@ import com.nukkitx.network.VarInts;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.geysermc.connector.world.GlobalBlockPalette;
import org.geysermc.connector.world.chunk.bitarray.BitArray;
import org.geysermc.connector.world.chunk.bitarray.BitArrayVersion;
@ -44,11 +43,11 @@ public class BlockStorage {
}
public synchronized int getFullBlock(int index) {
return this.legacyIdFor(this.bitArray.get(index));
return this.palette.getInt(this.bitArray.get(index));
}
public synchronized void setFullBlock(int index, int legacyId) {
int idx = this.idFor(legacyId);
public synchronized void setFullBlock(int index, int runtimeId) {
int idx = this.idFor(runtimeId);
this.bitArray.set(index, idx);
}
@ -72,8 +71,7 @@ public class BlockStorage {
this.bitArray = newBitArray;
}
private int idFor(int legacyId) {
int runtimeId = GlobalBlockPalette.getOrCreateRuntimeId(legacyId);
private int idFor(int runtimeId) {
int index = this.palette.indexOf(runtimeId);
if (index != -1) {
return index;
@ -91,11 +89,6 @@ public class BlockStorage {
return index;
}
private int legacyIdFor(int index) {
int runtimeId = this.palette.get(index);
return GlobalBlockPalette.getLegacyId(runtimeId);
}
public boolean isEmpty() {
if (this.palette.size() == 1) {
return true;

@ -1 +1 @@
Subproject commit 0f3d65b65076b16fc7dc9226230b31291b8f2cde
Subproject commit 3b962523aee0bb9e74a3a4d7f0c9a4964f942d38