use of org.spongepowered.common.bridge.world.inventory.InventoryBridge 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.bridge.world.inventory.InventoryBridge in project SpongeCommon by SpongePowered.
the class UnionQuery method execute.
@Override
public Inventory execute(Inventory inventory, InventoryAdapter adapter) {
final CompoundLens.Builder lensBuilder = CompoundLens.builder().add(adapter.inventoryAdapter$getRootLens());
final CompoundFabric fabric = new CompoundFabric(adapter.inventoryAdapter$getFabric(), ((InventoryBridge) this.other).bridge$getAdapter().inventoryAdapter$getFabric());
final CompoundSlotLensProvider provider = new CompoundSlotLensProvider().add(adapter);
for (final Inventory inv : this.other.children()) {
lensBuilder.add(((InventoryAdapter) inv).inventoryAdapter$getRootLens());
provider.add((InventoryAdapter) inv);
}
final CompoundLens lens = lensBuilder.build(provider);
return lens.getAdapter(fabric, inventory);
}
use of org.spongepowered.common.bridge.world.inventory.InventoryBridge in project SpongeCommon by SpongePowered.
the class TraitMixin_ArmorEquipable_Inventory_API method equip.
@Override
public boolean equip(final EquipmentType type, @Nullable final ItemStack equipment) {
final InventoryAdapter inv = ((InventoryBridge) this).bridge$getAdapter();
final EquipmentInventoryLens lens = this.impl$equipmentInventory(inv);
final Fabric fabric = inv.inventoryAdapter$getFabric();
return lens.getSlotLens(type).setStack(fabric, ItemStackUtil.toNative(equipment));
}
use of org.spongepowered.common.bridge.world.inventory.InventoryBridge in project SpongeCommon by SpongePowered.
the class TraitMixin_Equipable_Inventory_API method equipment.
@Override
public EquipmentInventory equipment() {
if (this.impl$equipmentInventory == null) {
final InventoryAdapter inv = ((InventoryBridge) this).bridge$getAdapter();
final EquipmentInventoryLens lens = (EquipmentInventoryLens) inv.inventoryAdapter$getRootLens();
this.impl$equipmentInventory = (EquipmentInventory) lens.getAdapter(inv.inventoryAdapter$getFabric(), null);
}
return this.impl$equipmentInventory;
}
use of org.spongepowered.common.bridge.world.inventory.InventoryBridge in project SpongeCommon by SpongePowered.
the class AbstractContainerMenuMixin_Fabric_Inventory method fabric$allInventories.
@Override
public Collection<InventoryBridge> fabric$allInventories() {
if (this.all == null) {
ImmutableSet.Builder<InventoryBridge> builder = ImmutableSet.builder();
for (Slot slot : this.slots) {
if (slot.container != null) {
builder.add((InventoryBridge) slot.container);
}
}
this.all = builder.build();
}
return this.all;
}
Aggregations