use of org.spongepowered.api.item.inventory.type.OrderedInventory in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method captureTransaction.
/**
* Captures a transaction
*
* @param captureIn the {@link IMixinInventory} to capture the transaction in
* @param inv the Inventory
* @param index the affected SlotIndex
* @param originalStack the original Stack
*/
public static void captureTransaction(IMixinInventory captureIn, Inventory inv, int index, ItemStack originalStack) {
// TODO make sure we never got null
if (captureIn == null || inv == null) {
return;
}
Inventory ordered = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(OrderedInventory.class));
if (ordered instanceof OrderedInventory) {
Optional<org.spongepowered.api.item.inventory.Slot> slot = ((OrderedInventory) ordered).getSlot(SlotIndex.of(index));
if (slot.isPresent()) {
SlotTransaction trans = new SlotTransaction(slot.get(), ItemStackUtil.snapshotOf(originalStack), ItemStackUtil.snapshotOf(slot.get().peek().orElse(org.spongepowered.api.item.inventory.ItemStack.empty())));
captureIn.getCapturedTransactions().add(trans);
}
}
}
use of org.spongepowered.api.item.inventory.type.OrderedInventory in project LanternServer by LanternPowered.
the class ContainerTileEntityStore method deserialize.
@Override
public void deserialize(T object, DataView dataView) {
final List<DataView> itemViews = dataView.getViewList(ITEMS).orElse(null);
if (itemViews != null) {
dataView.remove(ITEMS);
final OrderedInventory inventory = (OrderedInventory) object.getInventory();
final ObjectSerializer<LanternItemStack> itemStackSerializer = ObjectSerializerRegistry.get().get(LanternItemStack.class).get();
for (DataView itemView : itemViews) {
final int slot = itemView.getByte(SLOT).get() & 0xff;
final LanternItemStack itemStack = itemStackSerializer.deserialize(itemView);
inventory.set(new SlotIndex(slot), itemStack);
}
}
super.deserialize(object, dataView);
}
use of org.spongepowered.api.item.inventory.type.OrderedInventory in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method captureTransaction.
/**
* Captures a transaction
*
* @param captureIn the {@link IMixinInventory} to capture the transaction in
* @param inv the Inventory
* @param index the affected SlotIndex
* @param transaction the transaction to execute
* @return the result if the transaction
*/
public static ItemStack captureTransaction(IMixinInventory captureIn, Inventory inv, int index, Supplier<ItemStack> transaction) {
// TODO make sure we never got null
if (captureIn == null || inv == null) {
return transaction.get();
}
Inventory ordered = inv.query(QueryOperationTypes.INVENTORY_TYPE.of(OrderedInventory.class));
if (ordered instanceof OrderedInventory) {
Optional<org.spongepowered.api.item.inventory.Slot> slot = ((OrderedInventory) ordered).getSlot(SlotIndex.of(index));
if (slot.isPresent()) {
ItemStackSnapshot original = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
ItemStack remaining = transaction.get();
if (remaining.isEmpty()) {
ItemStackSnapshot replacement = slot.get().peek().map(ItemStackUtil::snapshotOf).orElse(ItemStackSnapshot.NONE);
captureIn.getCapturedTransactions().add(new SlotTransaction(slot.get(), original, replacement));
}
return remaining;
}
}
// else inventory was missing the slot for some reason
return transaction.get();
}
Aggregations