From bcdd44aece5271c1f8e2914bacf0ba065be3843b Mon Sep 17 00:00:00 2001 From: LexManos Date: Wed, 19 Jul 2023 00:02:11 -0700 Subject: [PATCH] Replace string with forge tag in vanilla recipes. Closes #9062 --- .../resources/data/minecraft/recipes/bow.json | 2 +- .../data/minecraft/recipes/candle.json | 20 +++++ .../data/minecraft/recipes/crossbow.json | 2 +- .../data/minecraft/recipes/fishing_rod.json | 2 +- .../data/minecraft/recipes/lead.json | 22 ++++++ .../data/minecraft/recipes/loom.json | 20 +++++ .../data/minecraft/recipes/scaffolding.json | 22 ++++++ .../common/data/ForgeRecipeProvider.java | 78 ++++++++----------- 8 files changed, 121 insertions(+), 47 deletions(-) create mode 100644 src/generated/resources/data/minecraft/recipes/candle.json create mode 100644 src/generated/resources/data/minecraft/recipes/lead.json create mode 100644 src/generated/resources/data/minecraft/recipes/loom.json create mode 100644 src/generated/resources/data/minecraft/recipes/scaffolding.json diff --git a/src/generated/resources/data/minecraft/recipes/bow.json b/src/generated/resources/data/minecraft/recipes/bow.json index 886def250b..f195d39c30 100644 --- a/src/generated/resources/data/minecraft/recipes/bow.json +++ b/src/generated/resources/data/minecraft/recipes/bow.json @@ -6,7 +6,7 @@ "tag": "forge:rods/wooden" }, "X": { - "item": "minecraft:string" + "tag": "forge:string" } }, "pattern": [ diff --git a/src/generated/resources/data/minecraft/recipes/candle.json b/src/generated/resources/data/minecraft/recipes/candle.json new file mode 100644 index 0000000000..442f62aa0a --- /dev/null +++ b/src/generated/resources/data/minecraft/recipes/candle.json @@ -0,0 +1,20 @@ +{ + "type": "minecraft:crafting_shaped", + "category": "misc", + "key": { + "H": { + "item": "minecraft:honeycomb" + }, + "S": { + "tag": "forge:string" + } + }, + "pattern": [ + "S", + "H" + ], + "result": { + "item": "minecraft:candle" + }, + "show_notification": true +} \ No newline at end of file diff --git a/src/generated/resources/data/minecraft/recipes/crossbow.json b/src/generated/resources/data/minecraft/recipes/crossbow.json index f98bd2e160..948585b353 100644 --- a/src/generated/resources/data/minecraft/recipes/crossbow.json +++ b/src/generated/resources/data/minecraft/recipes/crossbow.json @@ -12,7 +12,7 @@ "tag": "forge:ingots/iron" }, "~": { - "item": "minecraft:string" + "tag": "forge:string" } }, "pattern": [ diff --git a/src/generated/resources/data/minecraft/recipes/fishing_rod.json b/src/generated/resources/data/minecraft/recipes/fishing_rod.json index f976d015a8..f28e75d948 100644 --- a/src/generated/resources/data/minecraft/recipes/fishing_rod.json +++ b/src/generated/resources/data/minecraft/recipes/fishing_rod.json @@ -6,7 +6,7 @@ "tag": "forge:rods/wooden" }, "X": { - "item": "minecraft:string" + "tag": "forge:string" } }, "pattern": [ diff --git a/src/generated/resources/data/minecraft/recipes/lead.json b/src/generated/resources/data/minecraft/recipes/lead.json new file mode 100644 index 0000000000..ddb73eba15 --- /dev/null +++ b/src/generated/resources/data/minecraft/recipes/lead.json @@ -0,0 +1,22 @@ +{ + "type": "minecraft:crafting_shaped", + "category": "equipment", + "key": { + "O": { + "item": "minecraft:slime_ball" + }, + "~": { + "tag": "forge:string" + } + }, + "pattern": [ + "~~ ", + "~O ", + " ~" + ], + "result": { + "count": 2, + "item": "minecraft:lead" + }, + "show_notification": true +} \ No newline at end of file diff --git a/src/generated/resources/data/minecraft/recipes/loom.json b/src/generated/resources/data/minecraft/recipes/loom.json new file mode 100644 index 0000000000..6001218dda --- /dev/null +++ b/src/generated/resources/data/minecraft/recipes/loom.json @@ -0,0 +1,20 @@ +{ + "type": "minecraft:crafting_shaped", + "category": "misc", + "key": { + "#": { + "tag": "minecraft:planks" + }, + "@": { + "tag": "forge:string" + } + }, + "pattern": [ + "@@", + "##" + ], + "result": { + "item": "minecraft:loom" + }, + "show_notification": true +} \ No newline at end of file diff --git a/src/generated/resources/data/minecraft/recipes/scaffolding.json b/src/generated/resources/data/minecraft/recipes/scaffolding.json new file mode 100644 index 0000000000..a32e956638 --- /dev/null +++ b/src/generated/resources/data/minecraft/recipes/scaffolding.json @@ -0,0 +1,22 @@ +{ + "type": "minecraft:crafting_shaped", + "category": "misc", + "key": { + "I": { + "item": "minecraft:bamboo" + }, + "~": { + "tag": "forge:string" + } + }, + "pattern": [ + "I~I", + "I I", + "I I" + ], + "result": { + "count": 6, + "item": "minecraft:scaffolding" + }, + "show_notification": true +} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/common/data/ForgeRecipeProvider.java b/src/main/java/net/minecraftforge/common/data/ForgeRecipeProvider.java index bcde2aa192..2c6733106d 100644 --- a/src/main/java/net/minecraftforge/common/data/ForgeRecipeProvider.java +++ b/src/main/java/net/minecraftforge/common/data/ForgeRecipeProvider.java @@ -29,34 +29,37 @@ import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; -import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; -public final class ForgeRecipeProvider extends VanillaRecipeProvider -{ +public final class ForgeRecipeProvider extends VanillaRecipeProvider { private final Map> replacements = new HashMap<>(); private final Set excludes = new HashSet<>(); - public ForgeRecipeProvider(PackOutput packOutput) - { + public ForgeRecipeProvider(PackOutput packOutput) { super(packOutput); } - private void exclude(ItemLike item) - { + private void exclude(ItemLike item) { excludes.add(ForgeRegistries.ITEMS.getKey(item.asItem())); } - private void replace(ItemLike item, TagKey tag) - { + private void exclude(String name) { + excludes.add(new ResourceLocation(name)); + } + + private void replace(ItemLike item, TagKey tag) { replacements.put(item.asItem(), tag); } @Override - protected void buildRecipes(Consumer consumer) - { + protected void buildRecipes(Consumer consumer) { replace(Items.STICK, Tags.Items.RODS_WOODEN); replace(Items.GOLD_INGOT, Tags.Items.INGOTS_GOLD); replace(Items.IRON_INGOT, Tags.Items.INGOTS_IRON); @@ -69,6 +72,9 @@ public final class ForgeRecipeProvider extends VanillaRecipeProvider replace(Blocks.COBBLESTONE, Tags.Items.COBBLESTONE_NORMAL); replace(Blocks.COBBLED_DEEPSLATE, Tags.Items.COBBLESTONE_DEEPSLATE); + replace(Items.STRING, Tags.Items.STRING); + exclude(getConversionRecipeName(Blocks.WHITE_WOOL, Items.STRING)); + exclude(Blocks.GOLD_BLOCK); exclude(Items.GOLD_NUGGET); exclude(Blocks.IRON_BLOCK); @@ -94,8 +100,7 @@ public final class ForgeRecipeProvider extends VanillaRecipeProvider } @Nullable - private FinishedRecipe enhance(FinishedRecipe vanilla) - { + private FinishedRecipe enhance(FinishedRecipe vanilla) { if (vanilla instanceof ShapelessRecipeBuilder.Result shapeless) return enhance(shapeless); if (vanilla instanceof ShapedRecipeBuilder.Result shaped) @@ -104,15 +109,12 @@ public final class ForgeRecipeProvider extends VanillaRecipeProvider } @Nullable - private FinishedRecipe enhance(ShapelessRecipeBuilder.Result vanilla) - { + private FinishedRecipe enhance(ShapelessRecipeBuilder.Result vanilla) { List ingredients = getField(ShapelessRecipeBuilder.Result.class, vanilla, 4); boolean modified = false; - for (int x = 0; x < ingredients.size(); x++) - { + for (int x = 0; x < ingredients.size(); x++) { Ingredient ing = enhance(vanilla.getId(), ingredients.get(x)); - if (ing != null) - { + if (ing != null) { ingredients.set(x, ing); modified = true; } @@ -122,29 +124,24 @@ public final class ForgeRecipeProvider extends VanillaRecipeProvider @Nullable @Override - protected CompletableFuture saveAdvancement(CachedOutput output, FinishedRecipe finishedRecipe, JsonObject advancementJson) - { + protected CompletableFuture saveAdvancement(CachedOutput output, FinishedRecipe recipe, JsonObject json) { // NOOP - We don't replace any of the advancement things yet... return null; } @Override - protected CompletableFuture buildAdvancement(CachedOutput p_253674_, ResourceLocation p_254102_, Advancement.Builder p_253712_) - { + protected CompletableFuture buildAdvancement(CachedOutput output, ResourceLocation name, Advancement.Builder builder) { // NOOP - We don't replace any of the advancement things yet... return CompletableFuture.allOf(); } @Nullable - private FinishedRecipe enhance(ShapedRecipeBuilder.Result vanilla) - { + private FinishedRecipe enhance(ShapedRecipeBuilder.Result vanilla) { Map ingredients = getField(ShapedRecipeBuilder.Result.class, vanilla, 5); boolean modified = false; - for (Character x : ingredients.keySet()) - { + for (Character x : ingredients.keySet()) { Ingredient ing = enhance(vanilla.getId(), ingredients.get(x)); - if (ing != null) - { + if (ing != null) { ingredients.put(x, ing); modified = true; } @@ -153,22 +150,18 @@ public final class ForgeRecipeProvider extends VanillaRecipeProvider } @Nullable - private Ingredient enhance(ResourceLocation name, Ingredient vanilla) - { + private Ingredient enhance(ResourceLocation name, Ingredient vanilla) { if (excludes.contains(name)) return null; boolean modified = false; List items = new ArrayList<>(); Value[] vanillaItems = getField(Ingredient.class, vanilla, 2); //This will probably crash between versions, if null fix index - for (Value entry : vanillaItems) - { - if (entry instanceof ItemValue) - { + for (Value entry : vanillaItems) { + if (entry instanceof ItemValue) { ItemStack stack = entry.getItems().stream().findFirst().orElse(ItemStack.EMPTY); TagKey replacement = replacements.get(stack.getItem()); - if (replacement != null) - { + if (replacement != null) { items.add(new TagValue(replacement)); modified = true; } else @@ -180,15 +173,12 @@ public final class ForgeRecipeProvider extends VanillaRecipeProvider } @SuppressWarnings("unchecked") - private R getField(Class clz, T inst, int index) - { + private R getField(Class clz, T inst, int index) { Field fld = clz.getDeclaredFields()[index]; fld.setAccessible(true); - try - { + try { return (R) fld.get(inst); - } catch (IllegalArgumentException | IllegalAccessException e) - { + } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } }