From e43325edd8d6e93c5c3768cbc11ae225f7a4383b Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Tue, 28 Apr 2020 03:02:29 +0200 Subject: [PATCH] Added ItemStack data type --- .../net/minestom/server/data/DataManager.java | 3 +++ .../server/data/type/ItemStackData.java | 25 +++++++++++++++++++ .../server/network/PacketProcessor.java | 4 +-- .../network/netty/channel/ClientChannel.java | 4 +-- .../server/network/packet/PacketReader.java | 8 +----- 5 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/minestom/server/data/type/ItemStackData.java diff --git a/src/main/java/net/minestom/server/data/DataManager.java b/src/main/java/net/minestom/server/data/DataManager.java index e2b2ad444..65f52393e 100644 --- a/src/main/java/net/minestom/server/data/DataManager.java +++ b/src/main/java/net/minestom/server/data/DataManager.java @@ -2,6 +2,7 @@ package net.minestom.server.data; import net.minestom.server.data.type.CharacterData; import net.minestom.server.data.type.*; +import net.minestom.server.item.ItemStack; import net.minestom.server.utils.PrimitiveConversion; import java.util.HashMap; @@ -24,6 +25,8 @@ public class DataManager { registerType(String.class, new StringData()); registerType(SerializableData.class, new SerializableDataData()); + + registerType(ItemStack.class, new ItemStackData()); } public void registerType(Class clazz, DataType dataType) { diff --git a/src/main/java/net/minestom/server/data/type/ItemStackData.java b/src/main/java/net/minestom/server/data/type/ItemStackData.java new file mode 100644 index 000000000..d603c0025 --- /dev/null +++ b/src/main/java/net/minestom/server/data/type/ItemStackData.java @@ -0,0 +1,25 @@ +package net.minestom.server.data.type; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import net.minestom.server.data.DataType; +import net.minestom.server.item.ItemStack; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; +import net.minestom.server.utils.Utils; + +public class ItemStackData extends DataType { + @Override + public byte[] encode(ItemStack value) { + PacketWriter packetWriter = new PacketWriter(); + packetWriter.writeItemStack(value); + return packetWriter.toByteArray(); + } + + @Override + public ItemStack decode(byte[] value) { + ByteBuf buffer = Unpooled.buffer(value.length); + ItemStack itemStack = Utils.readItemStack(new PacketReader(buffer)); + return itemStack; + } +} diff --git a/src/main/java/net/minestom/server/network/PacketProcessor.java b/src/main/java/net/minestom/server/network/PacketProcessor.java index 0b2c3fe94..51c9df835 100644 --- a/src/main/java/net/minestom/server/network/PacketProcessor.java +++ b/src/main/java/net/minestom/server/network/PacketProcessor.java @@ -39,14 +39,14 @@ public class PacketProcessor { private List printBlackList = Arrays.asList(17, 18, 19); - public void process(ChannelHandlerContext channel, ByteBuf buffer, int id, int length, int offset) { + public void process(ChannelHandlerContext channel, ByteBuf buffer, int id, int offset) { PlayerConnection playerConnection = connectionPlayerConnectionMap.computeIfAbsent(channel, c -> new PlayerConnection(channel)); ConnectionState connectionState = playerConnection.getConnectionState(); //if (!printBlackList.contains(id)) { //System.out.println("RECEIVED ID: 0x" + Integer.toHexString(id) + " State: " + connectionState); //} - PacketReader packetReader = new PacketReader(buffer, length); + PacketReader packetReader = new PacketReader(buffer); if (connectionState == ConnectionState.UNKNOWN) { // Should be handshake packet diff --git a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java index 8e21c7296..dbb737790 100644 --- a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java +++ b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java @@ -35,7 +35,7 @@ public class ClientChannel extends ChannelInboundHandlerAdapter { if (packetLength == 0xFE) { // Legacy server ping LegacyServerListPingPacket legacyServerListPingPacket = new LegacyServerListPingPacket(); - legacyServerListPingPacket.read(new PacketReader(buffer, 0)); + legacyServerListPingPacket.read(new PacketReader(buffer)); legacyServerListPingPacket.process(null, null); return; } @@ -44,7 +44,7 @@ public class ClientChannel extends ChannelInboundHandlerAdapter { int packetId = Utils.readVarInt(buffer); int offset = varIntLength + Utils.lengthVarInt(packetId); - packetProcessor.process(ctx, buffer, packetId, packetLength, offset); + packetProcessor.process(ctx, buffer, packetId, offset); buffer.release(); } diff --git a/src/main/java/net/minestom/server/network/packet/PacketReader.java b/src/main/java/net/minestom/server/network/packet/PacketReader.java index 4b54871e5..76273117e 100644 --- a/src/main/java/net/minestom/server/network/packet/PacketReader.java +++ b/src/main/java/net/minestom/server/network/packet/PacketReader.java @@ -9,11 +9,9 @@ import net.minestom.server.utils.Utils; public class PacketReader { private ByteBuf buffer; - private int length; - public PacketReader(ByteBuf buffer, int length) { + public PacketReader(ByteBuf buffer) { this.buffer = buffer; - this.length = length; } public int readVarInt() { @@ -90,8 +88,4 @@ public class PacketReader { public ByteBuf getBuffer() { return buffer; } - - public int getPacketLength() { - return length; - } }