Jump to content

Block Entity recipes not working


BlueFFlame

Recommended Posts

Hello!

I'm writing a Minecraft based on tech and machines, and I'm making a furnace-esque machine for processing ores. However, I can't seem to get the recipes to work. Whenever I put the ore into the slot, nothing happens. I added the inventory property to the watchlist and it showed that it was detecting the block in the right slot, but for some reason its not triggering crafting. I'm at a loss right now. Please note that I am new to modding, and I followed a tutorial for this code, so I don't really know code conventions yet. 

Also, there's no crashes at all. No errors thrown. It just doesn't do anything.

Below is some of the associated code:

1) The .json files for the recipes

{
  "type": "darkcollective:ore_refining",
  "ingredients": [
    {
      "item": "darkcollective:aernium_ore"
    }
  ],
  "output": {
    "count": 2,
    "item": "darkcollective:aernium_ingot"
  }
}

2) The code in the OreRefineryEntityClass that looks for the recipe and, if found, starts the crafting

private boolean hasRecipe() {
        Optional<OreRefineryRecipe> recipe = getCurrentRecipe();

        System.out.println(recipe);

        if(recipe.isEmpty()) {
            return false;
        }
        ItemStack result = recipe.get().getResultItem(getLevel().registryAccess());

        return canInsertAmountIntoOutputSlot(result.getCount()) && canInsertItemIntoOutputSlot(result.getItem());
    }

    private Optional<OreRefineryRecipe> getCurrentRecipe() {
        SimpleContainer inventory = new SimpleContainer(this.itemHandler.getSlots());
        for(int i = 0; i < itemHandler.getSlots(); i++) {
            inventory.setItem(i, this.itemHandler.getStackInSlot(i));
        }

        return this.level.getRecipeManager().getRecipeFor(OreRefineryRecipe.Type.INSTANCE, inventory, level);
    }

3) The code for the RecipeSerializer.

    public static class Serializer implements RecipeSerializer<OreRefineryRecipe>{

        public static final Serializer INSTANCE = new Serializer();
        public static final ResourceLocation ID = new ResourceLocation(DarkCollective.ModID, "ore_refining");

        @Override
        public OreRefineryRecipe fromJson(ResourceLocation recipeId, JsonObject serializedRecipe) {
            ItemStack output = ShapedRecipe.itemStackFromJson(GsonHelper.getAsJsonObject(serializedRecipe, "result"));

            JsonArray ingredients = GsonHelper.getAsJsonArray(serializedRecipe, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(1, Ingredient.EMPTY);

            for(int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.fromJson(ingredients.get(i)));
            }

            return new OreRefineryRecipe(inputs, output, recipeId);
        }

        @Override
        public @Nullable OreRefineryRecipe fromNetwork(ResourceLocation recipeID, FriendlyByteBuf buffer) {
            NonNullList<Ingredient> inputs = NonNullList.withSize(buffer.readInt(), Ingredient.EMPTY);

            for(int i = 0; i < inputs.size(); i++) {
                inputs.set(i, Ingredient.fromNetwork(buffer));
            }

            ItemStack output = buffer.readItem();
            return new OreRefineryRecipe(inputs, output, recipeID);
        }

        @Override
        public void toNetwork(FriendlyByteBuf buffer, OreRefineryRecipe recipe) {
            buffer.writeInt(recipe.inputItems.size());

            for (Ingredient ingredient : recipe.getIngredients()) {
                ingredient.toNetwork(buffer);
            }

            buffer.writeItemStack(recipe.getResultItem(null), false);

        }
    }

If there's anything I missed, just tell me and I'd be happy to send.

Also, I'm on version 1.20.

Thank you!

Edited by BlueFFlame
Clarification
Link to comment
Share on other sites

5 hours ago, BlueFFlame said:

Yeah, I’ve looked at it in the debugger. There’s no recipe found, as the getRecipeMethod is returning null. I’m not sure why though, as the inventory isn’t null: it’s detecting the correct item in the correct slot. 

RecipeManager#getRecipeFor uses OreRefineryRecipe#matches to enure the items in the container are valid for the given SimpleContainer are you sure the implemention is correct since you have a NonNullList as input but you only use the first item of the list?

Link to comment
Share on other sites

2 hours ago, Luis_ST said:

RecipeManager#getRecipeFor uses OreRefineryRecipe#matches to enure the items in the container are valid for the given SimpleContainer are you sure the implemention is correct since you have a NonNullList as input but you only use the first item of the list?

Hmm, not sure. I can check with the tutorial. Again, I'm not 100% sure as to how the whole process functions, as the tutorial didn't really explain well. From what I understand, the recipes are gathered from the Json files and stored in the NonNullList.

Link to comment
Share on other sites

12 hours ago, Luis_ST said:

RecipeManager#getRecipeFor uses OreRefineryRecipe#matches to enure the items in the container are valid for the given SimpleContainer are you sure the implemention is correct since you have a NonNullList as input but you only use the first item of the list?

Just checked with the tutorial, they’re doing the same thing. From what I understand, the non-null list is just storing the item that is in the input slot (in this case, the SimpleContainer slot 0). I can send the link to the tutorial GitHub: https://github.com/Tutorials-By-Kaupenjoe/Forge-Tutorial-1.20.X/tree/31-recipeTypes

Link to comment
Share on other sites

27 minutes ago, Luis_ST said:

I cloned your git repo to debug your mod locally on my PC, but it seems to be broken.
The project is missing some important gradle files because you are using a java .gitignore file, you should use the file which comes with the mdk.

Ok, I just changed the .gitignore. Not sure if that also added the gradle files you needed, but it added something. If it doesn't, I can just send you the files that you need manually.

Link to comment
Share on other sites

On 11/8/2023 at 4:24 PM, Luis_ST said:

I am still not able to import your project into my IDE, I would recommend you to remove all files from github that can be found in the .gitignore file. Iedally you create a new branch.

Ah, I realized my response didn't send. Would you be able to just copy and paste the necessary files into a new project? I didn't change much in my gradle files, basically just specifying the mod id (which can be seen in the main class) and the version of forge (40.0.14). If you're unable to do that, that's fine, but I may need some help with creating a new branch and working with Git since my Git expertise really only go up to branch merging.

Link to comment
Share on other sites

5 hours ago, BlueFFlame said:

Ah, I realized my response didn't send. Would you be able to just copy and paste the necessary files into a new project?

That would work, but note the correct .gitignore must be part of the first commit.

I would recommend you to learn the basics of Git, it is very helpful for a programmer.

Link to comment
Share on other sites

8 minutes ago, Luis_ST said:

That would work, but note the correct .gitignore must be part of the first commit.

I would recommend you to learn the basics of Git, it is very helpful for a programmer.

Yeah, it's been on my list of things to learn. As of now, I haven't really had a need to create branches or do anything more than commiting, pushing and pulling. I'll probably create a new branch for the .gitignore; I could just look at how to do it.

Link to comment
Share on other sites

On 11/10/2023 at 10:47 PM, Luis_ST said:

That would work, but note the correct .gitignore must be part of the first commit.

I would recommend you to learn the basics of Git, it is very helpful for a programmer.

How's debugging going? Have you figured a root cause? I had an idea of what it was (recipe returning null), but I couldn't figure out why it was doing that. If you haven't had time to work on it don't worry, I'm not really in a time rush.

Link to comment
Share on other sites

Sorry, I completely forgot about it.

First of all, there is a problem in your recipes. You load a property "result" from your recipes in the Serialiser, but in the recipe you specify "ouput". (Read your logs!)

Secondly, you have two ItemStackHandlers in your BlockEntity, you should use the one from the Capability.
Remove the ItemStackHandler named 'itemHandler' and the problem should be fixed.

It worked for me.

Link to comment
Share on other sites

On 11/17/2023 at 10:35 PM, Luis_ST said:

Sorry, I completely forgot about it.

First of all, there is a problem in your recipes. You load a property "result" from your recipes in the Serialiser, but in the recipe you specify "ouput". (Read your logs!)

Secondly, you have two ItemStackHandlers in your BlockEntity, you should use the one from the Capability.
Remove the ItemStackHandler named 'itemHandler' and the problem should be fixed.

It worked for me.

Oh my god, thank you so much! It works perfectly now. Of course it was a typo. Was there anything else I should change in my code, to keep up with conventions and make the game run better? Again, I'm new to modding, so I don't know a lot about conventions so I'd like to learn.

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