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've been using an IItemHandler (which, according to the Forge docs, we're supposed to use instead of IInventory) to handle the inventory of one of the processing blocks in my mod. However, I'm not sure how to get recipes with it. The Minecraft RecipeManager requires an IInventory as the second parameter of #getRecipe(), and I couldn't find a Forge RecipeManager nor a different method that would take an IItemHandler and return a recipe.

 

Does anyone know how I can get recipes using my IItemHandler?

 

Just for reference, here's what I'm trying to do:

public class GrinderTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {
    private LazyOptional<IItemHandler> itemHandler = LazyOptional.of(this::createItemHandler);
    private LazyOptional<IEnergyStorage> energyHandler = LazyOptional.of(this::createEnergyHandler);
    private int counter = 0;

    public GrinderTileEntity() {
        super(TileEntityTypes.grinder);
    }

    @Override
    public void tick() {
        if (world.isRemote)
            return;

        //Nothing to do if we have no energy
        AtomicBoolean hasEnergy = new AtomicBoolean();
        energyHandler.ifPresent( e -> hasEnergy.set(e.getEnergyStored() > 0) );
        if (!hasEnergy.get())
            return;

        //Nothing to do if there isn't an item.
        AtomicReference<ItemStack> stack = new AtomicReference<>();
        itemHandler.ifPresent( h -> stack.set(h.getStackInSlot(0).copy()) );
        if(stack.get().isEmpty())
            return;

        AtomicReference<IItemHandler> inventory = new AtomicReference<>();
        itemHandler.ifPresent(inventory::set);

        //#getRecipe() is where it breaks, specifically inventory.get()
        IRecipe<?> recipe = world.getRecipeManager()
                .getRecipe(RecipeTypes.GRINDING, inventory.get(), world)
                .orElse(null);
	
    	//processing...
    }
  
   /* ************************************************
    * Used to create the IItemHandler for itemHandler
    ***************************************************/
	private IItemHandler createItemHandler() {
        return new ItemStackHandler(2) {
            @Override
            public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
                return ItemTags.getCollection()
                        .get(ResourceLocation.tryCreate("forge:ores"))
                        .contains(stack.getItem())
                        && slot == 0;
            }

            @Nonnull
            @Override
            public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
                if (slot != 0
                        || !ItemTags.getCollection()
                        .get(ResourceLocation.tryCreate("forge:ores"))
                        .contains(stack.getItem())
                ) {
                    return stack;
                }
                return super.insertItem(slot, stack, simulate);
            }

            @Override
            protected void onContentsChanged(int slot) {
                markDirty();
            }
        };
    }

  //other functions...
}

 

Thanks!

9 minutes ago, TheMikeste1 said:

Does anyone know how I can get recipes using my IItemHandler?

Create a RecipeWrapper it takes in a IItemHandlerModifiable and it's an IInventory.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author
Just now, Animefan8888 said:

Create a RecipeWrapper it takes in a IItemHandlerModifiable and it's an IInventory.

Oh, awesome. I didn't notice that class. Thanks!

  • TheMikeste1 changed the title to [1.14.4] [SOLVED] How to get Recipes with IItemHandler

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.