Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16] How can I get Crafting Recipes in my Tile Entity


kiou.23
 Share

Recommended Posts

I'm trying to make a tile entity that would have a superset of the Crafting Table functionality. However the Crafting Table doesn't have a Tile Entity, and I'm having a hard time trying to get the Crafting Matrix. The CraftingInventory class requires a container to be passed in, so I tried using an ItemStackHandler, but the world.getRecipeManager().getRecipe() method requires a Crafting Inventory to be passed in to find the recipe. And I just can't see how to get through this.

any help?

Link to comment
Share on other sites

Howdy.

Is there any reason you can't create your own IInventory?

Or a Container to wrap your ItemStackHandler?

For example like this:

https://github.com/TheGreyGhost/MinecraftByExample/blob/master/src/main/java/minecraftbyexample/mbe30_inventory_basic/ChestContents.java

or this:

https://github.com/TheGreyGhost/MinecraftByExample/blob/master/src/main/java/minecraftbyexample/mbe30_inventory_basic/ContainerBasic.java

 

You might also find it helpful to make a copy of the vanilla code and refactor it (change the obfuscated method and variable names to something meaningful) to help you understand how it works and perhaps find a shortcut if you don't need the full CraftingInventory functionality.

 

-TGG

 

 

 

 

 

Link to comment
Share on other sites

13 minutes ago, TheGreyGhost said:

Howdy.

Is there any reason you can't create your own IInventory?

Or a Container to wrap your ItemStackHandler?

It need to be able to craft any crafting recipe as well, so I would need to use the getRecipe() method, passing IRecipeType.CRAFTING

but then the IInventory parameter needs to be of CraftingInventory

or is there a better way to get the recipes?

 

this is my current code:

public class ModWorkbenchTileEntity extends TileEntity implements INamedContainerProvider {

    final ItemStackHandler craftMatrix = new ItemStackHandler(9);

    public ModWorkbenchTileEntity() {
        super(ModTileEntityTypes.MOD_WORKBENCH.get());
    }

    private Optional<ICraftingRecipe> getRecipe() {
        ItemStack[] stacks = new ItemStack[9];
        for (int i = 0; i < craftMatrix.getSlots(); i++)
            stacks[i] = (craftMatrix.getStackInSlot(i));
        Inventory inv = new Inventory(stacks);

      	// This errors because inv needs to be a CraftingInventory
        return world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, inv, world);
    }


  	//this is unimplemented and isn't relevant to the issue
    @Override
    public ITextComponent getDisplayName() {
        return null;
    }

    @Override
    public Container createMenu(int windowId, PlayerInventory playerInv, PlayerEntity player) {
        return null;
    }
}

 

Link to comment
Share on other sites

2 hours ago, diesieben07 said:

You can make a subclass of CraftingInventory that overrides all methods to delegate to your IItemHandler instead.

like so?

public class ModWorkbenchInventory extends CraftingInventory {

    private final ModWorkbenchItemStackHandler itemStackHandler;

    public ModWorkbenchInventory(ModWorkbenchItemStackHandler itemStackHandler) {
        super(null, 3, 3);

        this.itemStackHandler = itemStackHandler;
    }

    @Override public int getHeight() { return 3; }

    @Override public int getWidth() { return 3; }

    @Override public ItemStack getStackInSlot(int index) { return itemStackHandler.getStackInSlot(index); }

    //

    @Override public int getSizeInventory() { return itemStackHandler.getSlots(); }

    @Override
    public boolean isEmpty() {
        for(int slot = 0; slot < itemStackHandler.getSlots(); slot++) {
            if (!itemStackHandler.getStackInSlot(slot).isEmpty()) {
                return false;
            }
        }

        return true;
    }

    @Override public ItemStack removeStackFromSlot(int index) { return ItemStack.EMPTY; }

    @Override public ItemStack decrStackSize(int index, int count) { return ItemStack.EMPTY; }

    @Override public void setInventorySlotContents(int index, ItemStack stack) {}

    @Override public void markDirty() {}

    @Override public void clear() {}

    @Override public void fillStackedContents(RecipeItemHelper helper) {}
}

 

public class ModWorkbenchItemStackHandler extends ItemStackHandler {

    public ModWorkbenchItemStackHandler() {
        super(9);
    }

    @Override
    public boolean isItemValid(int slot, ItemStack stack) {
        if (slot < 0 || slot >= 9)
            throw new IllegalArgumentException("Invalid slot number: " + slot);

        if (stack.isEmpty()) return false;

        return !(stack.getItem() instanceof BackpackItem);
    }
}

 

public class ModWorkbenchTileEntity extends TileEntity implements INamedContainerProvider {

    final ModWorkbenchItemStackHandler craftMatrix = new ModWorkbenchItemStackHandler();

    public ModWorkbenchTileEntity() {
        super(ModTileEntityTypes.MOD_WORKBENCH.get());
    }

    private Optional<ICraftingRecipe> getRecipe() {
        ItemStack[] stacks = new ItemStack[9];
        for (int i = 0; i < craftMatrix.getSlots(); i++)
            stacks[i] = (craftMatrix.getStackInSlot(i));

        return world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, new ModWorkbenchInventory(craftMatrix), world);
    }


    @Override
    public ITextComponent getDisplayName() {
        return null;
    }

    @Override
    public Container createMenu(int windowId, PlayerInventory playerInv, PlayerEntity player) {
        return null;
    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.