mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 09:01:54 +01:00
Optimization + a data data type (huh)
This commit is contained in:
parent
f4aa328849
commit
6da352f192
@ -143,7 +143,6 @@ public class PlayerInit {
|
||||
});
|
||||
|
||||
player.setEventCallback(PlayerLoginEvent.class, event -> {
|
||||
System.out.println("event: " + instanceContainer.hashCode());
|
||||
event.setSpawningInstance(instanceContainer);
|
||||
});
|
||||
|
||||
|
@ -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();
|
||||
|
@ -22,6 +22,8 @@ public class DataManager {
|
||||
registerType(Double.class, new DoubleData());
|
||||
|
||||
registerType(String.class, new StringData());
|
||||
|
||||
registerType(Data.class, new DataData());
|
||||
}
|
||||
|
||||
public <T> void registerType(Class<T> clazz, DataType<T> dataType) {
|
||||
|
25
src/main/java/fr/themode/minestom/data/type/DataData.java
Normal file
25
src/main/java/fr/themode/minestom/data/type/DataData.java
Normal file
@ -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<Data> {
|
||||
@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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user