package reborncore.common.crafting;

import com.google.gson.JsonObject;
import reborncore.common.fluid.container.FluidInstance;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry;
import reborncore.common.util.Tank;

import javax.annotation.Nonnull;

public abstract class RebornFluidRecipe extends RebornRecipe {

	@Nonnull
	private FluidInstance fluidInstance = FluidInstance.EMPTY;

	public RebornFluidRecipe(RebornRecipeType<?> type, Identifier name) {
		super(type, name);
	}

	public RebornFluidRecipe(RebornRecipeType<?> type, Identifier name, FluidInstance fluidInstance) {
		this(type, name);
		this.fluidInstance = fluidInstance;
	}

	@Override
	public void deserialize(JsonObject jsonObject) {
		super.deserialize(jsonObject);
		if(jsonObject.has("tank")){
			JsonObject tank = jsonObject.get("tank").getAsJsonObject();

			Identifier identifier = new Identifier(JsonHelper.getString(tank, "fluid"));
			Fluid fluid = Registry.FLUID.get(identifier);

			int amount = 1000;
			if(tank.has("amount")){
				amount = JsonHelper.getInt(tank, "amount");
			}

			fluidInstance = new FluidInstance(fluid, amount);
		}
	}

	public abstract Tank getTank(BlockEntity be);

	@Override
	public boolean canCraft(BlockEntity be) {
		final FluidInstance recipeFluid = fluidInstance;
		final FluidInstance tankFluid = getTank(be).getFluidInstance();
		if (fluidInstance.isEmpty()) {
			return true;
		}
		if (tankFluid.isEmpty()) {
			return false;
		}
		if (tankFluid.getFluid().equals(recipeFluid.getFluid())) {
			if (tankFluid.getAmount() >= recipeFluid.getAmount()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean onCraft(BlockEntity be) {
		final FluidInstance recipeFluid = fluidInstance;
		final FluidInstance tankFluid = getTank(be).getFluidInstance();
		if (fluidInstance.isEmpty()) {
			return true;
		}
		if (tankFluid.isEmpty()) {
			return false;
		}
		if (tankFluid.getFluid().equals(recipeFluid.getFluid())) {
			if (tankFluid.getAmount() >= recipeFluid.getAmount()) {
				tankFluid.subtractAmount(recipeFluid.getAmount());
				return true;
			}
		}
		return false;
	}

	@Nonnull
	public FluidInstance getFluidInstance() {
		return fluidInstance;
	}
}

@@ -65,16 +65,157 @@
 Object;%0A
+import net.minecraft.item.ItemStack;%0Aimport net.minecraft.util.DefaultedList;%0Aimport reborncore.common.crafting.ingredient.RebornIngredient;%0A
 import r
@@ -834,16 +834,347 @@
 er name,
+ DefaultedList%3CRebornIngredient%3E ingredients, DefaultedList%3CItemStack%3E outputs, int power, int time) %7B%0A%09%09super(type, name, ingredients, outputs, power, time);%0A%09%7D%0A%0A%09public RebornFluidRecipe(RebornRecipeType%3C?%3E type, Identifier name, DefaultedList%3CRebornIngredient%3E ingredients, DefaultedList%3CItemStack%3E outputs, int power, int time,
  FluidIn
@@ -1210,24 +1210,59 @@
 s(type, name
+, ingredients, outputs, power, time
 );%0A%09%09this.fl

Fix constructor for RebornFluidRecipe (#124)