use of org.spongepowered.common.item.inventory.lens.SlotProvider in project SpongeCommon by SpongePowered.
the class FurnaceInventoryLens method init.
@Override
protected void init(SlotProvider<IInventory, ItemStack> slots) {
this.addChild(new OrderedInventoryLensImpl(0, 3, 1, slots));
this.input = new InputSlotLensImpl(0, (i) -> true, (i) -> true);
// TODO SlotFurnaceFuel
this.fuel = new FuelSlotLensImpl(1, (i) -> true, (i) -> true);
// SlotFurnaceOutput
this.output = new OutputSlotLensImpl(2, (i) -> false, (i) -> false);
this.addSpanningChild(this.input, new SlotIndex(0));
this.addSpanningChild(this.fuel, new SlotIndex(1));
this.addSpanningChild(this.output, new SlotIndex(2));
}
use of org.spongepowered.common.item.inventory.lens.SlotProvider in project SpongeCommon by SpongePowered.
the class ContainerUtil method generateAdapterLens.
@Nullable
private static Lens<IInventory, ItemStack> generateAdapterLens(SlotProvider<IInventory, ItemStack> slots, int index, CraftingInventoryData crafting, List<Slot> slotList, @Nullable IInventory subInventory) {
if (!(subInventory instanceof InventoryAdapter)) {
return null;
}
Lens<IInventory, ItemStack> adapterLens = ((InventoryAdapter) subInventory).getRootLens();
if (adapterLens == null) {
return null;
}
if (subInventory.getSizeInventory() == 0) {
return new DefaultEmptyLens<>(((InventoryAdapter) subInventory));
}
if (adapterLens instanceof PlayerInventoryLens) {
if (slotList.size() == 36) {
return new DelegatingLens(index, new MainPlayerInventoryLensImpl(index, slots, true), slots);
}
return null;
}
// For Crafting Result we need the Slot to get Filter logic
if (subInventory instanceof InventoryCraftResult) {
Slot slot = slotList.get(0);
adapterLens = new CraftingOutputSlotLensImpl(index, item -> slot.isItemValid(((ItemStack) item)), itemType -> (slot.isItemValid((ItemStack) org.spongepowered.api.item.inventory.ItemStack.of(itemType, 1))));
crafting.out = index;
if (slot instanceof SlotCrafting) {
if (crafting.base == null) {
// In case we do not find the InventoryCrafting later assume it is directly after the SlotCrafting
// e.g. for IC2 ContainerIndustrialWorkbench
crafting.base = index + 1;
crafting.grid = ((SlotCrafting) slot).craftMatrix;
}
}
}
if (subInventory instanceof InventoryCrafting) {
crafting.base = index;
crafting.grid = ((InventoryCrafting) subInventory);
}
return new DelegatingLens(index, adapterLens, slots);
}
use of org.spongepowered.common.item.inventory.lens.SlotProvider in project SpongeCommon by SpongePowered.
the class MixinContainerPlayer method slotProvider.
@Override
public SlotProvider<IInventory, ItemStack> slotProvider(Fabric<IInventory> fabric, InventoryAdapter<IInventory, ItemStack> adapter) {
SlotCollection.Builder builder = new SlotCollection.Builder().add(1, CraftingOutputAdapter.class, (i) -> new CraftingOutputSlotLensImpl(i, (t) -> false, (t) -> false)).add(4).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.HEADWEAR)).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.CHESTPLATE)).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.LEGGINGS)).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.BOOTS)).add(36).add(1);
// Add additional slots (e.g. from mods)
builder.add(this.inventorySlots.size() - 46);
return builder.build();
}
use of org.spongepowered.common.item.inventory.lens.SlotProvider in project SpongeCommon by SpongePowered.
the class BrewingStandInventoryLens method init.
@Override
protected void init(SlotProvider<IInventory, ItemStack> slots) {
this.potions = new OrderedInventoryLensImpl(0, 3, 1, OutputSlot.class, slots);
// TODO filter PotionIngredients
this.ingredient = new InputSlotLensImpl(3, (i) -> true, (i) -> true);
this.fuel = new FuelSlotLensImpl(4, (i) -> BLAZE_POWDER.equals(i.getType()), BLAZE_POWDER::equals);
this.addSpanningChild(this.potions);
this.addSpanningChild(this.ingredient);
this.addSpanningChild(this.fuel);
}
use of org.spongepowered.common.item.inventory.lens.SlotProvider 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