diff --git a/src/main/java/fr/themode/demo/commands/SimpleCommand.java b/src/main/java/fr/themode/demo/commands/SimpleCommand.java index 127f92e8a..9e0127f94 100644 --- a/src/main/java/fr/themode/demo/commands/SimpleCommand.java +++ b/src/main/java/fr/themode/demo/commands/SimpleCommand.java @@ -1,11 +1,9 @@ package fr.themode.demo.commands; -import fr.themode.demo.generator.ChunkGeneratorDemo; -import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandProcessor; +import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.Player; import net.minestom.server.instance.Instance; -import net.minestom.server.world.Dimension; public class SimpleCommand implements CommandProcessor { @Override @@ -24,15 +22,10 @@ public class SimpleCommand implements CommandProcessor { Instance instance = player.getInstance(); - /*for (EntityCreature creature : instance.getCreatures()) { + for (EntityCreature creature : instance.getCreatures()) { creature.setPathTo(player.getPosition()); - }*/ + } - Instance inst = MinecraftServer.getInstanceManager().createInstanceContainer(Dimension.NETHER); - inst.enableAutoChunkLoad(true); - inst.setChunkGenerator(new ChunkGeneratorDemo()); - - player.setInstance(inst); return true; } diff --git a/src/main/java/net/minestom/server/data/Data.java b/src/main/java/net/minestom/server/data/Data.java index db06a817f..330647a6c 100644 --- a/src/main/java/net/minestom/server/data/Data.java +++ b/src/main/java/net/minestom/server/data/Data.java @@ -11,9 +11,13 @@ public class Data { protected ConcurrentHashMap data = new ConcurrentHashMap(); public void set(String key, T value, Class type) { - if (DATA_MANAGER.getDataType(type) == null) { - throw new UnsupportedOperationException("Type " + type.getName() + " hasn't been registered in DataManager#registerType"); + // Type registering is only relevant if data should be serialized + if (this instanceof SerializableData) { + if (DATA_MANAGER.getDataType(type) == null) { + throw new UnsupportedOperationException("Type " + type.getName() + " hasn't been registered in DataManager#registerType"); + } } + this.data.put(key, value); } diff --git a/src/main/java/net/minestom/server/data/DataManager.java b/src/main/java/net/minestom/server/data/DataManager.java index 65f52393e..fb536504e 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.data.type.array.ItemStackArrayData; import net.minestom.server.item.ItemStack; import net.minestom.server.utils.PrimitiveConversion; @@ -27,6 +28,7 @@ public class DataManager { registerType(SerializableData.class, new SerializableDataData()); registerType(ItemStack.class, new ItemStackData()); + registerType(ItemStack[].class, new ItemStackArrayData()); } public void registerType(Class clazz, DataType dataType) { diff --git a/src/main/java/net/minestom/server/data/DataType.java b/src/main/java/net/minestom/server/data/DataType.java index 173776ddd..7051f1a02 100644 --- a/src/main/java/net/minestom/server/data/DataType.java +++ b/src/main/java/net/minestom/server/data/DataType.java @@ -1,9 +1,12 @@ package net.minestom.server.data; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; + public abstract class DataType { - public abstract byte[] encode(T value); + public abstract void encode(PacketWriter packetWriter, T value); - public abstract T decode(byte[] value); + public abstract T decode(PacketReader packetReader, byte[] value); } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/data/SerializableData.java b/src/main/java/net/minestom/server/data/SerializableData.java index 93ec4cb26..66c84db24 100644 --- a/src/main/java/net/minestom/server/data/SerializableData.java +++ b/src/main/java/net/minestom/server/data/SerializableData.java @@ -1,5 +1,6 @@ package net.minestom.server.data; +import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.utils.PrimitiveConversion; import java.io.ByteArrayOutputStream; @@ -43,7 +44,9 @@ public class SerializableData extends Data { dos.writeShort(encodedName.length); dos.write(encodedName); - byte[] encodedValue = dataType.encode(value); // Data + PacketWriter packetWriter = new PacketWriter(); + dataType.encode(packetWriter, value); // Encode + byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes dos.writeInt(encodedValue.length); dos.write(encodedValue); } diff --git a/src/main/java/net/minestom/server/data/type/BooleanData.java b/src/main/java/net/minestom/server/data/type/BooleanData.java index 8826fa9be..e28bd41ef 100644 --- a/src/main/java/net/minestom/server/data/type/BooleanData.java +++ b/src/main/java/net/minestom/server/data/type/BooleanData.java @@ -1,15 +1,17 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class BooleanData extends DataType { @Override - public byte[] encode(Boolean value) { - return new byte[]{(byte) (value.booleanValue() ? 1 : 0)}; + public void encode(PacketWriter packetWriter, Boolean value) { + packetWriter.writeBoolean(value); } @Override - public Boolean decode(byte[] value) { - return value[0] == 1; + public Boolean decode(PacketReader packetReader, byte[] value) { + return packetReader.readBoolean(); } } diff --git a/src/main/java/net/minestom/server/data/type/ByteData.java b/src/main/java/net/minestom/server/data/type/ByteData.java index 61572927f..f50ddfb19 100644 --- a/src/main/java/net/minestom/server/data/type/ByteData.java +++ b/src/main/java/net/minestom/server/data/type/ByteData.java @@ -1,15 +1,17 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class ByteData extends DataType { @Override - public byte[] encode(Byte value) { - return new byte[]{value}; + public void encode(PacketWriter packetWriter, Byte value) { + packetWriter.writeByte(value); } @Override - public Byte decode(byte[] value) { - return value[0]; + public Byte decode(PacketReader packetReader, byte[] value) { + return packetReader.readByte(); } } diff --git a/src/main/java/net/minestom/server/data/type/CharacterData.java b/src/main/java/net/minestom/server/data/type/CharacterData.java index 050d3c080..68a75bd53 100644 --- a/src/main/java/net/minestom/server/data/type/CharacterData.java +++ b/src/main/java/net/minestom/server/data/type/CharacterData.java @@ -1,23 +1,18 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; - -import java.nio.ByteBuffer; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class CharacterData extends DataType { @Override - public byte[] encode(Character value) { - ByteBuffer buffer = ByteBuffer.allocate(Character.BYTES); - buffer.putChar(value); - return buffer.array(); + public void encode(PacketWriter packetWriter, Character value) { + packetWriter.writeChar(value); } @Override - public Character decode(byte[] value) { - ByteBuffer buffer = ByteBuffer.allocate(Character.BYTES); - buffer.put(value); - buffer.flip(); - return buffer.getChar(); + public Character decode(PacketReader packetReader, byte[] value) { + return packetReader.readChar(); } } diff --git a/src/main/java/net/minestom/server/data/type/DoubleData.java b/src/main/java/net/minestom/server/data/type/DoubleData.java index 6fc47e291..72906de9b 100644 --- a/src/main/java/net/minestom/server/data/type/DoubleData.java +++ b/src/main/java/net/minestom/server/data/type/DoubleData.java @@ -1,23 +1,18 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; - -import java.nio.ByteBuffer; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class DoubleData extends DataType { @Override - public byte[] encode(Double value) { - ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); - buffer.putDouble(value); - return buffer.array(); + public void encode(PacketWriter packetWriter, Double value) { + packetWriter.writeDouble(value); } @Override - public Double decode(byte[] value) { - ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); - buffer.put(value); - buffer.flip(); - return buffer.getDouble(); + public Double decode(PacketReader packetReader, byte[] value) { + return packetReader.readDouble(); } } diff --git a/src/main/java/net/minestom/server/data/type/FloatData.java b/src/main/java/net/minestom/server/data/type/FloatData.java index a4d370e5b..9e961d246 100644 --- a/src/main/java/net/minestom/server/data/type/FloatData.java +++ b/src/main/java/net/minestom/server/data/type/FloatData.java @@ -1,23 +1,18 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; - -import java.nio.ByteBuffer; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class FloatData extends DataType { @Override - public byte[] encode(Float value) { - ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); - buffer.putFloat(value); - return buffer.array(); + public void encode(PacketWriter packetWriter, Float value) { + packetWriter.writeFloat(value); } @Override - public Float decode(byte[] value) { - ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); - buffer.put(value); - buffer.flip(); - return buffer.getFloat(); + public Float decode(PacketReader packetReader, byte[] value) { + return packetReader.readFloat(); } } diff --git a/src/main/java/net/minestom/server/data/type/IntegerData.java b/src/main/java/net/minestom/server/data/type/IntegerData.java index 01e1d859d..a55099728 100644 --- a/src/main/java/net/minestom/server/data/type/IntegerData.java +++ b/src/main/java/net/minestom/server/data/type/IntegerData.java @@ -1,23 +1,18 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; - -import java.nio.ByteBuffer; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class IntegerData extends DataType { @Override - public byte[] encode(Integer value) { - ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); - buffer.putInt(value); - return buffer.array(); + public void encode(PacketWriter packetWriter, Integer value) { + packetWriter.writeVarInt(value); } @Override - public Integer decode(byte[] value) { - ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); - buffer.put(value); - buffer.flip(); - return buffer.getInt(); + public Integer decode(PacketReader packetReader, byte[] value) { + return packetReader.readVarInt(); } } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/data/type/ItemStackData.java b/src/main/java/net/minestom/server/data/type/ItemStackData.java index 1d38ad4c1..2b0a628ef 100644 --- a/src/main/java/net/minestom/server/data/type/ItemStackData.java +++ b/src/main/java/net/minestom/server/data/type/ItemStackData.java @@ -1,25 +1,18 @@ 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(); + public void encode(PacketWriter packetWriter, ItemStack value) { packetWriter.writeItemStack(value); - return packetWriter.toByteArray(); } @Override - public ItemStack decode(byte[] value) { - ByteBuf buffer = Unpooled.wrappedBuffer(value); - ItemStack itemStack = Utils.readItemStack(new PacketReader(buffer)); - return itemStack; + public ItemStack decode(PacketReader packetReader, byte[] value) { + return packetReader.readSlot(); } } diff --git a/src/main/java/net/minestom/server/data/type/LongData.java b/src/main/java/net/minestom/server/data/type/LongData.java index 589f6b713..74cf6806e 100644 --- a/src/main/java/net/minestom/server/data/type/LongData.java +++ b/src/main/java/net/minestom/server/data/type/LongData.java @@ -1,22 +1,17 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; - -import java.nio.ByteBuffer; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class LongData extends DataType { @Override - public byte[] encode(Long value) { - ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); - buffer.putLong(value); - return buffer.array(); + public void encode(PacketWriter packetWriter, Long value) { + packetWriter.writeLong(value); } @Override - public Long decode(byte[] value) { - ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); - buffer.put(value); - buffer.flip(); - return buffer.getLong(); + public Long decode(PacketReader packetReader, byte[] value) { + return packetReader.readLong(); } } diff --git a/src/main/java/net/minestom/server/data/type/SerializableDataData.java b/src/main/java/net/minestom/server/data/type/SerializableDataData.java index a86151645..44ad1f8fe 100644 --- a/src/main/java/net/minestom/server/data/type/SerializableDataData.java +++ b/src/main/java/net/minestom/server/data/type/SerializableDataData.java @@ -3,6 +3,8 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; import net.minestom.server.data.SerializableData; import net.minestom.server.io.DataReader; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; import java.io.IOException; @@ -10,9 +12,9 @@ import java.io.IOException; public class SerializableDataData extends DataType { @Override - public byte[] encode(SerializableData value) { + public void encode(PacketWriter packetWriter, SerializableData value) { try { - return value.getSerializedData(); + packetWriter.writeBytes(value.getSerializedData()); } catch (IOException e) { e.printStackTrace(); throw new IllegalArgumentException("error while writing the data"); @@ -20,7 +22,7 @@ public class SerializableDataData extends DataType { } @Override - public SerializableData decode(byte[] value) { + public SerializableData decode(PacketReader packetReader, byte[] value) { return DataReader.readData(value, false); } } diff --git a/src/main/java/net/minestom/server/data/type/ShortData.java b/src/main/java/net/minestom/server/data/type/ShortData.java index 6296ceddf..a99a2f9b5 100644 --- a/src/main/java/net/minestom/server/data/type/ShortData.java +++ b/src/main/java/net/minestom/server/data/type/ShortData.java @@ -1,23 +1,18 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; - -import java.nio.ByteBuffer; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class ShortData extends DataType { @Override - public byte[] encode(Short value) { - ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); - buffer.putShort(value); - return buffer.array(); + public void encode(PacketWriter packetWriter, Short value) { + packetWriter.writeShort(value); } @Override - public Short decode(byte[] value) { - ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); - buffer.put(value); - buffer.flip(); - return buffer.getShort(); + public Short decode(PacketReader packetReader, byte[] value) { + return packetReader.readShort(); } } diff --git a/src/main/java/net/minestom/server/data/type/StringData.java b/src/main/java/net/minestom/server/data/type/StringData.java index 18e345193..405ed7f51 100644 --- a/src/main/java/net/minestom/server/data/type/StringData.java +++ b/src/main/java/net/minestom/server/data/type/StringData.java @@ -1,16 +1,18 @@ package net.minestom.server.data.type; import net.minestom.server.data.DataType; +import net.minestom.server.network.packet.PacketReader; +import net.minestom.server.network.packet.PacketWriter; public class StringData extends DataType { @Override - public byte[] encode(String value) { - return value.getBytes(); + public void encode(PacketWriter packetWriter, String value) { + packetWriter.writeSizedString(value); } @Override - public String decode(byte[] value) { - return new String(value); + public String decode(PacketReader packetReader, byte[] value) { + return packetReader.readSizedString(); } } diff --git a/src/main/java/net/minestom/server/data/type/array/ItemStackArrayData.java b/src/main/java/net/minestom/server/data/type/array/ItemStackArrayData.java new file mode 100644 index 000000000..9d4b61e34 --- /dev/null +++ b/src/main/java/net/minestom/server/data/type/array/ItemStackArrayData.java @@ -0,0 +1,30 @@ +package net.minestom.server.data.type.array; + +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 java.util.ArrayList; +import java.util.List; + +public class ItemStackArrayData extends DataType { + @Override + public void encode(PacketWriter packetWriter, ItemStack[] value) { + packetWriter.writeVarInt(value.length); + for (ItemStack itemStack : value) { + packetWriter.writeItemStack(itemStack); + } + } + + @Override + public ItemStack[] decode(PacketReader packetReader, byte[] value) { + List items = new ArrayList<>(); + int size = packetReader.readVarInt(); + for (int i = 0; i < size; i++) { + items.add(packetReader.readSlot()); + } + ItemStack[] array = items.toArray(new ItemStack[items.size()]); + return array; + } +} diff --git a/src/main/java/net/minestom/server/io/DataReader.java b/src/main/java/net/minestom/server/io/DataReader.java index c0ff92e4f..c5eb6516e 100644 --- a/src/main/java/net/minestom/server/io/DataReader.java +++ b/src/main/java/net/minestom/server/io/DataReader.java @@ -1,8 +1,11 @@ package net.minestom.server.io; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import net.minestom.server.MinecraftServer; import net.minestom.server.data.DataManager; import net.minestom.server.data.SerializableData; +import net.minestom.server.network.packet.PacketReader; import net.minestom.server.utils.CompressionUtils; import java.io.ByteArrayInputStream; @@ -45,7 +48,9 @@ public class DataReader { Class type = Class.forName(new String(typeCache)); String name = new String(nameCache); - Object value = DATA_MANAGER.getDataType(type).decode(valueCache); + ByteBuf buffer = Unpooled.wrappedBuffer(valueCache); + PacketReader packetReader = new PacketReader(buffer); + Object value = DATA_MANAGER.getDataType(type).decode(packetReader, valueCache); data.set(name, value, type); } 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 76273117e..2c67fe377 100644 --- a/src/main/java/net/minestom/server/network/packet/PacketReader.java +++ b/src/main/java/net/minestom/server/network/packet/PacketReader.java @@ -30,6 +30,10 @@ public class PacketReader { return buffer.readShort(); } + public char readChar() { + return buffer.readChar(); + } + public int readUnsignedShort() { return buffer.readUnsignedShort(); } diff --git a/src/main/java/net/minestom/server/network/packet/PacketWriter.java b/src/main/java/net/minestom/server/network/packet/PacketWriter.java index e8204a686..6b2ffeeb6 100644 --- a/src/main/java/net/minestom/server/network/packet/PacketWriter.java +++ b/src/main/java/net/minestom/server/network/packet/PacketWriter.java @@ -38,6 +38,14 @@ public class PacketWriter { } } + public void writeChar(char s) { + try { + data.writeChar(s); + } catch (IOException e) { + e.printStackTrace(); + } + } + public void writeShort(short s) { try { data.writeShort(s);