use of org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.
the class CompoundSlotLensProvider method add.
/**
* Adds all slots from this inventory adapter.
*
* @param adapter The adapter
*
* @return this provider for chaining
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public CompoundSlotLensProvider add(InventoryAdapter adapter) {
for (Inventory slot : ((Inventory) adapter).slots()) {
SlotLens slotLens = ((SlotLens) ((SlotAdapter) slot).inventoryAdapter$getRootLens());
this.slotList.add(slotLens);
}
return this;
}
use of org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter 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.common.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handleSlotRestore.
public static void handleSlotRestore(@Nullable final Player player, @Nullable final AbstractContainerMenu containerMenu, final List<SlotTransaction> slotTransactions, final boolean eventCancelled) {
try (PhaseContext<@NonNull ?> ignored = BlockPhase.State.RESTORING_BLOCKS.createPhaseContext(PhaseTracker.SERVER).buildAndSwitch()) {
boolean restoredAny = false;
for (final SlotTransaction slotTransaction : slotTransactions) {
if ((!slotTransaction.custom().isPresent() && slotTransaction.isValid()) && !eventCancelled) {
continue;
}
restoredAny = true;
final org.spongepowered.api.item.inventory.Slot slot = slotTransaction.slot();
final ItemStackSnapshot snapshot = eventCancelled || !slotTransaction.isValid() ? slotTransaction.original() : slotTransaction.custom().get();
if (containerMenu == null || slot.viewedSlot() == slot) {
slot.set(snapshot.createStack());
} else {
final int slotNumber = ((SlotAdapter) slot).getOrdinal();
final Slot nmsSlot = containerMenu.getSlot(slotNumber);
if (nmsSlot != null) {
nmsSlot.set(ItemStackUtil.fromSnapshotToNative(snapshot));
}
}
}
if (restoredAny && player instanceof net.minecraft.server.level.ServerPlayer) {
if (containerMenu != null) {
containerMenu.broadcastChanges();
if (player.containerMenu == containerMenu) {
((net.minecraft.server.level.ServerPlayer) player).refreshContainer(containerMenu);
}
} else {
player.inventoryMenu.broadcastChanges();
}
}
}
}
use of org.spongepowered.common.inventory.adapter.impl.slots.SlotAdapter in project SpongeCommon by SpongePowered.
the class AdapterLogic method pollSequential.
public static InventoryTransactionResult.Poll pollSequential(Fabric fabric, @Nullable Lens lens, @Nullable Integer limit) {
if (lens == null || lens.getSlots(fabric).size() <= 0) {
return InventoryTransactionResult.builder().type(Type.NO_SLOT).poll(ItemStackSnapshot.empty()).build();
}
InventoryTransactionResult.Builder result = InventoryTransactionResult.builder().type(Type.SUCCESS);
// used when polling from multiple slots
ItemStack removedType = null;
int totalPolled = 0;
for (SlotLens slot : lens.getSlots(fabric)) {
net.minecraft.world.item.ItemStack stack = slot.getStack(fabric);
// Only remove one type of item
if (stack.isEmpty() || (removedType != null && !ItemStackUtil.compareIgnoreQuantity(removedType, stack))) {
continue;
}
// Poll up to limit items OR entire stack when no limit is set
int pollCount = limit != null ? Math.min(stack.getCount(), limit) : stack.getCount();
net.minecraft.world.item.ItemStack newStack = net.minecraft.world.item.ItemStack.EMPTY;
if (pollCount < stack.getCount()) {
// is stack not removed completely?
newStack = stack.copy();
newStack.setCount(newStack.getCount() - pollCount);
}
if (slot.setStack(fabric, newStack)) {
// Set new stack
// TODO parent??
SlotAdapter slotAdapter = (SlotAdapter) slot.getAdapter(fabric, null);
result.transaction(new SlotTransaction(slotAdapter, ItemStackUtil.snapshotOf(stack), ItemStackUtil.snapshotOf(newStack)));
if (removedType == null) {
// set removed type when first removing
removedType = ItemStackUtil.cloneDefensive(stack, 1);
}
if (limit == null) {
totalPolled = pollCount;
// no limit only polls the first non-empty slot
break;
}
// remove amount polled from slot
limit -= pollCount;
totalPolled += pollCount;
}
if (limit != null && limit <= 0) {
// polled all items requested
break;
}
}
if (removedType != null) {
// mark dirty if items were removed
fabric.fabric$markDirty();
}
if (limit != null && limit > 0) {
// not all items requested could be polled
result.type(Type.FAILURE);
}
if (removedType == null) {
removedType = ItemStack.empty();
} else {
removedType.setQuantity(totalPolled);
}
return result.poll(removedType.createSnapshot()).build();
}
Aggregations