Jump to content

[1.14.4] [SOLVED] How to get Recipes with IItemHandler


TheMikeste1

Recommended Posts

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!

Link to comment
Share on other sites

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.

  • Thanks 1

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.

Link to comment
Share on other sites

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

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