From 63e32bfb0961daf0ff105a63d31791d2a394d44b Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Sat, 13 Jul 2024 15:32:09 +0300 Subject: [PATCH] Fixed data component maps being too greedy in type wrapping, added RegistryAccessContainer.of(ctx) --- .../latvian/mods/kubejs/BuiltinKubeJSPlugin.java | 5 +++-- .../latvian/mods/kubejs/KubeJSTypeWrappers.java | 6 +++--- .../kubejs/bindings/DataComponentWrapper.java | 5 +++++ .../mods/kubejs/bindings/RegistryWrapper.java | 4 ++-- .../kubejs/block/state/BlockStatePredicate.java | 4 ++-- .../latvian/mods/kubejs/core/FluidStackKJS.java | 4 ++-- .../latvian/mods/kubejs/core/IngredientKJS.java | 4 ++-- .../latvian/mods/kubejs/core/ItemStackKJS.java | 16 ++++++++-------- .../mods/kubejs/core/RecipeHolderKJS.java | 3 ++- .../mods/kubejs/core/SizedIngredientKJS.java | 4 ++-- .../latvian/mods/kubejs/item/ChancedItem.java | 4 ++-- .../latvian/mods/kubejs/item/ItemPredicate.java | 4 ++-- .../recipe/component/RegistryComponent.java | 6 +++--- .../kubejs/recipe/match/ReplacementMatch.java | 4 ++-- .../recipe/viewer/RecipeViewerEntryType.java | 8 ++++---- .../server/ServerAddFluidEntriesKubeEvent.java | 4 ++-- .../ServerAddFluidInformationKubeEvent.java | 4 ++-- .../server/ServerAddItemEntriesKubeEvent.java | 4 ++-- .../ServerAddItemInformationKubeEvent.java | 4 ++-- .../server/ServerGroupFluidEntriesKubeEvent.java | 4 ++-- .../server/ServerGroupItemEntriesKubeEvent.java | 4 ++-- .../ServerRegisterFluidSubtypesKubeEvent.java | 4 ++-- .../ServerRegisterItemSubtypesKubeEvent.java | 4 ++-- .../ServerRemoveFluidEntriesKubeEvent.java | 4 ++-- .../server/ServerRemoveItemEntriesKubeEvent.java | 4 ++-- .../mods/kubejs/script/CodecTypeWrapper.java | 3 ++- .../mods/kubejs/script/MapCodecTypeWrapper.java | 3 ++- .../mods/kubejs/script/TypeWrapperRegistry.java | 2 +- .../kubejs/util/RegistryAccessContainer.java | 5 +++++ .../dev/latvian/mods/kubejs/util/SlotFilter.java | 3 +-- .../dev/latvian/mods/kubejs/util/WithCodec.java | 5 ++--- 31 files changed, 77 insertions(+), 65 deletions(-) diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index 3fa9e5361..8f7edf9be 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -141,6 +141,7 @@ import dev.latvian.mods.kubejs.util.NBTUtils; import dev.latvian.mods.kubejs.util.NotificationToastData; import dev.latvian.mods.kubejs.util.RegExpKJS; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.kubejs.util.RotationAxis; import dev.latvian.mods.kubejs.util.ScheduledEvents; import dev.latvian.mods.kubejs.util.SlotFilter; @@ -535,8 +536,8 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) { registry.register(ListTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagList); registry.register(Tag.class, NBTUtils::toTag); registry.register(DataComponentType.class, DataComponentWrapper::wrapType); - registry.register(DataComponentMap.class, DataComponentWrapper::mapOf); - registry.register(DataComponentPatch.class, DataComponentWrapper::patchOf); + registry.register(DataComponentMap.class, DataComponentWrapper::filter, (cx, from, target) -> DataComponentWrapper.mapOf(RegistryAccessContainer.of(cx), from)); + registry.register(DataComponentPatch.class, DataComponentWrapper::filter, (cx, from, target) -> DataComponentWrapper.patchOf(RegistryAccessContainer.of(cx), from)); registry.register(BlockPos.class, KubeJSTypeWrappers::blockPosOf); registry.register(Vec3.class, KubeJSTypeWrappers::vec3Of); diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java b/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java index 036271ddf..a0581688c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java @@ -1,8 +1,8 @@ package dev.latvian.mods.kubejs; import dev.latvian.mods.kubejs.level.BlockContainerJS; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.NBTUtils; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.kubejs.util.UtilsJS; import dev.latvian.mods.rhino.Context; import net.minecraft.core.BlockPos; @@ -58,7 +58,7 @@ static IntProvider intProviderOf(Context cx, Object o) { } } - var decoded = IntProvider.CODEC.parse(((KubeJSContext) cx).getRegistries().nbt(), NBTUtils.toTagCompound(cx, m)).result(); + var decoded = IntProvider.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), NBTUtils.toTagCompound(cx, m)).result(); if (decoded.isPresent()) { return decoded.get(); } @@ -90,7 +90,7 @@ static FloatProvider floatProviderOf(Context cx, Object o) { } } - var decoded = FloatProvider.CODEC.parse(((KubeJSContext) cx).getRegistries().nbt(), NBTUtils.toTagCompound(cx, m)).result(); + var decoded = FloatProvider.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), NBTUtils.toTagCompound(cx, m)).result(); if (decoded.isPresent()) { return decoded.get(); diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java index c88b4750c..c19949b6d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/DataComponentWrapper.java @@ -12,6 +12,7 @@ import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.kubejs.util.Lazy; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import dev.latvian.mods.rhino.NativeJavaMap; import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; @@ -220,6 +221,10 @@ static DataComponentPredicate readPredicate(DynamicOps registryOps, StringR return map.isEmpty() ? DataComponentPredicate.EMPTY : DataComponentPredicate.allOf(map); } + static boolean filter(Object from, TypeInfo target) { + return from == null || from instanceof DataComponentMap || from instanceof DataComponentPatch || from instanceof Map || from instanceof NativeJavaMap || from instanceof String s && (s.isEmpty() || s.charAt(0) == '['); + } + static DataComponentMap mapOf(RegistryAccessContainer registries, Object o) { try { return readMap(registries.nbt(), new StringReader(o.toString())); diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java index 8228e6207..4ef15db9d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/RegistryWrapper.java @@ -1,6 +1,6 @@ package dev.latvian.mods.kubejs.bindings; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; @@ -16,7 +16,7 @@ public record RegistryWrapper(Registry registry, ResourceKey unknownKey) implements Iterable { public static RegistryWrapper of(Context cx, ResourceLocation id) { - return ((KubeJSContext) cx).getRegistries().wrapRegistry(id); + return RegistryAccessContainer.of(cx).wrapRegistry(id); } public T get(ResourceLocation id) { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java b/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java index f8e29d9d2..33b017941 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/state/BlockStatePredicate.java @@ -7,11 +7,11 @@ import dev.latvian.mods.kubejs.level.ruletest.AnyMatchRuleTest; import dev.latvian.mods.kubejs.level.ruletest.InvertRuleTest; import dev.latvian.mods.kubejs.recipe.match.ReplacementMatch; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.ListJS; import dev.latvian.mods.kubejs.util.MapJS; import dev.latvian.mods.kubejs.util.NBTUtils; import dev.latvian.mods.kubejs.util.RegExpKJS; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.kubejs.util.Tags; import dev.latvian.mods.rhino.Context; import net.minecraft.Util; @@ -132,7 +132,7 @@ static RuleTest ruleTestOf(Context cx, Object o) { } return Optional.ofNullable(NBTUtils.toTagCompound(cx, o)) - .map(tag -> RuleTest.CODEC.parse(((KubeJSContext) cx).getRegistries().nbt(), tag)) + .map(tag -> RuleTest.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), tag)) .flatMap(DataResult::result) .or(() -> Optional.ofNullable(of(o).asRuleTest())) .orElseThrow(() -> new IllegalArgumentException("Could not parse valid rule test from " + o + "!")); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java index d523ec52f..f5ab5a6a9 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java @@ -3,7 +3,7 @@ import dev.latvian.mods.kubejs.fluid.FluidLike; import dev.latvian.mods.kubejs.fluid.FluidWrapper; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.fluids.FluidStack; @@ -36,7 +36,7 @@ public interface FluidStackKJS extends Replaceable, FluidLike { @Override default Object replaceThisWith(Context cx, Object with) { var t = kjs$self(); - var r = FluidWrapper.wrap(((KubeJSContext) cx).getRegistries(), with); + var r = FluidWrapper.wrap(RegistryAccessContainer.of(cx), with); if (!FluidStack.isSameFluidSameComponents(t, r)) { r.setAmount(t.getAmount()); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java index 7f7be6475..2756a8c22 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java @@ -6,7 +6,7 @@ import dev.latvian.mods.kubejs.item.ItemPredicate; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.kubejs.util.WithCodec; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.RemapPrefixForJS; @@ -70,7 +70,7 @@ default Codec getCodec(Context cx) { @Override default Object replaceThisWith(Context cx, Object with) { var t = kjs$self(); - var r = IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), with); + var r = IngredientJS.wrap(RegistryAccessContainer.of(cx), with); if (!r.equals(t)) { return r; diff --git a/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java index 72666394c..6d46ed134 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java @@ -7,8 +7,8 @@ import dev.latvian.mods.kubejs.item.ItemStackJS; import dev.latvian.mods.kubejs.level.BlockContainerJS; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.ID; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.kubejs.util.WithCodec; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.RemapPrefixForJS; @@ -62,7 +62,7 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) { return kjs$equalsIgnoringCount(s); } - return kjs$equalsIgnoringCount(ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), o)); + return kjs$equalsIgnoringCount(ItemStackJS.wrap(RegistryAccessContainer.of(cx), o)); } default boolean kjs$equalsIgnoringCount(ItemStack stack) { @@ -130,8 +130,8 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) { return is; } - default String kjs$getComponentString(KubeJSContext cx) { - return DataComponentWrapper.patchToString(new StringBuilder(), cx.getRegistries().nbt(), kjs$self().getComponentsPatch()).toString(); + default String kjs$getComponentString(Context cx) { + return DataComponentWrapper.patchToString(new StringBuilder(), RegistryAccessContainer.of(cx).nbt(), kjs$self().getComponentsPatch()).toString(); } @ReturnsSelf(copy = true) @@ -202,11 +202,11 @@ default boolean specialEquals(Context cx, Object o, boolean shallow) { @Override default String toStringJS(Context cx) { - return kjs$toItemString0(((KubeJSContext) cx).getRegistries().nbt()); + return kjs$toItemString0(RegistryAccessContainer.of(cx).nbt()); } - default String kjs$toItemString(KubeJSContext cx) { - return kjs$toItemString0(cx.getRegistries().nbt()); + default String kjs$toItemString(Context cx) { + return kjs$toItemString0(RegistryAccessContainer.of(cx).nbt()); } default String kjs$toItemString0(DynamicOps dynamicOps) { @@ -266,7 +266,7 @@ default Codec getCodec(Context cx) { @Override default Object replaceThisWith(Context cx, Object with) { var t = kjs$self(); - var r = ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), with); + var r = ItemStackJS.wrap(RegistryAccessContainer.of(cx), with); if (!ItemStack.isSameItemSameComponents(t, r)) { r.setCount(t.getCount()); diff --git a/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java index ee2b1b960..b46ed3c3d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/RecipeHolderKJS.java @@ -5,6 +5,7 @@ import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema; import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.server.ServerScriptManager; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.core.registries.BuiltInRegistries; @@ -75,7 +76,7 @@ default boolean replaceInput(Context cx, ReplacementMatchInfo match, Object with @Override default boolean hasOutput(Context cx, ReplacementMatchInfo match) { if (match.match() instanceof ItemMatch m) { - var result = kjs$getRecipe().getResultItem(((KubeJSContext) cx).getRegistries().access()); + var result = kjs$getRecipe().getResultItem(RegistryAccessContainer.of(cx).access()); //noinspection ConstantValue return result != null && result != ItemStack.EMPTY && !result.isEmpty() && m.matches(cx, result, match.exact()); } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java index f276e32bc..18f90159f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/SizedIngredientKJS.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.recipe.match.Replaceable; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.world.item.crafting.Ingredient; import net.neoforged.neoforge.common.crafting.SizedIngredient; @@ -14,7 +14,7 @@ public interface SizedIngredientKJS extends Replaceable, IngredientSupplierKJS { @Override default Object replaceThisWith(Context cx, Object with) { - var ingredient = IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), with); + var ingredient = IngredientJS.wrap(RegistryAccessContainer.of(cx), with); if (!ingredient.equals(kjs$self().ingredient())) { return new SizedIngredient(ingredient, kjs$self().count()); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ChancedItem.java b/src/main/java/dev/latvian/mods/kubejs/item/ChancedItem.java index 126b8dd7d..9f3b4642a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ChancedItem.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ChancedItem.java @@ -4,7 +4,7 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import dev.latvian.mods.kubejs.recipe.component.RecipeComponent; import dev.latvian.mods.kubejs.recipe.component.SimpleRecipeComponent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.RecordTypeInfo; import dev.latvian.mods.rhino.type.TypeInfo; @@ -37,7 +37,7 @@ public static ChancedItem wrap(Context cx, Object from) { if (from instanceof ItemStack is) { return new ChancedItem(is, DEFAULT_CHANCE); } else if (from instanceof CharSequence) { - return new ChancedItem(ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), from), DEFAULT_CHANCE); + return new ChancedItem(ItemStackJS.wrap(RegistryAccessContainer.of(cx), from), DEFAULT_CHANCE); } else { return (ChancedItem) TYPE_INFO.wrap(cx, from, TYPE_INFO); } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemPredicate.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemPredicate.java index fdb51f42b..03c0559c2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemPredicate.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemPredicate.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.core.IngredientSupplierKJS; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.BaseFunction; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.TypeInfo; @@ -35,7 +35,7 @@ static ItemPredicate wrap(Context cx, Object from) { } } - var in = IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), from); + var in = IngredientJS.wrap(RegistryAccessContainer.of(cx), from); if (in.isEmpty()) { return NONE; diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java index b014a1753..30b4b0ab2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/component/RegistryComponent.java @@ -7,8 +7,8 @@ import dev.latvian.mods.kubejs.recipe.KubeRecipe; import dev.latvian.mods.kubejs.recipe.schema.RecipeComponentFactory; import dev.latvian.mods.kubejs.registry.RegistryType; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.ID; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.Registry; @@ -53,7 +53,7 @@ public T wrap(Context cx, KubeRecipe recipe, Object from) { } else if (from instanceof Item) { return (T) from; } else { - return (T) ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), from).getItem(); + return (T) ItemStackJS.wrap(RegistryAccessContainer.of(cx), from).getItem(); } } else if (registry == BuiltInRegistries.FLUID) { if (from instanceof FluidStack fs) { @@ -61,7 +61,7 @@ public T wrap(Context cx, KubeRecipe recipe, Object from) { } else if (from instanceof Fluid) { return (T) from; } else { - return (T) FluidWrapper.wrap(((KubeJSContext) cx).getRegistries(), from).getFluid(); + return (T) FluidWrapper.wrap(RegistryAccessContainer.of(cx), from).getFluid(); } } else { if (regType != null && regType.baseClass().isInstance(from)) { diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java index 496e2eb83..0f7a2c47c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/match/ReplacementMatch.java @@ -1,7 +1,7 @@ package dev.latvian.mods.kubejs.recipe.match; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.crafting.FluidIngredient; @@ -25,7 +25,7 @@ static ReplacementMatch wrap(Context cx, Object o) { return new SingleFluidMatch(fs); } - var in = IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), o); + var in = IngredientJS.wrap(RegistryAccessContainer.of(cx), o); if (in.isEmpty()) { return NONE; diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/RecipeViewerEntryType.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/RecipeViewerEntryType.java index 3b040e9f9..dda62ef10 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/RecipeViewerEntryType.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/RecipeViewerEntryType.java @@ -6,8 +6,8 @@ import dev.latvian.mods.kubejs.item.ItemStackJS; import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.kubejs.util.Lazy; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.registries.Registries; @@ -40,7 +40,7 @@ public record Component(TypeInfo type, StreamCodec streamCodec, Predica ) { @Override public Object wrapEntry(Context cx, Object from) { - return ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), from); + return ItemStackJS.wrap(RegistryAccessContainer.of(cx), from); } @Override @@ -61,12 +61,12 @@ public Object getBase(Object from) { ) { @Override public Object wrapEntry(Context cx, Object from) { - return FluidWrapper.wrap(((KubeJSContext) cx).getRegistries(), from); + return FluidWrapper.wrap(RegistryAccessContainer.of(cx), from); } @Override public Object wrapPredicate(Context cx, Object from) { - return FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), from); + return FluidWrapper.wrapIngredient(RegistryAccessContainer.of(cx), from); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidEntriesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidEntriesKubeEvent.java index 6e66f0679..c1c33b35a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidEntriesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidEntriesKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.fluid.FluidWrapper; import dev.latvian.mods.kubejs.recipe.viewer.AddEntriesKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.neoforged.neoforge.fluids.FluidStack; @@ -18,7 +18,7 @@ public ServerAddFluidEntriesKubeEvent(List list) { @Override public void add(Context cx, Object[] items) { for (var item : items) { - list.add(FluidWrapper.wrap(((KubeJSContext) cx).getRegistries(), item)); + list.add(FluidWrapper.wrap(RegistryAccessContainer.of(cx), item)); } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidInformationKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidInformationKubeEvent.java index 52d30c26d..fbdd21f4c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidInformationKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddFluidInformationKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.fluid.FluidWrapper; import dev.latvian.mods.kubejs.recipe.viewer.AddInformationKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.network.chat.Component; @@ -17,6 +17,6 @@ public ServerAddFluidInformationKubeEvent(List list) { @Override public void add(Context cx, Object filter, List info) { - list.add(new FluidData.Info(FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), filter), info)); + list.add(new FluidData.Info(FluidWrapper.wrapIngredient(RegistryAccessContainer.of(cx), filter), info)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemEntriesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemEntriesKubeEvent.java index ba9a6f2a7..7c5cb1a8b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemEntriesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemEntriesKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.item.ItemStackJS; import dev.latvian.mods.kubejs.recipe.viewer.AddEntriesKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.world.item.ItemStack; @@ -18,7 +18,7 @@ public ServerAddItemEntriesKubeEvent(List list) { @Override public void add(Context cx, Object[] items) { for (var item : items) { - list.add(ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), item)); + list.add(ItemStackJS.wrap(RegistryAccessContainer.of(cx), item)); } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemInformationKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemInformationKubeEvent.java index 63518422b..c33ca5590 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemInformationKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerAddItemInformationKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.recipe.viewer.AddInformationKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.network.chat.Component; @@ -17,6 +17,6 @@ public ServerAddItemInformationKubeEvent(List list) { @Override public void add(Context cx, Object filter, List info) { - list.add(new ItemData.Info(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), filter), info)); + list.add(new ItemData.Info(IngredientJS.wrap(RegistryAccessContainer.of(cx), filter), info)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupFluidEntriesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupFluidEntriesKubeEvent.java index 961a24a4c..5b103a2be 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupFluidEntriesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupFluidEntriesKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.fluid.FluidWrapper; import dev.latvian.mods.kubejs.recipe.viewer.GroupEntriesKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -18,6 +18,6 @@ public ServerGroupFluidEntriesKubeEvent(List list) { @Override public void group(Context cx, Object filter, ResourceLocation groupId, Component description) { - list.add(new FluidData.Group(FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), filter), groupId, description)); + list.add(new FluidData.Group(FluidWrapper.wrapIngredient(RegistryAccessContainer.of(cx), filter), groupId, description)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupItemEntriesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupItemEntriesKubeEvent.java index c24f33f9f..e85e1f4a6 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupItemEntriesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerGroupItemEntriesKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.recipe.viewer.GroupEntriesKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; @@ -18,6 +18,6 @@ public ServerGroupItemEntriesKubeEvent(List list) { @Override public void group(Context cx, Object filter, ResourceLocation groupId, Component description) { - list.add(new ItemData.Group(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), filter), groupId, description)); + list.add(new ItemData.Group(IngredientJS.wrap(RegistryAccessContainer.of(cx), filter), groupId, description)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterFluidSubtypesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterFluidSubtypesKubeEvent.java index 0ee3ce007..11844e38d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterFluidSubtypesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterFluidSubtypesKubeEvent.java @@ -3,7 +3,7 @@ import dev.latvian.mods.kubejs.fluid.FluidWrapper; import dev.latvian.mods.kubejs.recipe.viewer.RegisterSubtypesKubeEvent; import dev.latvian.mods.kubejs.recipe.viewer.SubtypeInterpreter; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.core.component.DataComponentType; @@ -23,6 +23,6 @@ public void register(Context cx, Object filter, SubtypeInterpreter interpreter) @Override public void useComponents(Context cx, Object filter, List> components) { - list.add(new FluidData.DataComponentSubtypes(FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), filter), components)); + list.add(new FluidData.DataComponentSubtypes(FluidWrapper.wrapIngredient(RegistryAccessContainer.of(cx), filter), components)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterItemSubtypesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterItemSubtypesKubeEvent.java index 170eaf586..e076cdbe9 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterItemSubtypesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRegisterItemSubtypesKubeEvent.java @@ -3,7 +3,7 @@ import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.recipe.viewer.RegisterSubtypesKubeEvent; import dev.latvian.mods.kubejs.recipe.viewer.SubtypeInterpreter; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.core.component.DataComponentType; @@ -23,6 +23,6 @@ public void register(Context cx, Object filter, SubtypeInterpreter interpreter) @Override public void useComponents(Context cx, Object filter, List> components) { - list.add(new ItemData.DataComponentSubtypes(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), filter), components)); + list.add(new ItemData.DataComponentSubtypes(IngredientJS.wrap(RegistryAccessContainer.of(cx), filter), components)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveFluidEntriesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveFluidEntriesKubeEvent.java index 51df5e553..c5f89ac44 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveFluidEntriesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveFluidEntriesKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.fluid.FluidWrapper; import dev.latvian.mods.kubejs.recipe.viewer.RemoveEntriesKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.neoforged.neoforge.fluids.crafting.FluidIngredient; @@ -17,6 +17,6 @@ public ServerRemoveFluidEntriesKubeEvent(List removedEntries) { @Override public void remove(Context cx, Object filter) { - removedEntries.add(FluidWrapper.wrapIngredient(((KubeJSContext) cx).getRegistries(), filter)); + removedEntries.add(FluidWrapper.wrapIngredient(RegistryAccessContainer.of(cx), filter)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveItemEntriesKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveItemEntriesKubeEvent.java index 4a48bebf7..d35319726 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveItemEntriesKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/viewer/server/ServerRemoveItemEntriesKubeEvent.java @@ -2,7 +2,7 @@ import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; import dev.latvian.mods.kubejs.recipe.viewer.RemoveEntriesKubeEvent; -import dev.latvian.mods.kubejs.script.KubeJSContext; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import net.minecraft.world.item.crafting.Ingredient; @@ -17,6 +17,6 @@ public ServerRemoveItemEntriesKubeEvent(List removedEntries) { @Override public void remove(Context cx, Object filter) { - removedEntries.add(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), filter)); + removedEntries.add(IngredientJS.wrap(RegistryAccessContainer.of(cx), filter)); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/script/CodecTypeWrapper.java b/src/main/java/dev/latvian/mods/kubejs/script/CodecTypeWrapper.java index ed36aa524..b8e7ad1ba 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/CodecTypeWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/CodecTypeWrapper.java @@ -2,6 +2,7 @@ import com.mojang.serialization.Codec; import dev.latvian.mods.kubejs.util.Cast; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.Wrapper; import dev.latvian.mods.rhino.type.TypeInfo; @@ -17,7 +18,7 @@ public T wrap(Context cx, Object o, TypeInfo target) { } else if (target.asClass().isInstance(o)) { return Cast.to(o); } else { - return ((KubeJSContext) cx).getRegistries().decode(cx, codec, o); + return RegistryAccessContainer.of(cx).decode(cx, codec, o); } } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/script/MapCodecTypeWrapper.java b/src/main/java/dev/latvian/mods/kubejs/script/MapCodecTypeWrapper.java index 89e19fa4f..90f508b94 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/MapCodecTypeWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/MapCodecTypeWrapper.java @@ -2,6 +2,7 @@ import com.mojang.serialization.MapCodec; import dev.latvian.mods.kubejs.util.Cast; +import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.Wrapper; import dev.latvian.mods.rhino.type.TypeInfo; @@ -17,7 +18,7 @@ public T wrap(Context cx, Object o, TypeInfo target) { } else if (target.asClass().isInstance(o)) { return Cast.to(o); } else { - return ((KubeJSContext) cx).getRegistries().decodeMap(cx, codec, o); + return RegistryAccessContainer.of(cx).decodeMap(cx, codec, o); } } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java b/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java index 5072bc171..a09e6624b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java @@ -51,7 +51,7 @@ public void register(Class target, ContextFromFunction factory) { } public void register(Class target, RegistriesFromFunction factory) { - typeWrappers.register(target, (cx, from, t) -> factory.apply(((KubeJSContext) cx).getRegistries(), from)); + typeWrappers.register(target, (cx, from, t) -> factory.apply(RegistryAccessContainer.of(cx), from)); } public void register(Class target, TypeWrapperValidator validator, DirectTypeWrapperFactory factory) { diff --git a/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java b/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java index 2cad32b0a..94b45badb 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/RegistryAccessContainer.java @@ -8,6 +8,7 @@ import dev.latvian.mods.kubejs.bindings.RegistryWrapper; import dev.latvian.mods.kubejs.recipe.CachedItemTagLookup; import dev.latvian.mods.kubejs.recipe.CachedTagLookup; +import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.rhino.Context; import net.minecraft.core.Registry; import net.minecraft.core.RegistryAccess; @@ -35,6 +36,10 @@ public final class RegistryAccessContainer { @ApiStatus.Internal public static RegistryAccessContainer current = BUILTIN; + public static RegistryAccessContainer of(Context cx) { + return cx instanceof KubeJSContext kcx ? kcx.getRegistries() : current; + } + private final RegistryAccess.Frozen access; private final RegistryOps nbt; private final RegistryOps json; diff --git a/src/main/java/dev/latvian/mods/kubejs/util/SlotFilter.java b/src/main/java/dev/latvian/mods/kubejs/util/SlotFilter.java index a0967f1fe..cb57ecc9a 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/SlotFilter.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/SlotFilter.java @@ -3,7 +3,6 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.regexp.NativeRegExp; import dev.latvian.mods.rhino.type.JSObjectTypeInfo; @@ -42,7 +41,7 @@ public static SlotFilter wrap(Context cx, Object o, TypeInfo target) { if (o instanceof Number num) { return of(Ingredient.EMPTY, num.intValue()); } else if (o instanceof String || o instanceof Ingredient || o instanceof NativeRegExp || o instanceof Pattern) { - return of(IngredientJS.wrap(((KubeJSContext) cx).getRegistries(), o), -1); + return of(IngredientJS.wrap(RegistryAccessContainer.of(cx), o), -1); } else { return (SlotFilter) ((RecordTypeInfo) target).wrap(cx, o, target); } diff --git a/src/main/java/dev/latvian/mods/kubejs/util/WithCodec.java b/src/main/java/dev/latvian/mods/kubejs/util/WithCodec.java index ccaed5a9a..0db7e71e9 100644 --- a/src/main/java/dev/latvian/mods/kubejs/util/WithCodec.java +++ b/src/main/java/dev/latvian/mods/kubejs/util/WithCodec.java @@ -2,7 +2,6 @@ import com.google.gson.JsonElement; import com.mojang.serialization.Codec; -import dev.latvian.mods.kubejs.script.KubeJSContext; import dev.latvian.mods.rhino.Context; import net.minecraft.nbt.Tag; @@ -11,11 +10,11 @@ public interface WithCodec extends NBTSerializable, JsonSerializable { @Override default Tag toNBT(Context cx) { - return getCodec(cx).encodeStart(((KubeJSContext) cx).getRegistries().nbt(), Cast.to(this)).getOrThrow(); + return getCodec(cx).encodeStart(RegistryAccessContainer.of(cx).nbt(), Cast.to(this)).getOrThrow(); } @Override default JsonElement toJson(Context cx) { - return getCodec(cx).encodeStart(((KubeJSContext) cx).getRegistries().json(), Cast.to(this)).getOrThrow(); + return getCodec(cx).encodeStart(RegistryAccessContainer.of(cx).json(), Cast.to(this)).getOrThrow(); } } \ No newline at end of file