Search in sources :

Example 11 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project modules-extra by CubeEngine.

the class Observe method transactions.

/**
 * Observes a SlotTransaction
 *
 * @param transactions the transaction to observe
 * @return the observed data
 */
public static List<Map<String, Object>> transactions(List<SlotTransaction> transactions) {
    List<Map<String, Object>> list = new ArrayList<>();
    for (SlotTransaction transaction : transactions) {
        Map<String, Object> data = new HashMap<>();
        ItemStackSnapshot originalStack = transaction.getOriginal();
        ItemStackSnapshot finalStack = transaction.getFinal();
        data.put(ChangeInventoryReport.ORIGINAL, toRawData(originalStack.toContainer()));
        data.put(ChangeInventoryReport.REPLACEMENT, toRawData(finalStack.toContainer()));
        data.put(ChangeInventoryReport.SLOT_INDEX, transaction.getSlot().getInventoryProperty(SlotIndex.class).map(SlotIndex::getValue).orElse(-1));
        list.add(data);
    }
    return list;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction)

Example 12 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project LanternServer by LanternPowered.

the class UserStore method serializeSlot.

private static void serializeSlot(Inventory parent, Slot slot, int indexOffset, ObjectSerializer<LanternItemStack> itemStackSerializer, List<DataView> views) {
    // Key doesn't matter
    final SlotIndex index = parent.getProperty(slot, SlotIndex.class, "index").get();
    serializeSlot(index.getValue() + indexOffset, slot, itemStackSerializer, views);
}
Also used : SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex)

Example 13 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project LanternServer by LanternPowered.

the class UserStore method deserializePlayerInventory.

private static void deserializePlayerInventory(AbstractUserInventory<?> inventory, List<DataView> itemViews) {
    final LanternMainPlayerInventory mainInventory = inventory.getMain();
    final LanternPlayerEquipmentInventory equipmentInventory = inventory.getEquipment();
    final AbstractSlot offHandSlot = inventory.getOffhand();
    for (DataView itemView : itemViews) {
        final int slot = itemView.getByte(SLOT).get() & 0xff;
        final LanternItemStack itemStack = ItemStackStore.INSTANCE.deserialize(itemView);
        if (slot >= 0 && slot < mainInventory.capacity()) {
            mainInventory.set(new SlotIndex(slot), itemStack);
        } else if (slot >= 100 && slot - 100 < equipmentInventory.capacity()) {
            equipmentInventory.set(new SlotIndex(slot - 100), itemStack);
        } else if (slot == 150) {
            offHandSlot.set(itemStack);
        }
    }
}
Also used : DataView(org.spongepowered.api.data.DataView) AbstractSlot(org.lanternpowered.server.inventory.AbstractSlot) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternPlayerEquipmentInventory(org.lanternpowered.server.inventory.vanilla.LanternPlayerEquipmentInventory) LanternMainPlayerInventory(org.lanternpowered.server.inventory.vanilla.LanternMainPlayerInventory) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 14 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project LanternServer by LanternPowered.

the class UserStore method deserializeEnderChest.

private static void deserializeEnderChest(GridInventory enderChestInventory, List<DataView> itemViews) {
    for (DataView itemView : itemViews) {
        final int slot = itemView.getByte(SLOT).get() & 0xff;
        final LanternItemStack itemStack = ItemStackStore.INSTANCE.deserialize(itemView);
        enderChestInventory.set(new SlotIndex(slot), itemStack);
    }
}
Also used : DataView(org.spongepowered.api.data.DataView) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack)

Example 15 with SlotIndex

use of org.spongepowered.api.item.inventory.property.SlotIndex in project LanternServer by LanternPowered.

the class InventorySnapshot method ofInventory.

/**
 * Creates a {@link InventorySnapshot} for the specified {@link Inventory}.
 *
 * @param inventory The inventory
 * @return The snapshot
 */
public static InventorySnapshot ofInventory(Inventory inventory) {
    final Iterator<Slot> it = inventory.<Slot>slots().iterator();
    final Int2ObjectMap<ItemStackSnapshot> itemStackSnapshots = new Int2ObjectOpenHashMap<>();
    while (it.hasNext()) {
        final Slot slot = it.next();
        slot.peek().map(ItemStack::createSnapshot).ifPresent(itemStackSnapshot -> {
            // noinspection ConstantConditions
            final SlotIndex index = inventory.getProperty(slot, SlotIndex.class, null).get();
            itemStackSnapshots.put(index.getValue(), itemStackSnapshot);
        });
    }
    return new InventorySnapshot(itemStackSnapshots);
}
Also used : Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) Slot(org.spongepowered.api.item.inventory.Slot) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot)

Aggregations

SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)15 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4 LanternItemStack (org.lanternpowered.server.inventory.LanternItemStack)3 DataView (org.spongepowered.api.data.DataView)3 IInventory (net.minecraft.inventory.IInventory)2 Inventory (org.spongepowered.api.item.inventory.Inventory)2 Slot (org.spongepowered.api.item.inventory.Slot)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 IMerchant (net.minecraft.entity.IMerchant)1 AbstractHorse (net.minecraft.entity.passive.AbstractHorse)1 Container (net.minecraft.inventory.Container)1 ContainerBeacon (net.minecraft.inventory.ContainerBeacon)1 ContainerBrewingStand (net.minecraft.inventory.ContainerBrewingStand)1 ContainerChest (net.minecraft.inventory.ContainerChest)1 ContainerDispenser (net.minecraft.inventory.ContainerDispenser)1 ContainerEnchantment (net.minecraft.inventory.ContainerEnchantment)1