2022-04-02 04:50:39 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.crafting;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParseException;
|
2024-03-03 01:20:53 +01:00
|
|
|
import net.minecraft.core.RegistryAccess;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.data.recipes.FinishedRecipe;
|
|
|
|
import net.minecraft.network.FriendlyByteBuf;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.util.GsonHelper;
|
|
|
|
import net.minecraft.world.Container;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.crafting.Recipe;
|
|
|
|
import net.minecraft.world.item.crafting.RecipeSerializer;
|
|
|
|
import net.minecraft.world.item.crafting.RecipeType;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.material.Fluid;
|
2022-04-02 04:50:39 +02:00
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class LiquidFuelRecipe implements Recipe<Container> {
|
2022-04-02 04:50:39 +02:00
|
|
|
public static String NAME = "liquid_fuel";
|
|
|
|
private FluidStack fuel;
|
|
|
|
private int burnTime;
|
|
|
|
private int totalEnergy;
|
|
|
|
private ResourceLocation id;
|
|
|
|
|
2022-04-02 23:41:52 +02:00
|
|
|
/**
|
|
|
|
* Oil generator recipe
|
|
|
|
* @param id ResourceLocation of the recipe
|
|
|
|
* @param fuel The fluid
|
|
|
|
* @param totalEnergy The total power generated.
|
|
|
|
* @param burnTime The length the fluid burns for, in ticks.
|
|
|
|
*/
|
2022-04-02 04:50:39 +02:00
|
|
|
public LiquidFuelRecipe(ResourceLocation id, FluidStack fuel, int totalEnergy, int burnTime) {
|
|
|
|
this.fuel = fuel;
|
|
|
|
this.burnTime = burnTime;
|
|
|
|
this.totalEnergy = totalEnergy;
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getBurnTime() {
|
|
|
|
return burnTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getTotalEnergy() {
|
|
|
|
return totalEnergy;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public boolean matches(@Nonnull Container pInv,@Nonnull Level pLevel) {
|
2022-04-02 04:50:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean matches(FluidStack stack) {
|
|
|
|
return this.fuel.isFluidEqual(stack);
|
|
|
|
}
|
|
|
|
|
2023-01-15 22:46:42 +01:00
|
|
|
@Override
|
|
|
|
public boolean isSpecial() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-02 23:41:52 +02:00
|
|
|
public int getFuelAmount() {
|
|
|
|
return this.fuel.getAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
public FluidStack getFuel() {
|
|
|
|
return fuel;
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:39 +02:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack assemble(Container pInv, RegistryAccess pRegistryAccess) {
|
2022-04-02 04:50:39 +02:00
|
|
|
return ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canCraftInDimensions(int pWidth, int pHeight) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-03 01:20:53 +01:00
|
|
|
public ItemStack getResultItem(RegistryAccess pRegistryAccess) {
|
2022-04-02 04:50:39 +02:00
|
|
|
return ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeSerializer<?> getSerializer() {
|
2022-04-02 04:50:39 +02:00
|
|
|
return ActuallyRecipes.LIQUID_FUEL_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeType<?> getType() {
|
2022-04-02 04:50:39 +02:00
|
|
|
return ActuallyRecipes.Types.LIQUID_FUEL;
|
|
|
|
}
|
|
|
|
|
2024-03-03 01:20:53 +01:00
|
|
|
public static class Serializer implements RecipeSerializer<LiquidFuelRecipe> {
|
2022-04-02 04:50:39 +02:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public LiquidFuelRecipe fromJson(@Nonnull ResourceLocation pId, JsonObject pJson) {
|
|
|
|
JsonObject ingredient = pJson.getAsJsonObject("ingredient");
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
ResourceLocation fluidRes = new ResourceLocation(GsonHelper.getAsString(ingredient, "fluid"));
|
2022-04-02 04:50:39 +02:00
|
|
|
Fluid fluid = ForgeRegistries.FLUIDS.getValue(fluidRes);
|
|
|
|
if (fluid == null)
|
|
|
|
throw new JsonParseException("Unknown fluid '" + fluidRes + "'");
|
2024-03-02 21:23:08 +01:00
|
|
|
int inputAmount = GsonHelper.getAsInt(ingredient, "amount", 50);
|
2022-04-02 04:50:39 +02:00
|
|
|
FluidStack input = new FluidStack(fluid, inputAmount);
|
|
|
|
|
|
|
|
JsonObject result = pJson.getAsJsonObject("result");
|
|
|
|
int totalEnergy = result.get("total_energy").getAsInt();
|
|
|
|
int burnTime = result.get("burn_time").getAsInt();
|
|
|
|
return new LiquidFuelRecipe(pId, input, totalEnergy, burnTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public LiquidFuelRecipe fromNetwork(@Nonnull ResourceLocation pId, @Nonnull FriendlyByteBuf pBuffer) {
|
2022-04-02 04:50:39 +02:00
|
|
|
ResourceLocation inputRes = new ResourceLocation(pBuffer.readUtf());
|
|
|
|
int inputAmount = pBuffer.readInt();
|
|
|
|
Fluid inputFluid = ForgeRegistries.FLUIDS.getValue(inputRes);
|
|
|
|
if(inputFluid == null)
|
|
|
|
throw new JsonParseException("Unknown input fluid '" + inputRes + "'");
|
|
|
|
FluidStack input = new FluidStack(inputFluid, inputAmount);
|
|
|
|
|
|
|
|
int totalEnergy = pBuffer.readInt();
|
|
|
|
int burnTime = pBuffer.readInt();
|
|
|
|
return new LiquidFuelRecipe(pId, input, totalEnergy, burnTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void toNetwork(@Nonnull FriendlyByteBuf pBuffer, LiquidFuelRecipe pRecipe) {
|
2024-03-03 01:20:53 +01:00
|
|
|
pBuffer.writeUtf(ForgeRegistries.FLUIDS.getKey(pRecipe.fuel.getFluid()).toString());
|
2022-04-02 04:50:39 +02:00
|
|
|
pBuffer.writeInt(pRecipe.fuel.getAmount());
|
|
|
|
pBuffer.writeInt(pRecipe.totalEnergy);
|
|
|
|
pBuffer.writeInt(pRecipe.burnTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static class Result implements FinishedRecipe {
|
2022-04-02 04:50:39 +02:00
|
|
|
private FluidStack fuel;
|
|
|
|
private int burnTime;
|
|
|
|
private int totalEnergy;
|
|
|
|
private ResourceLocation id;
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public Result(ResourceLocation id, FluidStack fuel, int totalEnergy, int burnTime) {
|
2022-04-02 04:50:39 +02:00
|
|
|
this.fuel = fuel;
|
|
|
|
this.burnTime = burnTime;
|
|
|
|
this.totalEnergy = totalEnergy;
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void serializeRecipeData(JsonObject pJson) {
|
|
|
|
JsonObject ingredient = new JsonObject();
|
2024-03-03 01:20:53 +01:00
|
|
|
ingredient.addProperty("fluid", ForgeRegistries.FLUIDS.getKey(fuel.getFluid()).toString());
|
2022-04-02 04:50:39 +02:00
|
|
|
ingredient.addProperty("amount", fuel.getAmount());
|
|
|
|
|
|
|
|
JsonObject result = new JsonObject();
|
|
|
|
result.addProperty("total_energy", totalEnergy);
|
|
|
|
result.addProperty("burn_time", burnTime);
|
|
|
|
|
|
|
|
pJson.add("ingredient", ingredient);
|
|
|
|
pJson.add("result", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public RecipeSerializer<?> getType() {
|
2022-04-02 04:50:39 +02:00
|
|
|
return ActuallyRecipes.LIQUID_FUEL_RECIPE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public JsonObject serializeAdvancement() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public ResourceLocation getAdvancementId() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|