diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java index d484a9e78..fd1cd92ae 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/MetadataRewriter.java @@ -150,12 +150,12 @@ public abstract class MetadataRewriter { protocol.registerOutgoing(State.PLAY, oldPacketId, newPacketId, new PacketRemapper() { @Override public void registerMap() { - map(Type.VAR_INT_ARRAY); // 0 - Entity ids + map(Type.VAR_INT_ARRAY_PRIMITIVE); // 0 - Entity ids handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { EntityTracker entityTracker = wrapper.user().get(entityTrackerClass); - for (int entity : wrapper.get(Type.VAR_INT_ARRAY, 0)) { + for (int entity : wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0)) { entityTracker.removeEntity(entity); } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java index 25c6cd0ab..6ae163471 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -14,32 +14,69 @@ import java.util.UUID; public abstract class Type implements ByteBufReader, ByteBufWriter { /* Defined Types */ public static final Type BYTE = new ByteType(); + /** + * @deprecated unreasonable overhead, use BYTE_ARRAY_PRIMITIVE + */ + @Deprecated public static final Type BYTE_ARRAY = new ArrayType<>(Type.BYTE); + public static final Type BYTE_ARRAY_PRIMITIVE = new ByteArrayType(); public static final Type REMAINING_BYTES = new RemainingBytesType(); public static final Type UNSIGNED_BYTE = new UnsignedByteType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type UNSIGNED_BYTE_ARRAY = new ArrayType<>(Type.UNSIGNED_BYTE); public static final Type BOOLEAN = new BooleanType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type BOOLEAN_ARRAY = new ArrayType<>(Type.BOOLEAN); /* Number Types */ public static final Type INT = new IntType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type INT_ARRAY = new ArrayType<>(Type.INT); public static final Type DOUBLE = new DoubleType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type DOUBLE_ARRAY = new ArrayType<>(Type.DOUBLE); public static final Type LONG = new LongType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type LONG_ARRAY = new ArrayType<>(Type.LONG); public static final Type FLOAT = new FloatType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type FLOAT_ARRAY = new ArrayType<>(Type.FLOAT); public static final Type SHORT = new ShortType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type SHORT_ARRAY = new ArrayType<>(Type.SHORT); public static final Type UNSIGNED_SHORT = new UnsignedShortType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type UNSIGNED_SHORT_ARRAY = new ArrayType<>(Type.UNSIGNED_SHORT); /* Other Types */ public static final Type STRING = new StringType(); @@ -49,9 +86,18 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type UUID_ARRAY = new ArrayType<>(Type.UUID); /* Variable Types */ public static final Type VAR_INT = new VarIntType(); + /** + * @deprecated unreasonable overhead, use VAR_INT_ARRAY_PRIMITIVE + */ + @Deprecated public static final Type VAR_INT_ARRAY = new ArrayType<>(Type.VAR_INT); + public static final Type VAR_INT_ARRAY_PRIMITIVE = new VarIntArrayType(); public static final Type OPTIONAL_VAR_INT = new OptionalVarIntType(); public static final Type VAR_LONG = new VarLongType(); + /** + * @deprecated unreasonable overhead + */ + @Deprecated public static final Type VAR_LONG_ARRAY = new ArrayType<>(Type.VAR_LONG); /* Special Types */ public static final Type NOTHING = new VoidType(); // This is purely used for remapping. diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java new file mode 100644 index 000000000..9b462f88f --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java @@ -0,0 +1,26 @@ +package us.myles.ViaVersion.api.type.types; + +import com.google.common.base.Preconditions; +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.type.Type; + +public class ByteArrayType extends Type { + public ByteArrayType() { + super(byte[].class); + } + + @Override + public void write(ByteBuf buffer, byte[] object) throws Exception { + Type.VAR_INT.write(buffer, object.length); + buffer.writeBytes(object); + } + + @Override + public byte[] read(ByteBuf buffer) throws Exception { + int length = Type.VAR_INT.read(buffer); + Preconditions.checkArgument(!buffer.isReadable(length), "Length is fewer than readable bytes"); + byte[] array = new byte[length]; + buffer.readBytes(array); + return array; + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/VarIntArrayType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/VarIntArrayType.java new file mode 100644 index 000000000..1cf1f719e --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/VarIntArrayType.java @@ -0,0 +1,30 @@ +package us.myles.ViaVersion.api.type.types; + +import com.google.common.base.Preconditions; +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.type.Type; + +public class VarIntArrayType extends Type { + public VarIntArrayType() { + super(int[].class); + } + + @Override + public int[] read(ByteBuf buffer) throws Exception { + int length = Type.VAR_INT.read(buffer); + Preconditions.checkArgument(buffer.isReadable(length)); // Sanity check, at least 1 byte will be used for each varint + int[] array = new int[length]; + for (int i = 0; i < array.length; i++) { + array[i] = Type.VAR_INT.read(buffer); + } + return array; + } + + @Override + public void write(ByteBuf buffer, int[] object) throws Exception { + Type.VAR_INT.write(buffer, object.length); + for (int i : object) { + Type.VAR_INT.write(buffer, i); + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java index b588662b3..14895b5e6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13_1to1_13/Protocol1_13_1To1_13.java @@ -187,7 +187,7 @@ public class Protocol1_13_1To1_13 extends Protocol { int blockTagsSize = wrapper.passthrough(Type.VAR_INT); // block tags for (int i = 0; i < blockTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] blocks = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] blocks = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < blocks.length; j++) { blocks[j] = getNewBlockId(blocks[j]); } @@ -195,7 +195,7 @@ public class Protocol1_13_1To1_13 extends Protocol { int itemTagsSize = wrapper.passthrough(Type.VAR_INT); // item tags for (int i = 0; i < itemTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] items = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] items = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < items.length; j++) { items[j] = InventoryPackets.getNewItemId(items[j]); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java index cca497ea8..3327c29b1 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/Protocol1_13To1_12_2.java @@ -87,17 +87,20 @@ public class Protocol1_13To1_12_2 extends Protocol { wrapper.write(Type.VAR_INT, MappingData.blockTags.size()); // block tags for (Map.Entry tag : MappingData.blockTags.entrySet()) { wrapper.write(Type.STRING, tag.getKey()); - wrapper.write(Type.VAR_INT_ARRAY, tag.getValue().clone()); + // Needs copy as other protocols may modify it + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); } wrapper.write(Type.VAR_INT, MappingData.itemTags.size()); // item tags for (Map.Entry tag : MappingData.itemTags.entrySet()) { wrapper.write(Type.STRING, tag.getKey()); - wrapper.write(Type.VAR_INT_ARRAY, tag.getValue().clone()); + // Needs copy as other protocols may modify it + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); } wrapper.write(Type.VAR_INT, MappingData.fluidTags.size()); // fluid tags for (Map.Entry tag : MappingData.fluidTags.entrySet()) { wrapper.write(Type.STRING, tag.getKey()); - wrapper.write(Type.VAR_INT_ARRAY, tag.getValue().clone()); + // Needs copy as other protocols may modify it + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, toPrimitive(tag.getValue())); } } }).send(Protocol1_13To1_12_2.class); @@ -482,7 +485,7 @@ public class Protocol1_13To1_12_2 extends Protocol { public void handle(PacketWrapper wrapper) throws Exception { int action = wrapper.get(Type.VAR_INT, 0); for (int i = 0; i < (action == 0 ? 2 : 1); i++) { - Integer[] ids = wrapper.read(Type.VAR_INT_ARRAY); + int[] ids = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE); String[] stringIds = new String[ids.length]; for (int j = 0; j < ids.length; j++) { stringIds[j] = "viaversion:legacy/" + ids[j]; @@ -1178,4 +1181,12 @@ public class Protocol1_13To1_12_2 extends Protocol { } return name; } + + public static int[] toPrimitive(Integer[] array) { + int[] prim = new int[array.length]; + for (int i = 0; i < array.length; i++) { + prim[i] = array[i]; + } + return prim; + } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java index c3e387772..af06b4a17 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/Protocol1_14To1_13_2.java @@ -153,62 +153,50 @@ public class Protocol1_14To1_13_2 extends Protocol { wrapper.write(Type.VAR_INT, blockTagsSize + 5); // block tags for (int i = 0; i < blockTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < blockIds.length; j++) { blockIds[j] = getNewBlockId(blockIds[j]); } } // Minecraft crashes if we not send signs tags wrapper.write(Type.STRING, "minecraft:signs"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{ getNewBlockId(150), getNewBlockId(155) }); wrapper.write(Type.STRING, "minecraft:wall_signs"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{ getNewBlockId(155) }); wrapper.write(Type.STRING, "minecraft:standing_signs"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{ getNewBlockId(150) }); // Fences and walls tags - used for block connections wrapper.write(Type.STRING, "minecraft:fences"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ - 189, - 248, - 472, - 473, - 474, - 475 - }); + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{189, 248, 472, 473, 474, 475}); wrapper.write(Type.STRING, "minecraft:walls"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ - 271, - 272, - }); + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{271, 272}); int itemTagsSize = wrapper.read(Type.VAR_INT); wrapper.write(Type.VAR_INT, itemTagsSize + 2); // item tags for (int i = 0; i < itemTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < itemIds.length; j++) { itemIds[j] = InventoryPackets.getNewItemId(itemIds[j]); } } // Should fix fuel shift clicking wrapper.write(Type.STRING, "minecraft:signs"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{ InventoryPackets.getNewItemId(541) }); // Arrows tag (used by bow) wrapper.write(Type.STRING, "minecraft:arrows"); - wrapper.write(Type.VAR_INT_ARRAY, new Integer[]{ - 526, 825, 826 - }); + wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{526, 825, 826}); int fluidTagsSize = wrapper.passthrough(Type.VAR_INT); // fluid tags for (int i = 0; i < fluidTagsSize; i++) { wrapper.passthrough(Type.STRING); - wrapper.passthrough(Type.VAR_INT_ARRAY); + wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); } wrapper.write(Type.VAR_INT, 0); // new entity tags - do we need to send this? } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 4de02892f..5e9cd678f 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -32,7 +32,7 @@ public class WorldPackets { private static final int VOID_AIR = MappingData.blockStateMappings.getNewId(8591); private static final int CAVE_AIR = MappingData.blockStateMappings.getNewId(8592); public static final int SERVERSIDE_VIEW_DISTANCE = 64; - private static final Byte[] FULL_LIGHT = new Byte[2048]; + private static final byte[] FULL_LIGHT = new byte[2048]; static { Arrays.fill(FULL_LIGHT, (byte) 0xff); @@ -234,22 +234,22 @@ public class WorldPackets { // not sending skylight/setting empty skylight causes client lag due to some weird calculations // only do this on the initial chunk send (not when chunk.isGroundUp() is false) if (chunk.isGroundUp()) - lightPacket.write(Type.BYTE_ARRAY, FULL_LIGHT); // chunk below 0 + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT); // chunk below 0 for (ChunkSection section : chunk.getSections()) { if (section == null || !section.hasSkyLight()) { if (chunk.isGroundUp()) { - lightPacket.write(Type.BYTE_ARRAY, FULL_LIGHT); + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT); } continue; } - lightPacket.write(Type.BYTE_ARRAY, fromPrimitiveArray(section.getSkyLight())); + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getSkyLight()); } if (chunk.isGroundUp()) - lightPacket.write(Type.BYTE_ARRAY, FULL_LIGHT); // chunk above 255 + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, FULL_LIGHT); // chunk above 255 for (ChunkSection section : chunk.getSections()) { if (section == null) continue; - lightPacket.write(Type.BYTE_ARRAY, fromPrimitiveArray(section.getBlockLight())); + lightPacket.write(Type.BYTE_ARRAY_PRIMITIVE, section.getBlockLight()); } EntityTracker1_14 entityTracker = wrapper.user().get(EntityTracker1_14.class); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java index 24abe0fca..142f4353e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_15to1_14_4/Protocol1_15To1_14_4.java @@ -122,7 +122,7 @@ public class Protocol1_15To1_14_4 extends Protocol { int blockTagsSize = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < blockTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < blockIds.length; j++) { blockIds[j] = getNewBlockId(blockIds[j]); } @@ -131,7 +131,7 @@ public class Protocol1_15To1_14_4 extends Protocol { int itemTagsSize = wrapper.passthrough(Type.VAR_INT); for (int i = 0; i < itemTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < itemIds.length; j++) { itemIds[j] = InventoryPackets.getNewItemId(itemIds[j]); } @@ -140,13 +140,13 @@ public class Protocol1_15To1_14_4 extends Protocol { int fluidTagsSize = wrapper.passthrough(Type.VAR_INT); // fluid tags for (int i = 0; i < fluidTagsSize; i++) { wrapper.passthrough(Type.STRING); - wrapper.passthrough(Type.VAR_INT_ARRAY); + wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); } int entityTagsSize = wrapper.passthrough(Type.VAR_INT); // entity tags for (int i = 0; i < entityTagsSize; i++) { wrapper.passthrough(Type.STRING); - Integer[] entitIds = wrapper.passthrough(Type.VAR_INT_ARRAY); + int[] entitIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE); for (int j = 0; j < entitIds.length; j++) { entitIds[j] = EntityPackets.getNewEntityId(entitIds[j]); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java index 9789b56fe..e2abd4c7a 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -55,10 +55,10 @@ public class EntityPackets { if (!tracker.getVehicleMap().containsKey(passenger)) return null; // Cancel passengerPacket.write(Type.VAR_INT, tracker.getVehicleMap().remove(passenger)); - passengerPacket.write(Type.VAR_INT_ARRAY, new Integer[]{}); + passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{}); } else { passengerPacket.write(Type.VAR_INT, vehicle); - passengerPacket.write(Type.VAR_INT_ARRAY, new Integer[]{passenger}); + passengerPacket.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[]{passenger}); tracker.getVehicleMap().put(passenger, vehicle); } passengerPacket.send(Protocol1_9To1_8.class); // Send the packet diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java index 6c70d74ce..46ef416ac 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/packets/SpawnPackets.java @@ -346,13 +346,13 @@ public class SpawnPackets { @Override public void registerMap() { - map(Type.VAR_INT_ARRAY); // 0 - Entities to destroy + map(Type.VAR_INT_ARRAY_PRIMITIVE); // 0 - Entities to destroy handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - Integer[] entities = wrapper.get(Type.VAR_INT_ARRAY, 0); - for (Integer entity : entities) { + int[] entities = wrapper.get(Type.VAR_INT_ARRAY_PRIMITIVE, 0); + for (int entity : entities) { // EntityTracker wrapper.user().get(EntityTracker1_9.class).removeEntity(entity); }