diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index 28d08d49b..2b14bd1a8 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -143,7 +143,6 @@ public class PlayerInit { }); player.setEventCallback(PlayerLoginEvent.class, event -> { - System.out.println("event: " + instanceContainer.hashCode()); event.setSpawningInstance(instanceContainer); }); diff --git a/src/main/java/fr/themode/minestom/data/DataContainer.java b/src/main/java/fr/themode/minestom/data/DataContainer.java index cbf292d99..793de0f87 100644 --- a/src/main/java/fr/themode/minestom/data/DataContainer.java +++ b/src/main/java/fr/themode/minestom/data/DataContainer.java @@ -20,10 +20,8 @@ public interface DataContainer { default void saveData(File file, Runnable callback) { IOManager.submit(() -> { Data data = getData(); - if (data == null) { - // TODO error trying to save null data - return; - } + if (data == null) + throw new NullPointerException("You cannot save null data!"); try (FileOutputStream fos = new FileOutputStream(file)) { byte[] serializedData = data.getSerializedData(); diff --git a/src/main/java/fr/themode/minestom/data/DataManager.java b/src/main/java/fr/themode/minestom/data/DataManager.java index 21a46581d..6f979575b 100644 --- a/src/main/java/fr/themode/minestom/data/DataManager.java +++ b/src/main/java/fr/themode/minestom/data/DataManager.java @@ -22,6 +22,8 @@ public class DataManager { registerType(Double.class, new DoubleData()); registerType(String.class, new StringData()); + + registerType(Data.class, new DataData()); } public void registerType(Class clazz, DataType dataType) { diff --git a/src/main/java/fr/themode/minestom/data/type/DataData.java b/src/main/java/fr/themode/minestom/data/type/DataData.java new file mode 100644 index 000000000..977cc982e --- /dev/null +++ b/src/main/java/fr/themode/minestom/data/type/DataData.java @@ -0,0 +1,25 @@ +package fr.themode.minestom.data.type; + +import fr.themode.minestom.data.Data; +import fr.themode.minestom.data.DataType; +import fr.themode.minestom.io.DataReader; + +import java.io.IOException; + +// Pretty weird name huh? +public class DataData extends DataType { + @Override + public byte[] encode(Data value) { + try { + return value.getSerializedData(); + } catch (IOException e) { + e.printStackTrace(); + throw new IllegalArgumentException("error while writing the data"); + } + } + + @Override + public Data decode(byte[] value) { + return DataReader.readData(value, false); + } +} diff --git a/src/main/java/fr/themode/minestom/instance/Instance.java b/src/main/java/fr/themode/minestom/instance/Instance.java index 2e7c316a4..002eaece4 100644 --- a/src/main/java/fr/themode/minestom/instance/Instance.java +++ b/src/main/java/fr/themode/minestom/instance/Instance.java @@ -10,6 +10,7 @@ import fr.themode.minestom.instance.block.Block; import fr.themode.minestom.instance.block.BlockManager; import fr.themode.minestom.instance.block.CustomBlock; import fr.themode.minestom.net.PacketWriterUtils; +import fr.themode.minestom.net.packet.server.play.BlockActionPacket; import fr.themode.minestom.net.packet.server.play.ChunkDataPacket; import fr.themode.minestom.utils.BlockPosition; import fr.themode.minestom.utils.ChunkUtils; @@ -176,6 +177,19 @@ public abstract class Instance implements BlockModifier, DataContainer { return getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ()); } + public void sendBlockAction(BlockPosition blockPosition, byte actionId, byte actionParam) { + short blockId = getBlockId(blockPosition); + + BlockActionPacket blockActionPacket = new BlockActionPacket(); + blockActionPacket.blockPosition = blockPosition; + blockActionPacket.actionId = actionId; + blockActionPacket.actionParam = actionParam; + blockActionPacket.blockId = blockId; // FIXME: block id and not block state? + + Chunk chunk = getChunkAt(blockPosition); + chunk.sendPacketToViewers(blockActionPacket); + } + public Data getBlockData(int x, int y, int z) { Chunk chunk = getChunkAt(x, z); return chunk.getData((byte) (x % 16), (byte) y, (byte) (z % 16)); diff --git a/src/main/java/fr/themode/minestom/net/packet/PacketReader.java b/src/main/java/fr/themode/minestom/net/packet/PacketReader.java index 2c1bcd106..aec6a9438 100644 --- a/src/main/java/fr/themode/minestom/net/packet/PacketReader.java +++ b/src/main/java/fr/themode/minestom/net/packet/PacketReader.java @@ -57,6 +57,7 @@ public class PacketReader { ByteBuf buf = buffer.readBytes(length); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); + buf.release(); return new String(bytes); } @@ -65,6 +66,7 @@ public class PacketReader { ByteBuf buf = buffer.readBytes(length); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); + buf.release(); return new String(bytes); } @@ -72,6 +74,7 @@ public class PacketReader { ByteBuf buf = buffer.readBytes(buffer.readableBytes()); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); + buf.release(); return bytes; }