Skip to content

Commit

Permalink
improv: use optional fields for recipes instead of nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
bconlon1 committed Feb 24, 2024
1 parent f450d52 commit cf74abc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResourceLocation> function;

public BlockStateRecipeBuilder(BlockPropertyPair result, BlockStateIngredient ingredient, BlockStateRecipeSerializer<?> serializer) {
this.result = result;
Expand All @@ -50,7 +50,7 @@ public RecipeBuilder group(@Nullable String groupName) {
return this;
}

public RecipeBuilder function(@Nullable ResourceLocation function) {
public RecipeBuilder function(Optional<ResourceLocation> function) {
this.function = function;
return this;
}
Expand Down Expand Up @@ -87,14 +87,13 @@ public static class Result implements FinishedRecipe {
private final BlockStateIngredient ingredient;
private final BlockPropertyPair result;
private final RecipeSerializer<? extends AbstractBlockStateRecipe> serializer;
@Nullable
private final ResourceLocation function;
private final Optional<ResourceLocation> function;

public Result(ResourceLocation id, BlockStateIngredient ingredient, BlockPropertyPair result, RecipeSerializer<? extends AbstractBlockStateRecipe> serializer) {
this(id, ingredient, result, serializer, null);
this(id, ingredient, result, serializer, Optional.empty());
}

public Result(ResourceLocation id, BlockStateIngredient ingredient, BlockPropertyPair result, RecipeSerializer<? extends AbstractBlockStateRecipe> serializer, @Nullable ResourceLocation function) {
public Result(ResourceLocation id, BlockStateIngredient ingredient, BlockPropertyPair result, RecipeSerializer<? extends AbstractBlockStateRecipe> serializer, Optional<ResourceLocation> function) {
this.id = id;
this.ingredient = ingredient;
this.result = result;
Expand All @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandFunction.CacheableFunction> function;
private final Optional<String> functionString;

public AbstractBlockStateRecipe(RecipeType<?> type, BlockStateIngredient ingredient, BlockPropertyPair result, @Nullable String functionString) {
public AbstractBlockStateRecipe(RecipeType<?> type, BlockStateIngredient ingredient, BlockPropertyPair result, Optional<String> 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);
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -80,13 +79,12 @@ public BlockPropertyPair getResult() {
return this.result;
}

@Nullable
@Override
public CommandFunction.CacheableFunction getFunction() {
public Optional<CommandFunction.CacheableFunction> getFunction() {
return this.function;
}

public String getFunctionString() {
public Optional<String> getFunctionString() {
return this.functionString;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,8 +23,7 @@ public interface BlockStateRecipe extends Recipe<Container> {

BlockState getResultState(BlockState originalState);

@Nullable
CommandFunction.CacheableFunction getFunction();
Optional<CommandFunction.CacheableFunction> getFunction();

@Override
default boolean matches(Container container, Level level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,20 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.item.crafting.RecipeSerializer;

import java.util.Optional;

public class BlockStateRecipeSerializer<T extends AbstractBlockStateRecipe> implements RecipeSerializer<T> {
private final Function3<BlockStateIngredient, BlockPropertyPair, String, T> factory;
private final Function3<BlockStateIngredient, BlockPropertyPair, Optional<String>, T> factory;

private final MapCodec<T> flatCodec;
private final Codec<T> codec;

public BlockStateRecipeSerializer(Function3<BlockStateIngredient, BlockPropertyPair, String, T> factory) {
public BlockStateRecipeSerializer(Function3<BlockStateIngredient, BlockPropertyPair, Optional<String>, 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<T> flatCodec() {
return this.flatCodec;
}

@Override
Expand All @@ -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<String> 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<CommandFunction.CacheableFunction> function = recipe.getFunction();
buffer.writeUtf(function.isPresent() && function.get().getId() != null ? function.get().getId().toString() : "");
}
}

0 comments on commit cf74abc

Please sign in to comment.