Skip to content

Commit

Permalink
made RecipeUtil a java service
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Oct 18, 2024
1 parent 5c182c3 commit 028e38f
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public SequencedAssemblyBuilder getSequencedAssemblyRecipe(Ingredient input){
}

public void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName) {
RecipeUtil.addConditionalRecipe(consumer, builtRecipe, configClass, configFieldName, recipeDomain, recipeName);
RecipeUtil.INSTANCE.addConditionalRecipe(consumer, builtRecipe, configClass, configFieldName, recipeDomain, recipeName);
}

public void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName) {
RecipeUtil.addConditionalRecipe(consumer, builtRecipe, config, configField, recipeDomain, recipeName);
RecipeUtil.INSTANCE.addConditionalRecipe(consumer, builtRecipe, config, configField, recipeDomain, recipeName);
}

public AntimatterShapedRecipeBuilder getItemRecipe(String groupName, boolean customCriterion, ItemLike output, ImmutableMap<Character, Object> inputs, String... inputPattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ default JsonObject toJson() {
array = new JsonArray();
if (this.getOutputItems(false) != null){
for (ItemStack stack : this.getOutputItems(false)){
array.add(RecipeUtil.itemstackToJson(stack));
array.add(RecipeUtil.INSTANCE.itemstackToJson(stack));
}
}
if (!array.isEmpty()){
Expand Down
47 changes: 13 additions & 34 deletions common/src/main/java/muramasa/antimatter/recipe/RecipeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.architectury.injectables.annotations.ExpectPlatform;
import earth.terrarium.botarium.common.fluid.base.FluidHolder;
import muramasa.antimatter.datagen.builder.AntimatterShapedRecipeBuilder;
import muramasa.antimatter.util.ImplLoader;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.item.ItemStack;
Expand All @@ -12,38 +13,22 @@

import java.util.function.Consumer;

public class RecipeUtil {
@ExpectPlatform
public static boolean isNBTIngredient(Class<? extends Ingredient> clazz){
throw new AssertionError();
}
public interface RecipeUtil {
RecipeUtil INSTANCE = ImplLoader.load(RecipeUtil.class);

@ExpectPlatform
public static boolean isCompoundIngredient(Class<? extends Ingredient> clazz){
throw new AssertionError();
}
boolean isNBTIngredient(Class<? extends Ingredient> clazz);

@ExpectPlatform
public static void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName) {
throw new AssertionError();
}
boolean isCompoundIngredient(Class<? extends Ingredient> clazz);

@ExpectPlatform
public static void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName) {
throw new AssertionError();
}
void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName);

@ExpectPlatform
public static ItemStack getItemStack(JsonObject object, boolean readNBT){
throw new AssertionError();
}
void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName);

@ExpectPlatform
public static JsonObject itemstackToJson(ItemStack stack){
throw new AssertionError();
}
ItemStack getItemStack(JsonObject object, boolean readNBT);

JsonObject itemstackToJson(ItemStack stack);

public static JsonObject fluidstackToJson(FluidHolder stack){
static JsonObject fluidstackToJson(FluidHolder stack){
JsonObject object = new JsonObject();
object.addProperty("fluid", FluidPlatformUtils.INSTANCE.getFluidId(stack.getFluid()).toString());
object.addProperty("amount", stack.getFluidAmount());
Expand All @@ -53,13 +38,7 @@ public static JsonObject fluidstackToJson(FluidHolder stack){
return object;
}

@ExpectPlatform
public static <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient){
throw new AssertionError();
}
<T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient);

@ExpectPlatform
public static Ingredient fromNetwork(FriendlyByteBuf buffer) {
throw new AssertionError();
}
Ingredient fromNetwork(FriendlyByteBuf buffer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public void serializeRecipeData(JsonObject json) {
}
array = new JsonArray();
for (ItemStack stack : itemsOutput){
array.add(RecipeUtil.itemstackToJson(stack));
array.add(RecipeUtil.INSTANCE.itemstackToJson(stack));
}
if (!array.isEmpty()){
json.add("outputItems", array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ public boolean acceptsFluid(FluidHolder fluid) {
public static boolean isIngredientSpecial(Ingredient i) {
Class<? extends Ingredient> clazz = i.getClass();
if (clazz == RecipeIngredient.class) return false;
return /* i.getMatchingStacks().length == 0 && */(clazz != Ingredient.class && !RecipeUtil.isNBTIngredient(clazz)
&& !RecipeUtil.isCompoundIngredient(clazz));
return /* i.getMatchingStacks().length == 0 && */(clazz != Ingredient.class && !RecipeUtil.INSTANCE.isNBTIngredient(clazz)
&& !RecipeUtil.INSTANCE.isCompoundIngredient(clazz));
}

protected static class Branch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Recipe fromJson(ResourceLocation recipeId, JsonObject json) {
}
ItemStack[] outputs = null;
if (json.has("outputItems")) {
outputs = Streams.stream(json.getAsJsonArray("outputItems")).map(t -> RecipeUtil.getItemStack(t.getAsJsonObject(), true)).toArray(ItemStack[]::new);
outputs = Streams.stream(json.getAsJsonArray("outputItems")).map(t -> RecipeUtil.INSTANCE.getItemStack(t.getAsJsonObject(), true)).toArray(ItemStack[]::new);
}
List<FluidIngredient> fluidInputs = new ObjectArrayList<>();
if (json.has("inputFluids")) {
Expand Down Expand Up @@ -162,7 +162,7 @@ public Recipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
List<Ingredient> ings = new ObjectArrayList<>(size);
if (size > 0) {
for (int i = 0; i < size; i++) {
ings.add(RecipeUtil.fromNetwork(buffer));
ings.add(RecipeUtil.INSTANCE.fromNetwork(buffer));
}
}
size = buffer.readInt();
Expand Down Expand Up @@ -238,7 +238,7 @@ public void toNetwork(FriendlyByteBuf buffer, Recipe recipe) {
}
buffer.writeInt(!recipe.hasInputItems() ? 0 : recipe.getInputItems().size());
if (recipe.hasInputItems()) {
recipe.getInputItems().forEach(t -> RecipeUtil.write(buffer, t));
recipe.getInputItems().forEach(t -> RecipeUtil.INSTANCE.write(buffer, t));
}
buffer.writeInt(!recipe.hasOutputItems() ? 0 : recipe.getOutputItems(false).length);
if (recipe.hasOutputItems()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.fabricators_of_create.porting_lib.data.ConditionalRecipe;
import io.github.tropheusj.serialization_hooks.ingredient.IngredientDeserializer;
import muramasa.antimatter.datagen.builder.AntimatterShapedRecipeBuilder;
import muramasa.antimatter.recipe.RecipeUtil;
import net.minecraft.core.Registry;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -15,31 +16,31 @@

import java.util.function.Consumer;

public class RecipeUtilImpl {
public static boolean isNBTIngredient(Class<? extends Ingredient> clazz){
public class RecipeUtilImpl implements RecipeUtil {
public boolean isNBTIngredient(Class<? extends Ingredient> clazz){
return clazz == NBTIngredient.class;
}

public static boolean isCompoundIngredient(Class<? extends Ingredient> clazz){
public boolean isCompoundIngredient(Class<? extends Ingredient> clazz){
return false;
//return clazz == CompoundIngredient.class;
}

public static void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName) {
public void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName) {
ConditionalRecipe.builder().addCondition(RecipeConditions.config(configClass, configFieldName))
.addRecipe(builtRecipe::build).build(consumer, recipeDomain, recipeName);
}

public static void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName) {
public void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName) {
ConditionalRecipe.builder().addCondition(RecipeConditions.tomlConfig(config, configField))
.addRecipe(builtRecipe::build).build(consumer, recipeDomain, recipeName);
}

public static ItemStack getItemStack(JsonObject object, boolean readNBT){
public ItemStack getItemStack(JsonObject object, boolean readNBT){
return CraftingHelper.getItemStack(object, readNBT);
}

public static JsonObject itemstackToJson(ItemStack stack){
public JsonObject itemstackToJson(ItemStack stack){
JsonObject resultObj = new JsonObject();
resultObj.addProperty("item", Registry.ITEM.getKey(stack.getItem()).toString());
if (stack.getCount() > 1) {
Expand All @@ -55,11 +56,11 @@ public static JsonObject itemstackToJson(ItemStack stack){
return resultObj;
}

public static <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient){
public <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient){
ingredient.toNetwork(buffer);
}

public static Ingredient fromNetwork(FriendlyByteBuf buffer) {
public Ingredient fromNetwork(FriendlyByteBuf buffer) {
return IngredientDeserializer.tryDeserializeNetwork(buffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
muramasa.antimatter.recipe.fabric.RecipeUtilImpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonObject;
import muramasa.antimatter.datagen.builder.AntimatterShapedRecipeBuilder;
import muramasa.antimatter.recipe.RecipeUtil;
import muramasa.antimatter.recipe.forge.condition.ConfigCondition;
import muramasa.antimatter.recipe.forge.condition.TomlConfigCondition;
import net.minecraft.core.Registry;
Expand All @@ -18,30 +19,30 @@

import java.util.function.Consumer;

public class RecipeUtilImpl {
public static boolean isNBTIngredient(Class<? extends Ingredient> clazz){
public class RecipeUtilImpl implements RecipeUtil {
public boolean isNBTIngredient(Class<? extends Ingredient> clazz){
return clazz == NBTIngredient.class;
}

public static boolean isCompoundIngredient(Class<? extends Ingredient> clazz){
public boolean isCompoundIngredient(Class<? extends Ingredient> clazz){
return clazz == CompoundIngredient.class;
}

public static void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName) {
public void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, Class configClass, String configFieldName, String recipeDomain, String recipeName) {
ConditionalRecipe.builder().addCondition(new ConfigCondition(configClass, configFieldName))
.addRecipe(builtRecipe::build).build(consumer, recipeDomain, recipeName);
}

public static void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName) {
public void addConditionalRecipe(Consumer<FinishedRecipe> consumer, AntimatterShapedRecipeBuilder builtRecipe, String config, String configField, String recipeDomain, String recipeName) {
ConditionalRecipe.builder().addCondition(new TomlConfigCondition(config, configField))
.addRecipe(builtRecipe::build).build(consumer, recipeDomain, recipeName);
}

public static ItemStack getItemStack(JsonObject object, boolean readNBT){
public ItemStack getItemStack(JsonObject object, boolean readNBT){
return CraftingHelper.getItemStack(object, readNBT);
}

public static JsonObject itemstackToJson(ItemStack stack){
public JsonObject itemstackToJson(ItemStack stack){
JsonObject resultObj = new JsonObject();
resultObj.addProperty("item", Registry.ITEM.getKey(stack.getItem()).toString());
if (stack.getCount() > 1) {
Expand All @@ -54,15 +55,11 @@ public static JsonObject itemstackToJson(ItemStack stack){
return resultObj;
}

public static JsonObject fluidstackToJson(FluidStack stack){
throw new AssertionError();
}

public static <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient){
public <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient){
CraftingHelper.write(buffer, ingredient);
}

public static Ingredient fromNetwork(FriendlyByteBuf buffer) {
public Ingredient fromNetwork(FriendlyByteBuf buffer) {
return Ingredient.fromNetwork(buffer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
muramasa.antimatter.recipe.forge.RecipeUtilImpl

0 comments on commit 028e38f

Please sign in to comment.