Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

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

 

 

 

 

 

  • Author
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;
    }
}

 

  • Author
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;
    }
}

 

  • Author
Just now, diesieben07 said:

Looks right, although getDisplayName and createMenu should not return null.

Yeah, I haven't implemented those methods yet

Just wanted to solve the crafting Inventory problem

Thanks

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.