Skip to content

Commit

Permalink
Remove empty cauldron contents
Browse files Browse the repository at this point in the history
Empty in most places was used as a wildcard, recipes normally just checked levels instead. Easier to just make nullable for wildcard and use water elsewhere
Fixes an error where cauldrons placed from vanilla show as empty
  • Loading branch information
KnightMiner committed Sep 12, 2020
1 parent a1c782d commit d9a3120
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 171 deletions.
Expand Up @@ -7,22 +7,22 @@
import io.netty.handler.codec.DecoderException;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.recipe.cauldron.contents.CauldronContentType;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyContentType;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.recipes.recipe.cauldron.contents.ColorContentType;
import knightminer.inspirations.recipes.recipe.cauldron.contents.CustomContentType;
import knightminer.inspirations.recipes.recipe.cauldron.contents.DyeContentType;
import knightminer.inspirations.recipes.recipe.cauldron.contents.FluidContentType;
import knightminer.inspirations.recipes.recipe.cauldron.contents.PotionContentType;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.DyeColor;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.potion.Potion;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.Lazy;

import javax.annotation.Nullable;

Expand All @@ -36,8 +36,8 @@ public class CauldronContentTypes {

/* Public constants */

/** Generic water type */
public static final EmptyContentType EMPTY = register("empty", new EmptyContentType());
/** Contains a specific fluid */
public static final CauldronContentType<Fluid> FLUID = register("fluid", new FluidContentType());

/** Contains an arbitrary color */
public static final CauldronContentType<Integer> COLOR = register("color", new ColorContentType());
Expand All @@ -48,12 +48,12 @@ public class CauldronContentTypes {
/** Contains a specific color */
public static final CauldronContentType<Potion> POTION = register("potion", new PotionContentType());

/** Contains a specific fluid */
public static final CauldronContentType<Fluid> FLUID = register("fluid", new FluidContentType());

/** Contains a specific fluid */
public static final CauldronContentType<ResourceLocation> CUSTOM = register("custom", new CustomContentType());

/** Default cauldron content type, return when reading if type invalid */
public static final Lazy<ICauldronContents> DEFAULT = Lazy.of(() -> FLUID.of(Fluids.WATER));

/**
* Registers a new content type
* @param name Name
Expand Down Expand Up @@ -157,7 +157,7 @@ public static ICauldronContents read(CompoundNBT nbt) {
}
}
}
return EmptyCauldronContents.INSTANCE;
return CauldronContentTypes.DEFAULT.get();
}

/**
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -11,7 +11,7 @@
public interface ICauldronState extends IEmptyInventory {
/**
* Gets the contents of the cauldron.
* Should return {@link knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents#INSTANCE} if {@link #getLevel()} returns 0. Should never return empty otherwise.
* If {@link #getLevel()} returns 0, value is indeterminate.
* @return Cauldron contents
*/
ICauldronContents getContents();
Expand Down
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.inventory.ICauldronState;
Expand All @@ -12,6 +12,7 @@
import net.minecraft.util.JSONUtils;
import net.minecraftforge.fluids.FluidStack;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -24,6 +25,7 @@ public abstract class AbstractCauldronRecipe implements ICauldronRecipeDisplay {
protected final ICauldronIngredient ingredient;
protected final LevelPredicate level;
protected final TemperaturePredicate temperature;
@Nullable
protected final ICauldronContents outputContents;

// values for JEI display
Expand All @@ -38,7 +40,7 @@ public abstract class AbstractCauldronRecipe implements ICauldronRecipeDisplay {
* @param temperature Predicate for required cauldron temperature
* @param output Output stack, use empty for no output
*/
public AbstractCauldronRecipe(ICauldronIngredient ingredient, LevelPredicate level, TemperaturePredicate temperature, ICauldronContents output) {
public AbstractCauldronRecipe(ICauldronIngredient ingredient, LevelPredicate level, TemperaturePredicate temperature, @Nullable ICauldronContents output) {
this.ingredient = ingredient;
this.level = level;
this.temperature = temperature;
Expand Down Expand Up @@ -85,10 +87,11 @@ public TemperaturePredicate getTemperature() {

@Override
public ICauldronContents getContentOutput() {
if (outputContents == EmptyCauldronContents.INSTANCE) {
// if output is null, display first input
if (outputContents == null) {
if (displayContents == null) {
List<ICauldronContents> inputs = getContentInputs();
displayContents = inputs.isEmpty() ? EmptyCauldronContents.INSTANCE : inputs.get(0);
displayContents = inputs.isEmpty() ? CauldronContentTypes.DEFAULT.get() : inputs.get(0);
}
return displayContents;
}
Expand Down
Expand Up @@ -5,7 +5,6 @@
import knightminer.inspirations.library.recipe.RecipeSerializers;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.CauldronIngredients;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.ingredient.SizedIngredient;
Expand Down Expand Up @@ -54,12 +53,12 @@ public class CauldronRecipe extends AbstractCauldronRecipe implements ICauldronR
* @param temperature Predicate for required cauldron temperature
* @param output Output stack, use empty for no output
* @param copyNBT If true, copies the input NBT to the output
* @param newContents Output contents, use {@link EmptyCauldronContents#INSTANCE} to keep old contents
* @param newContents Output contents, use {@code null} to keep old contents
* @param levelUpdate Level updater
* @param container Container output. If null, fetches container from the item. If empty, no container
*/
public CauldronRecipe(ResourceLocation id, String group, SizedIngredient input, ICauldronIngredient contents, LevelPredicate level, TemperaturePredicate temperature,
ItemStack output, boolean copyNBT, ICauldronContents newContents, LevelUpdate levelUpdate, @Nullable ItemStack container, SoundEvent sound) {
ItemStack output, boolean copyNBT, @Nullable ICauldronContents newContents, LevelUpdate levelUpdate, @Nullable ItemStack container, SoundEvent sound) {
super(contents, level, temperature, newContents);
this.id = id;
this.group = group;
Expand All @@ -77,7 +76,7 @@ public CauldronRecipe(ResourceLocation id, String group, SizedIngredient input,
@Override
public boolean matches(ICauldronInventory inv, World worldIn) {
// if this cauldron only supports simple recipes, block if the result is not simple
if (inv.isSimple() && !outputContents.isSimple()) {
if (inv.isSimple() && outputContents != null && !outputContents.isSimple()) {
return false;
}
// check common matches logic
Expand All @@ -93,7 +92,7 @@ public boolean matches(ICauldronInventory inv, World worldIn) {
public void handleRecipe(IModifyableCauldronInventory inventory) {
// update level
// only update contents if the level is not empty and we have new contents
if (!inventory.updateLevel(levelUpdate) && outputContents != EmptyCauldronContents.INSTANCE) {
if (!inventory.updateLevel(levelUpdate) && outputContents != null) {
inventory.setContents(outputContents);
}

Expand Down Expand Up @@ -207,7 +206,7 @@ public CauldronRecipe read(ResourceLocation id, JsonObject json) {
output = CraftingHelper.getItemStack(JSONUtils.getJsonObject(outputJson, "item"), true);
copyNBT = JSONUtils.getBoolean(outputJson, "copy_nbt", false);
}
ICauldronContents newContents = EmptyCauldronContents.INSTANCE;
ICauldronContents newContents = null;
if (outputJson.has("contents")) {
newContents = CauldronContentTypes.read(JSONUtils.getJsonObject(outputJson, "contents"));
}
Expand Down Expand Up @@ -246,7 +245,12 @@ public void write(PacketBuffer buffer, CauldronRecipe recipe) {
buffer.writeEnumValue(recipe.temperature);
buffer.writeItemStack(recipe.output);
buffer.writeBoolean(recipe.copyNBT);
CauldronContentTypes.write(recipe.outputContents, buffer);
if (recipe.outputContents != null) {
buffer.writeBoolean(true);
CauldronContentTypes.write(recipe.outputContents, buffer);
} else {
buffer.writeBoolean(false);
}
recipe.levelUpdate.write(buffer);
if (recipe.container == null) {
buffer.writeBoolean(false);
Expand All @@ -267,7 +271,10 @@ public CauldronRecipe read(ResourceLocation id, PacketBuffer buffer) {
TemperaturePredicate boiling = buffer.readEnumValue(TemperaturePredicate.class);
ItemStack output = buffer.readItemStack();
boolean copyNBT = buffer.readBoolean();
ICauldronContents newContents = CauldronContentTypes.read(buffer);
ICauldronContents newContents = null;
if (buffer.readBoolean()) {
newContents = CauldronContentTypes.read(buffer);
}
LevelUpdate levelUpdate = LevelUpdate.read(buffer);
ItemStack container = null;
if (buffer.readBoolean()) {
Expand Down
Expand Up @@ -4,7 +4,6 @@
import knightminer.inspirations.library.recipe.RecipeSerializers;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.CauldronIngredients;
import knightminer.inspirations.library.recipe.cauldron.contents.EmptyCauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.ingredient.SizedIngredient;
Expand Down Expand Up @@ -38,7 +37,8 @@ public class CauldronRecipeBuilder extends AbstractRecipeBuilder<CauldronRecipeB
private TemperaturePredicate temperature = TemperaturePredicate.ANY;
private ItemStack output = ItemStack.EMPTY;
private boolean copyNBT = false;
private ICauldronContents newContents = EmptyCauldronContents.INSTANCE;
@Nullable
private ICauldronContents newContents = null;
private LevelUpdate levelUpdate = LevelUpdate.IDENTITY;
@Nullable
private ItemStack container = null;
Expand Down Expand Up @@ -252,7 +252,7 @@ public void build(Consumer<IFinishedRecipe> consumer) {
build(consumer, Objects.requireNonNull(output.getItem().getRegistryName()));
return;
}
if (newContents != EmptyCauldronContents.INSTANCE) {
if (newContents != null) {
ResourceLocation name = nameFromContents(newContents);
if (name != null) {
build(consumer, name);
Expand Down Expand Up @@ -298,6 +298,7 @@ private static class Result implements IFinishedRecipe {
private final TemperaturePredicate temperature;
private final ItemStack output;
private final boolean copyNBT;
@Nullable
private final ICauldronContents newContents;
private final LevelUpdate levelUpdate;
@Nullable
Expand All @@ -307,7 +308,7 @@ private static class Result implements IFinishedRecipe {
private final Advancement.Builder advancementBuilder;
private final ResourceLocation advancementId;

private Result(ResourceLocation id, String group, SizedIngredient input, ICauldronIngredient contents, LevelPredicate level, TemperaturePredicate temperature, ItemStack output, boolean copyNBT, ICauldronContents newContents, LevelUpdate levelUpdate, @Nullable ItemStack container, @Nullable SoundEvent sound, Builder advancementBuilder, ResourceLocation advancementId) {
private Result(ResourceLocation id, String group, SizedIngredient input, ICauldronIngredient contents, LevelPredicate level, TemperaturePredicate temperature, ItemStack output, boolean copyNBT, @Nullable ICauldronContents newContents, LevelUpdate levelUpdate, @Nullable ItemStack container, @Nullable SoundEvent sound, Builder advancementBuilder, ResourceLocation advancementId) {
this.id = id;
this.group = group;
this.input = input;
Expand Down Expand Up @@ -373,7 +374,7 @@ public void serialize(JsonObject json) {
outputJson.add("container", toJson(container));
}
}
if (newContents != EmptyCauldronContents.INSTANCE) {
if (newContents != null) {
outputJson.add("contents", CauldronContentTypes.toJson(newContents));
}
if (levelUpdate != LevelUpdate.IDENTITY) {
Expand Down
Expand Up @@ -167,7 +167,7 @@ public void write(PacketBuffer buffer, CauldronTransform recipe) {
CauldronIngredients.write(recipe.ingredient, buffer);
buffer.writeEnumValue(recipe.temperature);
recipe.level.write(buffer);
CauldronContentTypes.write(recipe.outputContents, buffer);
CauldronContentTypes.write(recipe.outputContents == null ? CauldronContentTypes.DEFAULT.get() : recipe.outputContents, buffer);
buffer.writeVarInt(recipe.time);
buffer.writeResourceLocation(Objects.requireNonNull(recipe.sound.getRegistryName()));
}
Expand Down
Expand Up @@ -173,7 +173,7 @@ protected Stream<DisplayCauldronRecipe> getDisplayRecipes(ItemStack stack) {
List<ItemStack> inputs = Arrays.stream(DyeColor.values()).map(color -> Util.setColor(stack.copy(), color.getColorValue())).collect(Collectors.toList());
return Stream.of(DisplayCauldronRecipe.builder(1, 0)
.setItemInputs(inputs)
.setContentInputs(DisplayCauldronRecipe.WATER_CONTENTS.getValue())
.setContentInputs(DisplayCauldronRecipe.WATER_CONTENTS.get())
.setItemOutput(Util.clearColor(stack.copy()))
.build());
}
Expand Down

0 comments on commit d9a3120

Please sign in to comment.