diff --git a/src/main/java/com/aetherteam/nitrogen/recipe/builder/BlockStateRecipeBuilder.java b/src/main/java/com/aetherteam/nitrogen/recipe/builder/BlockStateRecipeBuilder.java index a9e9cac..eaf6994 100644 --- a/src/main/java/com/aetherteam/nitrogen/recipe/builder/BlockStateRecipeBuilder.java +++ b/src/main/java/com/aetherteam/nitrogen/recipe/builder/BlockStateRecipeBuilder.java @@ -19,13 +19,13 @@ import javax.annotation.Nullable; import java.util.Map; +import java.util.Optional; public class BlockStateRecipeBuilder implements RecipeBuilder { private final BlockPropertyPair result; private final BlockStateIngredient ingredient; private final BlockStateRecipeSerializer serializer; - @Nullable - private ResourceLocation function; + private Optional function; public BlockStateRecipeBuilder(BlockPropertyPair result, BlockStateIngredient ingredient, BlockStateRecipeSerializer serializer) { this.result = result; @@ -50,7 +50,7 @@ public RecipeBuilder group(@Nullable String groupName) { return this; } - public RecipeBuilder function(@Nullable ResourceLocation function) { + public RecipeBuilder function(Optional function) { this.function = function; return this; } @@ -87,14 +87,13 @@ public static class Result implements FinishedRecipe { private final BlockStateIngredient ingredient; private final BlockPropertyPair result; private final RecipeSerializer serializer; - @Nullable - private final ResourceLocation function; + private final Optional function; public Result(ResourceLocation id, BlockStateIngredient ingredient, BlockPropertyPair result, RecipeSerializer serializer) { - this(id, ingredient, result, serializer, null); + this(id, ingredient, result, serializer, Optional.empty()); } - public Result(ResourceLocation id, BlockStateIngredient ingredient, BlockPropertyPair result, RecipeSerializer serializer, @Nullable ResourceLocation function) { + public Result(ResourceLocation id, BlockStateIngredient ingredient, BlockPropertyPair result, RecipeSerializer serializer, Optional function) { this.id = id; this.ingredient = ingredient; this.result = result; @@ -110,7 +109,7 @@ public void serializeRecipeData(JsonObject json) { } else { json.add("result", BlockStateIngredient.of(this.result).toJson(false)); } - if (this.function != null) { + if (this.function.isPresent()) { json.addProperty("mcfunction", this.function.toString()); } } diff --git a/src/main/java/com/aetherteam/nitrogen/recipe/recipes/AbstractBlockStateRecipe.java b/src/main/java/com/aetherteam/nitrogen/recipe/recipes/AbstractBlockStateRecipe.java index 6b7ab17..b1c9611 100644 --- a/src/main/java/com/aetherteam/nitrogen/recipe/recipes/AbstractBlockStateRecipe.java +++ b/src/main/java/com/aetherteam/nitrogen/recipe/recipes/AbstractBlockStateRecipe.java @@ -10,23 +10,22 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.properties.Property; -import javax.annotation.Nullable; import java.util.Map; +import java.util.Optional; public abstract class AbstractBlockStateRecipe implements BlockStateRecipe { protected final RecipeType type; protected final BlockStateIngredient ingredient; protected final BlockPropertyPair result; - @Nullable - protected final CommandFunction.CacheableFunction function; - private final String functionString; + protected final Optional function; + private final Optional functionString; - public AbstractBlockStateRecipe(RecipeType type, BlockStateIngredient ingredient, BlockPropertyPair result, @Nullable String functionString) { + public AbstractBlockStateRecipe(RecipeType type, BlockStateIngredient ingredient, BlockPropertyPair result, Optional functionString) { this.type = type; this.ingredient = ingredient; this.result = result; - this.functionString = functionString == null || functionString.isBlank() ? "" : functionString; - this.function = BlockStateRecipeUtil.buildMCFunction(this.functionString); + this.functionString = functionString.isEmpty() || functionString.get().isBlank() ? Optional.empty() : functionString; + this.function = this.functionString.map(BlockStateRecipeUtil::buildMCFunction); } /** @@ -40,7 +39,7 @@ public boolean set(Level level, BlockPos pos, BlockState oldState) { if (this.matches(level, pos, oldState)) { BlockState newState = this.getResultState(oldState); level.setBlockAndUpdate(pos, newState); - BlockStateRecipeUtil.executeFunction(level, pos, this.getFunction()); + this.getFunction().ifPresent((mcFunction) -> BlockStateRecipeUtil.executeFunction(level, pos, mcFunction)); return true; } return false; @@ -80,13 +79,12 @@ public BlockPropertyPair getResult() { return this.result; } - @Nullable @Override - public CommandFunction.CacheableFunction getFunction() { + public Optional getFunction() { return this.function; } - public String getFunctionString() { + public Optional getFunctionString() { return this.functionString; } } diff --git a/src/main/java/com/aetherteam/nitrogen/recipe/recipes/BlockStateRecipe.java b/src/main/java/com/aetherteam/nitrogen/recipe/recipes/BlockStateRecipe.java index 21c6ee5..67012ea 100644 --- a/src/main/java/com/aetherteam/nitrogen/recipe/recipes/BlockStateRecipe.java +++ b/src/main/java/com/aetherteam/nitrogen/recipe/recipes/BlockStateRecipe.java @@ -11,7 +11,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; -import javax.annotation.Nullable; +import java.util.Optional; /** * Overrides anything container-related or item-related because these in-world recipes have no container and are {@link BlockState}-based. Instead, custom behavior is implemented by recipes that extend this. @@ -23,8 +23,7 @@ public interface BlockStateRecipe extends Recipe { BlockState getResultState(BlockState originalState); - @Nullable - CommandFunction.CacheableFunction getFunction(); + Optional getFunction(); @Override default boolean matches(Container container, Level level) { diff --git a/src/main/java/com/aetherteam/nitrogen/recipe/serializer/BlockStateRecipeSerializer.java b/src/main/java/com/aetherteam/nitrogen/recipe/serializer/BlockStateRecipeSerializer.java index 1035349..f2f2111 100644 --- a/src/main/java/com/aetherteam/nitrogen/recipe/serializer/BlockStateRecipeSerializer.java +++ b/src/main/java/com/aetherteam/nitrogen/recipe/serializer/BlockStateRecipeSerializer.java @@ -12,26 +12,20 @@ import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.item.crafting.RecipeSerializer; +import java.util.Optional; + public class BlockStateRecipeSerializer implements RecipeSerializer { - private final Function3 factory; + private final Function3, T> factory; - private final MapCodec flatCodec; private final Codec codec; - public BlockStateRecipeSerializer(Function3 factory) { + public BlockStateRecipeSerializer(Function3, T> factory) { this.factory = factory; - - this.flatCodec = RecordCodecBuilder.mapCodec(inst -> inst.group( + this.codec = RecordCodecBuilder.create(inst -> inst.group( BlockStateIngredient.CODEC.fieldOf("ingredient").forGetter(AbstractBlockStateRecipe::getIngredient), BlockPropertyPair.BLOCKSTATE_CODEC.fieldOf("result").forGetter(AbstractBlockStateRecipe::getResult), - Codec.STRING.fieldOf("mcfunction").orElse("").forGetter(AbstractBlockStateRecipe::getFunctionString) + Codec.STRING.optionalFieldOf("mcfunction").forGetter(AbstractBlockStateRecipe::getFunctionString) ).apply(inst, this.factory)); - - this.codec = this.flatCodec.codec(); - } - - public MapCodec flatCodec() { - return this.flatCodec; } @Override @@ -44,15 +38,16 @@ public T fromNetwork(FriendlyByteBuf buffer) { BlockStateIngredient ingredient = BlockStateIngredient.fromNetwork(buffer); BlockPropertyPair result = BlockStateRecipeUtil.readPair(buffer); String functionString = buffer.readUtf(); - return this.factory.apply(ingredient, result, functionString); + Optional function = functionString.isBlank() ? Optional.empty() : Optional.of(functionString); + return this.factory.apply(ingredient, result, function); } @Override public void toNetwork(FriendlyByteBuf buffer, T recipe) { recipe.getIngredient().toNetwork(buffer); BlockStateRecipeUtil.writePair(buffer, recipe.getResult()); - CommandFunction.CacheableFunction function = recipe.getFunction(); - buffer.writeUtf(function != null && function.getId() != null ? function.getId().toString() : ""); + Optional function = recipe.getFunction(); + buffer.writeUtf(function.isPresent() && function.get().getId() != null ? function.get().getId().toString() : ""); } }