Search in sources :

Example 1 with ICraftingMatrixStackHandler

use of com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler in project artisan-worktables by codetaylor.

the class AWContainer method transferStackInSlot.

@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) {
    ItemStack itemStackCopy = ItemStack.EMPTY;
    Slot slot = this.inventorySlots.get(slotIndex);
    if (slot != null && slot.getHasStack()) {
        ItemStack itemStack = slot.getStack();
        itemStackCopy = itemStack.copy();
        if (this.tile.isLocked() && (this.isSlotIndexInventory(slotIndex) || this.isSlotIndexHotbar(slotIndex))) {
            // If the table is locked and the player has shift-clicked something in
            // the inventory or hotbar...
            ICraftingMatrixStackHandler craftingMatrixHandler = this.tile.getCraftingMatrixHandler();
            ICraftingMatrixStackHandler craftingMatrixHandlerGhost = this.tile.getCraftingMatrixHandlerGhost();
            int count = itemStack.getCount();
            for (int i = 0; i < count; i++) {
                List<Tuple> list = RoundRobinHelper.getSortedIndices(itemStack, craftingMatrixHandler, craftingMatrixHandlerGhost);
                if (list.isEmpty()) {
                    return ItemStack.EMPTY;
                }
                ItemStack copy = itemStack.copy();
                copy.setCount(1);
                int index = (int) list.get(0).getFirst();
                ItemStack result = craftingMatrixHandler.insertItem(index, copy, false);
                if (result.isEmpty()) {
                    itemStack.shrink(1);
                } else {
                    return ItemStack.EMPTY;
                }
            }
            return ItemStack.EMPTY;
        }
        if (this.isSlotIndexResult(slotIndex)) {
            // Result
            // This is executed on both the client and server for each craft. If the crafting
            // grid has multiple, complete recipes, this will be executed for each complete
            // recipe.
            IArtisanRecipe recipe = this.tile.getRecipe(playerIn);
            if (recipe == null) {
                return ItemStack.EMPTY;
            }
            if (recipe.hasMultipleWeightedOutputs()) {
                // it has been retrieved.
                return ItemStack.EMPTY;
            }
            if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
            itemStack.getItem().onCreated(itemStack, this.world, playerIn);
            slot.onSlotChange(itemStack, itemStackCopy);
        } else if (this.isSlotIndexInventory(slotIndex)) {
            if (this.swapTools(slotIndex)) {
                // swapped tools
                return ItemStack.EMPTY;
            }
            if (!this.mergeBlankPattern(itemStack, false) && !this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (this.isSlotIndexHotbar(slotIndex)) {
            if (this.swapTools(slotIndex)) {
                // swapped tools
                return ItemStack.EMPTY;
            }
            if (!this.mergeBlankPattern(itemStack, false) && !this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeInventory(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (this.isSlotIndexToolbox(slotIndex)) {
            if (this.swapTools(slotIndex)) {
                // swapped tools
                return ItemStack.EMPTY;
            }
            if (!this.mergeCraftingMatrix(itemStack, false) && !this.mergeSecondaryInput(itemStack, false) && !this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (this.isSlotIndexTool(slotIndex)) {
            if (this.canPlayerUseToolbox()) {
                if (!this.mergeToolbox(itemStack, false) && !this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
                return ItemStack.EMPTY;
            }
        } else if (!this.mergeInventory(itemStack, false) && !this.mergeHotbar(itemStack, false)) {
            // All others: crafting matrix, secondary output
            return ItemStack.EMPTY;
        }
        if (itemStack.isEmpty()) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }
        if (itemStack.getCount() == itemStackCopy.getCount()) {
            return ItemStack.EMPTY;
        }
        ItemStack itemStack2 = slot.onTake(playerIn, itemStack);
        if (slotIndex == 0) {
            playerIn.dropItem(itemStack2, false);
        }
    }
    return itemStackCopy;
}
Also used : ICraftingMatrixStackHandler(com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler) IArtisanRecipe(com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe) Slot(net.minecraft.inventory.Slot) ItemStack(net.minecraft.item.ItemStack) Tuple(net.minecraft.util.Tuple)

Example 2 with ICraftingMatrixStackHandler

use of com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler in project artisan-worktables by codetaylor.

the class CSPacketWorktableClear method clear.

public static void clear(TileEntityBase table, int clearFlags) {
    if ((clearFlags & CLEAR_FLUID) == CLEAR_FLUID) {
        FluidTank tank = table.getTank();
        tank.drain(tank.getCapacity(), true);
        ModuleWorktables.PACKET_SERVICE.sendToAllAround(new SCPacketWorktableFluidUpdate(table.getPos(), tank), table);
    }
    if ((clearFlags & CLEAR_GRID) == CLEAR_GRID) {
        ICraftingMatrixStackHandler craftingMatrixHandler = table.getCraftingMatrixHandler();
        for (int i = 0; i < craftingMatrixHandler.getSlots(); i++) {
            craftingMatrixHandler.setStackInSlot(i, ItemStack.EMPTY);
        }
    }
    if ((clearFlags & CLEAR_OUTPUT) == CLEAR_OUTPUT) {
        ItemStackHandler resultHandler = table.getResultHandler();
        resultHandler.setStackInSlot(0, ItemStack.EMPTY);
    }
    if ((clearFlags & CLEAR_TOOLS) == CLEAR_TOOLS) {
        ItemStackHandler toolHandler = table.getToolHandler();
        for (int i = 0; i < toolHandler.getSlots(); i++) {
            toolHandler.setStackInSlot(i, ItemStack.EMPTY);
        }
    }
    if ((clearFlags & CLEAR_EXTRA_OUTPUT) == CLEAR_EXTRA_OUTPUT) {
        ItemStackHandler secondaryOutputHandler = table.getSecondaryOutputHandler();
        for (int i = 0; i < secondaryOutputHandler.getSlots(); i++) {
            secondaryOutputHandler.setStackInSlot(i, ItemStack.EMPTY);
        }
    }
    if (table instanceof TileEntitySecondaryInputBase && (clearFlags & CLEAR_SECONDARY_INPUT) == CLEAR_SECONDARY_INPUT) {
        IItemHandlerModifiable secondaryIngredientHandler = ((TileEntitySecondaryInputBase) table).getSecondaryIngredientHandler();
        for (int i = 0; i < secondaryIngredientHandler.getSlots(); i++) {
            secondaryIngredientHandler.setStackInSlot(i, ItemStack.EMPTY);
        }
    }
}
Also used : FluidTank(net.minecraftforge.fluids.FluidTank) ItemStackHandler(net.minecraftforge.items.ItemStackHandler) ICraftingMatrixStackHandler(com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) TileEntitySecondaryInputBase(com.codetaylor.mc.artisanworktables.modules.worktables.tile.spi.TileEntitySecondaryInputBase)

Example 3 with ICraftingMatrixStackHandler

use of com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler in project artisan-worktables by codetaylor.

the class ZSRecipeExport method getExportString.

public static String getExportString(AWContainer container, TileEntityBase tileEntity, boolean shaped) {
    ICraftingMatrixStackHandler craftingMatrixHandler = tileEntity.getCraftingMatrixHandler();
    int width = craftingMatrixHandler.getWidth();
    int height = craftingMatrixHandler.getHeight();
    ItemStackHandler resultHandler = tileEntity.getResultHandler();
    ItemStackHandler secondaryOutputHandler = tileEntity.getSecondaryOutputHandler();
    FluidTank tank = tileEntity.getTank();
    // - start
    StringBuilder out = new StringBuilder();
    out.append("RecipeBuilder.get(\"").append(tileEntity.getType().getName()).append("\")\n");
    // -------------------------------------------------------------------------
    if (shaped) {
        out.append("  .setShaped([\n");
        int xMin = width, xMax = 0;
        int yMin = height, yMax = 0;
        for (int i = container.slotIndexCraftingMatrixStart; i <= container.slotIndexCraftingMatrixEnd; i++) {
            int x = (i - container.slotIndexCraftingMatrixStart) % width;
            int y = (i - container.slotIndexCraftingMatrixStart) / width;
            if (container.getSlot(i).getHasStack()) {
                if (x < xMin) {
                    xMin = x;
                }
                if (x > xMax) {
                    xMax = x;
                }
                if (y < yMin) {
                    yMin = y;
                }
                if (y > yMax) {
                    yMax = y;
                }
            }
        }
        int actualWidth = xMax - xMin + 1;
        int actualHeight = yMax - yMin + 1;
        ItemStack[] remappedGrid = new ItemStack[actualWidth * actualHeight];
        for (int i = container.slotIndexCraftingMatrixStart; i <= container.slotIndexCraftingMatrixEnd; i++) {
            int x = (i - container.slotIndexCraftingMatrixStart) % width;
            int y = (i - container.slotIndexCraftingMatrixStart) / height;
            if (x < xMin || x > xMax) {
                continue;
            }
            if (y < yMin || y > yMax) {
                continue;
            }
            Slot slot = container.getSlot(i);
            ItemStack stack = slot.getStack();
            int xMapped = x - xMin;
            int yMapped = y - yMin;
            remappedGrid[xMapped + yMapped * actualWidth] = stack;
        }
        for (int i = 0; i < remappedGrid.length; i++) {
            int x = i % actualWidth;
            int y = i / actualWidth;
            if (x == 0) {
                out.append("    [");
            }
            int containerIndex = container.slotIndexCraftingMatrixStart + (x + xMin) + (y + yMin) * width;
            String oreDict = tileEntity.oreDictMap.lookup(containerIndex);
            ItemStack stackInSlot = remappedGrid[i];
            if (oreDict == null) {
                ZSRecipeExport.getItemString(stackInSlot, out, true, false, false);
            } else {
                ZSRecipeExport.getItemStringOredict(oreDict, stackInSlot, out, true);
            }
            if (i == remappedGrid.length - 1) {
                // finished with the whole thing
                out.append("]])\n");
            } else if (x == actualWidth - 1) {
                // finished with the row
                out.append("],\n");
            } else {
                out.append(", ");
            }
        }
    } else {
        List<String> list = new ArrayList<>();
        for (int i = container.slotIndexCraftingMatrixStart; i <= container.slotIndexCraftingMatrixEnd; i++) {
            String oreDict = tileEntity.oreDictMap.lookup(i);
            ItemStack stackInSlot = container.getSlot(i).getStack();
            if (!stackInSlot.isEmpty()) {
                StringBuilder builder = new StringBuilder();
                if (oreDict == null) {
                    ZSRecipeExport.getItemString(stackInSlot, builder, true, false, false);
                } else {
                    ZSRecipeExport.getItemStringOredict(oreDict, stackInSlot, builder, true);
                }
                list.add(builder.toString());
            }
        }
        out.append("  .setShapeless([").append(String.join(", ", list)).append("])\n");
    }
    // -------------------------------------------------------------------------
    if (tank.getFluidAmount() > 0 && tank.getFluid() != null) {
        FluidStack fluid = tank.getFluid();
        String name = fluid.getFluid().getName();
        out.append("  .setFluid(<").append("liquid:").append(name).append("> * ").append(fluid.amount).append(")\n");
    }
    // -------------------------------------------------------------------------
    if (tileEntity instanceof TileEntitySecondaryInputBase) {
        IItemHandlerModifiable secondaryIngredientHandler = ((TileEntitySecondaryInputBase) tileEntity).getSecondaryIngredientHandler();
        int total = 0;
        int count = 0;
        for (int i = 0; i < secondaryIngredientHandler.getSlots(); i++) {
            if (!secondaryIngredientHandler.getStackInSlot(i).isEmpty()) {
                total += 1;
            }
        }
        if (total > 0) {
            out.append("  .setSecondaryIngredients([");
            for (int i = container.slotIndexSecondaryInputStart; i <= container.slotIndexSecondaryInputEnd; i++) {
                String oreDict = tileEntity.oreDictMap.lookup(i);
                ItemStack stackInSlot = container.getSlot(i).getStack();
                if (stackInSlot.isEmpty()) {
                    continue;
                }
                StringBuilder builder = new StringBuilder();
                if (oreDict == null) {
                    ZSRecipeExport.getItemString(stackInSlot, builder, false, false, false);
                } else {
                    ZSRecipeExport.getItemStringOredict(oreDict, stackInSlot, builder, false);
                }
                count += 1;
                out.append(builder.toString());
                out.append((count == total) ? "" : ", ");
            }
            out.append("])\n");
        }
    }
    // - addTool
    // -------------------------------------------------------------------------
    {
        for (int i = container.slotIndexToolsStart; i <= container.slotIndexToolsEnd; i++) {
            String oreDict = tileEntity.oreDictMap.lookup(i);
            ItemStack stackInSlot = container.getSlot(i).getStack();
            if (!stackInSlot.isEmpty()) {
                out.append("  .addTool(");
                StringBuilder builder = new StringBuilder();
                if (oreDict == null) {
                    ZSRecipeExport.getItemString(stackInSlot, builder, true, false, false);
                } else {
                    ZSRecipeExport.getItemStringOredict(oreDict, stackInSlot, builder, true);
                }
                out.append(builder.toString());
                out.append(", 1)\n");
            }
        }
    }
    // - setOutput
    // -------------------------------------------------------------------------
    {
        ItemStack stackInSlot = resultHandler.getStackInSlot(0);
        out.append("  .addOutput(");
        ZSRecipeExport.getItemString(stackInSlot, out, false, false, false);
        out.append(")\n");
    }
    // - setSecondaryOutput
    // -------------------------------------------------------------------------
    {
        int slot = 0;
        for (int i = 0; i < secondaryOutputHandler.getSlots(); i++) {
            ItemStack stackInSlot = secondaryOutputHandler.getStackInSlot(i);
            if (stackInSlot.isEmpty()) {
                continue;
            }
            if (slot == 0) {
                slot += 1;
                out.append("  .setExtraOutputOne(");
                ZSRecipeExport.getItemString(stackInSlot, out, false, false, false);
                out.append(", 1.0)\n");
            } else if (slot == 1) {
                slot += 1;
                out.append("  .setExtraOutputTwo(");
                ZSRecipeExport.getItemString(stackInSlot, out, false, false, false);
                out.append(", 1.0)\n");
            } else if (slot == 2) {
                slot += 1;
                out.append("  .setExtraOutputThree(");
                ZSRecipeExport.getItemString(stackInSlot, out, false, false, false);
                out.append(", 1.0)\n");
            }
        }
    }
    // - create
    // -------------------------------------------------------------------------
    out.append("  .create();");
    return out.toString();
}
Also used : ItemStackHandler(net.minecraftforge.items.ItemStackHandler) FluidStack(net.minecraftforge.fluids.FluidStack) TileEntitySecondaryInputBase(com.codetaylor.mc.artisanworktables.modules.worktables.tile.spi.TileEntitySecondaryInputBase) ArrayList(java.util.ArrayList) FluidTank(net.minecraftforge.fluids.FluidTank) ICraftingMatrixStackHandler(com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler) IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) Slot(net.minecraft.inventory.Slot) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ICraftingMatrixStackHandler (com.codetaylor.mc.artisanworktables.api.internal.recipe.ICraftingMatrixStackHandler)3 TileEntitySecondaryInputBase (com.codetaylor.mc.artisanworktables.modules.worktables.tile.spi.TileEntitySecondaryInputBase)2 Slot (net.minecraft.inventory.Slot)2 ItemStack (net.minecraft.item.ItemStack)2 FluidTank (net.minecraftforge.fluids.FluidTank)2 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)2 ItemStackHandler (net.minecraftforge.items.ItemStackHandler)2 IArtisanRecipe (com.codetaylor.mc.artisanworktables.api.recipe.IArtisanRecipe)1 ArrayList (java.util.ArrayList)1 Tuple (net.minecraft.util.Tuple)1 FluidStack (net.minecraftforge.fluids.FluidStack)1