Search in sources :

Example 1 with PrimaryPlayerInventoryLens

use of org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens 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);
}
Also used : CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) ArrayList(java.util.ArrayList) CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) BasicSlotLens(org.spongepowered.common.inventory.lens.impl.slot.BasicSlotLens) CraftingGridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingGridInventoryLens) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) BrewingStandInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.BrewingStandInventoryLens) FurnaceInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.FurnaceInventoryLens) ContainerPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerPlayerInventoryLens) LargeChestInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.LargeChestInventoryLens) Lens(org.spongepowered.common.inventory.lens.Lens) SingleIndexedLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleIndexedLens) SingleGridLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens)

Example 2 with PrimaryPlayerInventoryLens

use of org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens 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);
}
Also used : CraftingContainer(net.minecraft.world.inventory.CraftingContainer) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) PlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.PlayerInventoryLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) ResultSlotAccessor(org.spongepowered.common.accessor.world.inventory.ResultSlotAccessor) Slot(net.minecraft.world.inventory.Slot) CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) GridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) CompoundLens(org.spongepowered.common.inventory.lens.impl.CompoundLens) Lens(org.spongepowered.common.inventory.lens.Lens) SingleGridLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens) PlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.PlayerInventoryLens) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) ResultContainer(net.minecraft.world.inventory.ResultContainer)

Example 3 with PrimaryPlayerInventoryLens

use of org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens 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);
}
Also used : CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) ResultContainer(net.minecraft.world.inventory.ResultContainer) ResultSlotAccessor(org.spongepowered.common.accessor.world.inventory.ResultSlotAccessor) HopperMenuAccessor(org.spongepowered.common.accessor.world.inventory.HopperMenuAccessor) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) Random(java.util.Random) InventoryBridge(org.spongepowered.common.bridge.world.inventory.InventoryBridge) ContainerBridge(org.spongepowered.common.bridge.world.inventory.container.ContainerBridge) AbstractFurnaceMenuAccessor(org.spongepowered.common.accessor.world.inventory.AbstractFurnaceMenuAccessor) DispenserMenuAccessor(org.spongepowered.common.accessor.world.inventory.DispenserMenuAccessor) Containers(net.minecraft.world.Containers) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) Map(java.util.Map) GridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens) BeaconMenu(net.minecraft.world.inventory.BeaconMenu) BrewingStandMenuAccessor(org.spongepowered.common.accessor.world.inventory.BrewingStandMenuAccessor) SpongeLocationCarrier(org.spongepowered.common.inventory.SpongeLocationCarrier) Carrier(org.spongepowered.api.item.inventory.Carrier) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Collectors(java.util.stream.Collectors) Player(net.minecraft.world.entity.player.Player) Slot(net.minecraft.world.inventory.Slot) ChestMenu(net.minecraft.world.inventory.ChestMenu) List(java.util.List) MerchantMenuAccessor(org.spongepowered.common.accessor.world.inventory.MerchantMenuAccessor) CraftingContainer(net.minecraft.world.inventory.CraftingContainer) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) CompoundContainer(net.minecraft.world.CompoundContainer) ItemEntity(net.minecraft.world.entity.item.ItemEntity) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Optional(java.util.Optional) ItemCombinerMenuAccessor(org.spongepowered.common.accessor.world.inventory.ItemCombinerMenuAccessor) Level(net.minecraft.world.level.Level) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Inventory(org.spongepowered.api.item.inventory.Inventory) NonNull(org.checkerframework.checker.nullness.qual.NonNull) BeaconMenuAccessor(org.spongepowered.common.accessor.world.inventory.BeaconMenuAccessor) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) CompoundLens(org.spongepowered.common.inventory.lens.impl.CompoundLens) CompoundSlotLensProvider(org.spongepowered.common.inventory.lens.CompoundSlotLensProvider) ServerLevel(net.minecraft.server.level.ServerLevel) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ServerPlayer(net.minecraft.server.level.ServerPlayer) Lens(org.spongepowered.common.inventory.lens.Lens) Nullable(org.checkerframework.checker.nullness.qual.Nullable) SingleGridLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens) CustomContainer(org.spongepowered.common.inventory.custom.CustomContainer) AbstractContainerMenuAccessor(org.spongepowered.common.accessor.world.inventory.AbstractContainerMenuAccessor) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) SlotLensProvider(org.spongepowered.common.inventory.lens.impl.slot.SlotLensProvider) PlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.PlayerInventoryLens) HorseInventoryMenuAccessor(org.spongepowered.common.accessor.world.inventory.HorseInventoryMenuAccessor) Container(org.spongepowered.api.item.inventory.Container) SpongeBlockEntityCarrier(org.spongepowered.common.inventory.SpongeBlockEntityCarrier) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) ArrayList(java.util.ArrayList) ResultContainer(net.minecraft.world.inventory.ResultContainer) CraftingContainer(net.minecraft.world.inventory.CraftingContainer) CompoundContainer(net.minecraft.world.CompoundContainer) CustomContainer(org.spongepowered.common.inventory.custom.CustomContainer) Container(org.spongepowered.api.item.inventory.Container) GridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens) CompoundSlotLensProvider(org.spongepowered.common.inventory.lens.CompoundSlotLensProvider) List(java.util.List) ArrayList(java.util.ArrayList) Optional(java.util.Optional) InventoryBridge(org.spongepowered.common.bridge.world.inventory.InventoryBridge) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) SingleGridLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens) CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) Slot(net.minecraft.world.inventory.Slot) CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) GridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) CompoundLens(org.spongepowered.common.inventory.lens.impl.CompoundLens) Lens(org.spongepowered.common.inventory.lens.Lens) SingleGridLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens) PlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.PlayerInventoryLens) CompoundLens(org.spongepowered.common.inventory.lens.impl.CompoundLens) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DelegatingLens(org.spongepowered.common.inventory.lens.impl.DelegatingLens)

Example 4 with PrimaryPlayerInventoryLens

use of org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens in project SpongeCommon by SpongePowered.

the class PlayerInventoryLens method init.

protected void init(SlotLensProvider slots) {
    // Adding slots
    for (int ord = 0, slot = this.base; ord < this.size; ord++, slot++) {
        this.addChild(slots.getSlotLens(slot), KeyValuePair.slotIndex(ord));
    }
    int base = this.base;
    Map<EquipmentType, SlotLens> equipmentLenses = new LinkedHashMap<>();
    if (this.isContainer) {
        this.armor = new ArmorInventoryLens(base, slots, true);
        equipmentLenses.put(EquipmentTypes.HEAD.get(), slots.getSlotLens(base + 0));
        equipmentLenses.put(EquipmentTypes.CHEST.get(), slots.getSlotLens(base + 1));
        equipmentLenses.put(EquipmentTypes.LEGS.get(), slots.getSlotLens(base + 2));
        equipmentLenses.put(EquipmentTypes.FEET.get(), slots.getSlotLens(base + 3));
        // 4
        base += PlayerInventoryLens.ARMOR;
        this.primary = new PrimaryPlayerInventoryLens(base, slots, true);
        base += this.primary.slotCount();
        this.offhand = slots.getSlotLens(base);
        base += PlayerInventoryLens.OFFHAND;
        equipmentLenses.put(EquipmentTypes.OFF_HAND.get(), this.offhand);
        this.addSpanningChild(this.armor);
        this.addSpanningChild(this.primary);
        this.addSpanningChild(this.offhand);
    } else {
        this.primary = new PrimaryPlayerInventoryLens(base, slots, false);
        base += this.primary.slotCount();
        this.armor = new ArmorInventoryLens(base, slots, false);
        equipmentLenses.put(EquipmentTypes.FEET.get(), slots.getSlotLens(base + 0));
        equipmentLenses.put(EquipmentTypes.LEGS.get(), slots.getSlotLens(base + 1));
        equipmentLenses.put(EquipmentTypes.CHEST.get(), slots.getSlotLens(base + 2));
        equipmentLenses.put(EquipmentTypes.HEAD.get(), slots.getSlotLens(base + 3));
        base += PlayerInventoryLens.ARMOR;
        this.offhand = slots.getSlotLens(base);
        base += PlayerInventoryLens.OFFHAND;
        equipmentLenses.put(EquipmentTypes.OFF_HAND.get(), this.offhand);
        this.addSpanningChild(this.primary);
        this.addSpanningChild(this.armor);
        this.addSpanningChild(this.offhand);
    }
    equipmentLenses.put(EquipmentTypes.MAIN_HAND.get(), new HeldHandSlotLens(this.primary.getHotbar()));
    this.equipment = new EquipmentInventoryLens(equipmentLenses);
    for (Map.Entry<EquipmentType, SlotLens> entry : equipmentLenses.entrySet()) {
        this.addChild(entry.getValue(), KeyValuePair.of(Keys.EQUIPMENT_TYPE, entry.getKey()));
    }
    this.addChild(this.equipment);
    this.addMissingSpanningSlots(base, slots);
}
Also used : HeldHandSlotLens(org.spongepowered.common.inventory.lens.impl.slot.HeldHandSlotLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) ArmorInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.ArmorInventoryLens) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) EquipmentInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.EquipmentInventoryLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) HeldHandSlotLens(org.spongepowered.common.inventory.lens.impl.slot.HeldHandSlotLens) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with PrimaryPlayerInventoryLens

use of org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens in project SpongeCommon by SpongePowered.

the class LensRegistrar method lensRepairContainer.

@SuppressWarnings("unchecked")
private static Lens lensRepairContainer(Object inventory, int size, SlotLensProvider slotLensProvider) {
    final List<Lens> lenses = new ArrayList<>();
    lenses.add(new DefaultIndexedLens(0, 3, slotLensProvider));
    lenses.add(new PrimaryPlayerInventoryLens(3, slotLensProvider, true));
    return new ContainerLens(size, (Class<? extends Inventory>) inventory.getClass(), slotLensProvider, lenses);
}
Also used : PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) ArrayList(java.util.ArrayList) CraftingInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens) SlotLens(org.spongepowered.common.inventory.lens.slots.SlotLens) PrimaryPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens) BasicSlotLens(org.spongepowered.common.inventory.lens.impl.slot.BasicSlotLens) CraftingGridInventoryLens(org.spongepowered.common.inventory.lens.impl.comp.CraftingGridInventoryLens) ContainerLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens) BrewingStandInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.BrewingStandInventoryLens) FurnaceInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.FurnaceInventoryLens) ContainerPlayerInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerPlayerInventoryLens) LargeChestInventoryLens(org.spongepowered.common.inventory.lens.impl.minecraft.LargeChestInventoryLens) Lens(org.spongepowered.common.inventory.lens.Lens) SingleIndexedLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleIndexedLens) SingleGridLens(org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens)

Aggregations

PrimaryPlayerInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.PrimaryPlayerInventoryLens)5 Lens (org.spongepowered.common.inventory.lens.Lens)4 CraftingInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.CraftingInventoryLens)4 SingleGridLens (org.spongepowered.common.inventory.lens.impl.minecraft.SingleGridLens)4 ContainerLens (org.spongepowered.common.inventory.lens.impl.minecraft.container.ContainerLens)4 ArrayList (java.util.ArrayList)3 SlotLens (org.spongepowered.common.inventory.lens.slots.SlotLens)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 CraftingContainer (net.minecraft.world.inventory.CraftingContainer)2 ResultContainer (net.minecraft.world.inventory.ResultContainer)2 Slot (net.minecraft.world.inventory.Slot)2 ResultSlotAccessor (org.spongepowered.common.accessor.world.inventory.ResultSlotAccessor)2 CompoundLens (org.spongepowered.common.inventory.lens.impl.CompoundLens)2 DelegatingLens (org.spongepowered.common.inventory.lens.impl.DelegatingLens)2 GridInventoryLens (org.spongepowered.common.inventory.lens.impl.comp.GridInventoryLens)2 PlayerInventoryLens (org.spongepowered.common.inventory.lens.impl.minecraft.PlayerInventoryLens)2 List (java.util.List)1 Optional (java.util.Optional)1 Random (java.util.Random)1