Search in sources :

Example 41 with NonNullList

use of net.minecraft.util.NonNullList in project LogisticsPipes by RS485.

the class AssemblyTable method importRecipe.

@Override
public boolean importRecipe(TileEntity tile, IItemIdentifierInventory inventory) {
    if (!(tile instanceof TileAssemblyTable)) {
        return false;
    }
    TileAssemblyTable table = (TileAssemblyTable) tile;
    // current pipe inputs/outputs
    final ItemIdentifierInventory inputs = new ItemIdentifierInventory(inventory.getSizeInventory() - 2, "AssemblyTableDummyInv", 64, false);
    for (int i = 0; i < inventory.getSizeInventory() - 2; i++) {
        inputs.setInventorySlotContents(i, inventory.getIDStackInSlot(i));
    }
    final ItemStack output = inventory.getStackInSlot(inventory.getSizeInventory() - 2);
    // see if there's a recipe planned in the table that matches the current pipe settings, if yes take the next, otherwise take the first
    AssemblyRecipeBasic firstRecipe = null;
    AssemblyRecipeBasic nextRecipe = null;
    boolean takeNext = false;
    for (AssemblyRecipe r : AssemblyRecipeRegistry.REGISTRY.values()) {
        if (!(r instanceof AssemblyRecipeBasic)) {
            continue;
        }
        if (table.recipesStates.entrySet().stream().filter(it -> it.getKey().recipe == r).anyMatch(it -> it.getValue() != EnumAssemblyRecipeState.POSSIBLE)) {
            if (firstRecipe == null) {
                firstRecipe = (AssemblyRecipeBasic) r;
            }
            if (takeNext) {
                nextRecipe = (AssemblyRecipeBasic) r;
                break;
            }
            if (!output.isEmpty() && r.getOutputPreviews().stream().anyMatch(it -> ItemStack.areItemStacksEqual(output, it))) {
                if (!r.getOutputs(inputs.toNonNullList()).isEmpty()) {
                    takeNext = true;
                }
            }
        }
    }
    if (nextRecipe == null) {
        nextRecipe = firstRecipe;
    }
    if (nextRecipe == null) {
        return false;
    }
    // Import
    inventory.setInventorySlotContents(inventory.getSizeInventory() - 2, nextRecipe.getOutputPreviews().stream().findFirst().orElse(ItemStack.EMPTY));
    try {
        for (int i = 0; i < inventory.getSizeInventory() - 2; i++) {
            inventory.clearInventorySlotContents(i);
        }
        int i = 0;
        for (Object input : nextRecipe.getInputsFor(inventory.getStackInSlot(inventory.getSizeInventory() - 2))) {
            ItemStack processed = ItemStack.EMPTY;
            if (input instanceof String) {
                NonNullList<ItemStack> ores = OreDictionary.getOres((String) input);
                if (ores != null && ores.size() > 0) {
                    processed = ores.get(0);
                }
            } else if (input instanceof ItemStack) {
                processed = (ItemStack) input;
            } else if (input instanceof Item) {
                processed = new ItemStack((Item) input);
            } else if (input instanceof Block) {
                processed = new ItemStack((Block) input, 1, 0);
            } else if (input instanceof Integer) {
            // was null
            } else {
                throw new IllegalArgumentException("Unknown Object passed to recipe!");
            }
            if (!processed.isEmpty()) {
                inventory.setInventorySlotContents(i, processed);
                ++i;
            }
        }
    } catch (ClassCastException e) {
    // TODO: make it show a nice error or
    // remove this hack altogether.
    }
    inventory.getSlotAccess().compactFirst(9);
    return true;
}
Also used : IItemIdentifierInventory(network.rs485.logisticspipes.inventory.IItemIdentifierInventory) EnumAssemblyRecipeState(buildcraft.silicon.EnumAssemblyRecipeState) Item(net.minecraft.item.Item) AssemblyRecipe(buildcraft.api.recipes.AssemblyRecipe) ItemIdentifierInventory(logisticspipes.utils.item.ItemIdentifierInventory) TileAssemblyTable(buildcraft.silicon.tile.TileAssemblyTable) AssemblyRecipeBasic(buildcraft.api.recipes.AssemblyRecipeBasic) ItemStack(net.minecraft.item.ItemStack) Block(net.minecraft.block.Block) OreDictionary(net.minecraftforge.oredict.OreDictionary) AssemblyRecipeRegistry(buildcraft.lib.recipe.AssemblyRecipeRegistry) TileEntity(net.minecraft.tileentity.TileEntity) NonNullList(net.minecraft.util.NonNullList) ICraftingRecipeProvider(logisticspipes.proxy.interfaces.ICraftingRecipeProvider) IItemIdentifierInventory(network.rs485.logisticspipes.inventory.IItemIdentifierInventory) ItemIdentifierInventory(logisticspipes.utils.item.ItemIdentifierInventory) Item(net.minecraft.item.Item) AssemblyRecipeBasic(buildcraft.api.recipes.AssemblyRecipeBasic) TileAssemblyTable(buildcraft.silicon.tile.TileAssemblyTable) AssemblyRecipe(buildcraft.api.recipes.AssemblyRecipe) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack)

Example 42 with NonNullList

use of net.minecraft.util.NonNullList in project LogisticsPipes by RS485.

the class RecipeTransferHandler method transferRecipe.

@Nullable
@Override
public IRecipeTransferError transferRecipe(@Nonnull DummyContainer container, @Nonnull IRecipeLayout recipeLayout, @Nonnull EntityPlayer player, boolean maxTransfer, boolean doTransfer) {
    LogisticsBaseGuiScreen gui = container.guiHolderForJEI;
    if (gui instanceof GuiLogisticsCraftingTable || gui instanceof GuiRequestTable) {
        TileEntity tile;
        if (gui instanceof GuiLogisticsCraftingTable) {
            tile = ((GuiLogisticsCraftingTable) gui)._crafter;
        } else {
            tile = ((GuiRequestTable) gui)._table.container;
        }
        if (tile == null) {
            return recipeTransferHandlerHelper.createInternalError();
        }
        if (!recipeLayout.getRecipeCategory().getUid().equals(VanillaRecipeCategoryUid.CRAFTING)) {
            return recipeTransferHandlerHelper.createInternalError();
        }
        NEISetCraftingRecipe packet = PacketHandler.getPacket(NEISetCraftingRecipe.class);
        NonNullList<ItemStack> stackList = packet.getStackList();
        ItemStack[][] stacks = new ItemStack[9][];
        boolean hasCanidates = false;
        IGuiItemStackGroup guiItemStackGroup = recipeLayout.getItemStacks();
        Map<Integer, ? extends IGuiIngredient<ItemStack>> guiIngredients = guiItemStackGroup.getGuiIngredients();
        if (doTransfer) {
            for (Map.Entry<Integer, ? extends IGuiIngredient<ItemStack>> ps : guiIngredients.entrySet()) {
                if (!ps.getValue().isInput())
                    continue;
                int slot = ps.getKey() - 1;
                if (slot < 9) {
                    final ItemStack displayedIngredient = ps.getValue().getDisplayedIngredient();
                    stackList.set(slot, displayedIngredient == null ? ItemStack.EMPTY : displayedIngredient);
                    NonNullList<ItemStack> itemCandidateList = NonNullList.create();
                    // add all non-null non-empty ingredients to the itemCandidateList
                    ps.getValue().getAllIngredients().stream().filter(itemStack -> Objects.nonNull(itemStack) && !itemStack.isEmpty()).forEach(itemCandidateList::add);
                    if (!itemCandidateList.isEmpty()) {
                        Iterator<ItemStack> iter = itemCandidateList.iterator();
                        while (iter.hasNext()) {
                            ItemStack wildCardCheckStack = iter.next();
                            if (wildCardCheckStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
                                iter.remove();
                                final CreativeTabs creativeTab = wildCardCheckStack.getItem().getCreativeTab();
                                if (creativeTab != null) {
                                    NonNullList<ItemStack> secondList = NonNullList.create();
                                    wildCardCheckStack.getItem().getSubItems(creativeTab, secondList);
                                    itemCandidateList.addAll(secondList);
                                }
                                iter = itemCandidateList.iterator();
                            }
                        }
                        stacks[slot] = itemCandidateList.toArray(new ItemStack[0]);
                        if (stacks[slot].length > 1) {
                            hasCanidates = true;
                        } else if (stacks[slot].length == 1) {
                            stackList.set(slot, stacks[slot][0]);
                        }
                    }
                }
            }
            if (hasCanidates) {
                gui.setSubGui(new GuiRecipeImport(tile, stacks));
            } else {
                MainProxy.sendPacketToServer(packet.setTilePos(tile));
            }
        }
        return null;
    }
    return recipeTransferHandlerHelper.createInternalError();
}
Also used : DummyContainer(logisticspipes.utils.gui.DummyContainer) NEISetCraftingRecipe(logisticspipes.network.packets.NEISetCraftingRecipe) GuiRecipeImport(logisticspipes.gui.popup.GuiRecipeImport) MainProxy(logisticspipes.proxy.MainProxy) LogisticsBaseGuiScreen(logisticspipes.utils.gui.LogisticsBaseGuiScreen) ItemStack(net.minecraft.item.ItemStack) OreDictionary(net.minecraftforge.oredict.OreDictionary) Map(java.util.Map) IGuiIngredient(mezz.jei.api.gui.IGuiIngredient) CreativeTabs(net.minecraft.creativetab.CreativeTabs) NonNullList(net.minecraft.util.NonNullList) IRecipeTransferHandlerHelper(mezz.jei.api.recipe.transfer.IRecipeTransferHandlerHelper) Nonnull(javax.annotation.Nonnull) IRecipeLayout(mezz.jei.api.gui.IRecipeLayout) Nullable(javax.annotation.Nullable) IRecipeTransferError(mezz.jei.api.recipe.transfer.IRecipeTransferError) Iterator(java.util.Iterator) GuiRequestTable(logisticspipes.gui.orderer.GuiRequestTable) PacketHandler(logisticspipes.network.PacketHandler) IGuiItemStackGroup(mezz.jei.api.gui.IGuiItemStackGroup) GuiLogisticsCraftingTable(logisticspipes.gui.GuiLogisticsCraftingTable) Objects(java.util.Objects) Stream(java.util.stream.Stream) VanillaRecipeCategoryUid(mezz.jei.api.recipe.VanillaRecipeCategoryUid) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TileEntity(net.minecraft.tileentity.TileEntity) IRecipeTransferHandler(mezz.jei.api.recipe.transfer.IRecipeTransferHandler) GuiRecipeImport(logisticspipes.gui.popup.GuiRecipeImport) GuiLogisticsCraftingTable(logisticspipes.gui.GuiLogisticsCraftingTable) LogisticsBaseGuiScreen(logisticspipes.utils.gui.LogisticsBaseGuiScreen) IGuiItemStackGroup(mezz.jei.api.gui.IGuiItemStackGroup) CreativeTabs(net.minecraft.creativetab.CreativeTabs) TileEntity(net.minecraft.tileentity.TileEntity) GuiRequestTable(logisticspipes.gui.orderer.GuiRequestTable) NEISetCraftingRecipe(logisticspipes.network.packets.NEISetCraftingRecipe) ItemStack(net.minecraft.item.ItemStack) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Aggregations

NonNullList (net.minecraft.util.NonNullList)42 ItemStack (net.minecraft.item.ItemStack)37 List (java.util.List)13 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 Item (net.minecraft.item.Item)8 TileEntity (net.minecraft.tileentity.TileEntity)8 BlockPos (net.minecraft.util.math.BlockPos)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 ResourceLocation (net.minecraft.util.ResourceLocation)6 Iterator (java.util.Iterator)5 Map (java.util.Map)5 Nonnull (javax.annotation.Nonnull)5 Nullable (javax.annotation.Nullable)5 CreativeTabs (net.minecraft.creativetab.CreativeTabs)5 World (net.minecraft.world.World)5 Block (net.minecraft.block.Block)4 IBlockState (net.minecraft.block.state.IBlockState)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 RayTraceResult (net.minecraft.util.math.RayTraceResult)4