Skip to content

Commit

Permalink
More separation of wrap() and of() methods, moved Utils.entitySelecto…
Browse files Browse the repository at this point in the history
…r(x) to EntitySelector.of(x)
  • Loading branch information
LatvianModder committed Dec 17, 2024
1 parent 46420b2 commit c982918
Show file tree
Hide file tree
Showing 30 changed files with 604 additions and 492 deletions.
71 changes: 36 additions & 35 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import dev.latvian.mods.kubejs.bindings.ColorWrapper;
import dev.latvian.mods.kubejs.bindings.DamageSourceWrapper;
import dev.latvian.mods.kubejs.bindings.DirectionWrapper;
import dev.latvian.mods.kubejs.bindings.EntitySelectorWrapper;
import dev.latvian.mods.kubejs.bindings.IngredientWrapper;
import dev.latvian.mods.kubejs.bindings.ItemWrapper;
import dev.latvian.mods.kubejs.bindings.JavaWrapper;
import dev.latvian.mods.kubejs.bindings.KMath;
import dev.latvian.mods.kubejs.bindings.NBTWrapper;
import dev.latvian.mods.kubejs.bindings.ParticleOptionsWrapper;
import dev.latvian.mods.kubejs.bindings.RegistryWrapper;
import dev.latvian.mods.kubejs.bindings.SizedIngredientWrapper;
Expand Down Expand Up @@ -145,7 +147,6 @@
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.KubeResourceLocation;
import dev.latvian.mods.kubejs.util.NBTIOWrapper;
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;
Expand All @@ -155,7 +156,6 @@
import dev.latvian.mods.kubejs.util.TickDuration;
import dev.latvian.mods.kubejs.util.TimeJS;
import dev.latvian.mods.kubejs.util.Tristate;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.registrypredicate.RegistryPredicate;
import dev.latvian.mods.kubejs.web.LocalWebServerRegistry;
import dev.latvian.mods.kubejs.web.local.KubeJSWeb;
Expand Down Expand Up @@ -491,7 +491,7 @@ public void registerBindings(BindingRegistry bindings) {
bindings.add("Item", ItemWrapper.class);
bindings.add("Items", Items.class);
bindings.add("Ingredient", IngredientWrapper.class);
bindings.add("NBT", NBTUtils.class);
bindings.add("NBT", NBTWrapper.class);
bindings.add("NBTIO", NBTIOWrapper.class);
bindings.add("Direction", DirectionWrapper.class);
bindings.add("Facing", DirectionWrapper.class);
Expand All @@ -502,6 +502,7 @@ public void registerBindings(BindingRegistry bindings) {
bindings.add("SizedIngredient", SizedIngredientWrapper.class);
bindings.add("ParticleOptions", ParticleOptionsWrapper.class);
bindings.add("Registry", RegistryWrapper.class);
bindings.add("EntitySelector", EntitySelectorWrapper.class);

bindings.add("Fluid", FluidWrapper.class);

Expand Down Expand Up @@ -543,43 +544,43 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(JsonArray.class, JsonUtils::arrayOf);
registry.register(JsonElement.class, JsonUtils::of);
registry.register(JsonPrimitive.class, JsonUtils::primitiveOf);
registry.register(Path.class, KubeJSTypeWrappers::pathOf);
registry.register(File.class, KubeJSTypeWrappers::fileOf);
registry.register(TemporalAmount.class, TimeJS::temporalAmountOf);
registry.register(Duration.class, TimeJS::durationOf);
registry.register(Path.class, KubeJSTypeWrappers::wrapPath);
registry.register(File.class, KubeJSTypeWrappers::wrapFile);
registry.register(TemporalAmount.class, TimeJS::wrapTemporalAmount);
registry.register(Duration.class, TimeJS::wrapDuration);
registry.register(TickDuration.class, TickDuration::wrap);

registry.register(ResourceLocation.class, ID::mc);
registry.register(KubeResourceLocation.class, KubeResourceLocation::wrap);
registry.register(CompoundTag.class, (from, target) -> NBTUtils.isTagCompound(from), NBTUtils::toTagCompound);
registry.register(CollectionTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagCollection);
registry.register(ListTag.class, (from, target) -> NBTUtils.isTagCollection(from), NBTUtils::toTagList);
registry.register(Tag.class, NBTUtils::toTag);
registry.register(CompoundTag.class, (from, target) -> NBTWrapper.isTagCompound(from), NBTWrapper::wrapCompound);
registry.register(CollectionTag.class, (from, target) -> NBTWrapper.isTagCollection(from), NBTWrapper::wrapCollection);
registry.register(ListTag.class, (from, target) -> NBTWrapper.isTagCollection(from), NBTWrapper::wrapListTag);
registry.register(Tag.class, NBTWrapper::wrap);
registry.register(DataComponentType.class, DataComponentWrapper::wrapType);
registry.register(DataComponentMap.class, DataComponentWrapper::filter, (cx, from, target) -> DataComponentWrapper.mapOf(RegistryAccessContainer.of(cx).nbt(), from));
registry.register(DataComponentPatch.class, DataComponentWrapper::filter, (cx, from, target) -> DataComponentWrapper.patchOf(RegistryAccessContainer.of(cx).nbt(), from));

registry.register(BlockPos.class, KubeJSTypeWrappers::blockPosOf);
registry.register(Vec3.class, KubeJSTypeWrappers::vec3Of);
registry.register(Vec3i.class, KubeJSTypeWrappers::blockPosOf);
registry.register(BlockPos.class, KubeJSTypeWrappers::wrapBlockPos);
registry.register(Vec3.class, KubeJSTypeWrappers::wrapVec3);
registry.register(Vec3i.class, KubeJSTypeWrappers::wrapBlockPos);

registry.register(Item.class, ItemWrapper::wrapItem);
registry.register(ItemLike.class, ItemWrapper::wrapItem);
registry.registerEnumFromStringCodec(MobCategory.class, MobCategory.CODEC);
registry.register(ItemEnchantments.class, ItemEnchantmentsWrapper::from);
registry.register(ItemEnchantments.class, ItemEnchantmentsWrapper::wrap);

registry.register(AABB.class, AABBWrapper::wrap);
registry.register(IntProvider.class, KubeJSTypeWrappers::intProviderOf);
registry.register(FloatProvider.class, KubeJSTypeWrappers::floatProviderOf);
registry.register(NumberProvider.class, KubeJSTypeWrappers::numberProviderOf);
registry.register(IntProvider.class, KubeJSTypeWrappers::wrapIntProvider);
registry.register(FloatProvider.class, KubeJSTypeWrappers::wrapFloatProvider);
registry.register(NumberProvider.class, KubeJSTypeWrappers::wrapNumberProvider);
registry.registerEnumFromStringCodec(LootContext.EntityTarget.class, LootContext.EntityTarget.CODEC);
registry.registerEnumFromStringCodec(CopyNameFunction.NameSource.class, CopyNameFunction.NameSource.CODEC);
// FIXME registry.register(Enchantment.Cost.class, EnchantmentBuilder::costOf);
// FIXME registry.register(Enchantment.Cost.class, EnchantmentBuilder::wrapCost);
registry.registerEnumFromStringCodec(ArmorItem.Type.class, ArmorItem.Type.CODEC);
registry.register(BlockSetType.class, BlockWrapper::setTypeOf);
registry.register(BlockSetType.class, BlockWrapper::wrapSetType);
registry.register(BlockState.class, BlockWrapper::wrapBlockState);
registry.register(ItemAbility.class, ItemWrapper::itemAbilityOf);
registry.register(ColorRGBA.class, ColorWrapper::colorRGBAOf);
registry.register(ItemAbility.class, ItemWrapper::wrapItemAbility);
registry.register(ColorRGBA.class, ColorWrapper::wrapColorRGBA);

// KubeJS //
registry.register(ItemStack.class, ItemWrapper::wrap);
Expand All @@ -591,28 +592,28 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(FluidStack.class, FluidWrapper::wrap);
registry.register(FluidIngredient.class, FluidWrapper::wrapIngredient);
registry.register(SizedFluidIngredient.class, FluidWrapper::wrapSizedIngredient);
registry.register(RecipeFilter.class, RecipeFilter::of);
registry.register(RecipeFilter.class, RecipeFilter::wrap);
registry.register(SlotFilter.class, SlotFilter::wrap);
registry.register(Tier.class, ItemToolTiers::wrap);
registry.register(PlayerSelector.class, PlayerSelector::of);
registry.register(DamageSource.class, DamageSourceWrapper::of);
registry.register(EntitySelector.class, UtilsJS::entitySelector);
registry.register(PlayerSelector.class, PlayerSelector::wrap);
registry.register(DamageSource.class, DamageSourceWrapper::wrap);
registry.register(EntitySelector.class, EntitySelectorWrapper::wrap);
registry.register(ReplacementMatch.class, ReplacementMatch::wrap);
registry.register(ReplacementMatchInfo.class, ReplacementMatchInfo::wrap);
registry.register(Stat.class, PlayerStatsJS::statOf);
registry.register(MapColor.class, MapColorHelper::of);
registry.register(Stat.class, PlayerStatsJS::wrapStat);
registry.register(MapColor.class, MapColorHelper::wrap);
registry.register(SoundType.class, SoundTypeWrapper.INSTANCE);
registry.register(ParticleOptions.class, ParticleOptionsWrapper::wrap);
registry.register(ItemTintFunction.class, ItemTintFunction::of);
registry.register(BlockTintFunction.class, BlockTintFunction::of);
registry.register(ItemTintFunction.class, ItemTintFunction::wrap);
registry.register(BlockTintFunction.class, BlockTintFunction::wrap);
registry.register(Tristate.class, Tristate::wrap);

// components //
registry.register(Component.class, TextWrapper::of);
registry.register(MutableComponent.class, TextWrapper::of);
registry.register(KubeColor.class, ColorWrapper::of);
registry.register(TextColor.class, ColorWrapper::textColorOf);
registry.register(ClickEvent.class, TextWrapper::clickEventOf);
registry.register(Component.class, TextWrapper::wrap);
registry.register(MutableComponent.class, TextWrapper::wrap);
registry.register(KubeColor.class, ColorWrapper::wrap);
registry.register(TextColor.class, ColorWrapper::wrapTextColor);
registry.register(ClickEvent.class, TextWrapper::wrapClickEvent);

// codecs
registry.registerCodec(Fireworks.class, Fireworks.CODEC);
Expand Down
22 changes: 11 additions & 11 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.bindings.NBTWrapper;
import dev.latvian.mods.kubejs.bindings.StringUtilsWrapper;
import dev.latvian.mods.kubejs.level.LevelBlock;
import dev.latvian.mods.kubejs.util.NBTUtils;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -30,7 +30,7 @@

public interface KubeJSTypeWrappers {
@SuppressWarnings("unchecked")
static IntProvider intProviderOf(Context cx, Object o) {
static IntProvider wrapIntProvider(Context cx, Object o) {
if (o instanceof Number n) {
return ConstantInt.of(n.intValue());
} else if (o instanceof List l && !l.isEmpty()) {
Expand All @@ -44,7 +44,7 @@ static IntProvider intProviderOf(Context cx, Object o) {
if (intBounds != null) {
return intBounds;
} else if (m.containsKey("clamped")) {
var source = intProviderOf(cx, m.get("clamped"));
var source = wrapIntProvider(cx, m.get("clamped"));
var clampTo = parseIntBounds(m);
if (clampTo != null) {
return ClampedInt.of(source, clampTo.getMinValue(), clampTo.getMaxValue());
Expand All @@ -58,7 +58,7 @@ static IntProvider intProviderOf(Context cx, Object o) {
}
}

var decoded = IntProvider.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), NBTUtils.toTagCompound(cx, m)).result();
var decoded = IntProvider.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), NBTWrapper.wrapCompound(cx, m)).result();
if (decoded.isPresent()) {
return decoded.get();
}
Expand All @@ -68,7 +68,7 @@ static IntProvider intProviderOf(Context cx, Object o) {
}

@SuppressWarnings("unchecked")
static FloatProvider floatProviderOf(Context cx, Object o) {
static FloatProvider wrapFloatProvider(Context cx, Object o) {
if (o instanceof Number n) {
return ConstantFloat.of(n.floatValue());
} else if (o instanceof List l && !l.isEmpty()) {
Expand All @@ -90,7 +90,7 @@ static FloatProvider floatProviderOf(Context cx, Object o) {
}
}

var decoded = FloatProvider.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), NBTUtils.toTagCompound(cx, m)).result();
var decoded = FloatProvider.CODEC.parse(RegistryAccessContainer.of(cx).nbt(), NBTWrapper.wrapCompound(cx, m)).result();

if (decoded.isPresent()) {
return decoded.get();
Expand All @@ -101,7 +101,7 @@ static FloatProvider floatProviderOf(Context cx, Object o) {
}

@SuppressWarnings("unchecked")
static NumberProvider numberProviderOf(Object o) {
static NumberProvider wrapNumberProvider(Object o) {
if (o instanceof Number n) {
var f = n.floatValue();
return UniformGenerator.between(f, f);
Expand All @@ -124,7 +124,7 @@ static NumberProvider numberProviderOf(Object o) {
return ConstantValue.exactly(0);
}

static Vec3 vec3Of(@Nullable Object o) {
static Vec3 wrapVec3(@Nullable Object o) {
if (o instanceof Vec3 vec) {
return vec;
} else if (o instanceof Entity entity) {
Expand All @@ -140,7 +140,7 @@ static Vec3 vec3Of(@Nullable Object o) {
return Vec3.ZERO;
}

static BlockPos blockPosOf(@Nullable Object o) {
static BlockPos wrapBlockPos(@Nullable Object o) {
if (o instanceof BlockPos pos) {
return pos;
} else if (o instanceof List<?> list && list.size() >= 3) {
Expand Down Expand Up @@ -183,7 +183,7 @@ private static UniformFloat parseFloatBounds(Map<String, Object> m) {
}

@Nullable
static Path pathOf(Object o) {
static Path wrapPath(Object o) {
try {
if (o instanceof Path) {
return KubeJSPaths.verifyFilePath((Path) o);
Expand All @@ -198,7 +198,7 @@ static Path pathOf(Object o) {
}

@Nullable
static File fileOf(Object o) {
static File wrapFile(Object o) {
try {
if (o instanceof File) {
return KubeJSPaths.verifyFilePath(((File) o).toPath()).toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static BlockState parseBlockState(RegistryAccessContainer registries, Str
}
}

public static BlockSetType setTypeOf(Context cx, Object from, TypeInfo target) {
public static BlockSetType wrapSetType(Context cx, Object from, TypeInfo target) {
return switch (from) {
case null -> null;
case BlockSetType type -> type;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/bindings/ColorWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ColorWrapper {
}
});

static KubeColor of(Object o) {
static KubeColor wrap(Object o) {
if (o instanceof KubeColor) {
return (KubeColor) o;
} else if (o instanceof String) {
Expand Down Expand Up @@ -56,16 +56,16 @@ static KubeColor of(Object o) {
return NONE;
}

static TextColor textColorOf(Object o) {
return of(o).kjs$createTextColor();
static TextColor wrapTextColor(Object o) {
return wrap(o).kjs$createTextColor();
}

static ColorRGBA colorRGBAOf(Object o) {
return new ColorRGBA(of(o).kjs$getARGB());
static ColorRGBA wrapColorRGBA(Object o) {
return new ColorRGBA(wrap(o).kjs$getARGB());
}

static KubeColor createMapped(Object o, String... names) {
KubeColor c = of(o);
KubeColor c = wrap(o);

for (String s : names) {
MAP.put(s, c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraft.world.entity.player.Player;

public class DamageSourceWrapper {
public static DamageSource of(RegistryAccessContainer registries, Object from) {
public static DamageSource wrap(RegistryAccessContainer registries, Object from) {
return switch (from) {
case DamageSource source -> source;
case Player player -> registries.damageSources().playerAttack(player);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.latvian.mods.kubejs.bindings;

import com.mojang.brigadier.StringReader;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.commands.arguments.selector.EntitySelectorParser;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class EntitySelectorWrapper {
private static final Map<String, EntitySelector> ENTITY_SELECTOR_CACHE = new HashMap<>();
private static final EntitySelector ALL_ENTITIES_SELECTOR = new EntitySelector(EntitySelector.INFINITE, true, false, List.of(), MinMaxBounds.Doubles.ANY, Function.identity(), null, EntitySelectorParser.ORDER_RANDOM, false, null, null, null, true);

public static EntitySelector of(EntitySelector selector) {
return selector;
}

@HideFromJS
public static EntitySelector wrap(@Nullable Object o) {
if (o == null) {
return ALL_ENTITIES_SELECTOR;
} else if (o instanceof EntitySelector s) {
return s;
}

String s = o.toString();

if (s.isBlank()) {
return ALL_ENTITIES_SELECTOR;
}

var sel = ENTITY_SELECTOR_CACHE.get(s);

if (sel == null) {
sel = ALL_ENTITIES_SELECTOR;

try {
sel = new EntitySelectorParser(new StringReader(s), true).parse();
} catch (Exception ex) {
ex.printStackTrace();
}
}

ENTITY_SELECTOR_CACHE.put(s, sel);
return sel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static ItemStack playerHeadFromSkinHash(String hash) {
return playerHeadFromUrl("https://textures.minecraft.net/texture/" + hash);
}

static ItemAbility itemAbilityOf(Object object) {
static ItemAbility wrapItemAbility(Object object) {
if (object instanceof ItemAbility ta) {
return ta;
} else if (object != null) {
Expand Down
Loading

0 comments on commit c982918

Please sign in to comment.