Skip to content

Commit

Permalink
Update cutting recipe generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Jun 30, 2024
1 parent fcb58b7 commit 6570248
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -76,6 +77,7 @@ public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {

@Override
public void buildRegistry(RegistryBuilder builder) {
CuttingBoardRecipeJsonBuilder.CuttingBoardRecipe.bootstrap();
builder.addRegistry(RegistryKeys.BIOME, UWorldGen.REGISTRY);
builder.addRegistry(RegistryKeys.DAMAGE_TYPE, UDamageTypes.REGISTRY);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
/*
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<>();

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);
}
Expand All @@ -45,18 +55,23 @@ protected CuttingBoardRecipeJsonBuilder(ItemConvertible output, TagKey<Item> too
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;
}

Expand All @@ -72,7 +87,15 @@ public void offerTo(RecipeExporter exporter, Identifier id) {
.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) {
Expand All @@ -87,57 +110,66 @@ public void offerTo(RecipeExporter exporter, String recipePath) {
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));
}

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";
}
});
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 JsonObject toJson() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("type", "farmersdelight:cutting");
serialize(jsonObject);
return jsonObject;
}
public boolean matches(Inventory inventory, World world) { return false; }

@Override
public void serialize(JsonObject json) {
JsonArray ingredientsJson = new JsonArray();
for (var ingredient : ingredients) {
ingredientsJson.add(ingredient.toJson(false));
}
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);
}
public ItemStack craft(Inventory inventory, DynamicRegistryManager registryManager) { return ItemStack.EMPTY; }

@Override
public Identifier id() {
return recipeId;
}
public boolean fits(int width, int height) { return false; }

@Override
public RecipeSerializer<?> serializer() {
return RecipeSerializer.SHAPELESS;
}
public ItemStack getResult(DynamicRegistryManager registryManager) { return ItemStack.EMPTY; }

@Override
public AdvancementEntry advancement() {
return advancement;
}
public RecipeSerializer<?> getSerializer() { return SERIALIZER; }

@Override
public RecipeType<?> getType() { return TYPE; }
}
}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -27,12 +28,14 @@

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;
Expand All @@ -49,6 +52,8 @@
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);
Expand Down Expand Up @@ -85,7 +90,7 @@ public void generate(RecipeExporter exporter) {
.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) {
Expand Down Expand Up @@ -693,28 +698,29 @@ private void offerBedSheetRecipes(RecipeExporter exporter) {
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;
var unwaxed = UBlockFamilies.ZAP.getVariant(variant);
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 -> {
family.getVariants().forEach((variant, block) -> {
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(
Expand All @@ -725,11 +731,11 @@ private void offerFarmersDelightCuttingRecipes(RecipeExporter exporter) {
).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);
Expand Down

0 comments on commit 6570248

Please sign in to comment.