Added InventoryData

This commit is contained in:
Felix Cravic 2020-04-28 18:16:00 +02:00
parent 6d9ac0b35d
commit a4922e60fa
2 changed files with 41 additions and 0 deletions

View File

@ -3,6 +3,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.inventory.Inventory;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.PrimitiveConversion;
@ -29,6 +30,8 @@ public class DataManager {
registerType(ItemStack.class, new ItemStackData());
registerType(ItemStack[].class, new ItemStackArrayData());
registerType(Inventory.class, new InventoryData());
}
public <T> void registerType(Class<T> clazz, DataType<T> dataType) {

View File

@ -0,0 +1,38 @@
package net.minestom.server.data.type;
import net.minestom.server.data.DataType;
import net.minestom.server.inventory.Inventory;
import net.minestom.server.inventory.InventoryType;
import net.minestom.server.network.packet.PacketReader;
import net.minestom.server.network.packet.PacketWriter;
public class InventoryData extends DataType<Inventory> {
@Override
public void encode(PacketWriter packetWriter, Inventory value) {
InventoryType inventoryType = value.getInventoryType();
int size = inventoryType.getAdditionalSlot();
packetWriter.writeSizedString(value.getTitle());
packetWriter.writeSizedString(inventoryType.name());
for (int i = 0; i < size; i++) {
packetWriter.writeItemStack(value.getItemStack(i));
}
}
@Override
public Inventory decode(PacketReader packetReader) {
String title = packetReader.readSizedString();
InventoryType inventoryType = InventoryType.valueOf(packetReader.readSizedString());
int size = inventoryType.getAdditionalSlot();
Inventory inventory = new Inventory(inventoryType, title);
for (int i = 0; i < size; i++) {
inventory.setItemStack(i, packetReader.readSlot());
}
return inventory;
}
}