Jump to content

I'm trying to get an enchantment from a json file but it doesn't work (I'm making a mod)


PadFoot2008

Recommended Posts

I am trying to make a tile entity with 2 slots - top one will get a tool and bottom one will get a crystal. The tool will be enchanted based on the crystal and get placed in the top slot and the crystal will be deleted. The recipe is provided in I'm using Kaupenjoe's tutorial for this: 

Here's the original code by Kaupenjoe:

@Override
        public LightningChannelerRecipe read(ResourceLocation recipeId, JsonObject json) {
            ItemStack output = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(json, "output"));
            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

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

            return new LightningChannelerRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather));
        }

Here the Json file (original):

{
  "type": "immersivesurvival:enchanting",
  "weather": "clear",
  "ingredients" : [
    {
      "item": "minecraft:diamond_sword"
    },
    {
      "item": "immersivesurvival:fire_shard"
    }
  ],
    "output": {
    "item": "minecraft:diamond_shovel"
}}

This code works properly and output a diamond shovel, but of course that's not what I want to do.

I edited the code such that it gets the item from the first slot and the enchantment given in the Json and then enchants it with the enchantment and then returns it.

@Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "output");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));
            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            String rawtool = ingredients.get(0).getAsString();
            Item outputraw = ForgeRegistries.ITEMS.getValue(new ResourceLocation(rawtool));
            ItemStack output = new ItemStack(outputraw);
            output.addEnchantment(enchant,1);

            String weather = JSONUtils.getString(json, "weather");

            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

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

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather));
        }

And the recipe json:

{
  "type": "immersivesurvival:enchanting",
  "weather": "clear",
  "ingredients" : [
    {
      "item": "minecraft:diamond_sword"
    },
    {
      "item": "immersivesurvival:fire_shard"
    }
  ],
    "output": {
    "item": "Enchantments.FIRE_ASPECT"
}}

But it isn't working. Any help will be appreciated.

Link to comment
Share on other sites

It worked! I also edited the code a bit, and created a new field called enchantment in the recipe json. Thanks for all the help.

@Override
        public EnchantingPedestralRecipe read(ResourceLocation recipeId, JsonObject json) {
            String enchantraw = JSONUtils.getString(json, "enchantment");
            Enchantment enchant = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(enchantraw));

            ItemStack output = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(json, "output"));
            output.addEnchantment(enchant,1);

            String weather = JSONUtils.getString(json, "weather");

            JsonArray ingredients = JSONUtils.getJsonArray(json, "ingredients");
            NonNullList<Ingredient> inputs = NonNullList.withSize(2, Ingredient.EMPTY);

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

            return new EnchantingPedestralRecipe(recipeId, output,
                    inputs, Weather.getWeatherByString(weather));
        }

 

 

Edited by PadFoot2008
Mistakenly posted
Link to comment
Share on other sites

And the recipe json:

{
  "type": "immersivesurvival:enchanting",
  "weather": "clear",
  "enchantment": "minecraft:fire_aspect",
  "ingredients" : [
    {
      "item": "minecraft:diamond_sword"
    },
    {
      "item": "immersivesurvival:fire_shard"
    }
  ],
    "output": {
    "item": "minecraft:diamond_sword"
  }
}

Thanks again a lot!

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.