use of org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens in project SpongeCommon by SpongePowered.
the class SpongeInventoryBuilder method grid.
public BuildingStep grid(int sizeX, int sizeY) {
this.size += sizeX * sizeY;
net.minecraft.world.SimpleContainer adapter = new net.minecraft.world.SimpleContainer(sizeX * sizeY);
this.lenses.add(new GridInventoryLens(0, sizeX, sizeY, ((InventoryAdapter) adapter).inventoryAdapter$getSlotLensProvider()));
this.inventories.add((Inventory) adapter);
return this;
}
use of org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens 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.impl.comp.GridInventoryLens in project SpongeCommon by SpongePowered.
the class GridQuery method execute.
@Override
public Inventory execute(Inventory inventory, InventoryAdapter adapter) {
if (!(adapter instanceof GridInventoryAdapter)) {
return new EmptyInventoryImpl(inventory);
}
GridInventoryAdapter gridAdapter = (GridInventoryAdapter) adapter;
Vector2i max = gridAdapter.dimensions();
if (max.x() < this.offset.x() + this.size.x() && max.y() < this.offset.y() + this.size.y()) {
// queried grid does not fit inventory
return new EmptyInventoryImpl(inventory);
}
// Get slots for new grid
CompoundSlotLensProvider slotProvider = new CompoundSlotLensProvider();
for (int dy = 0; dy < this.size.y(); dy++) {
for (int dx = 0; dx < this.size.x(); dx++) {
slotProvider.add(gridAdapter.getSlotLens(this.offset.x() + dx, this.offset.y() + dy));
}
}
// build new grid lens
GridInventoryLens lens = new GridInventoryLens(0, this.size.x(), this.size.y(), slotProvider);
return new GridInventoryAdapter(adapter.inventoryAdapter$getFabric(), lens, inventory);
}
use of org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens in project SpongeCommon by SpongePowered.
the class LargeChestInventoryLens method init.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void init(final SlotLensProvider slots) {
// add grids
int base = 0;
this.addSpanningChild(new ChestPartLens(base, 9, this.upperChest / 9, slots, true));
base += this.upperChest;
this.addSpanningChild(new ChestPartLens(base, 9, this.lowerChest / 9, slots, false));
base += this.lowerChest;
this.addChild(new GridInventoryLens(0, 9, (this.upperChest + this.lowerChest) / 9, slots));
this.addMissingSpanningSlots(base, slots);
}
Aggregations