From f5b49bc9ce7ee6a2616e159f1d386e3eb07c3ffd Mon Sep 17 00:00:00 2001 From: Matsv Date: Wed, 22 Jun 2016 19:07:46 +0200 Subject: [PATCH] Experimental implementation for block entities into chunks --- .../us/myles/ViaVersion/api/type/Type.java | 4 +-- .../type/types/minecraft/NBTArrayType.java | 33 +++++++++++++++++++ .../FakeTileEntity.java | 5 +++ .../Protocol1_9_3TO1_9_1_2.java | 4 +-- .../protocol1_9to1_8/types/ChunkType.java | 17 ++++++++++ 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTArrayType.java diff --git a/src/main/java/us/myles/ViaVersion/api/type/Type.java b/src/main/java/us/myles/ViaVersion/api/type/Type.java index 2bd3ffdce..81dc97ed3 100644 --- a/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -10,8 +10,7 @@ import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.type.types.*; import us.myles.ViaVersion.api.type.types.minecraft.*; -import java.util.HashSet; -import java.util.Set; +import java.util.List; import java.util.UUID; @Getter @@ -61,6 +60,7 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type ROTATION = new EulerAngleType(); public static final Type VECTOR = new VectorType(); public static final Type NBT = new NBTType(); + public static final Type> NBT_ARRAY = new NBTArrayType(); public static final Type OPTIONAL_UUID = new OptUUIDType(); public static final Type OPTIONAL_POSITION = new OptPositionType(); diff --git a/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTArrayType.java b/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTArrayType.java new file mode 100644 index 000000000..a39794169 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTArrayType.java @@ -0,0 +1,33 @@ +package us.myles.ViaVersion.api.type.types.minecraft; + +import io.netty.buffer.ByteBuf; +import org.spacehq.opennbt.tag.builtin.CompoundTag; +import us.myles.ViaVersion.api.type.Type; + +import java.util.ArrayList; +import java.util.List; + +public class NBTArrayType extends Type> { + public NBTArrayType() { + super("NBT Array", List.class); + } + + @Override + public List read(ByteBuf buffer) throws Exception { + int amount = Type.VAR_INT.read(buffer); + + List nbtData = new ArrayList<>(); + for (int i = 0; i < amount; i++) + nbtData.add(Type.NBT.read(buffer)); + + return nbtData; + } + + @Override + public void write(ByteBuf buffer, List list) throws Exception { + Type.VAR_INT.write(buffer, list.size()); + + for (CompoundTag tag : list) + Type.NBT.write(buffer, tag); + } +} diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/FakeTileEntity.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/FakeTileEntity.java index c537edf2c..a3823fe79 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/FakeTileEntity.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/FakeTileEntity.java @@ -94,12 +94,17 @@ public class FakeTileEntity { register(m.getId(), name, tags); } + public static boolean hasBlock(int block) { + return tileEntities.containsKey(block); + } + public static CompoundTag getFromBlock(int x, int y, int z, int block) { if (tileEntities.containsKey(block)) { CompoundTag tag = tileEntities.get(block).clone(); tag.put(new IntTag("x", x)); tag.put(new IntTag("y", y)); tag.put(new IntTag("z", z)); + System.out.println("Found tile entity " + block + " at position " + x + " " + y + " " + z); return tag; } return null; diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java index 29c6263d1..9ff12b9a9 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/Protocol1_9_3TO1_9_1_2.java @@ -60,7 +60,7 @@ public class Protocol1_9_3TO1_9_1_2 extends Protocol { } }); - // Chunk packet + /* // Chunk packet TODO uncomment when it's no longer hardcoded in 1.8 registerOutgoing(State.PLAY, 0x20, 0x20, new PacketRemapper() { @Override public void registerMap() { @@ -72,7 +72,7 @@ public class Protocol1_9_3TO1_9_1_2 extends Protocol { } }); } - }); + });*/ } @Override diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java index 693d3cbad..2fb99634c 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/types/ChunkType.java @@ -3,10 +3,12 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.types; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import org.bukkit.Bukkit; +import org.spacehq.opennbt.tag.builtin.CompoundTag; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.type.PartialType; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.types.minecraft.BaseChunkType; +import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.FakeTileEntity; import us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks.Chunk1_9to1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.chunks.ChunkSection1_9to1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; @@ -14,7 +16,9 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.ClientChunks; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; +import java.util.ArrayList; import java.util.BitSet; +import java.util.List; import java.util.logging.Level; public class ChunkType extends PartialType { @@ -145,14 +149,25 @@ public class ChunkType extends PartialType { output.writeByte(chunk.isGroundUp() ? 0x01 : 0x00); Type.VAR_INT.write(output, chunk.getPrimaryBitmask()); + List tags = new ArrayList<>(); + ByteBuf buf = Unpooled.buffer(); for (int i = 0; i < SECTION_COUNT; i++) { ChunkSection1_9to1_8 section = chunk.getSections()[i]; if (section == null) continue; // Section not set section.writeBlocks(buf); section.writeBlockLight(buf); + for (int x = 0; x < 16; x++) + for (int y = 0; y < 16; y++) + for (int z = 0; z < 16; z++) { + int block = section.getBlockId(x, y, z); + if (FakeTileEntity.hasBlock(block)) + tags.add(FakeTileEntity.getFromBlock(x * chunk.getX(), y, z * chunk.getZ(), block)); + } + if (!section.hasSkyLight()) continue; // No sky light, we're done here. section.writeSkyLight(buf); + } buf.readerIndex(0); Type.VAR_INT.write(output, buf.readableBytes() + (chunk.hasBiomeData() ? 256 : 0)); @@ -163,6 +178,8 @@ public class ChunkType extends PartialType { if (chunk.hasBiomeData()) { output.writeBytes(chunk.getBiomeData()); } + + Type.NBT_ARRAY.write(buf, tags); }