Search in sources :

Example 1 with VoxelInventoryComponent

use of io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent in project chunkstories-api by Hugobros3.

the class InventoryTranslator method obtainInventoryHandle.

public static Inventory obtainInventoryHandle(DataInputStream in, PacketReceptionContext context) throws IOException {
    byte holderType = in.readByte();
    if (holderType == 0x01) {
        long uuid = in.readLong();
        short componentId = in.readShort();
        Entity entity = context.getWorld().getEntityByUUID(uuid);
        EntityComponent cpn = entity.getComponents().getComponentById(componentId);
        if (cpn != null && cpn instanceof EntityComponentInventory) {
            return ((EntityComponentInventory) cpn).getInventory();
        }
    } else if (holderType == 0x03) {
        int x = in.readInt();
        int y = in.readInt();
        int z = in.readInt();
        String componentName = in.readUTF();
        try {
            ChunkCell voxelContext = context.getWorld().peek(x, y, z);
            VoxelComponent com = voxelContext.components().get(componentName);
            if (com != null && com instanceof VoxelInventoryComponent) {
                VoxelInventoryComponent comp = (VoxelInventoryComponent) com;
                return comp.getInventory();
            }
        } catch (WorldException e) {
        // TODO log as warning
        // Ignore and return null
        }
    }
    return null;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) VoxelComponent(io.xol.chunkstories.api.voxel.components.VoxelComponent) ChunkCell(io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell) EntityComponentInventory(io.xol.chunkstories.api.entity.components.EntityComponentInventory) EntityComponent(io.xol.chunkstories.api.entity.components.EntityComponent) VoxelInventoryComponent(io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)

Example 2 with VoxelInventoryComponent

use of io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent in project chunkstories-api by Hugobros3.

the class InventoryTranslator method writeInventoryHandle.

public static void writeInventoryHandle(DataOutputStream out, Inventory inventory) throws IOException {
    /*if(inventory instanceof InventoryLocalCreativeMenu)
			out.writeByte(0x02); else */
    if (inventory == null || inventory.getHolder() == null)
        out.writeByte(0x00);
    else if (inventory instanceof EntityComponentInventory.EntityInventory) {
        EntityComponentInventory.EntityInventory entityInventory = (EntityComponentInventory.EntityInventory) inventory;
        out.writeByte(0x01);
        out.writeLong(((Entity) inventory.getHolder()).getUUID());
        out.writeShort(entityInventory.asComponent().getEntityComponentId());
    } else if (inventory.getHolder() instanceof VoxelInventoryComponent) {
        VoxelInventoryComponent component = (VoxelInventoryComponent) inventory.getHolder();
        out.writeByte(0x03);
        out.writeInt(component.holder().getX());
        out.writeInt(component.holder().getY());
        out.writeInt(component.holder().getZ());
        out.writeUTF(component.holder().name(component));
    } else
        out.writeByte(0x00);
// throw new RuntimeException("Untranslatable and Unknown Inventory : "+inventory+", can't describe it in outgoing packets");
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityComponentInventory(io.xol.chunkstories.api.entity.components.EntityComponentInventory) VoxelInventoryComponent(io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)

Example 3 with VoxelInventoryComponent

use of io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent in project chunkstories-core by Hugobros3.

the class VoxelChest method whenPlaced.

@Override
public void whenPlaced(FreshChunkCell cell) {
    // Create a new component and insert it into the chunk
    VoxelInventoryComponent component = new VoxelInventoryComponent(cell.components(), 10, 6);
    cell.registerComponent("chestInventory", component);
}
Also used : VoxelInventoryComponent(io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)

Example 4 with VoxelInventoryComponent

use of io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent in project chunkstories-core by Hugobros3.

the class VoxelChest method getInventory.

private Inventory getInventory(ChunkCell context) {
    VoxelComponent comp = context.components().get("chestInventory");
    VoxelInventoryComponent component = (VoxelInventoryComponent) comp;
    return component.getInventory();
}
Also used : VoxelComponent(io.xol.chunkstories.api.voxel.components.VoxelComponent) VoxelInventoryComponent(io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)

Aggregations

VoxelInventoryComponent (io.xol.chunkstories.api.voxel.components.VoxelInventoryComponent)4 Entity (io.xol.chunkstories.api.entity.Entity)2 EntityComponentInventory (io.xol.chunkstories.api.entity.components.EntityComponentInventory)2 VoxelComponent (io.xol.chunkstories.api.voxel.components.VoxelComponent)2 EntityComponent (io.xol.chunkstories.api.entity.components.EntityComponent)1 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)1 ChunkCell (io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell)1