use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class SlotData method getter.
// @formatter:on
private static <D> D getter(final Slot holder, final Key<Value<D>> key) {
final Lens parentLens = ((InventoryBridge) holder.parent()).bridge$getAdapter().inventoryAdapter$getRootLens();
final Lens childLens = ((InventoryBridge) holder).bridge$getAdapter().inventoryAdapter$getRootLens();
final Map<Key<?>, Object> dataMap = parentLens.getDataFor(childLens);
return (D) dataMap.get(key);
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class LensRegistrar method lensWorkbenchContainer.
@SuppressWarnings("unchecked")
private static Lens lensWorkbenchContainer(Object inventory, int size, SlotLensProvider slotLensProvider) {
final List<Lens> lenses = new ArrayList<>();
lenses.add(new CraftingInventoryLens(0, 1, 3, 3, slotLensProvider));
lenses.add(new PrimaryPlayerInventoryLens(3 * 3 + 1, slotLensProvider, true));
return new ContainerLens(size, (Class<? extends Inventory>) inventory.getClass(), slotLensProvider, lenses);
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class ContainerUtil method generateAdapterLens.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Lens generateAdapterLens(final SlotLensProvider slots, final int index, final org.spongepowered.common.inventory.util.ContainerUtil.CraftingInventoryData crafting, final List<Slot> slotList, final net.minecraft.world.@Nullable Container subInventory) {
Lens lens = ((InventoryBridge) subInventory).bridge$getAdapter().inventoryAdapter$getRootLens();
if (lens instanceof PlayerInventoryLens) {
if (slotList.size() == 36) {
return new DelegatingLens(index, new PrimaryPlayerInventoryLens(0, slots, true), slots);
}
return lens;
}
// For Crafting Result we need the Slot to get Filter logic
if (subInventory instanceof ResultContainer) {
final Slot slot = slotList.get(0);
if (slot instanceof ResultSlotAccessor) {
crafting.out = index;
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 = ((ResultSlotAccessor) slot).accessor$craftSlots();
}
}
}
if (subInventory instanceof CraftingContainer) {
crafting.base = index;
crafting.grid = ((CraftingContainer) subInventory);
}
return new DelegatingLens(index, slotList, lens, slots);
}
use of org.spongepowered.common.inventory.lens.Lens 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")
public static Lens generateLens(final net.minecraft.world.inventory.AbstractContainerMenu container, final SlotLensProvider slots) {
// Get all inventories viewed in the Container & count slots & retain order
final Map<Optional<net.minecraft.world.Container>, List<Slot>> viewed = container.slots.stream().collect(Collectors.groupingBy(i -> Optional.<net.minecraft.world.Container>ofNullable(i.container), LinkedHashMap::new, Collectors.toList()));
// Count the index
int index = 0;
final CraftingInventoryData crafting = new CraftingInventoryData();
int chestHeight = 0;
final List<Lens> lenses = new ArrayList<>();
for (final Map.Entry<Optional<net.minecraft.world.Container>, List<Slot>> entry : viewed.entrySet()) {
final List<Slot> slotList = entry.getValue();
final int slotCount = slotList.size();
final net.minecraft.world.Container subInventory = entry.getKey().orElse(null);
// Generate Lens based on existing InventoryAdapter
Lens lens = ContainerUtil.generateAdapterLens(slots, index, crafting, slotList, subInventory);
// Inventory size <> Lens size
if (lens.slotCount() != slotCount) {
CompoundSlotLensProvider slotProvider = new CompoundSlotLensProvider().add(((InventoryBridge) subInventory).bridge$getAdapter());
CompoundLens.Builder lensBuilder = CompoundLens.builder();
for (Slot slot : slotList) {
lensBuilder.add(((InventoryBridge) slot).bridge$getAdapter().inventoryAdapter$getRootLens());
}
lens = lensBuilder.build(slotProvider);
}
lenses.add(lens);
index += slotCount;
// Count height of 9 width grid
if (chestHeight != -1) {
if (lens instanceof DelegatingLens) {
Lens delegated = ((DelegatingLens) lens).getDelegate();
if (delegated instanceof PrimaryPlayerInventoryLens) {
delegated = ((PrimaryPlayerInventoryLens) delegated).getFullGrid();
}
if (delegated instanceof SingleGridLens) {
delegated = delegated.getSpanningChildren().get(0);
}
if (delegated instanceof GridInventoryLens) {
if (((GridInventoryLens) delegated).getWidth() == 9) {
chestHeight += ((GridInventoryLens) delegated).getHeight();
} else {
chestHeight = -1;
}
} else {
chestHeight = -1;
}
} else {
chestHeight = -1;
}
}
}
final List<Lens> additional = new ArrayList<>();
try {
if (crafting.out != null && crafting.base != null && crafting.grid != null) {
additional.add(new CraftingInventoryLens(crafting.out, crafting.base, crafting.grid.getWidth(), crafting.grid.getHeight(), slots));
} else if (crafting.base != null && crafting.grid != null) {
additional.add(new GridInventoryLens(crafting.base, crafting.grid.getWidth(), crafting.grid.getHeight(), slots));
}
} catch (Exception e) {
SpongeCommon.logger().error("Error while creating CraftingInventoryLensImpl or GridInventoryLensImpl for " + container.getClass().getName(), e);
}
if (chestHeight > 0) {
// Add container grid for chest/double chest
additional.add(new GridInventoryLens(0, 9, chestHeight, slots));
}
// Lens containing/delegating to other lenses
return new ContainerLens(container.slots.size(), (Class<? extends Inventory>) container.getClass(), slots, lenses, additional);
}
use of org.spongepowered.common.inventory.lens.Lens in project SpongeCommon by SpongePowered.
the class SpongeQuery method toResult.
protected Inventory toResult(Inventory inventory, Fabric fabric, Map<Lens, Integer> matches) {
if (matches.isEmpty()) {
return new EmptyInventoryImpl(inventory);
}
if (matches.size() == 1) {
final Map.Entry<Lens, Integer> entry = matches.entrySet().iterator().next();
if (entry.getValue() == 0) {
return entry.getKey().getAdapter(fabric, inventory);
}
final LensRegistrar.BasicSlotLensProvider slotProvider = new LensRegistrar.BasicSlotLensProvider(entry.getKey().slotCount());
// TODO check correct slotprovider
final DelegatingLens delegate = new DelegatingLens(entry.getValue(), entry.getKey(), slotProvider);
return delegate.getAdapter(fabric, inventory);
}
final QueryLens lens = new QueryLens(matches, this);
return lens.getAdapter(fabric, inventory);
}
Aggregations