Jump to content

Recommended Posts

Posted (edited)

I am trying to make a custom crafting table with only my custom recipes.

I don't think I have the best way of going about it, but it seems to work, except when I place an item in the first slot in the crafting matrix (index 1), it appears in the output slot (index 0), also I cannot figure out how to stop players from putting items in the output slot.

Also, do I have to manually listen for the GUI render event and cancel the rendering of the hotbar, health, etc...

Current code:

public class CulinaryWorkbenchContainer extends Container {
    private CulinaryWorkbenchRecipe recipe;
    private World world;
    private BlockPos table;
    private InventoryCrafting matrix;
    private InventoryCraftResult result;
    private EntityPlayer player;
    private Boolean closed = true;

    CulinaryWorkbenchContainer(InventoryPlayer playerInventory, World world, BlockPos table) {
        world = world;
        table = table;
        matrix = new InventoryCrafting(this, 3, 3);
        player = playerInventory.player;

        result = new InventoryCraftResult();

        addSlotToContainer(new Slot(matrix, 0, 124, 35));
        for (int i = 0; i < 3; i++) {
            for (int j = 1; j < 4; j++) {
                addSlotToContainer(new Slot(matrix, (j - 1) + i * 3, 30 + (j - 1) * 18, 17 + i * 18));
            }
        }

        for (int k = 0; k < 3; k++) {
            for (int i = 1; i < 10; i++) {
                addSlotToContainer(new Slot(playerInventory, (i - 1) + k * 9 + 9, 8 + (i - 1) * 18, 84 + k * 18));
            }
        }

        for (int i = 1; i < 10; i++) {
            addSlotToContainer(new Slot(playerInventory, i - 1, 8 + (i - 1) * 18, 142));
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
        BlockPos pos = playerIn.rayTrace(6, 1.0f).getBlockPos();
        World world = playerIn.getEntityWorld();
        Block block = world.getBlockState(pos).getBlock();
        Boolean equals = block.equals(BlockRegistry.blocks[0]);
        double distance = playerIn.getDistanceSq(pos);
        Boolean equals2 = distance <= 8 * 8;
        return equals && equals2;
    }

    @Override
    public void onContainerClosed(EntityPlayer playerIn) {
        super.onContainerClosed(playerIn);

        if (!world.isRemote) {
            for (int i = 0; i < 9; ++i) {
                ItemStack itemstack = matrix.removeStackFromSlot(i);

                if (!itemstack.isEmpty()) {
                    playerIn.dropItem(itemstack, false);
                }
            }
        }
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = inventorySlots.get(index);

        if (slot != null && slot.getHasStack()) {
            ItemStack currentItem = slot.getStack();
            itemstack = currentItem.copy();

            if (index == 0) {
                if (!getSlot(0).equals(ItemStack.EMPTY)) {
                    currentItem.getItem().onCreated(currentItem, world, playerIn);
                    onPickupFromSlot(recipe);
                    slot.onSlotChange(currentItem, itemstack);
                }
            } else if (index >= 11 && index < 38) {
                if (!mergeItemStack(currentItem, 37, 46, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (index >= 38 && index < 46) {
                if (!mergeItemStack(currentItem, 10, 37, false)) {
                    return ItemStack.EMPTY;
                }
                if (recipe != null)
                    putStackInSlot(0, recipe.output);
            } else if (!mergeItemStack(currentItem, 10, 46, false)) {
                return ItemStack.EMPTY;
            }

            if (currentItem.isEmpty()) {
                slot.putStack(ItemStack.EMPTY);
            } else {
                slot.onSlotChanged();
            }

            if (currentItem.getCount() == itemstack.getCount()) {
                return ItemStack.EMPTY;
            }
            slot.onTake(playerIn, currentItem);
            if (index == 0) {
                playerIn.dropItem(currentItem, false);
            }
        }

        return itemstack;
    }

    @Override
    public boolean canMergeSlot(ItemStack stack, Slot slotIn) {
        return slotIn.slotNumber != 0 && super.canMergeSlot(stack, slotIn);
    }

    @Override
    public void onCraftMatrixChanged(IInventory in) {
        Boolean recipeFound = false;
        for (CulinaryWorkbenchRecipe recipe : CulinaryCraft.recipes) {
            boolean found = true;
            if (recipe.shaped) {
                for (int i = 0; i < 9; i++) {
                    for (ItemStack ingredient : OreDictionary.getOres(recipe.ingredients[i])) {
                        if (!found) break;
                        ItemStack item = matrix.getStackInSlot(i);
                        if (ingredient.getItem() != item.getItem()) found = false;
                        if (ingredient.getCount() > item.getCount()) found = false;
                    }
                    if (!found) break;
                }
            } else {
                for (String sIngredient : recipe.ingredients) {
                    int addedAmt = 0;
                    for (int i = 0; i < 9; i++) {
                        for (ItemStack ingredient : OreDictionary.getOres(sIngredient)) {
                            ItemStack item = matrix.getStackInSlot(i);
                            if (ingredient.getItem().equals(item.getItem()) && item.getCount() == ingredient.getCount()) {
                                addedAmt += 1;
                            }
                        }
                    }
                    if (addedAmt != recipe.ingredients.length) {
                        found = false;
                    }
                }
            }
            if (found) {
                recipeFound = true;
                result.setInventorySlotContents(0, recipe.output);
                recipe = recipe;
            }
        }
        if(!recipeFound) {
            recipe = null;
        }
    }

    private void onPickupFromSlot(CulinaryWorkbenchRecipe recipe) {
        for (int i = 0; i < matrix.getSizeInventory(); i++) {
            ItemStack currentItem = matrix.getStackInSlot(i);

            if (currentItem != null) {
                NonNullList<ItemStack> ores = OreDictionary.getOres(recipe.ingredients[i]);
                for (int i2 = 0; i < ores.size(); i++) {
                    if (matrix.getStackInSlot(i).getItem() == ores.get(i2).getItem()) {
                        matrix.decrStackSize(i, recipe.amounts[i2]);
                    }
                }
                if (currentItem.getItem().hasContainerItem(currentItem)) {
                    ItemStack containerItem = currentItem.getItem().getContainerItem(currentItem);

                    if (containerItem.isItemStackDamageable() && containerItem.getItemDamage() > containerItem.getMaxDamage()) {
                        MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, containerItem, EnumHand.MAIN_HAND));
                        containerItem = null;
                    }

                    if (containerItem != null || !player.inventory.addItemStackToInventory(containerItem)) {
                        if (matrix.getStackInSlot(i) == null) {
                            matrix.setInventorySlotContents(i, containerItem);
                        } else {
                            player.dropItem(containerItem, false);
                        }
                    }
                }
            }
        }
    }
}

Recipe class:

public class CulinaryWorkbenchRecipe {

    public String[] ingredients;
    public int[] amounts;
    public ItemStack output;
    public Boolean shaped;

    public CulinaryWorkbenchRecipe(String[] ingredients, int[] amounts, ItemStack output, Boolean shaped) {
        this.ingredients = ingredients;
        this.output = output;
        this.amounts = amounts;
        this.shaped = shaped;
    }
}

I don't think much else is needed.

(I plan to add recipes to a list in my main class, and add shaped/shapeless ore dict. recipes. I know I can have a class extend IRecipe but I don't think I need to)

Edited by Big_Bad_E
Posted (edited)

Update: My problem was the output slot and the first slot both had an index of 0. I solved this by making an index variable and increasing it by 1 when I add a slot. Also, I made it to where I get the GUI's name from the lang, so yeah.

 

Edit: Also, to my problem of not letting people put items in the output slot, I just made an extension of Slot, and overrided isItemValid().

Edited by Big_Bad_E
Posted

This stuff got really painful for me (I was however trying to save the items to another inventory)

hope my code helps you

https://github.com/Cadiboo/WIPTech/blob/master/src/main/java/cadiboo/wiptech/inventory/ContainerAssemblyTable.java

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.