mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-27 03:27:56 +01:00
Reworked data management
This commit is contained in:
parent
08015586a0
commit
b128074c75
@ -62,7 +62,7 @@ public interface DataContainer {
|
||||
return;
|
||||
}
|
||||
|
||||
SerializableData data = DataReader.readData(array, true);
|
||||
SerializableData data = DataReader.readCompressedData(array);
|
||||
|
||||
setData(data);
|
||||
if (callback != null)
|
||||
|
@ -7,6 +7,6 @@ public abstract class DataType<T> {
|
||||
|
||||
public abstract void encode(PacketWriter packetWriter, T value);
|
||||
|
||||
public abstract T decode(PacketReader packetReader, byte[] value);
|
||||
public abstract T decode(PacketReader packetReader);
|
||||
|
||||
}
|
@ -11,7 +11,7 @@ public class BooleanData extends DataType<Boolean> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean decode(PacketReader packetReader, byte[] value) {
|
||||
public Boolean decode(PacketReader packetReader) {
|
||||
return packetReader.readBoolean();
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class ByteData extends DataType<Byte> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte decode(PacketReader packetReader, byte[] value) {
|
||||
public Byte decode(PacketReader packetReader) {
|
||||
return packetReader.readByte();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class CharacterData extends DataType<Character> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character decode(PacketReader packetReader, byte[] value) {
|
||||
public Character decode(PacketReader packetReader) {
|
||||
return packetReader.readChar();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class DoubleData extends DataType<Double> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double decode(PacketReader packetReader, byte[] value) {
|
||||
public Double decode(PacketReader packetReader) {
|
||||
return packetReader.readDouble();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class FloatData extends DataType<Float> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float decode(PacketReader packetReader, byte[] value) {
|
||||
public Float decode(PacketReader packetReader) {
|
||||
return packetReader.readFloat();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class IntegerData extends DataType<Integer> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer decode(PacketReader packetReader, byte[] value) {
|
||||
public Integer decode(PacketReader packetReader) {
|
||||
return packetReader.readVarInt();
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ public class ItemStackData extends DataType<ItemStack> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decode(PacketReader packetReader, byte[] value) {
|
||||
public ItemStack decode(PacketReader packetReader) {
|
||||
return packetReader.readSlot();
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class LongData extends DataType<Long> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long decode(PacketReader packetReader, byte[] value) {
|
||||
public Long decode(PacketReader packetReader) {
|
||||
return packetReader.readLong();
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ public class SerializableDataData extends DataType<SerializableData> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SerializableData decode(PacketReader packetReader, byte[] value) {
|
||||
return DataReader.readData(value, false);
|
||||
public SerializableData decode(PacketReader packetReader) {
|
||||
return DataReader.readData(packetReader.getBuffer());
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class ShortData extends DataType<Short> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short decode(PacketReader packetReader, byte[] value) {
|
||||
public Short decode(PacketReader packetReader) {
|
||||
return packetReader.readShort();
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public class StringData extends DataType<String> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String decode(PacketReader packetReader, byte[] value) {
|
||||
public String decode(PacketReader packetReader) {
|
||||
return packetReader.readSizedString();
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ public class ItemStackArrayData extends DataType<ItemStack[]> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack[] decode(PacketReader packetReader, byte[] value) {
|
||||
public ItemStack[] decode(PacketReader packetReader) {
|
||||
List<ItemStack> items = new ArrayList<>();
|
||||
int size = packetReader.readVarInt();
|
||||
for (int i = 0; i < size; i++) {
|
||||
|
@ -288,13 +288,12 @@ public class Chunk implements Viewable {
|
||||
dos.writeBoolean(isCustomBlock); // Determine the type of the ID
|
||||
dos.writeShort(id);
|
||||
|
||||
if (data instanceof SerializableData) {
|
||||
dos.writeBoolean(hasData);
|
||||
if (hasData) {
|
||||
byte[] d = ((SerializableData) data).getSerializedData();
|
||||
dos.writeInt(d.length);
|
||||
dos.write(d);
|
||||
}
|
||||
hasData = (data != null && (data instanceof SerializableData)) && hasData;
|
||||
dos.writeBoolean(hasData);
|
||||
if (hasData) {
|
||||
byte[] d = ((SerializableData) data).getSerializedData();
|
||||
dos.writeInt(d.length);
|
||||
dos.write(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,10 @@ public class InstanceManager {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public InstanceContainer createInstanceContainer(File file) {
|
||||
return createInstanceContainer(Dimension.OVERWORLD, file);
|
||||
}
|
||||
|
||||
public InstanceContainer createInstanceContainer(Dimension dimension) {
|
||||
return createInstanceContainer(dimension, null);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.minestom.server.io;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minestom.server.data.Data;
|
||||
import net.minestom.server.instance.Biome;
|
||||
import net.minestom.server.instance.Chunk;
|
||||
@ -41,7 +42,7 @@ public class ChunkReader {
|
||||
if (hasData) {
|
||||
int dataLength = stream.readInt();
|
||||
byte[] dataArray = stream.readNBytes(dataLength);
|
||||
data = DataReader.readData(dataArray, false);
|
||||
data = DataReader.readData(Unpooled.wrappedBuffer(dataArray));
|
||||
}
|
||||
|
||||
if (isCustomBlock) {
|
||||
|
@ -8,23 +8,22 @@ import net.minestom.server.data.SerializableData;
|
||||
import net.minestom.server.network.packet.PacketReader;
|
||||
import net.minestom.server.utils.CompressionUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DataReader {
|
||||
|
||||
private static final DataManager DATA_MANAGER = MinecraftServer.getDataManager();
|
||||
|
||||
public static SerializableData readData(byte[] b, boolean shouldDecompress) {
|
||||
b = shouldDecompress ? CompressionUtils.getDecompressedData(b) : b;
|
||||
public static SerializableData readCompressedData(byte[] b) {
|
||||
b = CompressionUtils.getDecompressedData(b);
|
||||
|
||||
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(b));
|
||||
return readData(Unpooled.wrappedBuffer(b));
|
||||
}
|
||||
|
||||
// Only uncompressed
|
||||
public static SerializableData readData(ByteBuf buffer) {
|
||||
SerializableData data = new SerializableData();
|
||||
try {
|
||||
while (true) {
|
||||
short typeLength = stream.readShort();
|
||||
short typeLength = buffer.readShort();
|
||||
|
||||
if (typeLength == 0xff) {
|
||||
// End of data
|
||||
@ -33,29 +32,26 @@ public class DataReader {
|
||||
|
||||
byte[] typeCache = new byte[typeLength];
|
||||
for (int i = 0; i < typeLength; i++) {
|
||||
typeCache[i] = stream.readByte();
|
||||
typeCache[i] = buffer.readByte();
|
||||
}
|
||||
|
||||
short nameLength = stream.readShort();
|
||||
String className = new String(typeCache);
|
||||
Class type = Class.forName(className);
|
||||
|
||||
short nameLength = buffer.readShort();
|
||||
byte[] nameCache = new byte[nameLength];
|
||||
for (int i = 0; i < nameLength; i++) {
|
||||
nameCache[i] = stream.readByte();
|
||||
nameCache[i] = buffer.readByte();
|
||||
}
|
||||
|
||||
int valueLength = stream.readInt();
|
||||
byte[] valueCache = stream.readNBytes(valueLength);
|
||||
|
||||
Class type = Class.forName(new String(typeCache));
|
||||
ByteBuf valueCache = buffer.readBytes(buffer.readInt());
|
||||
|
||||
String name = new String(nameCache);
|
||||
ByteBuf buffer = Unpooled.wrappedBuffer(valueCache);
|
||||
PacketReader packetReader = new PacketReader(buffer);
|
||||
Object value = DATA_MANAGER.getDataType(type).decode(packetReader, valueCache);
|
||||
PacketReader packetReader = new PacketReader(valueCache);
|
||||
Object value = DATA_MANAGER.getDataType(type).decode(packetReader);
|
||||
|
||||
data.set(name, value, type);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user