diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/smithing/FabricRecipeGenerator.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/smithing/FabricRecipeGenerator.java new file mode 100644 index 0000000000..4374b91de6 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/smithing/FabricRecipeGenerator.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.datagen.v1.smithing; + +import org.jetbrains.annotations.Nullable; + +import net.minecraft.component.ComponentChanges; +import net.minecraft.data.recipe.SmithingTransformRecipeJsonBuilder; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.recipe.Ingredient; +import net.minecraft.recipe.book.RecipeCategory; + +/** + * Fabric-provided extensions for {@link net.minecraft.data.recipe.RecipeGenerator}. + * + *

Adds recipe types that the vanilla class does not implement. + */ +public interface FabricRecipeGenerator { + default SmithingTransformRecipeJsonBuilder createSmithingTransformRecipe(Ingredient template, Ingredient input, Ingredient addition, RecipeCategory category, Item result, int count, @Nullable ComponentChanges componentChanges) { + throw new UnsupportedOperationException("Implemented via mixin"); + } + + default SmithingTransformRecipeJsonBuilder createSmithingTransformRecipe(Ingredient template, Ingredient input, Ingredient addition, RecipeCategory category, ItemStack result) { + return createSmithingTransformRecipe(template, input, addition, category, result.getItem(), result.getCount(), result.getComponentChanges()); + } + + default SmithingTransformRecipeJsonBuilder createSmithingTransformRecipe(Item template, Item input, Item addition, RecipeCategory category, ItemStack result) { + return createSmithingTransformRecipe(Ingredient.ofItem(template), Ingredient.ofItem(input), Ingredient.ofItem(addition), category, result); + } +} diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/smithing/FabricSmithingTransformRecipeJsonBuilder.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/smithing/FabricSmithingTransformRecipeJsonBuilder.java new file mode 100644 index 0000000000..4a045463b3 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/api/datagen/v1/smithing/FabricSmithingTransformRecipeJsonBuilder.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.api.datagen.v1.smithing; + +import net.minecraft.component.ComponentChanges; + +/** + * Fabric-provided extensions for {@link net.minecraft.data.recipe.SmithingTransformRecipeJsonBuilder}. + * + *

Allows specification of the transform recipe result item count and component changes. + */ +public interface FabricSmithingTransformRecipeJsonBuilder { + default ComponentChanges getComponentChanges() { + throw new UnsupportedOperationException("Implemented via mixin"); + } + + default int getCount() { + throw new UnsupportedOperationException("Implemented via mixin"); + } + + default void setComponentChanges(ComponentChanges componentChanges) { + throw new UnsupportedOperationException("Implemented via mixin"); + } + + default void setCount(int count) { + throw new UnsupportedOperationException("Implemented via mixin"); + } +} diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/smithing/RecipeGeneratorMixin.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/smithing/RecipeGeneratorMixin.java new file mode 100644 index 0000000000..2c9c8434e7 --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/smithing/RecipeGeneratorMixin.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.datagen.smithing; + +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; + +import net.minecraft.component.ComponentChanges; +import net.minecraft.data.recipe.RecipeGenerator; +import net.minecraft.data.recipe.SmithingTransformRecipeJsonBuilder; +import net.minecraft.item.Item; +import net.minecraft.recipe.Ingredient; +import net.minecraft.recipe.book.RecipeCategory; + +import net.fabricmc.fabric.api.datagen.v1.smithing.FabricRecipeGenerator; + +@Mixin(RecipeGenerator.class) +abstract class RecipeGeneratorMixin implements FabricRecipeGenerator { + @Override + @Unique + public SmithingTransformRecipeJsonBuilder createSmithingTransformRecipe(Ingredient template, Ingredient input, Ingredient addition, RecipeCategory category, Item result, int count, @Nullable ComponentChanges componentChanges) { + SmithingTransformRecipeJsonBuilder builder = SmithingTransformRecipeJsonBuilder.create(template, input, addition, category, result); + builder.setCount(count); + builder.setComponentChanges(componentChanges != null ? componentChanges : ComponentChanges.EMPTY); + return builder; + } +} diff --git a/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/smithing/SmithingTransformRecipeJsonBuilderMixin.java b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/smithing/SmithingTransformRecipeJsonBuilderMixin.java new file mode 100644 index 0000000000..fdb258303a --- /dev/null +++ b/fabric-data-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/smithing/SmithingTransformRecipeJsonBuilderMixin.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.mixin.datagen.smithing; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +import net.minecraft.component.ComponentChanges; +import net.minecraft.data.recipe.SmithingTransformRecipeJsonBuilder; +import net.minecraft.recipe.TransmuteRecipeResult; + +import net.fabricmc.fabric.api.datagen.v1.smithing.FabricSmithingTransformRecipeJsonBuilder; + +@Mixin(SmithingTransformRecipeJsonBuilder.class) +class SmithingTransformRecipeJsonBuilderMixin implements FabricSmithingTransformRecipeJsonBuilder { + @Unique + private int count = 1; + + @Unique + private ComponentChanges componentChanges = ComponentChanges.EMPTY; + + @ModifyArg(method = "offerTo(Lnet/minecraft/data/recipe/RecipeExporter;Lnet/minecraft/registry/RegistryKey;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/SmithingTransformRecipe;(Ljava/util/Optional;Lnet/minecraft/recipe/Ingredient;Ljava/util/Optional;Lnet/minecraft/recipe/TransmuteRecipeResult;)V"), index = 3) + private TransmuteRecipeResult editResultParameters(TransmuteRecipeResult result) { + return new TransmuteRecipeResult(result.itemEntry(), getCount(), getComponentChanges()); + } + + @Override + public ComponentChanges getComponentChanges() { + return componentChanges; + } + + @Override + public int getCount() { + return count; + } + + @Override + public void setComponentChanges(ComponentChanges componentChanges) { + this.componentChanges = componentChanges; + } + + @Override + public void setCount(int count) { + this.count = count; + } +} diff --git a/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json b/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json index 1007a40a43..694b9d883d 100644 --- a/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json +++ b/fabric-data-generation-api-v1/src/main/resources/fabric-data-generation-api-v1.mixins.json @@ -14,7 +14,9 @@ "loot.BlockLootTableGeneratorAccessor", "loot.BlockLootTableGeneratorMixin", "loot.EntityLootTableGeneratorAccessor", - "loot.EntityLootTableGeneratorMixin" + "loot.EntityLootTableGeneratorMixin", + "smithing.RecipeGeneratorMixin", + "smithing.SmithingTransformRecipeJsonBuilderMixin" ], "server": [ "server.MainMixin" diff --git a/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json b/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json index 9142bf07fe..9e968da546 100644 --- a/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json +++ b/fabric-data-generation-api-v1/src/main/resources/fabric.mod.json @@ -33,7 +33,9 @@ "loom:injected_interfaces": { "net/minecraft/class_7788": ["net/fabricmc/fabric/api/datagen/v1/loot/FabricBlockLootTableGenerator"], "net/minecraft/class_7789": ["net/fabricmc/fabric/api/datagen/v1/loot/FabricEntityLootTableGenerator"], - "net/minecraft/class_11389": ["net/fabricmc/fabric/api/datagen/v1/provider/FabricProvidedTagBuilder"] + "net/minecraft/class_11389": ["net/fabricmc/fabric/api/datagen/v1/provider/FabricProvidedTagBuilder"], + "net/minecraft/class_8074": ["net/fabricmc/fabric/api/datagen/v1/smithing/FabricSmithingTransformRecipeJsonBuilder"], + "net/minecraft/class_2446": ["net/fabricmc/fabric/api/datagen/v1/smithing/FabricRecipeGenerator"] } } } diff --git a/fabric-data-generation-api-v1/src/testmod/generated/data/fabric-data-gen-api-v1-testmod/advancement/recipes/misc/stick_smithing.json b/fabric-data-generation-api-v1/src/testmod/generated/data/fabric-data-gen-api-v1-testmod/advancement/recipes/misc/stick_smithing.json new file mode 100644 index 0000000000..bef0415776 --- /dev/null +++ b/fabric-data-generation-api-v1/src/testmod/generated/data/fabric-data-gen-api-v1-testmod/advancement/recipes/misc/stick_smithing.json @@ -0,0 +1,32 @@ +{ + "parent": "minecraft:recipes/root", + "criteria": { + "has_book": { + "trigger": "minecraft:inventory_changed", + "conditions": { + "items": [ + { + "items": "minecraft:book" + } + ] + } + }, + "has_the_recipe": { + "trigger": "minecraft:recipe_unlocked", + "conditions": { + "recipe": "minecraft:stick_smithing" + } + } + }, + "requirements": [ + [ + "has_the_recipe", + "has_book" + ] + ], + "rewards": { + "recipes": [ + "minecraft:stick_smithing" + ] + } +} \ No newline at end of file diff --git a/fabric-data-generation-api-v1/src/testmod/generated/data/fabric-data-gen-api-v1-testmod/recipe/stick_smithing.json b/fabric-data-generation-api-v1/src/testmod/generated/data/fabric-data-gen-api-v1-testmod/recipe/stick_smithing.json new file mode 100644 index 0000000000..40d1d12f3f --- /dev/null +++ b/fabric-data-generation-api-v1/src/testmod/generated/data/fabric-data-gen-api-v1-testmod/recipe/stick_smithing.json @@ -0,0 +1,12 @@ +{ + "type": "minecraft:smithing_transform", + "addition": "minecraft:experience_bottle", + "base": "minecraft:stick", + "result": { + "components": { + "minecraft:enchantment_glint_override": true + }, + "id": "minecraft:stick" + }, + "template": "minecraft:book" +} \ No newline at end of file diff --git a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java index a4d0124bf0..dab5d368ad 100644 --- a/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java +++ b/fabric-data-generation-api-v1/src/testmod/java/net/fabricmc/fabric/test/datagen/DataGeneratorTestEntrypoint.java @@ -52,6 +52,7 @@ import net.minecraft.data.recipe.RecipeGenerator; import net.minecraft.entity.EntityType; import net.minecraft.entity.attribute.EntityAttributes; +import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.loot.LootPool; import net.minecraft.loot.LootTable; @@ -174,6 +175,9 @@ protected RecipeGenerator getRecipeGenerator(RegistryWrapper.WrapperLookup regis @Override public void generate() { offerPlanksRecipe2(SIMPLE_BLOCK, ItemTags.ACACIA_LOGS, 1); + createSmithingTransformRecipe(Items.BOOK, Items.STICK, Items.EXPERIENCE_BOTTLE, RecipeCategory.MISC, new ItemStack(Items.STICK.getRegistryEntry(), 1, ComponentChanges.builder().add(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true).build())) + .criterion("has_book", conditionsFromItem(Items.BOOK)) + .offerTo(this.exporter, getItemPath(Items.STICK) + "_smithing"); createShapeless(RecipeCategory.MISC, Items.DIAMOND_ORE, 4).input(Items.ITEM_FRAME) .criterion("has_frame", conditionsFromItem(Items.ITEM_FRAME))