Skip to content

Commit

Permalink
fix heat frame cooling recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Aug 4, 2024
1 parent d568ed7 commit 9eb3e5c
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@
* @param either the fluidstack OR fluid-tag/amount which must be contained in matching items
*/
public record FluidContainerIngredient(Either<FluidStack,TagWithAmount> either) implements ICustomIngredient {
public static final MapCodec<FluidContainerIngredient> CODEC = RecordCodecBuilder.mapCodec(builder -> builder.group(
public static final MapCodec<FluidContainerIngredient> MAP_CODEC = RecordCodecBuilder.mapCodec(builder -> builder.group(
Codec.either(FluidStack.CODEC, TagWithAmount.CODEC).fieldOf("fluid").forGetter(FluidContainerIngredient::either)
).apply(builder, FluidContainerIngredient::new));
public static final Codec<FluidContainerIngredient> CODEC = RecordCodecBuilder.create(builder -> builder.group(
Codec.either(FluidStack.CODEC, TagWithAmount.CODEC).fieldOf("fluid").forGetter(FluidContainerIngredient::either)
).apply(builder, FluidContainerIngredient::new));
public static final StreamCodec<RegistryFriendlyByteBuf, FluidContainerIngredient> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.either(FluidStack.STREAM_CODEC, TagWithAmount.STREAM_CODEC), FluidContainerIngredient::either,
FluidContainerIngredient::new
);

private static final Supplier<List<Block>> TANK_BLOCKS = Suppliers.memoize(() ->
Stream.of(
Expand Down Expand Up @@ -105,6 +112,10 @@ public IngredientType<?> getType() {
return PneumaticRegistry.getInstance().getCustomIngredientTypes().fluidContainerType().get();
}

public int amount() {
return either.map(FluidStack::getAmount, TagWithAmount::amount);
}

public record TagWithAmount(TagKey<Fluid> tag, int amount) {
public static final Codec<TagWithAmount> CODEC = RecordCodecBuilder.create(builder -> builder.group(
TagKey.codec(Registries.FLUID).fieldOf("tag").forGetter(TagWithAmount::tag),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
package me.desht.pneumaticcraft.api.crafting.recipe;

import com.mojang.datafixers.util.Either;
import me.desht.pneumaticcraft.api.crafting.ingredient.FluidContainerIngredient;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.fluids.capability.IFluidHandlerItem;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;

import java.util.concurrent.ThreadLocalRandom;

Expand All @@ -33,7 +32,7 @@ public abstract class HeatFrameCoolingRecipe extends PneumaticCraftRecipe {
*
* @return the input ingredient
*/
public abstract Either<Ingredient, SizedFluidIngredient> getInput();
public abstract Either<Ingredient, FluidContainerIngredient> getInput();

/**
* Get the output item. This does not take into account any bonus multiplier.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"neoforge:conditions": [
{
"type": "neoforge:mod_loaded",
"modid": "patchouli"
}
],
"parent": "minecraft:recipes/root",
"criteria": {
"has_ingot_iron_compressed": {
"conditions": {
"items": [
{
"items": "pneumaticcraft:ingot_iron_compressed"
}
]
},
"trigger": "minecraft:inventory_changed"
},
"has_the_recipe": {
"conditions": {
"recipe": "patchouli:guide_book"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_the_recipe",
"has_ingot_iron_compressed"
]
],
"rewards": {
"recipes": [
"patchouli:guide_book"
]
}
}
25 changes: 25 additions & 0 deletions src/generated/resources/data/patchouli/recipe/guide_book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"neoforge:conditions": [
{
"type": "neoforge:mod_loaded",
"modid": "patchouli"
}
],
"type": "minecraft:crafting_shapeless",
"category": "misc",
"ingredients": [
{
"item": "pneumaticcraft:ingot_iron_compressed"
},
{
"item": "minecraft:book"
}
],
"result": {
"components": {
"patchouli:book": "pneumaticcraft:book"
},
"count": 1,
"id": "patchouli:guide_book"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"type": "pneumaticcraft:heat_frame_cooling",
"input": {
"amount": 1000,
"tag": "minecraft:water"
"fluid": {
"amount": 1000,
"tag": "minecraft:water"
}
},
"output": {
"count": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"bonusLimit": 0.5,
"bonusMultiplier": 0.025,
"input": {
"amount": 1000,
"tag": "minecraft:lava"
"fluid": {
"amount": 1000,
"tag": "minecraft:lava"
}
},
"output": {
"count": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"bonusLimit": 0.75,
"bonusMultiplier": 0.01,
"input": {
"amount": 1000,
"tag": "pneumaticcraft:plastic"
"fluid": {
"amount": 1000,
"tag": "pneumaticcraft:plastic"
}
},
"output": {
"count": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,35 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import me.desht.pneumaticcraft.api.crafting.ingredient.FluidContainerIngredient;
import me.desht.pneumaticcraft.api.crafting.recipe.HeatFrameCoolingRecipe;
import me.desht.pneumaticcraft.common.registry.ModRecipeSerializers;
import me.desht.pneumaticcraft.common.registry.ModRecipeTypes;
import me.desht.pneumaticcraft.common.util.IOHelper;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;

import java.util.Collection;

public class HeatFrameCoolingRecipeImpl extends HeatFrameCoolingRecipe {
// cache the highest threshold temperature of all recipes, to reduce the recipe searching heat frames need to do
private static int maxThresholdTemp = Integer.MIN_VALUE;

public final Either<Ingredient,SizedFluidIngredient> input;
public final Either<Ingredient,FluidContainerIngredient> input;
private final int temperature;
public final ItemStack output;
private final float bonusMultiplier;
private final float bonusLimit;

public HeatFrameCoolingRecipeImpl(Either<Ingredient, SizedFluidIngredient> input, int temperature, ItemStack output) {
public HeatFrameCoolingRecipeImpl(Either<Ingredient, FluidContainerIngredient> input, int temperature, ItemStack output) {
this(input, temperature, output, 0f, 0f);
}

public HeatFrameCoolingRecipeImpl(Either<Ingredient,SizedFluidIngredient> input, int temperature, ItemStack output, float bonusMultiplier, float bonusLimit) {
public HeatFrameCoolingRecipeImpl(Either<Ingredient,FluidContainerIngredient> input, int temperature, ItemStack output, float bonusMultiplier, float bonusLimit) {
this.input = input;
this.temperature = temperature;
this.output = output;
Expand All @@ -60,7 +58,7 @@ public HeatFrameCoolingRecipeImpl(Either<Ingredient,SizedFluidIngredient> input,
}

@Override
public Either<Ingredient,SizedFluidIngredient> getInput() {
public Either<Ingredient, FluidContainerIngredient> getInput() {
return input;
}

Expand Down Expand Up @@ -88,16 +86,10 @@ public float getBonusLimit() {
public boolean matches(ItemStack stack) {
return input.map(
ingredient -> ingredient.test(stack),
fluidIngredient -> testForContainedFluid(fluidIngredient, stack)
fluidIngredient -> fluidIngredient.test(stack)
);
}

private boolean testForContainedFluid(SizedFluidIngredient fluidIngredient, ItemStack stack) {
return IOHelper.getFluidHandlerForItem(stack)
.map(handler -> fluidIngredient.test(handler.drain(fluidIngredient.amount(), IFluidHandler.FluidAction.SIMULATE)))
.orElse(false);
}

@Override
public RecipeSerializer<?> getSerializer() {
return ModRecipeSerializers.HEAT_FRAME_COOLING.get();
Expand Down Expand Up @@ -127,7 +119,7 @@ public static int getMaxThresholdTemp(Level world) {
}

public interface IFactory<T extends HeatFrameCoolingRecipe> {
T create(Either<Ingredient,SizedFluidIngredient> input, int temperature, ItemStack out, float bonusMultiplier, float bonusLimit);
T create(Either<Ingredient,FluidContainerIngredient> input, int temperature, ItemStack out, float bonusMultiplier, float bonusLimit);
}

public static class Serializer<T extends HeatFrameCoolingRecipe> implements RecipeSerializer<T> {
Expand All @@ -136,7 +128,7 @@ public static class Serializer<T extends HeatFrameCoolingRecipe> implements Reci

public Serializer(IFactory<T> factory) {
codec = RecordCodecBuilder.mapCodec(inst -> inst.group(
Codec.either(Ingredient.CODEC_NONEMPTY, SizedFluidIngredient.FLAT_CODEC).fieldOf("input")
Codec.either(Ingredient.CODEC_NONEMPTY, FluidContainerIngredient.CODEC).fieldOf("input")
.forGetter(HeatFrameCoolingRecipe::getInput),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("temperature")
.forGetter(HeatFrameCoolingRecipe::getThresholdTemperature),
Expand All @@ -148,7 +140,7 @@ public Serializer(IFactory<T> factory) {
.forGetter(HeatFrameCoolingRecipe::getBonusLimit)
).apply(inst, factory::create));
streamCodec = StreamCodec.composite(
ByteBufCodecs.either(Ingredient.CONTENTS_STREAM_CODEC, SizedFluidIngredient.STREAM_CODEC), HeatFrameCoolingRecipe::getInput,
ByteBufCodecs.either(Ingredient.CONTENTS_STREAM_CODEC, FluidContainerIngredient.STREAM_CODEC), HeatFrameCoolingRecipe::getInput,
ByteBufCodecs.INT, HeatFrameCoolingRecipe::getThresholdTemperature,
ItemStack.STREAM_CODEC, HeatFrameCoolingRecipe::getOutput,
ByteBufCodecs.FLOAT, HeatFrameCoolingRecipe::getBonusMultiplier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class ModIngredientTypes {
= DeferredRegister.create(NeoForgeRegistries.INGREDIENT_TYPES, Names.MOD_ID);

public static final Supplier<IngredientType<FluidContainerIngredient>> FLUID_CONTAINER
= INGREDIENT_TYPES.register("fluid_container", () -> new IngredientType<>(FluidContainerIngredient.CODEC));
= INGREDIENT_TYPES.register("fluid_container",
() -> new IngredientType<>(FluidContainerIngredient.MAP_CODEC, FluidContainerIngredient.STREAM_CODEC));


public enum Getter implements CustomIngredientTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.blamejared.crafttweaker.api.fluid.MCFluidStack;
import com.blamejared.crafttweaker.api.ingredient.IIngredientWithAmount;
import com.blamejared.crafttweaker.api.item.IItemStack;
import me.desht.pneumaticcraft.api.crafting.ingredient.FluidContainerIngredient;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
Expand Down Expand Up @@ -72,4 +73,12 @@ private static FluidIngredient flattenIngredients(Stream<FluidIngredient> stream
.toArray(Fluid[]::new)
);
}

public static FluidContainerIngredient toFluidContainerIngredient(CTFluidIngredient ingredient) {
return ingredient.mapTo(
fluidStack -> FluidContainerIngredient.of(fluidStack.getFluid(), (int) fluidStack.getAmount()),
FluidContainerIngredient::of,
stream -> stream.findFirst().orElseThrow()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void addRecipe(String name, CTFluidIngredient inputFluid, IItemStack outp
CraftTweakerAPI.apply(new ActionAddRecipe<>(this,
new RecipeHolder<>(ResourceLocation.fromNamespaceAndPath("crafttweaker", fixRecipeName(name)),
new HeatFrameCoolingRecipeImpl(
Either.right(CTUtils.toSizedFluidIngredient(inputFluid)),
Either.right(CTUtils.toFluidContainerIngredient(inputFluid)),
temperature,
output.getInternal(),
bonusMult, bonusLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import mezz.jei.api.gui.drawable.IDrawableAnimated;
import mezz.jei.api.gui.drawable.IDrawableStatic;
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
import mezz.jei.api.neoforge.NeoForgeTypes;
import mezz.jei.api.recipe.IFocusGroup;
import mezz.jei.api.recipe.RecipeIngredientRole;
import net.minecraft.ChatFormatting;
Expand All @@ -37,7 +36,6 @@
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static me.desht.pneumaticcraft.common.util.PneumaticCraftUtils.xlate;
Expand Down Expand Up @@ -65,8 +63,8 @@ public void setRecipe(IRecipeLayoutBuilder builder, HeatFrameCoolingRecipe recip
recipe.getInput()
.ifLeft(ingredient -> builder.addSlot(RecipeIngredientRole.INPUT, 1, 1)
.addIngredients(ingredient))
.ifRight(sizedFluidIngredient -> builder.addSlot(RecipeIngredientRole.INPUT, 1, 1)
.addIngredients(NeoForgeTypes.FLUID_STACK, Arrays.asList(sizedFluidIngredient.getFluids())));
.ifRight(fluidContainerIngredient -> builder.addSlot(RecipeIngredientRole.INPUT, 1, 1)
.addIngredients(VanillaTypes.ITEM_STACK, fluidContainerIngredient.getItems().toList()));

builder.addSlot(RecipeIngredientRole.OUTPUT, 65, 1).addItemStack(recipe.getOutput());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1407,13 +1407,13 @@ protected void buildRecipes(RecipeOutput consumer) {
.save(consumer, RL("explosion_crafting/wheat_flour"));

// heat frame cooling
heatFrameCooling(SizedFluidIngredient.of(FluidTags.WATER, 1000), 273,
heatFrameCooling(FluidContainerIngredient.of(FluidTags.WATER, 1000), 273,
new ItemStack(Blocks.ICE))
.save(consumer, RL("heat_frame_cooling/ice"));
heatFrameCooling(SizedFluidIngredient.of(FluidTags.LAVA, 1000), 273,
heatFrameCooling(FluidContainerIngredient.of(FluidTags.LAVA, 1000), 273,
new ItemStack(Blocks.OBSIDIAN), 0.025f, 0.5f)
.save(consumer, RL("heat_frame_cooling/obsidian"));
heatFrameCooling(SizedFluidIngredient.of(PneumaticCraftTags.Fluids.PLASTIC, 1000), 273,
heatFrameCooling(FluidContainerIngredient.of(PneumaticCraftTags.Fluids.PLASTIC, 1000), 273,
new ItemStack(ModItems.PLASTIC.get()), 0.01f, 0.75f)
.save(consumer, RL("heat_frame_cooling/plastic"));

Expand Down Expand Up @@ -1819,15 +1819,15 @@ private RecipeBuilder heatFrameCooling(Ingredient ingredient, int maxTemp, ItemS
return heatFrameCooling(ingredient, maxTemp, result, 0f, 0f);
}

private RecipeBuilder heatFrameCooling(SizedFluidIngredient ingredient, int maxTemp, ItemStack result) {
private RecipeBuilder heatFrameCooling(FluidContainerIngredient ingredient, int maxTemp, ItemStack result) {
return heatFrameCooling(ingredient, maxTemp, result, 0f, 0f);
}

private RecipeBuilder heatFrameCooling(Ingredient ingredient, int maxTemp, ItemStack result, float bonusMult, float bonusLimit) {
return new HeatFrameCoolingRecipeBuilder(Either.left(ingredient), maxTemp, result, bonusMult, bonusLimit)
.unlockedBy(getHasName(ModItems.HEAT_FRAME.get()), has(ModItems.HEAT_FRAME.get()));
}
private RecipeBuilder heatFrameCooling(SizedFluidIngredient ingredient, int maxTemp, ItemStack result, float bonusMult, float bonusLimit) {
private RecipeBuilder heatFrameCooling(FluidContainerIngredient ingredient, int maxTemp, ItemStack result, float bonusMult, float bonusLimit) {
return new HeatFrameCoolingRecipeBuilder(Either.right(ingredient), maxTemp, result, bonusMult, bonusLimit)
.unlockedBy(getHasName(ModItems.HEAT_FRAME.get()), has(ModItems.HEAT_FRAME.get()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
package me.desht.pneumaticcraft.datagen.recipe;

import com.mojang.datafixers.util.Either;
import me.desht.pneumaticcraft.api.crafting.ingredient.FluidContainerIngredient;
import me.desht.pneumaticcraft.common.recipes.machine.HeatFrameCoolingRecipeImpl;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;

public class HeatFrameCoolingRecipeBuilder extends AbstractPNCRecipeBuilder {
private final Either<Ingredient, SizedFluidIngredient> input;
private final Either<Ingredient, FluidContainerIngredient> input;
private final int temperature;
private final ItemStack outputItem;
private final float bonusMultiplier;
private final float bonusLimit;

protected HeatFrameCoolingRecipeBuilder(Either<Ingredient, SizedFluidIngredient> input, int temperature, ItemStack output) {
protected HeatFrameCoolingRecipeBuilder(Either<Ingredient, FluidContainerIngredient> input, int temperature, ItemStack output) {
this(input, temperature, output, 0f, 0f);
}

public HeatFrameCoolingRecipeBuilder(Either<Ingredient, SizedFluidIngredient> input, int temperature, ItemStack output, float bonusMultiplier, float bonusLimit) {
public HeatFrameCoolingRecipeBuilder(Either<Ingredient, FluidContainerIngredient> input, int temperature, ItemStack output, float bonusMultiplier, float bonusLimit) {
this.input = input;
this.temperature = temperature;
this.outputItem = output;
Expand Down

0 comments on commit 9eb3e5c

Please sign in to comment.