/*
 * This file is part of TechReborn, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2020 TechReborn
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package reborncore.common.crafting;

import com.google.gson.JsonObject;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DefaultedList;
import reborncore.common.crafting.ingredient.RebornIngredient;
import reborncore.common.fluid.FluidValue;
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, DefaultedList<RebornIngredient> ingredients, DefaultedList<ItemStack> outputs, int power, int time) {
		super(type, name, ingredients, outputs, power, time);
	}

	public RebornFluidRecipe(RebornRecipeType<?> type, Identifier name, DefaultedList<RebornIngredient> ingredients, DefaultedList<ItemStack> outputs, int power, int time, FluidInstance fluidInstance) {
		this(type, name, ingredients, outputs, power, time);
		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);

			FluidValue value = FluidValue.BUCKET;
			if(tank.has("amount")){
				value = FluidValue.parseFluidValue(tank.get("amount"));
			}

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

	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().equalOrMoreThan(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().equalOrMoreThan(recipeFluid.getAmount())) {
				tankFluid.subtractAmount(recipeFluid.getAmount());
				return true;
			}
		}
		return false;
	}

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

@@ -1283,427 +1283,427 @@
 aft.
-item.ItemStack;%0Aimport net.minecraft.util.DefaultedList;%0Aimport reborncore.common.crafting.ingredient.RebornIngredient;%0Aimport reborncore.common.fluid.FluidValue;%0Aimport reborncore.common.fluid.container.FluidInstance;%0Aimport net.minecraft.block.entity.BlockEntity;%0Aimport net.minecraft.fluid.Fluid;%0Aimport net.minecraft.util.Identifier;%0Aimport net.minecraft.util.JsonHelper;%0Aimport net.minecraft.util.registry.Registry
+block.entity.BlockEntity;%0Aimport net.minecraft.fluid.Fluid;%0Aimport net.minecraft.item.ItemStack;%0Aimport net.minecraft.util.DefaultedList;%0Aimport net.minecraft.util.Identifier;%0Aimport net.minecraft.util.JsonHelper;%0Aimport net.minecraft.util.registry.Registry;%0Aimport reborncore.common.crafting.ingredient.RebornIngredient;%0Aimport reborncore.common.fluid.FluidValue;%0Aimport reborncore.common.fluid.container.FluidInstance
 ;%0Aim
@@ -3038,24 +3038,374 @@
 e);%0A%09%09%7D%0A%09%7D%0A%0A
+%09@Override%0A%09public void serialize(JsonObject jsonObject) %7B%0A%09%09super.serialize(jsonObject);%0A%09%09%0A%09%09JsonObject tankObject = new JsonObject();%0A%09%09tankObject.addProperty(%22fluid%22, Registry.FLUID.getId(fluidInstance.getFluid()).toString());%0A%09%09tankObject.addProperty(%22value%22, fluidInstance.getAmount().getRawValue());%0A%0A%09%09jsonObject.add(%22tank%22, tankObject);%0A%09%7D%0A%0A
 %09public abst

Fix https://github.com/TechReborn/TechReborn/issues/2042