mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-26 11:07:53 +01:00
Added InventoryData
This commit is contained in:
parent
6d9ac0b35d
commit
a4922e60fa
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user