use of org.spongepowered.common.item.inventory.lens.impl.comp.GridInventoryLensImpl in project SpongeCommon by SpongePowered.
the class CustomLens method addLensFor.
private int addLensFor(InventoryProperty<?, ?> size, int base, SlotProvider<IInventory, ItemStack> slots) {
Lens<IInventory, ItemStack> lens;
int slotCount;
if (size instanceof InventoryDimension) {
InventoryDimension dimension = ((InventoryDimension) size);
slotCount = dimension.getColumns() * dimension.getRows();
lens = new GridInventoryLensImpl(base, dimension.getColumns(), dimension.getRows(), dimension.getColumns(), slots);
} else if (size instanceof InventoryCapacity) {
InventoryCapacity capacity = ((InventoryCapacity) size);
slotCount = capacity.getValue();
lens = new OrderedInventoryLensImpl(base, capacity.getValue(), 1, slots);
} else {
throw new IllegalStateException("Unknown Inventory Size Property " + size.getClass().getName());
}
this.addSpanningChild(lens);
return slotCount;
}
use of org.spongepowered.common.item.inventory.lens.impl.comp.GridInventoryLensImpl in project SpongeCommon by SpongePowered.
the class MixinEntityMinecartChest method onConstructed.
@Inject(method = "<init>*", at = @At("RETURN"))
public void onConstructed(CallbackInfo ci) {
this.fabric = new IInventoryFabric(this);
this.slots = new SlotCollection.Builder().add(27).build();
this.lens = new GridInventoryLensImpl(0, 9, 3, 9, this.slots);
}
use of org.spongepowered.common.item.inventory.lens.impl.comp.GridInventoryLensImpl in project SpongeCommon by SpongePowered.
the class LargeChestInventoryLens method initLargeChest.
private void initLargeChest(SlotProvider<IInventory, ItemStack> slots) {
// add grids
int base = 0;
this.addSpanningChild(new GridInventoryLensImpl(base, 9, this.upperChest / 9, 9, (Class) TileEntityChest.class, slots));
base += this.upperChest;
this.addSpanningChild(new GridInventoryLensImpl(base, 9, this.lowerChest / 9, 9, (Class) TileEntityChest.class, slots));
base += this.lowerChest;
this.addChild(new GridInventoryLensImpl(0, 9, (this.upperChest + this.lowerChest) / 9, 9, slots));
// add slot childs for grids
for (int ord = 0, slot = this.base; ord < base; ord++, slot++) {
this.addChild(slots.getSlot(slot), new SlotIndex(ord));
}
// handle possible extension by mods:
for (int i = base; i < this.slotCount(); i++) {
this.addSpanningChild(new SlotLensImpl(i), SlotIndex.of(i));
}
}
use of org.spongepowered.common.item.inventory.lens.impl.comp.GridInventoryLensImpl in project SpongeCommon by SpongePowered.
the class ContainerUtil method generateLens.
/**
* Generates a fallback lens for given Container
*
* @param container The Container to generate a lens for
* @param slots The slots of the Container
* @return The generated fallback lens
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Lens<IInventory, ItemStack> generateLens(net.minecraft.inventory.Container container, SlotProvider<IInventory, ItemStack> slots) {
// Get all inventories viewed in the Container & count slots & retain order
Map<Optional<IInventory>, List<Slot>> viewed = container.inventorySlots.stream().collect(Collectors.groupingBy(i -> Optional.<IInventory>ofNullable(i.inventory), LinkedHashMap::new, Collectors.toList()));
// Count the index
int index = 0;
CraftingInventoryData crafting = new CraftingInventoryData();
List<Lens<IInventory, ItemStack>> lenses = new ArrayList<>();
for (Map.Entry<Optional<IInventory>, List<Slot>> entry : viewed.entrySet()) {
List<Slot> slotList = entry.getValue();
int slotCount = slotList.size();
IInventory subInventory = entry.getKey().orElse(null);
// Generate Lens based on existing InventoryAdapter
Lens<IInventory, ItemStack> lens = generateAdapterLens(slots, index, crafting, slotList, subInventory);
// Check if sub-inventory is LensProvider
if (lens == null && subInventory instanceof LensProvider) {
Fabric<IInventory> keyFabric = MinecraftFabric.of(subInventory);
lens = ((LensProvider) subInventory).rootLens(keyFabric, new VanillaAdapter(keyFabric, container));
}
// Unknown Inventory or Inventory size <> Lens size
if (lens == null || lens.slotCount() != slotCount) {
if (subInventory instanceof InventoryCraftResult) {
// InventoryCraftResult is a Slot
Slot slot = slotList.get(0);
lens = new CraftingOutputSlotLensImpl(index, item -> slot.isItemValid(((ItemStack) item)), itemType -> (slot.isItemValid((ItemStack) org.spongepowered.api.item.inventory.ItemStack.of(itemType, 1))));
} else if (subInventory instanceof InventoryCrafting) {
// InventoryCrafting has width and height and is Input
InventoryCrafting craftGrid = (InventoryCrafting) subInventory;
lens = new GridInventoryLensImpl(index, craftGrid.getWidth(), craftGrid.getHeight(), craftGrid.getWidth(), InputSlot.class, slots);
} else if (slotCount == 1) {
// Unknown - A single Slot
lens = new SlotLensImpl(index);
} else if (subInventory instanceof InventoryBasic && subInventory.getClass().isAnonymousClass()) {
// Anonymous InventoryBasic -> Check for Vanilla Containers:
switch(subInventory.getName()) {
// Container InputSlots
case "Enchant":
case // Container InputSlots
"Repair":
lens = new OrderedInventoryLensImpl(index, slotCount, 1, InputSlot.class, slots);
break;
default:
// Unknown
lens = new OrderedInventoryLensImpl(index, slotCount, 1, slots);
}
} else {
// Unknown - fallback to OrderedInventory
lens = new OrderedInventoryLensImpl(index, slotCount, 1, slots);
}
}
lenses.add(lens);
index += slotCount;
}
List<Lens<IInventory, ItemStack>> additional = new ArrayList<>();
try {
if (crafting.out != null && crafting.base != null && crafting.grid != null) {
additional.add(new CraftingInventoryLensImpl(crafting.out, crafting.base, crafting.grid.getWidth(), crafting.grid.getHeight(), slots));
}
} catch (Exception e) {
SpongeImpl.getLogger().error("Error while creating CraftingInventoryLensImpl for " + container.getClass().getName(), e);
}
// Lens containing/delegating to other lenses
return new ContainerLens((InventoryAdapter<IInventory, ItemStack>) container, slots, lenses, additional);
}
Aggregations