Testing GridRecipe for DataGen

This commit is contained in:
Frank 2023-04-08 15:01:56 +02:00
parent bd47215916
commit f60fcd02d5
31 changed files with 151 additions and 1749 deletions

View file

@ -4,18 +4,24 @@ import org.betterx.bclib.BCLib;
import org.betterx.bclib.config.PathConfig;
import net.minecraft.core.NonNullList;
import net.minecraft.data.recipes.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.ItemLike;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public class GridRecipe extends AbstractAdvancementRecipe {
private static final GridRecipe INSTANCE = new GridRecipe();
@ -28,10 +34,12 @@ public class GridRecipe extends AbstractAdvancementRecipe {
private boolean shaped;
private String[] shape;
private final Map<Character, Ingredient> materialKeys = Maps.newHashMap();
private final Map<Character, TagKey<Item>> materialTagKeys = Maps.newHashMap();
private int count;
private boolean showNotification;
private boolean exist;
protected CraftingBookCategory bookCategory;
protected RecipeCategory bookCategory;
private GridRecipe() {
}
@ -43,21 +51,25 @@ public class GridRecipe extends AbstractAdvancementRecipe {
* @param output
* @return
*/
private GridRecipe(ResourceLocation id, ItemLike output) {
this.id = id;
this.output = output;
this.group = "";
this.type = RecipeType.CRAFTING;
this.shaped = true;
this.showNotification = true;
this.shape = new String[]{"#"};
this.materialKeys.clear();
this.count = 1;
this.bookCategory = RecipeCategory.MISC;
this.exist = output != null && BCLRecipeManager.exists(output);
this.createAdvancement(id, output);
}
static GridRecipe make(ResourceLocation id, ItemLike output) {
INSTANCE.id = id;
INSTANCE.output = output;
INSTANCE.group = "";
INSTANCE.type = RecipeType.CRAFTING;
INSTANCE.shaped = true;
INSTANCE.shape = new String[]{"#"};
INSTANCE.materialKeys.clear();
INSTANCE.count = 1;
INSTANCE.bookCategory = CraftingBookCategory.MISC;
INSTANCE.exist = output != null && BCLRecipeManager.exists(output);
INSTANCE.createAdvancement(id, output);
return INSTANCE;
return new GridRecipe(id, output);
}
public GridRecipe checkConfig(PathConfig config) {
@ -83,7 +95,8 @@ public class GridRecipe extends AbstractAdvancementRecipe {
public GridRecipe addMaterial(char key, TagKey<Item> value) {
unlockedBy(value);
return addMaterial(key, Ingredient.of(value));
materialTagKeys.put(key, value);
return this;
}
public GridRecipe addMaterial(char key, ItemStack... values) {
@ -109,6 +122,11 @@ public class GridRecipe extends AbstractAdvancementRecipe {
return this;
}
public GridRecipe showNotification(boolean showNotification) {
this.showNotification = showNotification;
return this;
}
private NonNullList<Ingredient> getMaterials(int width, int height) {
NonNullList<Ingredient> materials = NonNullList.withSize(width * height, Ingredient.EMPTY);
int pos = 0;
@ -116,7 +134,9 @@ public class GridRecipe extends AbstractAdvancementRecipe {
for (String line : shape) {
for (int i = 0; i < width; i++) {
char c = line.charAt(i);
Ingredient material = materialKeys.get(c);
Ingredient material = materialKeys.containsKey(c)
? materialKeys.get(c)
: Ingredient.of(materialTagKeys.get(c));
if (material != null && !material.isEmpty()) hasNonEmpty = true;
materials.set(pos++, material == null ? Ingredient.EMPTY : material);
}
@ -125,13 +145,29 @@ public class GridRecipe extends AbstractAdvancementRecipe {
return materials;
}
public GridRecipe setCraftingBookCategory(CraftingBookCategory c) {
public GridRecipe setCategory(RecipeCategory c) {
bookCategory = c;
return this;
}
private static List<GridRecipe> RECIPES;
public void build() {
public GridRecipe build() {
if (RECIPES == null) RECIPES = new ArrayList<>();
RECIPES.add(this);
return this;
}
public static void registerRecipes(Consumer<FinishedRecipe> cc) {
if (RECIPES == null) return;
for (var r : RECIPES) {
r.build(cc);
}
RECIPES.clear();
}
public void build(Consumer<FinishedRecipe> cc) {
if (!exist) {
BCLib.LOGGER.warning("Unable to build Recipe " + id);
return;
@ -144,23 +180,70 @@ public class GridRecipe extends AbstractAdvancementRecipe {
BCLib.LOGGER.warning("Unable to build Recipe " + id + ": Result is AIR");
return;
}
NonNullList<Ingredient> materials = this.getMaterials(width, height);
if (materials == null || materials.isEmpty()) {
BCLib.LOGGER.warning("Unable to build Recipe " + id + ": Empty Material List");
return;
}
CraftingRecipe recipe = shaped ? new ShapedRecipe(
id,
group,
bookCategory,
width,
height,
materials,
result
) : new ShapelessRecipe(id, group, bookCategory, result, materials);
if (shaped) {
final ShapedRecipeBuilder builder = ShapedRecipeBuilder
.shaped(bookCategory, output, count)
.group(group)
.showNotification(showNotification);
BCLRecipeManager.addRecipe(type, recipe);
registerAdvancement(recipe, output);
for (String row : this.shape) {
builder.pattern(row);
}
for (Map.Entry<Character, Ingredient> in : materialKeys.entrySet()) {
Arrays
.stream(in.getValue().getItems())
.filter(i -> i.getCount() > 0)
.forEach(stack -> builder.unlockedBy(
"has_" + stack.getDescriptionId(),
RecipeProvider.has(stack.getItem())
));
builder.define(in.getKey(), in.getValue());
}
for (Map.Entry<Character, TagKey<Item>> in : materialTagKeys.entrySet()) {
builder.unlockedBy(
"has_tag_" + in.getValue().location().getNamespace() + "_" + in.getValue().location().getPath(),
RecipeProvider.has(in.getValue())
);
builder.define(in.getKey(), in.getValue());
}
builder.save(cc, id);
} else {
final ShapelessRecipeBuilder builder = ShapelessRecipeBuilder
.shapeless(bookCategory, output, count)
.group(group);
for (Map.Entry<Character, Ingredient> in : materialKeys.entrySet()) {
Arrays
.stream(in.getValue().getItems())
.filter(i -> i.getCount() > 0)
.forEach(stack -> builder.unlockedBy(
"has_" + stack.getDescriptionId(),
RecipeProvider.has(stack.getItem())
));
builder.requires(in.getValue());
}
for (Map.Entry<Character, TagKey<Item>> in : materialTagKeys.entrySet()) {
builder.unlockedBy(
"has_tag_" + in.getValue().location().getNamespace() + "_" + in.getValue().location().getPath(),
RecipeProvider.has(in.getValue())
);
builder.requires(in.getValue());
}
builder.save(cc, id);
}
}
}

View file

@ -4,6 +4,7 @@ import org.betterx.bclib.BCLib;
import org.betterx.datagen.bclib.advancement.BCLAdvancementDataProvider;
import org.betterx.datagen.bclib.preset.WorldPresetDataProvider;
import org.betterx.datagen.bclib.tests.TestBiomes;
import org.betterx.datagen.bclib.tests.TestRecipes;
import org.betterx.datagen.bclib.tests.TestWorldgenProvider;
import org.betterx.datagen.bclib.worldgen.BCLibRegistriesDataProvider;
@ -13,7 +14,7 @@ import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
public class BCLibDatagen implements DataGeneratorEntrypoint {
public static final boolean ADD_TESTS = false;
public static final boolean ADD_TESTS = true;
@Override
public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
@ -23,6 +24,7 @@ public class BCLibDatagen implements DataGeneratorEntrypoint {
if (ADD_TESTS) {
pack.addProvider(TestWorldgenProvider::new);
pack.addProvider(TestBiomes::new);
pack.addProvider(TestRecipes::new);
}
pack.addProvider(WorldPresetDataProvider::new);

View file

@ -0,0 +1,34 @@
package org.betterx.datagen.bclib.tests;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.recipes.BCLRecipeBuilder;
import org.betterx.bclib.recipes.GridRecipe;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.data.recipes.RecipeCategory;
import net.minecraft.world.item.Items;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider;
import java.util.function.Consumer;
public class TestRecipes extends FabricRecipeProvider {
public TestRecipes(FabricDataOutput output) {
super(output);
}
final GridRecipe WONDER = BCLRecipeBuilder
.crafting(BCLib.makeID("test_star"), Items.NETHER_STAR)
.setOutputCount(1)
.setShape("ggg", "glg", "ggg")
.addMaterial('g', Items.GLASS_PANE)
.addMaterial('l', Items.LAPIS_LAZULI)
.setCategory(RecipeCategory.TOOLS)
.build();
@Override
public void buildRecipes(Consumer<FinishedRecipe> exporter) {
GridRecipe.registerRecipes(exporter);
}
}