Update cutting recipe generator

This commit is contained in:
Sollace 2024-06-30 01:12:29 +01:00
parent fcb58b759a
commit 6570248b32
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 97 additions and 57 deletions

View file

@ -14,6 +14,7 @@ import com.minelittlepony.unicopia.datagen.providers.loot.UChestAdditionsLootTab
import com.minelittlepony.unicopia.datagen.providers.loot.UChestLootTableProvider;
import com.minelittlepony.unicopia.datagen.providers.loot.UEntityAdditionsLootTableProvider;
import com.minelittlepony.unicopia.datagen.providers.loot.UEntityLootTableProvider;
import com.minelittlepony.unicopia.datagen.providers.recipe.CuttingBoardRecipeJsonBuilder;
import com.minelittlepony.unicopia.datagen.providers.recipe.URecipeProvider;
import com.minelittlepony.unicopia.datagen.providers.tag.UBlockTagProvider;
import com.minelittlepony.unicopia.datagen.providers.tag.UDamageTypeProvider;
@ -76,6 +77,7 @@ public class Datagen implements DataGeneratorEntrypoint {
@Override
public void buildRegistry(RegistryBuilder builder) {
CuttingBoardRecipeJsonBuilder.CuttingBoardRecipe.bootstrap();
builder.addRegistry(RegistryKeys.BIOME, UWorldGen.REGISTRY);
builder.addRegistry(RegistryKeys.DAMAGE_TYPE, UDamageTypes.REGISTRY);
}

View file

@ -1,30 +1,38 @@
/*
package com.minelittlepony.unicopia.datagen.providers.recipe;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.spongepowered.include.com.google.common.base.Preconditions;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementCriterion;
import net.minecraft.advancement.AdvancementEntry;
import net.minecraft.advancement.AdvancementRequirements;
import net.minecraft.advancement.AdvancementRewards;
import net.minecraft.advancement.criterion.RecipeUnlockedCriterion;
import net.minecraft.data.server.recipe.RecipeExporter;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import net.minecraft.world.World;
public class CuttingBoardRecipeJsonBuilder {
private final Map<String, AdvancementCriterion<?>> criterions = new LinkedHashMap<>();
@ -32,9 +40,11 @@ public class CuttingBoardRecipeJsonBuilder {
private final ItemConvertible output;
private final TagKey<Item> tool;
private final List<Supplier<Identifier>> results = new ArrayList<>();
private final List<Result> results = new ArrayList<>();
private final List<Ingredient> ingredients = new ArrayList<>();
private Identifier sound = new Identifier("minecraft:item.axe.strip");
public static CuttingBoardRecipeJsonBuilder create(ItemConvertible output, TagKey<Item> tool) {
return new CuttingBoardRecipeJsonBuilder(output, tool);
}
@ -45,18 +55,23 @@ public class CuttingBoardRecipeJsonBuilder {
result(output);
}
public CuttingBoardRecipeJsonBuilder sound(SoundEvent sound) {
this.sound = Registries.SOUND_EVENT.getId(sound);
return this;
}
public CuttingBoardRecipeJsonBuilder input(ItemConvertible input) {
ingredients.add(Ingredient.ofItems(input));
return this;
}
public CuttingBoardRecipeJsonBuilder result(ItemConvertible result) {
results.add(() -> Registries.ITEM.getId(result.asItem()));
results.add(new Result(Registries.ITEM.getId(result.asItem()), 1));
return this;
}
public CuttingBoardRecipeJsonBuilder result(Identifier result) {
results.add(() -> result);
results.add(new Result(result, 1));
return this;
}
@ -72,7 +87,15 @@ public class CuttingBoardRecipeJsonBuilder {
.criterion("has_the_recipe", RecipeUnlockedCriterion.create(id))
.rewards(AdvancementRewards.Builder.recipe(id))
.criteriaMerger(AdvancementRequirements.CriterionMerger.OR);
exporter.accept(new JsonProvider(id, advancementBuilder.build(id.withPrefixedPath("recipes/"))));
exporter.accept(id,
new CuttingBoardRecipe(
ingredients,
new Tool(new Identifier("farmersdelight:tool"), tool),
sound,
results
),
advancementBuilder.build(id.withPrefixedPath("recipes/"))
);
}
public void offerTo(RecipeExporter exporter) {
@ -87,57 +110,66 @@ public class CuttingBoardRecipeJsonBuilder {
offerTo(exporter, recipeId);
}
private class JsonProvider implements RecipeJsonProvider {
private final Identifier recipeId;
private final AdvancementEntry advancement;
public JsonProvider(Identifier recipeId, AdvancementEntry advancement) {
this.recipeId = recipeId;
this.advancement = advancement;
}
public record Tool(Identifier type, TagKey<Item> tag) {
static final Codec<Tool> CODEC = RecordCodecBuilder.create(ii -> ii.group(
Identifier.CODEC.fieldOf("type").forGetter(Tool::type),
TagKey.codec(RegistryKeys.ITEM).fieldOf("tag").forGetter(Tool::tag)
).apply(ii, Tool::new));
}
public record Result(Identifier item, int count) {
public static final Codec<Result> CODEC = RecordCodecBuilder.create(i -> i.group(
Identifier.CODEC.fieldOf("item").forGetter(Result::item),
Codecs.createStrictOptionalFieldCodec(Codecs.POSITIVE_INT, "count", 1).forGetter(Result::count)
).apply(i, Result::new));
}
@Override
public JsonObject toJson() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "farmersdelight:cutting");
serialize(jsonObject);
return jsonObject;
}
@Override
public void serialize(JsonObject json) {
JsonArray ingredientsJson = new JsonArray();
for (var ingredient : ingredients) {
ingredientsJson.add(ingredient.toJson(false));
public record CuttingBoardRecipe(
List<Ingredient> ingredients,
Tool tool,
Identifier sound,
List<Result> result
) implements Recipe<Inventory> {
static final Identifier ID = new Identifier("farmersdelight", "cutting");
static final RecipeType<CuttingBoardRecipe> TYPE = Registry.register(Registries.RECIPE_TYPE, ID, new RecipeType<>() {
@Override
public String toString() {
return "farmersdelight:cutting";
}
json.add("ingredients", ingredientsJson);
JsonObject toolJson = new JsonObject();
toolJson.addProperty("type", "farmersdelight:tool");
toolJson.addProperty("tag", tool.id().toString());
json.add("tool", toolJson);
JsonArray resultJson = new JsonArray();
for (var result : results) {
JsonObject o = new JsonObject();
o.addProperty("item", result.get().toString());
resultJson.add(o);
}
json.add("result", resultJson);
}
});
static final RecipeSerializer<CuttingBoardRecipe> SERIALIZER = Registry.register(Registries.RECIPE_SERIALIZER, ID, new RecipeSerializer<>() {
@Override
public Codec<CuttingBoardRecipe> codec() { return CODEC; }
@Override
public CuttingBoardRecipe read(PacketByteBuf buf) { return null; }
@Override
public void write(PacketByteBuf buf, CuttingBoardRecipe recipe) { }
});
static final Codec<CuttingBoardRecipe> CODEC = RecordCodecBuilder.create(i -> i.group(
Ingredient.DISALLOW_EMPTY_CODEC.listOf().fieldOf("ingredients").forGetter(CuttingBoardRecipe::ingredients),
Tool.CODEC.fieldOf("tool").forGetter(CuttingBoardRecipe::tool),
Identifier.CODEC.fieldOf("sound").forGetter(CuttingBoardRecipe::sound),
Result.CODEC.listOf().fieldOf("result").forGetter(CuttingBoardRecipe::result)
).apply(i, CuttingBoardRecipe::new));
public static void bootstrap() {}
@Override
public Identifier id() {
return recipeId;
}
public boolean matches(Inventory inventory, World world) { return false; }
@Override
public RecipeSerializer<?> serializer() {
return RecipeSerializer.SHAPELESS;
}
public ItemStack craft(Inventory inventory, DynamicRegistryManager registryManager) { return ItemStack.EMPTY; }
@Override
public AdvancementEntry advancement() {
return advancement;
}
public boolean fits(int width, int height) { return false; }
@Override
public ItemStack getResult(DynamicRegistryManager registryManager) { return ItemStack.EMPTY; }
@Override
public RecipeSerializer<?> getSerializer() { return SERIALIZER; }
@Override
public RecipeType<?> getType() { return TYPE; }
}
}
*/

View file

@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.datagen.providers.recipe;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
@ -27,12 +28,14 @@ import com.mojang.datafixers.util.Either;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider;
import net.fabricmc.fabric.api.resource.conditions.v1.DefaultResourceConditions;
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalItemTags;
import net.minecraft.advancement.AdvancementCriterion;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.advancement.criterion.InventoryChangedCriterion;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.data.family.BlockFamily.Variant;
import net.minecraft.data.server.recipe.ComplexRecipeJsonBuilder;
import net.minecraft.data.server.recipe.RecipeExporter;
import net.minecraft.data.server.recipe.RecipeProvider;
@ -49,6 +52,8 @@ import net.minecraft.registry.Registries;
import net.minecraft.registry.tag.ItemTags;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.resource.featuretoggle.FeatureSet;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
public class URecipeProvider extends FabricRecipeProvider {
private static final List<Item> WOOLS = List.of(Items.BLACK_WOOL, Items.BLUE_WOOL, Items.BROWN_WOOL, Items.CYAN_WOOL, Items.GRAY_WOOL, Items.GREEN_WOOL, Items.LIGHT_BLUE_WOOL, Items.LIGHT_GRAY_WOOL, Items.LIME_WOOL, Items.MAGENTA_WOOL, Items.ORANGE_WOOL, Items.PINK_WOOL, Items.PURPLE_WOOL, Items.RED_WOOL, Items.YELLOW_WOOL, Items.WHITE_WOOL);
@ -85,7 +90,7 @@ public class URecipeProvider extends FabricRecipeProvider {
.offerTo(exporter, convertBetween(UItems.SUNGLASSES, UItems.BROKEN_SUNGLASSES));
// farmers delight
//offerFarmersDelightCuttingRecipes(withConditions(exporter, DefaultResourceConditions.allModsLoaded("farmersdelight")));
offerFarmersDelightCuttingRecipes(withConditions(exporter, DefaultResourceConditions.allModsLoaded("farmersdelight")));
}
private void generateVanillaRecipeExtensions(RecipeExporter exporter) {
@ -693,9 +698,7 @@ public class URecipeProvider extends FabricRecipeProvider {
PatternTemplate.SEVEN_COLOR.offerTo(exporter, UItems.RAINBOW_BED_SHEETS, UItems.RAINBOW_BED_SHEETS, Items.LIGHT_BLUE_WOOL, Items.RED_WOOL, Items.ORANGE_WOOL, Items.YELLOW_WOOL, Items.BLUE_WOOL, Items.GREEN_WOOL, Items.PURPLE_WOOL);
}
/*
private void offerFarmersDelightCuttingRecipes(RecipeExporter exporter) {
// unwaxing
UBlockFamilies.WAXED_ZAP.getVariants().forEach((variant, waxed) -> {
if (variant == Variant.WALL_SIGN) return;
@ -703,6 +706,7 @@ public class URecipeProvider extends FabricRecipeProvider {
CuttingBoardRecipeJsonBuilder.create(unwaxed, ItemTags.AXES)
.input(waxed).criterion(hasItem(waxed), conditionsFromItem(waxed))
.result(Items.HONEYCOMB)
.sound(SoundEvents.ITEM_AXE_WAX_OFF)
.offerTo(exporter, getItemPath(unwaxed) + "_from_waxed");
});
List.of(UBlockFamilies.ZAP, UBlockFamilies.PALM).forEach(family -> {
@ -710,11 +714,13 @@ public class URecipeProvider extends FabricRecipeProvider {
if (variant == Variant.WALL_SIGN) return;
CuttingBoardRecipeJsonBuilder.create(family.getBaseBlock(), ItemTags.AXES)
.input(block).criterion(hasItem(block), conditionsFromItem(block))
.sound(SoundEvents.ITEM_AXE_STRIP)
.offerTo(exporter, getItemPath(block));
});
});
CuttingBoardRecipeJsonBuilder.create(UBlocks.PALM_PLANKS, ItemTags.AXES)
.input(UBlocks.PALM_HANGING_SIGN).criterion(hasItem(UBlocks.PALM_HANGING_SIGN), conditionsFromItem(UBlocks.PALM_HANGING_SIGN))
.sound(SoundEvents.ITEM_AXE_STRIP)
.offerTo(exporter);
Map.of(
@ -725,11 +731,11 @@ public class URecipeProvider extends FabricRecipeProvider {
).forEach((unstripped, stripped) -> {
CuttingBoardRecipeJsonBuilder.create(stripped, ItemTags.AXES)
.input(unstripped).criterion(hasItem(unstripped), conditionsFromItem(unstripped))
.sound(SoundEvents.ITEM_AXE_STRIP)
.result(new Identifier("farmersdelight:tree_bark"))
.offerTo(exporter, convertBetween(stripped, unstripped));
});
}
*/
public static void offerCompactingRecipe(RecipeExporter exporter, RecipeCategory category, ItemConvertible output, ItemConvertible input, int resultCount) {
offerCompactingRecipe(exporter, category, output, input, hasItem(input), resultCount);