use of org.spongepowered.common.inventory.lens.slots.SlotLens in project SpongeCommon by SpongePowered.
the class SlotMixin_Lens_Inventory method lensGeneratorBridge$generateLens.
@Override
public Lens lensGeneratorBridge$generateLens(SlotLensProvider slotLensProvider) {
try {
final Lens rootLens = ((InventoryAdapter) this.container).inventoryAdapter$getRootLens();
SlotLens lens = rootLens.getSlotLens(this.inventoryAdapter$getFabric(), this.slot);
if (lens != null) {
return lens;
}
} catch (Exception ignored) {
}
// this works as a fallback but removes Inventory Property Support completely
return new BasicSlotLens(0);
}
use of org.spongepowered.common.inventory.lens.slots.SlotLens 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();
}
use of org.spongepowered.common.inventory.lens.slots.SlotLens in project SpongeCommon by SpongePowered.
the class LivingEntityMixin_Inventory method impl$getSpongeSlot.
@SuppressWarnings("ConstantConditions")
protected Slot impl$getSpongeSlot(final EquipmentSlot equipmentSlot) {
final EquipmentType equipmentType = (EquipmentType) (Object) equipmentSlot;
if (this instanceof InventoryBridge) {
final InventoryAdapter adapter = ((InventoryBridge) this).bridge$getAdapter();
final Lens lens = adapter.inventoryAdapter$getRootLens();
if (lens instanceof EquipmentInventoryLens) {
final SlotLens slotLens = ((EquipmentInventoryLens) lens).getSlotLens(equipmentType);
return slotLens.getAdapter(adapter.inventoryAdapter$getFabric(), (Inventory) adapter);
}
throw new IllegalStateException("Expected EquipmentInventoryLens for " + this.getClass().getName() + " Inventory but found: " + lens.getClass().getName());
}
throw new IllegalStateException("Living Entity has no InventoryAdapter: " + this.getClass().getName());
}
use of org.spongepowered.common.inventory.lens.slots.SlotLens in project SpongeCommon by SpongePowered.
the class Inventory2DLens method init.
private void init(SlotLensProvider slots) {
for (int y = 0, slot = this.base; y < this.height; y++) {
for (int x = 0; x < this.width; x++, slot += this.stride) {
SlotLens slotLens = slots.getSlotLens(slot);
this.addChild(slotLens, KeyValuePair.of(Keys.SLOT_POSITION, new Vector2i(this.xBase + x, this.yBase + y)));
}
}
}
use of org.spongepowered.common.inventory.lens.slots.SlotLens in project SpongeCommon by SpongePowered.
the class ItemStackQuery method matches.
@Override
public boolean matches(Lens lens, Lens parent, Inventory inventory) {
if (lens instanceof SlotLens) {
Fabric fabric = ((InventoryBridge) inventory).bridge$getAdapter().inventoryAdapter$getFabric();
ItemStack stack = ItemStackUtil.fromNative(((SlotLens) lens).getStack(fabric));
if (stack == null) {
return false;
}
return this.matches(stack, this.arg);
}
return false;
}
Aggregations