Changed DataType class and its implementation

This commit is contained in:
Felix Cravic 2020-04-28 17:11:12 +02:00
parent 03f4e19db3
commit 60cc562435
20 changed files with 130 additions and 107 deletions

View File

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

View File

@ -11,9 +11,13 @@ public class Data {
protected ConcurrentHashMap<String, Object> data = new ConcurrentHashMap();
public <T> void set(String key, T value, Class<T> 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);
}

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<SerializableData> {
@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<SerializableData> {
}
@Override
public SerializableData decode(byte[] value) {
public SerializableData decode(PacketReader packetReader, byte[] value) {
return DataReader.readData(value, false);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -30,6 +30,10 @@ public class PacketReader {
return buffer.readShort();
}
public char readChar() {
return buffer.readChar();
}
public int readUnsignedShort() {
return buffer.readUnsignedShort();
}

View File

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