Skip to content

Commit

Permalink
Ported to Patchouli 1.21
Browse files Browse the repository at this point in the history
Book doesn't work right now, not sure if PNC or patchouli problem...
  • Loading branch information
desht committed Jul 22, 2024
1 parent a5a32d2 commit c16e6d4
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 99 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ dependencies {
//runtimeOnly("vazkii.botania:Botania:${botania_version}")

compileOnly("vazkii.patchouli:Patchouli:${patchouli_version}:api")
//runtimeOnly("vazkii.patchouli:Patchouli:${patchouli_version}")
runtimeOnly("vazkii.patchouli:Patchouli:${patchouli_version}")

compileOnly("cc.tweaked:cc-tweaked-1.21-forge-api:${cc_tweaked_version}")
// runtimeOnly("cc.tweaked:cc-tweaked-1.21-forge:${cc_tweaked_version}")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ jade_curse_id = 5493270
crafttweaker_version=20.0.4
cc_tweaked_version=1.111.0
mekanism_version=1.21-10.6.5.52
patchouli_version=1.21-87-NEOFORGE-SNAPSHOT

# TODO update deps when available
immersive_engineering_version=1.20.4-11.1.0-172.110
patchouli_version=1.20.4-85-NEOFORGE-SNAPSHOT

# more out-of-date deps :p
botania_version=1.20.1-441-FORGE-SNAPSHOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,15 @@

package me.desht.pneumaticcraft.common.recipes;

import com.google.gson.*;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.JsonOps;
import me.desht.pneumaticcraft.common.util.PneumaticCraftUtils;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.TagParser;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidStack;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.function.Predicate;

public class ModCraftingHelper {
// private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

public static FluidStack fluidStackFromJson(JsonElement json) {
return FluidStack.OPTIONAL_CODEC.parse(JsonOps.INSTANCE, json).result().orElse(FluidStack.EMPTY);
// String fluidName = GsonHelper.getAsString(json, "fluid");
// if (fluidName.equals("minecraft:empty")) return FluidStack.EMPTY;
// Fluid fluid = BuiltInRegistries.FLUID.get(new ResourceLocation(fluidName));
// if (fluid == Fluids.EMPTY) {
// throw new JsonSyntaxException("unknown fluid: " + fluidName);
// }
// int amount = GsonHelper.getAsInt(json, "amount", 1000);
// FluidStack fluidStack = new FluidStack(fluid, amount);
// if (json.has("nbt")) {
// JsonElement element = json.get("nbt");
// try {
// if (element.isJsonObject()) {
// fluidStack.setTag(TagParser.parseTag(GSON.toJson(element)));
// } else {
// fluidStack.setTag(TagParser.parseTag(GsonHelper.convertToString(element, "nbt")));
// }
// } catch (CommandSyntaxException e) {
// throw new JsonSyntaxException(e);
// }
// }
// return fluidStack;
}

public static JsonElement fluidStackToJson(FluidStack f) {
return FluidStack.OPTIONAL_CODEC.encodeStart(JsonOps.INSTANCE, f).result().orElse(new JsonObject());
// JsonObject json = new JsonObject();
// json.addProperty("fluid", PneumaticCraftUtils.getRegistryName(f.getFluid()).orElseThrow().toString());
// json.addProperty("amount", f.getAmount());
// if (f.hasTag()) {
// json.addProperty("nbt", f.getTag().toString());
// }
// return json;
}

public static List<ItemStack> findItems(CraftingInput inv, List<Predicate<ItemStack>> predicates) {
List<ItemStack> res = new ArrayList<>();
BitSet matchedSlots = new BitSet(inv.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package me.desht.pneumaticcraft.common.thirdparty.patchouli;

import me.desht.pneumaticcraft.client.gui.widget.WidgetTank;
import me.desht.pneumaticcraft.client.util.ClientUtils;
import net.minecraft.client.gui.GuiGraphics;
import net.neoforged.neoforge.fluids.FluidStack;
import vazkii.patchouli.api.IComponentRenderContext;
Expand All @@ -38,7 +39,7 @@ public class ComponentFluid implements ICustomComponent {

@Override
public void build(int componentX, int componentY, int pageNum) {
tankWidget = new WidgetTank(componentX, componentY, 16, 64, fluidStacks.isEmpty() ? FluidStack.EMPTY : fluidStacks.get(0), scaleParsed);
tankWidget = new WidgetTank(componentX, componentY, 16, 64, fluidStacks.isEmpty() ? FluidStack.EMPTY : fluidStacks.getFirst(), scaleParsed);
}

@Override
Expand All @@ -56,7 +57,7 @@ public void render(GuiGraphics graphics, IComponentRenderContext ctx, float ptic

@Override
public void onVariablesAvailable(UnaryOperator<IVariable> lookup) {
fluidStacks = lookup.apply(this.fluid).asStreamOrSingleton()
fluidStacks = lookup.apply(this.fluid).asStreamOrSingleton(ClientUtils.getClientLevel().registryAccess())
.map((x) -> x.as(FluidStack.class))
.collect(Collectors.toList());
String scaleStr = lookup.apply(scale).asString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package me.desht.pneumaticcraft.common.thirdparty.patchouli;

import com.google.gson.JsonElement;
import me.desht.pneumaticcraft.common.recipes.ModCraftingHelper;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.material.Fluid;
Expand All @@ -27,21 +29,21 @@

public class FluidStackVariableSerializer implements IVariableSerializer<FluidStack> {
@Override
public FluidStack fromJson(JsonElement json) {
public FluidStack fromJson(JsonElement json, HolderLookup.Provider provider) {
if (json.isJsonNull()) {
return FluidStack.EMPTY;
} else if (json.isJsonPrimitive()) {
return fluidStackFromString(json.getAsString());
} else if (json.isJsonObject()) {
return ModCraftingHelper.fluidStackFromJson(json.getAsJsonObject());
return fluidStackFromJson(json.getAsJsonObject(), provider);
} else {
throw new IllegalArgumentException("Can't make an FluidStack from an array!");
}
}

@Override
public JsonElement toJson(FluidStack fluidStack) {
return ModCraftingHelper.fluidStackToJson(fluidStack);
public JsonElement toJson(FluidStack fluidStack, HolderLookup.Provider provider) {
return fluidStackToJson(fluidStack, provider);
}

private static FluidStack fluidStackFromString(String s) {
Expand All @@ -59,4 +61,15 @@ private static FluidStack fluidStackFromString(String s) {
Fluid f = BuiltInRegistries.FLUID.get(fluidId);
return new FluidStack(f, count);
}


private static FluidStack fluidStackFromJson(JsonElement json, HolderLookup.Provider provider) {
return FluidStack.OPTIONAL_CODEC.parse(provider.createSerializationContext(JsonOps.INSTANCE), json)
.result().orElse(FluidStack.EMPTY);
}

private static JsonElement fluidStackToJson(FluidStack f, HolderLookup.Provider provider) {
return FluidStack.OPTIONAL_CODEC.encodeStart(provider.createSerializationContext(JsonOps.INSTANCE), f)
.result().orElse(new JsonObject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.desht.pneumaticcraft.common.config.ConfigHelper;
import me.desht.pneumaticcraft.common.registry.ModBlocks;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
Expand Down Expand Up @@ -94,19 +95,19 @@ static boolean openBookEntry(ResourceLocation page) {
return Patchouli.PNC_BOOK.equals(PatchouliAPI.get().getOpenBookGui());
}

static IVariable getStacks(SizedIngredient ingr) {
return IVariable.wrapList(Arrays.stream(ingr.getItems()).map(IVariable::from).toList());
static IVariable getStacks(SizedIngredient ingr, HolderLookup.Provider lookup) {
return IVariable.wrapList(Arrays.stream(ingr.getItems()).map((ItemStack object) -> IVariable.from(object, lookup)).toList(), lookup);
}

static IVariable getStacks(Ingredient ingr) {
return IVariable.wrapList(Arrays.stream(ingr.getItems()).map(IVariable::from).toList());
static IVariable getStacks(Ingredient ingr, HolderLookup.Provider lookup) {
return IVariable.wrapList(Arrays.stream(ingr.getItems()).map((ItemStack object) -> IVariable.from(object, lookup)).toList(), lookup);
}

static IVariable getStacks(List<ItemStack> stacks) {
return IVariable.wrapList(stacks.stream().map(IVariable::from).toList());
static IVariable getStacks(List<ItemStack> stacks, HolderLookup.Provider lookup) {
return IVariable.wrapList(stacks.stream().map((ItemStack object) -> IVariable.from(object, lookup)).toList(), lookup);
}

public static IVariable getFluidStacks(SizedFluidIngredient ingr) {
return IVariable.wrapList(Arrays.stream(ingr.getFluids()).map(IVariable::from).toList());
public static IVariable getFluidStacks(SizedFluidIngredient ingr, HolderLookup.Provider lookup) {
return IVariable.wrapList(Arrays.stream(ingr.getFluids()).map((FluidStack object) -> IVariable.from(object, lookup)).toList(), lookup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,15 @@

package me.desht.pneumaticcraft.common.thirdparty.patchouli;

import com.google.common.base.Suppliers;
import me.desht.pneumaticcraft.lib.ModIds;
import net.minecraft.core.registries.BuiltInRegistries;
import me.desht.pneumaticcraft.api.PneumaticRegistry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import java.util.function.Supplier;
import vazkii.patchouli.api.PatchouliAPI;

public class PatchouliBookCrafting {
private static final String NBT_KEY = "patchouli:book";
private static final String NBT_VAL = "pneumaticcraft:book";

private static final Supplier<Item> guideBook
= Suppliers.memoize(() -> BuiltInRegistries.ITEM.get(ResourceLocation.fromNamespaceAndPath(ModIds.PATCHOULI, "guide_book")));
private static final ResourceLocation BOOK_ID = PneumaticRegistry.RL("book");

public static ItemStack makeGuideBook() {
// return Util.make(new ItemStack(guideBook.get()), s -> s.getOrCreateTag().putString(NBT_KEY, NBT_VAL));
// TODO patchouli will presumable add a data component for this?
return new ItemStack(guideBook.get());
return PatchouliAPI.get().getBookStack(BOOK_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public class ProcessorAmadronTrade implements IComponentProcessor {

@Override
public void setup(Level level, IVariableProvider iVariableProvider) {
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe").asString());
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe", level.registryAccess()).asString());
ModRecipeTypes.AMADRON.get().getRecipe(Minecraft.getInstance().level, recipeId)
.ifPresentOrElse(holder -> recipe = holder.value(),
() -> Log.warning("Missing amadron offer recipe: " + recipeId));

text = iVariableProvider.has("text") ? iVariableProvider.get("text").asString() : null;
text = iVariableProvider.has("text") ? iVariableProvider.get("text", level.registryAccess()).asString() : null;
}

@Override
public IVariable process(Level level, String key) {
if (recipe == null) return null;

return switch (key) {
case "input" -> IVariable.from(recipe.getInput().apply(itemStack -> itemStack, fluidStack -> fluidStack));
case "output" -> IVariable.from(recipe.getOutput().apply(itemStack -> itemStack, fluidStack -> fluidStack));
case "input" -> IVariable.from(recipe.getInput().apply(itemStack -> itemStack, fluidStack -> fluidStack), level.registryAccess());
case "output" -> IVariable.from(recipe.getOutput().apply(itemStack -> itemStack, fluidStack -> fluidStack), level.registryAccess());
case "name" -> IVariable.wrap(recipe.getOutput().getName());
case "text" -> IVariable.wrap(text == null ? "" : I18n.get(text));
default -> null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ProcessorAssemblySystem implements IComponentProcessor {

@Override
public void setup(Level level, IVariableProvider iVariableProvider) {
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe").asString());
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe", level.registryAccess()).asString());

ModRecipeTypes.ASSEMBLY_DRILL_LASER.get().getRecipe(Minecraft.getInstance().level, recipeId)
.ifPresentOrElse(h -> recipe = h.value(),
Expand All @@ -52,9 +52,9 @@ public IVariable process(Level level, String key) {

ItemStack programStack = new ItemStack(AssemblyProgramItem.fromProgramType(recipe.getProgramType()));
return switch (key) {
case "input" -> PatchouliAccess.getStacks(recipe.getInput());
case "output" -> IVariable.from(recipe.getOutput());
case "program" -> IVariable.from(programStack);
case "input" -> PatchouliAccess.getStacks(recipe.getInput(), level.registryAccess());
case "output" -> IVariable.from(recipe.getOutput(), level.registryAccess());
case "program" -> IVariable.from(programStack, level.registryAccess());
case "name" -> IVariable.wrap(recipe.getOutput().getHoverName().getString());
case "desc" -> IVariable.wrap(xlate("pneumaticcraft.patchouli.processor.assembly.desc",
recipe.getOutput().getHoverName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public class ProcessorPressureChamber implements IComponentProcessor {

@Override
public void setup(Level level, IVariableProvider iVariableProvider) {
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe").asString());
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe", level.registryAccess()).asString());
ModRecipeTypes.PRESSURE_CHAMBER.get().getRecipe(Minecraft.getInstance().level, recipeId)
.ifPresentOrElse(h -> recipe = h.value(),
() -> Log.warning("Missing pressure chamber recipe: " + recipeId));
this.header = iVariableProvider.has("header") ? iVariableProvider.get("header").asString() : "";
this.header = iVariableProvider.has("header") ? iVariableProvider.get("header", level.registryAccess()).asString() : "";
}

@Override
Expand All @@ -57,13 +57,17 @@ public IVariable process(Level level, String s) {
} else if (s.startsWith("input")) {
int index = Integer.parseInt(s.substring(5)) - 1;
if (index >= 0 && index < recipe.getInputsForDisplay(ClientUtils.getClientLevel().registryAccess()).size()) {
return PatchouliAccess.getStacks(recipe.getInputsForDisplay(ClientUtils.getClientLevel().registryAccess()).get(index));
return PatchouliAccess.getStacks(recipe.getInputsForDisplay(ClientUtils.getClientLevel().registryAccess()).get(index), level.registryAccess());
}
} else if (s.startsWith("output")) {
int index = Integer.parseInt(s.substring(6)) - 1;
List<? extends List<ItemStack>> results = recipe.getResultsForDisplay(ClientUtils.getClientLevel().registryAccess());
if (index >= 0 && index < results.size()) {
return IVariable.wrapList(results.get(index).stream().map(IVariable::from).collect(ImmutableList.toImmutableList()));
return IVariable.wrapList(results.get(index).stream()
.map((ItemStack object) -> IVariable.from(object, level.registryAccess()))
.collect(ImmutableList.toImmutableList()),
level.registryAccess()
);
}
} else if (s.equals("pressure")) {
String pr = PneumaticCraftUtils.roundNumberTo(recipe.getCraftingPressureForDisplay(), 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public class ProcessorThermoPlant implements IComponentProcessor {

@Override
public void setup(Level level, IVariableProvider iVariableProvider) {
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe").asString());
ResourceLocation recipeId = ResourceLocation.parse(iVariableProvider.get("recipe", level.registryAccess()).asString());
ModRecipeTypes.THERMO_PLANT.get().getRecipe(Minecraft.getInstance().level, recipeId)
.ifPresentOrElse(h -> recipe = h.value(),
() -> Log.warning("Missing thermoplant recipe: " + recipeId));
this.header = iVariableProvider.has("header") ? iVariableProvider.get("header").asString() : "";
this.header = iVariableProvider.has("header") ? iVariableProvider.get("header", level.registryAccess()).asString() : "";
}

@Override
Expand All @@ -55,13 +55,13 @@ public IVariable process(Level level, String s) {
case "header":
return IVariable.wrap(header.isEmpty() ? defaultHeader() : header);
case "item_input":
return PatchouliAccess.getStacks(recipe.getInputItem().orElse(Ingredient.EMPTY));
return PatchouliAccess.getStacks(recipe.getInputItem().orElse(Ingredient.EMPTY), level.registryAccess());
case "fluid_input":
return PatchouliAccess.getFluidStacks(recipe.getInputFluid().orElse(SizedFluidIngredient.of(FluidStack.EMPTY)));
return PatchouliAccess.getFluidStacks(recipe.getInputFluid().orElse(SizedFluidIngredient.of(FluidStack.EMPTY)), level.registryAccess());
case "item_output":
return IVariable.from(recipe.getOutputItem());
return IVariable.from(recipe.getOutputItem(), level.registryAccess());
case "fluid_output":
return IVariable.from(recipe.getOutputFluid());
return IVariable.from(recipe.getOutputFluid(), level.registryAccess());
case "text":
String pr = PneumaticCraftUtils.roundNumberTo(recipe.getRequiredPressure(), 1);
String temp = recipe.getOperatingTemperature().asString(TemperatureRange.TemperatureScale.CELSIUS);
Expand Down

0 comments on commit c16e6d4

Please sign in to comment.