Search in sources :

Example 1 with PlayerInventoryLens

use of org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens in project SpongeCommon by SpongePowered.

the class ContainerUtil method generateAdapterLens.

@Nullable
private static Lens<IInventory, ItemStack> generateAdapterLens(SlotProvider<IInventory, ItemStack> slots, int index, CraftingInventoryData crafting, List<Slot> slotList, @Nullable IInventory subInventory) {
    if (!(subInventory instanceof InventoryAdapter)) {
        return null;
    }
    Lens<IInventory, ItemStack> adapterLens = ((InventoryAdapter) subInventory).getRootLens();
    if (adapterLens == null) {
        return null;
    }
    if (subInventory.getSizeInventory() == 0) {
        return new DefaultEmptyLens<>(((InventoryAdapter) subInventory));
    }
    if (adapterLens instanceof PlayerInventoryLens) {
        if (slotList.size() == 36) {
            return new DelegatingLens(index, new MainPlayerInventoryLensImpl(index, slots, true), slots);
        }
        return null;
    }
    // For Crafting Result we need the Slot to get Filter logic
    if (subInventory instanceof InventoryCraftResult) {
        Slot slot = slotList.get(0);
        adapterLens = new CraftingOutputSlotLensImpl(index, item -> slot.isItemValid(((ItemStack) item)), itemType -> (slot.isItemValid((ItemStack) org.spongepowered.api.item.inventory.ItemStack.of(itemType, 1))));
        crafting.out = index;
        if (slot instanceof SlotCrafting) {
            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 = ((SlotCrafting) slot).craftMatrix;
            }
        }
    }
    if (subInventory instanceof InventoryCrafting) {
        crafting.base = index;
        crafting.grid = ((InventoryCrafting) subInventory);
    }
    return new DelegatingLens(index, adapterLens, slots);
}
Also used : IInventory(net.minecraft.inventory.IInventory) 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) MainPlayerInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.MainPlayerInventoryLensImpl) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) InventoryCraftResult(net.minecraft.inventory.InventoryCraftResult) InventoryCrafting(net.minecraft.inventory.InventoryCrafting) CraftingOutputSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.CraftingOutputSlotLensImpl) InventoryAdapter(org.spongepowered.common.item.inventory.adapter.InventoryAdapter) DefaultEmptyLens(org.spongepowered.common.item.inventory.lens.impl.DefaultEmptyLens) InputSlot(org.spongepowered.api.item.inventory.slot.InputSlot) Slot(net.minecraft.inventory.Slot) ItemStack(net.minecraft.item.ItemStack) DelegatingLens(org.spongepowered.common.item.inventory.lens.impl.DelegatingLens) SlotCrafting(net.minecraft.inventory.SlotCrafting) Nullable(javax.annotation.Nullable)

Example 2 with PlayerInventoryLens

use of org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens in project SpongeCommon by SpongePowered.

the class MixinInventoryPlayer method onConstructed.

@Inject(method = "<init>*", at = @At("RETURN"), remap = false)
private void onConstructed(EntityPlayer playerIn, CallbackInfo ci) {
    // Find offhand slot
    for (NonNullList<ItemStack> inventory : this.allInventories) {
        if (inventory == this.offHandInventory) {
            break;
        }
        this.offhandIndex += inventory.size();
    }
    // Set Carrier if we got a real Player
    if (playerIn instanceof EntityPlayerMP) {
        this.carrier = (Player) playerIn;
        this.inventory = new IInventoryFabric((IInventory) this);
        Class clazz = this.getClass();
        if (clazz == InventoryPlayer.class) {
            // Build Player Lens
            // We only care about Server inventories
            this.slots = new SlotCollection.Builder().add(this.mainInventory.size()).add(this.offHandInventory.size()).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.BOOTS)).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.LEGGINGS)).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.CHESTPLATE)).add(EquipmentSlotAdapter.class, index -> new EquipmentSlotLensImpl(index, i -> true, t -> true, e -> e == EquipmentTypes.HEADWEAR)).add(this.armorInventory.size() - 4, EquipmentSlotAdapter.class).add(this.getSizeInventory() - this.mainInventory.size() - this.offHandInventory.size() - this.armorInventory.size()).build();
            this.lens = new PlayerInventoryLens(this, this.slots);
        } else if (this.getSizeInventory() != 0) {
            // Fallback OrderedLens when not 0 sized inventory
            this.slots = new SlotCollection.Builder().add(this.getSizeInventory()).build();
            this.lens = new OrderedInventoryLensImpl(0, this.getSizeInventory(), 1, slots);
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) Inventory(org.spongepowered.api.item.inventory.Inventory) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) InventoryEventArgs(org.spongepowered.common.item.inventory.observer.InventoryEventArgs) Inject(org.spongepowered.asm.mixin.injection.Inject) EnumHand(net.minecraft.util.EnumHand) EquipmentInventory(org.spongepowered.api.item.inventory.equipment.EquipmentInventory) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Overwrite(org.spongepowered.asm.mixin.Overwrite) MainPlayerInventoryAdapter(org.spongepowered.common.item.inventory.adapter.impl.comp.MainPlayerInventoryAdapter) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) Mixin(org.spongepowered.asm.mixin.Mixin) NonNullList(net.minecraft.util.NonNullList) At(org.spongepowered.asm.mixin.injection.At) Fabric(org.spongepowered.common.item.inventory.lens.Fabric) SlotProvider(org.spongepowered.common.item.inventory.lens.SlotProvider) IInventoryFabric(org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric) Redirect(org.spongepowered.asm.mixin.injection.Redirect) EquipmentInventoryAdapter(org.spongepowered.common.item.inventory.adapter.impl.comp.EquipmentInventoryAdapter) EquipmentSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.EquipmentSlotLensImpl) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) Slot(org.spongepowered.api.item.inventory.Slot) Final(org.spongepowered.asm.mixin.Final) EquipmentSlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.EquipmentSlotAdapter) HotbarAdapter(org.spongepowered.common.item.inventory.adapter.impl.comp.HotbarAdapter) List(java.util.List) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IInventory(net.minecraft.inventory.IInventory) Lens(org.spongepowered.common.item.inventory.lens.Lens) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) SlotCollection(org.spongepowered.common.item.inventory.lens.impl.collections.SlotCollection) Player(org.spongepowered.api.entity.living.player.Player) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) SlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.SlotAdapter) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) SPacketHeldItemChange(net.minecraft.network.play.server.SPacketHeldItemChange) MainPlayerInventory(org.spongepowered.api.item.inventory.entity.MainPlayerInventory) IInventoryFabric(org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric) EquipmentSlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.EquipmentSlotAdapter) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) OrderedInventoryLensImpl(org.spongepowered.common.item.inventory.lens.impl.comp.OrderedInventoryLensImpl) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) EquipmentSlotLensImpl(org.spongepowered.common.item.inventory.lens.impl.slots.EquipmentSlotLensImpl) ItemStack(net.minecraft.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 3 with PlayerInventoryLens

use of org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens in project SpongeCommon by SpongePowered.

the class MixinSpongeUserInventory method onConstructed.

@Inject(method = "<init>*", at = @At("RETURN"), remap = false)
private void onConstructed(SpongeUser playerIn, CallbackInfo ci) {
    // We only care about Server inventories
    this.inventory = new IInventoryFabric((IInventory) this);
    this.slots = new SlotCollection.Builder().add(this.mainInventory.size()).add(this.offHandInventory.size()).add(this.armorInventory.size(), EquipmentSlotAdapter.class).add(this.getSizeInventory() - this.mainInventory.size() - this.offHandInventory.size() - this.armorInventory.size()).build();
    this.carrier = ((User) playerIn);
    this.lens = new PlayerInventoryLens(this, this.slots);
}
Also used : IInventory(net.minecraft.inventory.IInventory) IInventoryFabric(org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric) EquipmentSlotAdapter(org.spongepowered.common.item.inventory.adapter.impl.slots.EquipmentSlotAdapter) SpongeUser(org.spongepowered.common.entity.player.SpongeUser) User(org.spongepowered.api.entity.living.player.User) PlayerInventoryLens(org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

IInventory (net.minecraft.inventory.IInventory)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 ItemStack (net.minecraft.item.ItemStack)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 EquipmentSlotAdapter (org.spongepowered.common.item.inventory.adapter.impl.slots.EquipmentSlotAdapter)2 IInventoryFabric (org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric)2 PlayerInventoryLens (org.spongepowered.common.item.inventory.lens.impl.minecraft.PlayerInventoryLens)2 Multimap (com.google.common.collect.Multimap)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 EntityItem (net.minecraft.entity.item.EntityItem)1