Search in sources :

Example 11 with OrderedInventoryLensImpl

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

the class HorseInventoryLens method init.

@Override
protected void init(SlotProvider<IInventory, ItemStack> slots) {
    // 0-1
    this.horseEquipment = new OrderedInventoryLensImpl(0, 2, 1, slots);
    // TODO only for "chested" horses. Will this cause problems?
    // this.chest = new GridInventoryLensImpl(2, 5, 3, 5, slots);             // 2-16
    this.addSpanningChild(this.horseEquipment);
// this.addSpanningChild(this.chest);
}
Also used : OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl)

Example 12 with OrderedInventoryLensImpl

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

the class PlayerInventoryLens method init.

@Override
protected void init(SlotProvider<IInventory, ItemStack> slots) {
    // Adding slots
    for (int ord = 0, slot = this.base; ord < this.size; ord++, slot++) {
        this.addChild(slots.getSlot(slot), new SlotIndex(ord));
    }
    int base = 0;
    this.main = new MainPlayerInventoryLensImpl(base, slots, false);
    base += this.main.slotCount();
    this.equipment = new EquipmentInventoryLensImpl(this.player, base, EQUIPMENT, 1, slots, false);
    base += EQUIPMENT;
    this.offhand = slots.getSlot(base);
    // TODO Hotbar in Vanilla is part of the main inventory (first 9 slots) ; maybe wrap it in a Lens?
    this.addSpanningChild(this.main);
    this.addSpanningChild(this.equipment);
    this.addSpanningChild(this.offhand);
    // Additional Slots for bigger modded inventories
    int additionalSlots = this.size - base - 1;
    if (additionalSlots > 0) {
        this.addSpanningChild(new OrderedInventoryLensImpl(base, additionalSlots, 1, slots));
    }
}
Also used : MainPlayerInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.MainPlayerInventoryLensImpl) EquipmentInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.EquipmentInventoryLensImpl) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex)

Example 13 with OrderedInventoryLensImpl

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

the class ContainerPlayerInventoryLens method init.

@Override
protected void init(SlotProvider<IInventory, ItemStack> slots) {
    // 1
    int base = CRAFTING_OUTPUT;
    final CraftingInventoryLensImpl crafting = new CraftingInventoryLensImpl(0, base, CRAFTING_GRID, CRAFTING_GRID, slots);
    // 4
    base += CRAFTING_GRID * CRAFTING_GRID;
    // TODO pass player for carrier to EquipmentInventory
    final EquipmentInventoryLensImpl armor = new EquipmentInventoryLensImpl(null, base, EQUIPMENT, 1, slots, true);
    // 4
    base += EQUIPMENT;
    final MainPlayerInventoryLensImpl main = new MainPlayerInventoryLensImpl(base, slots, true);
    // 9
    base += MAIN_INVENTORY_HEIGHT * INVENTORY_WIDTH + HOTBAR * INVENTORY_WIDTH;
    final SlotLens<IInventory, ItemStack> offHand = slots.getSlot(base);
    base += OFFHAND;
    this.viewedInventories = new ArrayList<>(Arrays.asList(crafting, armor, offHand, main));
    int additionalSlots = this.size - base - 1;
    if (additionalSlots > 0) {
        viewedInventories.add(new OrderedInventoryLensImpl(base, additionalSlots, 1, slots));
    }
    // TODO actual Container order is:
    // CraftingOutput (1) -> Crafting (4) -> ArmorSlots (4) -> MainInventory (27) -> Hotbar (9) -> Offhand (1)
    // how to handle issues like in #939? ; e.g. Inventory#offer using a different insertion order
    super.init(slots);
}
Also used : IInventory(net.minecraft.inventory.IInventory) MainPlayerInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.MainPlayerInventoryLensImpl) EquipmentInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.EquipmentInventoryLensImpl) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) CraftingInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.CraftingInventoryLensImpl) ItemStack(net.minecraft.item.ItemStack)

Example 14 with OrderedInventoryLensImpl

use of org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl 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);
}
Also used : MainPlayerInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.MainPlayerInventoryLensImpl) MinecraftFabric(org.spongepowered.common.item.inventory.lens.impl.MinecraftFabric) LargeChestInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.LargeChestInventoryLens) Random(java.util.Random) ContainerChest(net.minecraft.inventory.ContainerChest) ContainerWorkbench(net.minecraft.inventory.ContainerWorkbench) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) DelegatingLens(org.spongepowered.common.item.inventory.lens.impl.DelegatingLens) PhaseData(org.spongepowered.common.event.tracking.PhaseData) CustomContainer(org.spongepowered.common.item.inventory.custom.CustomContainer) BlockCarrier(org.spongepowered.api.item.inventory.BlockCarrier) Map(java.util.Map) InventoryArchetypes(org.spongepowered.api.item.inventory.InventoryArchetypes) AbstractChestHorse(net.minecraft.entity.passive.AbstractChestHorse) EntityItem(net.minecraft.entity.item.EntityItem) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) CraftingOutputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.CraftingOutputSlotLensImpl) ContainerBeacon(net.minecraft.inventory.ContainerBeacon) Location(org.spongepowered.api.world.Location) InventoryAdapter(org.spongepowered.common.item.inventory.adapter.InventoryAdapter) InventoryHelper(net.minecraft.inventory.InventoryHelper) Collection(java.util.Collection) GridInventoryLens(org.spongepowered.common.item.inventory.lens.comp.GridInventoryLens) Carrier(org.spongepowered.api.item.inventory.Carrier) Collectors(java.util.stream.Collectors) SlotCrafting(net.minecraft.inventory.SlotCrafting) InventoryArchetype(org.spongepowered.api.item.inventory.InventoryArchetype) InventoryBasic(net.minecraft.inventory.InventoryBasic) BrewingStandInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.BrewingStandInventoryLens) List(java.util.List) ContainerLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.container.ContainerLens) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) CraftingOutputAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.CraftingOutputAdapter) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ContainerDispenser(net.minecraft.inventory.ContainerDispenser) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) Lens(org.spongepowered.common.item.inventory.lens.Lens) Optional(java.util.Optional) MixinInventoryHelper(org.spongepowered.common.mixin.core.inventory.MixinInventoryHelper) LensProvider(org.spongepowered.common.item.inventory.lens.LensProvider) ContainerRepair(net.minecraft.inventory.ContainerRepair) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) ContainerMerchant(net.minecraft.inventory.ContainerMerchant) SpongeImpl(org.spongepowered.common.SpongeImpl) GridInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.GridInventoryLensImpl) AbstractHorse(net.minecraft.entity.passive.AbstractHorse) VanillaAdapter(org.spongepowered.common.item.inventory.adapter.impl.VanillaAdapter) SpongeImplHooks(org.spongepowered.common.SpongeImplHooks) ContainerHopper(net.minecraft.inventory.ContainerHopper) InventoryCraftResult(net.minecraft.inventory.InventoryCraftResult) Multimap(com.google.common.collect.Multimap) TileEntityChest(net.minecraft.tileentity.TileEntityChest) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ItemStack(net.minecraft.item.ItemStack) InputSlot(org.spongepowered.api.item.inventory.slot.InputSlot) Inventory2DLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.Inventory2DLensImpl) ContainerEnchantment(net.minecraft.inventory.ContainerEnchantment) DefaultEmptyLens(org.spongepowered.common.item.inventory.lens.impl.DefaultEmptyLens) InventoryLargeChest(net.minecraft.inventory.InventoryLargeChest) ContainerFurnace(net.minecraft.inventory.ContainerFurnace) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) ContainerBrewingStand(net.minecraft.inventory.ContainerBrewingStand) WorldServer(net.minecraft.world.WorldServer) Nullable(javax.annotation.Nullable) FurnaceInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.FurnaceInventoryLens) Fabric(org.spongepowered.common.item.inventory.lens.Fabric) Inventory2DLens(org.spongepowered.common.item.inventory.lens.comp.Inventory2DLens) SlotProvider(org.spongepowered.common.item.inventory.lens.SlotProvider) ContainerHorseInventory(net.minecraft.inventory.ContainerHorseInventory) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) IMixinSingleBlockCarrier(org.spongepowered.common.interfaces.IMixinSingleBlockCarrier) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) CraftingInventoryLens(org.spongepowered.common.item.inventory.lens.comp.CraftingInventoryLens) CraftingInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.CraftingInventoryLensImpl) ContainerPlayer(net.minecraft.inventory.ContainerPlayer) Container(org.spongepowered.api.item.inventory.Container) SlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.SlotLensImpl) IInventory(net.minecraft.inventory.IInventory) Slot(net.minecraft.inventory.Slot) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) VecHelper(org.spongepowered.common.util.VecHelper) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) TileEntity(net.minecraft.tileentity.TileEntity) SlotCollection(org.spongepowered.common.item.inventory.lens.impl.collections.SlotCollection) Collections(java.util.Collections) InventoryCrafting(net.minecraft.inventory.InventoryCrafting) ArrayList(java.util.ArrayList) InventoryCrafting(net.minecraft.inventory.InventoryCrafting) GridInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.GridInventoryLensImpl) CraftingOutputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.CraftingOutputSlotLensImpl) List(java.util.List) ArrayList(java.util.ArrayList) IInventory(net.minecraft.inventory.IInventory) Optional(java.util.Optional) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) CraftingInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.CraftingInventoryLensImpl) ContainerLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.container.ContainerLens) InventoryCraftResult(net.minecraft.inventory.InventoryCraftResult) LensProvider(org.spongepowered.common.item.inventory.lens.LensProvider) VanillaAdapter(org.spongepowered.common.item.inventory.adapter.impl.VanillaAdapter) CraftingOutputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.CraftingOutputSlotLensImpl) SlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.SlotLensImpl) InputSlot(org.spongepowered.api.item.inventory.slot.InputSlot) Slot(net.minecraft.inventory.Slot) LargeChestInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.LargeChestInventoryLens) DelegatingLens(org.spongepowered.common.item.inventory.lens.impl.DelegatingLens) GridInventoryLens(org.spongepowered.common.item.inventory.lens.comp.GridInventoryLens) BrewingStandInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.BrewingStandInventoryLens) ContainerLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.container.ContainerLens) Lens(org.spongepowered.common.item.inventory.lens.Lens) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) DefaultEmptyLens(org.spongepowered.common.item.inventory.lens.impl.DefaultEmptyLens) FurnaceInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.FurnaceInventoryLens) Inventory2DLens(org.spongepowered.common.item.inventory.lens.comp.Inventory2DLens) CraftingInventoryLens(org.spongepowered.common.item.inventory.lens.comp.CraftingInventoryLens) ItemStack(net.minecraft.item.ItemStack) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) InventoryBasic(net.minecraft.inventory.InventoryBasic)

Example 15 with OrderedInventoryLensImpl

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

the class MixinTileEntityLockable method generateLens.

@Override
public ReusableLens<?> generateLens(Fabric<IInventory> fabric, InventoryAdapter<IInventory, ItemStack> adapter) {
    SlotCollection slots = new SlotCollection.Builder().add(this.getSizeInventory()).build();
    OrderedInventoryLensImpl lens = new OrderedInventoryLensImpl(0, this.getSizeInventory(), 1, slots);
    return new ReusableLens<>(slots, lens);
}
Also used : SlotCollection(org.spongepowered.common.item.inventory.lens.impl.collections.SlotCollection) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) ReusableLens(org.spongepowered.common.item.inventory.lens.impl.ReusableLens)

Aggregations

OrderedInventoryLensImpl (org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl)16 SlotCollection (org.spongepowered.common.item.inventory.lens.impl.collections.SlotCollection)9 IInventory (net.minecraft.inventory.IInventory)8 ItemStack (net.minecraft.item.ItemStack)7 SlotProvider (org.spongepowered.common.item.inventory.lens.SlotProvider)4 SlotIndex (org.spongepowered.api.item.inventory.property.SlotIndex)3 Inject (org.spongepowered.asm.mixin.injection.Inject)3 InventoryAdapter (org.spongepowered.common.item.inventory.adapter.InventoryAdapter)3 Lens (org.spongepowered.common.item.inventory.lens.Lens)3 MainPlayerInventoryLensImpl (org.spongepowered.common.item.inventory.lens.impl.comp.MainPlayerInventoryLensImpl)3 IInventoryFabric (org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 Fabric (org.spongepowered.common.item.inventory.lens.Fabric)2 LensProvider (org.spongepowered.common.item.inventory.lens.LensProvider)2 DefaultEmptyLens (org.spongepowered.common.item.inventory.lens.impl.DefaultEmptyLens)2 ReusableLens (org.spongepowered.common.item.inventory.lens.impl.ReusableLens)2