Added ItemStack data type

This commit is contained in:
Felix Cravic 2020-04-28 03:02:29 +02:00
parent 0946c8ce11
commit e43325edd8
5 changed files with 33 additions and 11 deletions

View File

@ -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 <T> void registerType(Class<T> clazz, DataType<T> dataType) {

View File

@ -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<ItemStack> {
@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;
}
}

View File

@ -39,14 +39,14 @@ public class PacketProcessor {
private List<Integer> 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

View File

@ -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();
}

View File

@ -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;
}
}