Skip to content

Commit

Permalink
Changed unique recipe ID system
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 3, 2024
1 parent e17804a commit 5455cc0
Show file tree
Hide file tree
Showing 23 changed files with 250 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import dev.latvian.mods.kubejs.recipe.component.BlockStateComponent;
import dev.latvian.mods.kubejs.recipe.component.BooleanComponent;
import dev.latvian.mods.kubejs.recipe.component.CharacterComponent;
import dev.latvian.mods.kubejs.recipe.component.EitherRecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.EnumComponent;
import dev.latvian.mods.kubejs.recipe.component.FluidIngredientComponent;
import dev.latvian.mods.kubejs.recipe.component.FluidStackComponent;
Expand Down Expand Up @@ -608,6 +609,7 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
registry.register("enum", EnumComponent.FACTORY);
registry.register("map", MapRecipeComponent.FACTORY);
registry.register("pattern", MapRecipeComponent.PATTERN_FACTORY);
registry.register("either", EitherRecipeComponent.FACTORY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public String checkEmpty(RecipeKey<Block> key, Block value) {
return "";
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, Block value) {
builder.append(value.kjs$getIdLocation());
}

@Override
public String toString() {
return "block";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public String checkEmpty(RecipeKey<BlockState> key, BlockState value) {
return "";
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, BlockState value) {
builder.append(value.kjs$getIdLocation());
}

@Override
public String toString() {
return preferObjectForm ? "block_state" : "block_state_string";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.recipe.RecipeExceptionJS;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.recipe.schema.RecipeComponentFactory;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;

@SuppressWarnings("OptionalIsPresent")
public record OrRecipeComponent<H, L>(RecipeComponent<H> high, RecipeComponent<L> low) implements RecipeComponent<Either<H, L>> {
public record EitherRecipeComponent<H, L>(RecipeComponent<H> high, RecipeComponent<L> low) implements RecipeComponent<Either<H, L>> {
public static final RecipeComponentFactory FACTORY = RecipeComponentFactory.readTwoComponents(EitherRecipeComponent::new);

@Override
public Codec<Either<H, L>> codec() {
return Codec.either(high.codec(), low.codec());
Expand Down Expand Up @@ -81,6 +84,17 @@ public boolean checkValueHasChanged(Either<H, L> oldValue, Either<H, L> newValue
return oldValue != newValue;
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, Either<H, L> value) {
var left = value.left();

if (left.isPresent()) {
high.buildUniqueId(builder, left.get());
} else {
low.buildUniqueId(builder, value.right().get());
}
}

@Override
public String toString() {
return "either<" + high + ", " + low + ">";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public record EnumComponent<T extends Enum<T> & StringRepresentable>(EnumTypeInf
for (var c : enumTypeInfo.enumConstants()) {
if (c instanceof RemappedEnumConstant r && r.getRemappedEnumConstantName().equalsIgnoreCase(s)) {
return c;
} else if (c instanceof Enum e && e.name().equalsIgnoreCase(s)) {
} else if (c instanceof Enum<?> e && e.name().equalsIgnoreCase(s)) {
return c;
}
}
Expand All @@ -55,6 +55,17 @@ public TypeInfo typeInfo() {
return enumTypeInfo;
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, T value) {
if (value instanceof RemappedEnumConstant r) {
builder.append(r.getRemappedEnumConstantName());
} else if (value instanceof Enum<?> e) {
builder.append(e.name());
} else {
builder.append(value.toString());
}
}

@Override
public String toString() {
return "enum<" + enumTypeInfo.asClass().getName() + ">";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.match.FluidMatch;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;
import org.jetbrains.annotations.Nullable;

public class FluidIngredientComponent implements RecipeComponent<FluidIngredient> {
public static final FluidIngredientComponent FLUID_INGREDIENT = new FluidIngredientComponent();
Expand Down Expand Up @@ -48,9 +46,10 @@ public String checkEmpty(RecipeKey<FluidIngredient> key, FluidIngredient value)
}

@Override
@Nullable
public String createUniqueId(FluidIngredient value) {
return value == null || value.isEmpty() || value.hasNoFluids() ? null : RecipeSchema.normalizeId(value.getStacks()[0].getFluid().kjs$getId()).replace('/', '_');
public void buildUniqueId(UniqueIdBuilder builder, FluidIngredient value) {
if (!value.isEmpty()) {
builder.append(value.getStacks()[0].getFluid().kjs$getIdLocation());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.match.FluidMatch;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;
import org.jetbrains.annotations.Nullable;

public class FluidStackComponent implements RecipeComponent<FluidStack> {
public static final FluidStackComponent FLUID_STACK = new FluidStackComponent();
Expand Down Expand Up @@ -48,9 +46,10 @@ public String checkEmpty(RecipeKey<FluidStack> key, FluidStack value) {
}

@Override
@Nullable
public String createUniqueId(FluidStack value) {
return value == null || value.isEmpty() ? null : RecipeSchema.normalizeId(value.getFluid().kjs$getId()).replace('/', '_');
public void buildUniqueId(UniqueIdBuilder builder, FluidStack value) {
if (!value.isEmpty()) {
builder.append(value.getFluid().kjs$getIdLocation());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package dev.latvian.mods.kubejs.recipe.component;

import com.mojang.serialization.Codec;
import dev.latvian.mods.kubejs.bindings.IngredientWrapper;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.match.ItemMatch;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
import dev.latvian.mods.kubejs.util.TinyMap;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -91,10 +90,18 @@ public RecipeComponent<TinyMap<Character, Ingredient>> asPatternKey() {
}

@Override
@Nullable
public String createUniqueId(Ingredient value) {
var item = value == null ? null : value.kjs$getFirst();
return item == null || item.isEmpty() ? null : RecipeSchema.normalizeId(item.kjs$getId()).replace('/', '_');
public void buildUniqueId(UniqueIdBuilder builder, Ingredient value) {
var tag = IngredientWrapper.tagKeyOf(value);

if (tag != null) {
builder.append(tag.location());
} else {
var first = value.kjs$getFirst();

if (!first.isEmpty()) {
builder.append(first.kjs$getIdLocation());
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.match.ItemMatch;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

public class ItemStackComponent implements RecipeComponent<ItemStack> {
public static final ItemStackComponent ITEM_STACK = new ItemStackComponent("item_stack", ItemStack.OPTIONAL_CODEC);
Expand Down Expand Up @@ -54,9 +52,10 @@ public String checkEmpty(RecipeKey<ItemStack> key, ItemStack value) {
}

@Override
@Nullable
public String createUniqueId(ItemStack value) {
return value == null || value.isEmpty() ? null : RecipeSchema.normalizeId(value.kjs$getId()).replace('/', '_');
public void buildUniqueId(UniqueIdBuilder builder, ItemStack value) {
if (!value.isEmpty()) {
builder.append(value.kjs$getIdLocation());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand Down Expand Up @@ -130,27 +129,14 @@ public List<T> replace(Context cx, KubeRecipe recipe, List<T> original, Replacem
}

@Override
@Nullable
public String createUniqueId(List<T> value) {
if (value == null || value.isEmpty()) {
return null;
}

var sb = new StringBuilder();

for (var item : value) {
var u = component.createUniqueId(item);

if (u != null) {
if (!sb.isEmpty()) {
sb.append('_');
}

sb.append(u);
public void buildUniqueId(UniqueIdBuilder builder, List<T> value) {
for (int i = 0; i < value.size(); i++) {
if (i > 0) {
builder.appendSeparator();
}
}

return sb.isEmpty() ? null : sb.toString();
component.buildUniqueId(builder, value.get(i));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ public TinyMap<K, V> replace(Context cx, KubeRecipe recipe, TinyMap<K, V> origin
return map;
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, TinyMap<K, V> value) {
boolean first = true;

for (var entry : value.entries()) {
if (entry.value() != null) {
if (first) {
first = false;
} else {
builder.appendSeparator();
}

component.buildUniqueId(builder, entry.value());
}
}
}

@Override
public String toString() {
if (patternKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import java.util.List;

public record AndRecipeComponent<A, B>(RecipeComponent<A> a, RecipeComponent<B> b, Codec<Pair<A, B>> codec) implements RecipeComponent<Pair<A, B>> {
public AndRecipeComponent(RecipeComponent<A> a, RecipeComponent<B> b) {
public record PairRecipeComponent<A, B>(RecipeComponent<A> a, RecipeComponent<B> b, Codec<Pair<A, B>> codec) implements RecipeComponent<Pair<A, B>> {
public PairRecipeComponent(RecipeComponent<A> a, RecipeComponent<B> b) {
this(a, b, Codec.pair(a.codec(), b.codec()));
}

Expand All @@ -23,6 +23,13 @@ public TypeInfo typeInfo() {
return new JSFixedArrayTypeInfo(List.of(new JSOptionalParam("", a.typeInfo()), new JSOptionalParam("", b.typeInfo())));
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, Pair<A, B> value) {
a.buildUniqueId(builder, value.getFirst());
builder.appendSeparator();
b.buildUniqueId(builder, value.getSecond());
}

@Override
public String toString() {
return "pair<" + a + ", " + b + ">";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @param <T> The value type of this component
* @see RecipeComponentWithParent
* @see AndRecipeComponent
* @see PairRecipeComponent
*/
@Nullable
public interface RecipeComponent<T> {
Expand Down Expand Up @@ -159,7 +159,7 @@ default void readFromJson(KubeRecipe recipe, RecipeComponentValue<T> cv, JsonObj

/**
* Declares whether this component should take priority when being
* considered by e.g. an {@link OrRecipeComponent} during deserialization.
* considered by e.g. an {@link EitherRecipeComponent} during deserialization.
*
* @param recipe The recipe object used for context
* @param from The object to be deserialized from
Expand Down Expand Up @@ -200,9 +200,8 @@ default boolean checkValueHasChanged(T oldValue, T newValue) {
return oldValue != newValue;
}

@Nullable
default String createUniqueId(T value) {
return value == null ? null : value.toString().toLowerCase().replaceAll("\\W", "_").replaceAll("_{2,}", "_");
default void buildUniqueId(UniqueIdBuilder builder, T value) {
builder.append(value.toString());
}

default RecipeComponent<List<T>> asList() {
Expand All @@ -225,12 +224,12 @@ default RecipeComponent<TinyMap<Character, T>> asPatternKey() {
return new MapRecipeComponent<>(CharacterComponent.CHARACTER, this, true);
}

default <O> OrRecipeComponent<T, O> or(RecipeComponent<O> other) {
return new OrRecipeComponent<>(this, other);
default <O> EitherRecipeComponent<T, O> or(RecipeComponent<O> other) {
return new EitherRecipeComponent<>(this, other);
}

default <O> AndRecipeComponent<T, O> and(RecipeComponent<O> other) {
return new AndRecipeComponent<>(this, other);
default <O> PairRecipeComponent<T, O> and(RecipeComponent<O> other) {
return new PairRecipeComponent<>(this, other);
}

default RecipeComponent<T> withCodec(Codec<T> codec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.JSObjectTypeInfo;
import dev.latvian.mods.rhino.type.JSOptionalParam;
Expand Down Expand Up @@ -147,6 +148,23 @@ public RecipeComponentBuilderMap replace(Context cx, KubeRecipe recipe, RecipeCo
return original;
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, RecipeComponentBuilderMap value) {
boolean first = true;

for (var entry : value.entrySet()) {
if (entry.getValue() != null) {
if (first) {
first = false;
} else {
builder.appendSeparator();
}

entry.getKey().component.buildUniqueId(builder, Cast.to(entry.getValue()));
}
}
}

@Override
public String toString() {
return keys.stream().map(RecipeKey::toString).collect(Collectors.joining(", ", "builder<", ">"));
Expand Down
Loading

0 comments on commit 5455cc0

Please sign in to comment.