use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.
the class SlotAdapter method set.
@Override
public InventoryTransactionResult set(final ItemStack stack) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
final net.minecraft.world.item.ItemStack old = this.slot.getStack(this.inventoryAdapter$getFabric());
ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
if (stack.isEmpty()) {
// NONE item will clear the slot
this.clear();
SlotTransaction trans = new SlotTransaction(this, oldSnap, ItemStackSnapshot.empty());
return result.transaction(trans).build();
}
int remaining = stack.quantity();
final int push = Math.min(remaining, this.slot.getMaxStackSize(this.inventoryAdapter$getFabric()));
net.minecraft.world.item.ItemStack newStack = ItemStackUtil.cloneDefensiveNative(nativeStack, push);
if (this.slot.setStack(this.inventoryAdapter$getFabric(), newStack)) {
result.transaction(new SlotTransaction(this, oldSnap, ItemStackUtil.snapshotOf(newStack)));
remaining -= push;
}
if (remaining > 0) {
result.reject(ItemStackUtil.cloneDefensive(nativeStack, remaining));
}
return result.build();
}
use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.
the class SlotAdapter method offer.
@Override
public InventoryTransactionResult offer(final ItemStack... stacks) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
for (ItemStack stack : stacks) {
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
final int maxStackSize = this.slot.getMaxStackSize(this.inventoryAdapter$getFabric());
int remaining = stack.quantity();
final net.minecraft.world.item.ItemStack old = this.slot.getStack(this.inventoryAdapter$getFabric());
ItemStackSnapshot oldStack = ItemStackUtil.snapshotOf(old);
ItemStackSnapshot newStack = oldStack;
int push = Math.min(remaining, maxStackSize);
if (old.isEmpty() && this.slot.setStack(this.inventoryAdapter$getFabric(), ItemStackUtil.cloneDefensiveNative(nativeStack, push))) {
remaining -= push;
newStack = ItemStackUtil.snapshotOf(stack);
} else if (!old.isEmpty() && ItemStackUtil.compareIgnoreQuantity(old, stack)) {
this.inventoryAdapter$getFabric().fabric$markDirty();
// max() accounts for oversized stacks
push = Math.max(Math.min(maxStackSize - old.getCount(), remaining), 0);
old.setCount(old.getCount() + push);
remaining -= push;
newStack = ItemStackUtil.snapshotOf(old);
}
result.transaction(new SlotTransaction(this, oldStack, newStack));
if (remaining == stack.quantity()) {
// No items were consumed
result.reject(ItemStackUtil.cloneDefensive(nativeStack));
result.type(InventoryTransactionResult.Type.FAILURE);
}
}
return result.build();
}
use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.
the class AdapterLogic method insertStack.
private static InventoryTransactionResult insertStack(Fabric fabric, Lens lens, ItemStack stack) {
InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(Type.SUCCESS);
net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
if (stack.isEmpty() && lens.slotCount() == 1) {
final net.minecraft.world.item.ItemStack old = lens.getStack(fabric, 0);
final ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
lens.setStack(fabric, 0, nativeStack);
final SlotTransaction trans = new SlotTransaction((Slot) lens.getAdapter(fabric, null), oldSnap, ItemStackSnapshot.empty());
result.transaction(trans);
return result.build();
}
int maxStackSize = Math.min(lens.getMaxStackSize(fabric), nativeStack.getMaxStackSize());
int remaining = stack.quantity();
for (int ord = 0; ord < lens.slotCount() && remaining > 0; ord++) {
final net.minecraft.world.item.ItemStack old = lens.getStack(fabric, ord);
final ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
final int push = Math.min(remaining, maxStackSize);
final net.minecraft.world.item.ItemStack newStack = ItemStackUtil.cloneDefensiveNative(nativeStack, push);
if (lens.setStack(fabric, ord, newStack)) {
remaining -= push;
final Slot slot = lens.getSlotLens(fabric, ord).getAdapter(fabric, null);
final SlotTransaction trans = new SlotTransaction(slot, oldSnap, ItemStackUtil.snapshotOf(lens.getStack(fabric, ord)));
result.transaction(trans);
}
}
if (remaining > 0) {
result.reject(ItemStackUtil.cloneDefensive(nativeStack, remaining));
}
fabric.fabric$markDirty();
return result.build();
}
use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.
the class AdapterLogic method appendSequential.
public static InventoryTransactionResult appendSequential(Fabric fabric, @Nullable Lens lens, ItemStack stack) {
if (lens == null) {
return InventoryTransactionResult.builder().type(Type.FAILURE).reject(ItemStackUtil.cloneDefensive(stack)).build();
}
InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(Type.SUCCESS);
net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
int maxStackSize = Math.min(lens.getMaxStackSize(fabric), nativeStack.getMaxStackSize());
int remaining = stack.quantity();
for (int ord = 0; ord < lens.slotCount() && remaining > 0; ord++) {
net.minecraft.world.item.ItemStack old = lens.getStack(fabric, ord);
int push = Math.min(remaining, maxStackSize);
if (old.isEmpty() && lens.setStack(fabric, ord, ItemStackUtil.cloneDefensiveNative(nativeStack, push))) {
remaining -= push;
Slot slot = ((SlotAdapter) lens.getSlotLens(fabric, ord).getAdapter(fabric, null));
result.transaction(new SlotTransaction(slot, ItemStackUtil.snapshotOf(old), ItemStackUtil.snapshotOf(lens.getStack(fabric, ord))));
} else if (!old.isEmpty() && ItemStackUtil.compareIgnoreQuantity(old, stack)) {
ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
// max() accounts for oversized stacks
push = Math.max(Math.min(maxStackSize - old.getCount(), remaining), 0);
old.setCount(old.getCount() + push);
lens.setStack(fabric, ord, old);
remaining -= push;
Slot slot = ((SlotAdapter) lens.getSlotLens(fabric, ord).getAdapter(fabric, null));
result.transaction(new SlotTransaction(slot, oldSnap, ItemStackUtil.snapshotOf(lens.getStack(fabric, ord))));
}
}
if (remaining == stack.quantity()) {
// No items were consumed
result.type(Type.FAILURE).reject(ItemStackUtil.cloneDefensive(nativeStack));
} else {
stack.setQuantity(remaining);
fabric.fabric$markDirty();
}
return result.build();
}
use of org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult in project SpongeCommon by SpongePowered.
the class SlotMixin_Inventory_API method set.
@Override
public InventoryTransactionResult set(ItemStack stack) {
final InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(InventoryTransactionResult.Type.SUCCESS);
final net.minecraft.world.item.ItemStack nativeStack = ItemStackUtil.toNative(stack);
final net.minecraft.world.item.ItemStack old = this.shadow$getItem();
ItemStackSnapshot oldSnap = ItemStackUtil.snapshotOf(old);
int remaining = stack.quantity();
final int push = Math.min(remaining, this.shadow$getMaxStackSize());
net.minecraft.world.item.ItemStack newStack = ItemStackUtil.cloneDefensiveNative(nativeStack, push);
this.shadow$set(newStack);
result.transaction(new SlotTransaction(this, oldSnap, ItemStackUtil.snapshotOf(newStack)));
remaining -= push;
if (remaining > 0) {
result.reject(ItemStackUtil.cloneDefensive(nativeStack, remaining));
}
return result.build();
}
Aggregations