Skip to content

Commit

Permalink
Fixed data component maps being too greedy in type wrapping, added Re…
Browse files Browse the repository at this point in the history
…gistryAccessContainer.of(ctx)
  • Loading branch information
LatvianModder committed Jul 13, 2024
1 parent 3793802 commit 63e32bf
Show file tree
Hide file tree
Showing 31 changed files with 77 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -220,6 +221,10 @@ static DataComponentPredicate readPredicate(DynamicOps<Tag> 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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +16,7 @@

public record RegistryWrapper<T>(Registry<T> registry, ResourceKey<T> unknownKey) implements Iterable<T> {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 + "!"));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/dev/latvian/mods/kubejs/core/ItemStackKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<Tag> dynamicOps) {
Expand Down Expand Up @@ -266,7 +266,7 @@ default Codec<ItemStack> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/item/ChancedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/item/ItemPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,15 +53,15 @@ 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) {
return (T) fs.getFluid();
} 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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +40,7 @@ public record Component<T>(TypeInfo type, StreamCodec<?, T> 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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,7 +18,7 @@ public ServerAddFluidEntriesKubeEvent(List<FluidStack> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,6 +17,6 @@ public ServerAddFluidInformationKubeEvent(List<FluidData.Info> list) {

@Override
public void add(Context cx, Object filter, List<Component> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,7 +18,7 @@ public ServerAddItemEntriesKubeEvent(List<ItemStack> 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));
}
}
}
Loading

0 comments on commit 63e32bf

Please sign in to comment.