Skip to content

Commit

Permalink
More renames, repackages
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Dec 6, 2024
1 parent 63f4fd4 commit f1befc4
Show file tree
Hide file tree
Showing 42 changed files with 486 additions and 487 deletions.
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ minecraft_version=1.21.1
supported_versions=1.21.1, 1.21
mod_version=2101.7.2

neoforge_version=21.1.42
neoforge_version=21.1.83
parchment_mc_version=1.21
parchment_mapping_version=2024.07.28
parchment_mapping_version=2024.11.10
rhino_version=2101.2.5-build.54
tiny_server_version=1.0.0-build.18
tiny_server_version=1.0.0-build.25
gif_lib_version=1.7

architectury_version=13.0.6
rei_version=16.0.754
jei_version=19.16.4.176
emi_version=1.1.12+1.21
architectury_version=13.0.8
rei_version=16.0.788
jei_version=19.21.0.247
emi_version=1.1.18+1.21.1
21 changes: 6 additions & 15 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.latvian.mods.kubejs.bindings.ParticleOptionsWrapper;
import dev.latvian.mods.kubejs.bindings.RegistryWrapper;
import dev.latvian.mods.kubejs.bindings.SizedIngredientWrapper;
import dev.latvian.mods.kubejs.bindings.StringUtilsWrapper;
import dev.latvian.mods.kubejs.bindings.TextIcons;
import dev.latvian.mods.kubejs.bindings.TextWrapper;
import dev.latvian.mods.kubejs.bindings.UUIDWrapper;
Expand Down Expand Up @@ -145,8 +146,6 @@
import dev.latvian.mods.kubejs.util.JsonIO;
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.KubeResourceLocation;
import dev.latvian.mods.kubejs.util.ListJS;
import dev.latvian.mods.kubejs.util.MapJS;
import dev.latvian.mods.kubejs.util.NBTIOWrapper;
import dev.latvian.mods.kubejs.util.NBTUtils;
import dev.latvian.mods.kubejs.util.NotificationToastData;
Expand Down Expand Up @@ -256,12 +255,8 @@
import java.nio.file.Path;
import java.time.Duration;
import java.time.temporal.TemporalAmount;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -485,6 +480,7 @@ public void registerBindings(BindingRegistry bindings) {

bindings.add("KMath", KMath.class);
bindings.add("Utils", UtilsWrapper.class);
bindings.add("StringUtils", StringUtilsWrapper.class);
bindings.add("Java", JavaWrapper.class);
bindings.add("Text", TextWrapper.class);
bindings.add("Component", TextWrapper.class);
Expand Down Expand Up @@ -545,8 +541,8 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(CharSequence.class, String::valueOf);
registry.register(UUID.class, UUIDWrapper::fromString);
registry.register(Pattern.class, RegExpKJS::wrap);
registry.register(JsonObject.class, MapJS::json);
registry.register(JsonArray.class, ListJS::json);
registry.register(JsonObject.class, JsonUtils::objectOf);
registry.register(JsonArray.class, JsonUtils::arrayOf);
registry.register(JsonElement.class, JsonUtils::of);
registry.register(JsonPrimitive.class, JsonUtils::primitiveOf);
registry.register(Path.class, KubeJSTypeWrappers::pathOf);
Expand Down Expand Up @@ -588,17 +584,12 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(ColorRGBA.class, ColorWrapper::colorRGBAOf);

// KubeJS //
registry.register(Map.class, MapJS::of);
registry.register(List.class, ListJS::of);
registry.register(Iterable.class, ListJS::of);
registry.register(Collection.class, ListJS::of);
registry.register(Set.class, ListJS::ofSet);
registry.register(ItemStack.class, ItemStackJS::wrap);
registry.register(Ingredient.class, IngredientJS::wrap);
registry.register(ItemPredicate.class, ItemPredicate::wrap);
registry.register(SizedIngredient.class, SizedIngredientWrapper::wrap);
registry.register(BlockStatePredicate.class, BlockStatePredicate::of);
registry.register(RuleTest.class, BlockStatePredicate::ruleTestOf);
registry.register(BlockStatePredicate.class, BlockStatePredicate::wrap);
registry.register(RuleTest.class, BlockStatePredicate::wrapRuleTest);
registry.register(FluidStack.class, FluidWrapper::wrap);
registry.register(FluidIngredient.class, FluidWrapper::wrapIngredient);
registry.register(SizedFluidIngredient.class, FluidWrapper::wrapSizedIngredient);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.MapCodec;
import dev.latvian.mods.kubejs.bindings.StringUtilsWrapper;
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.type.EnumTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
import io.netty.buffer.ByteBuf;
Expand Down Expand Up @@ -127,7 +127,7 @@ static <E, X extends Throwable> JsonElement toJsonOrThrow(E value, Codec<E> code
}

static <T> String getUniqueId(T input, Codec<T> codec) {
return UtilsJS.getUniqueId(input, o -> toJsonOrThrow(o, codec));
return StringUtilsWrapper.getUniqueId(input, o -> toJsonOrThrow(o, codec));
}

static JsonElement numberProviderJson(NumberProvider gen) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.latvian.mods.kubejs;

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.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.BlockPos;
import net.minecraft.util.valueproviders.ClampedInt;
Expand Down Expand Up @@ -130,7 +130,7 @@ static Vec3 vec3Of(@Nullable Object o) {
} else if (o instanceof Entity entity) {
return entity.position();
} else if (o instanceof List<?> list && list.size() >= 3) {
return new Vec3(UtilsJS.parseDouble(list.get(0), 0), UtilsJS.parseDouble(list.get(1), 0), UtilsJS.parseDouble(list.get(2), 0));
return new Vec3(StringUtilsWrapper.parseDouble(list.get(0), 0), StringUtilsWrapper.parseDouble(list.get(1), 0), StringUtilsWrapper.parseDouble(list.get(2), 0));
} else if (o instanceof BlockPos pos) {
return new Vec3(pos.getX() + 0.5D, pos.getY() + 0.5D, pos.getZ() + 0.5D);
} else if (o instanceof LevelBlock block) {
Expand All @@ -144,7 +144,7 @@ static BlockPos blockPosOf(@Nullable Object o) {
if (o instanceof BlockPos pos) {
return pos;
} else if (o instanceof List<?> list && list.size() >= 3) {
return new BlockPos(UtilsJS.parseInt(list.get(0), 0), UtilsJS.parseInt(list.get(1), 0), UtilsJS.parseInt(list.get(2), 0));
return new BlockPos(StringUtilsWrapper.parseInt(list.get(0), 0), StringUtilsWrapper.parseInt(list.get(1), 0), StringUtilsWrapper.parseInt(list.get(2), 0));
} else if (o instanceof LevelBlock block) {
return block.getPos();
} else if (o instanceof Vec3 vec) {
Expand All @@ -156,7 +156,7 @@ static BlockPos blockPosOf(@Nullable Object o) {

private static UniformInt parseIntBounds(Map<String, Object> m) {
if (m.get("bounds") instanceof List bounds) {
return UniformInt.of(UtilsJS.parseInt(bounds.get(0), 0), UtilsJS.parseInt(bounds.get(1), 0));
return UniformInt.of(StringUtilsWrapper.parseInt(bounds.get(0), 0), StringUtilsWrapper.parseInt(bounds.get(1), 0));
} else if (m.containsKey("min") && m.containsKey("max")) {
return UniformInt.of(((Number) m.get("min")).intValue(), ((Number) m.get("max")).intValue());
} else if (m.containsKey("min_inclusive") && m.containsKey("max_inclusive")) {
Expand All @@ -170,7 +170,7 @@ private static UniformInt parseIntBounds(Map<String, Object> m) {

private static UniformFloat parseFloatBounds(Map<String, Object> m) {
if (m.get("bounds") instanceof List bounds) {
return UniformFloat.of((float) UtilsJS.parseDouble(bounds.get(0), 0), (float) UtilsJS.parseDouble(bounds.get(1), 0));
return UniformFloat.of((float) StringUtilsWrapper.parseDouble(bounds.get(0), 0), (float) StringUtilsWrapper.parseDouble(bounds.get(1), 0));
} else if (m.containsKey("min") && m.containsKey("max")) {
return UniformFloat.of(((Number) m.get("min")).floatValue(), ((Number) m.get("max")).floatValue());
} else if (m.containsKey("min_inclusive") && m.containsKey("max_inclusive")) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/bindings/JavaWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.rhino.BaseFunction;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;
import dev.latvian.mods.rhino.util.HideFromJS;
import org.jetbrains.annotations.Nullable;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -31,4 +36,18 @@ static Object tryLoadClass(KubeJSContext cx, String className) {
static ConsoleJS createConsole(KubeJSContext cx, String name) {
return new ConsoleJS(cx.getType(), LoggerFactory.getLogger(name));
}

static <T> T makeFunctionProxy(Context cx, TypeInfo targetClass, BaseFunction function) {
return Cast.to(cx.createInterfaceAdapter(targetClass, function));
}

@Nullable
@HideFromJS
static Class<?> tryLoadClass(String className) {
try {
return Class.forName(className);
} catch (Exception ignored) {
return null;
}
}
}
156 changes: 156 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/bindings/StringUtilsWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package dev.latvian.mods.kubejs.bindings;

import com.google.gson.JsonElement;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.JsonIO;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.regex.Pattern;

public interface StringUtilsWrapper {
Pattern SNAKE_CASE_SPLIT = Pattern.compile("[:_/]");
Set<String> ALWAYS_LOWER_CASE = new HashSet<>(Arrays.asList("a", "an", "the", "of", "on", "in", "and", "or", "but", "for"));
String[] EMPTY_STRING_ARRAY = new String[0];

@Info("Tries to parse the first parameter as an integer, and returns that. The second parameter is returned if parsing fails")
static int parseInt(@Nullable Object object, int def) {
if (object == null) {
return def;
} else if (object instanceof Number num) {
return num.intValue();
}

try {
var s = object.toString();

if (s.isEmpty()) {
return def;
}

return Integer.parseInt(s);
} catch (Exception ex) {
return def;
}
}

static long parseLong(@Nullable Object object, long def) {
if (object == null) {
return def;
} else if (object instanceof Number num) {
return num.longValue();
}

try {
var s = object.toString();

if (s.isEmpty()) {
return def;
}

return Long.parseLong(s);
} catch (Exception ex) {
return def;
}
}

@Info("Tries to parse the first parameter as a double and returns that. The second parameter is returned if parsing fails")
static double parseDouble(@Nullable Object object, double def) {
if (object == null) {
return def;
} else if (object instanceof Number num) {
return num.doubleValue();
}

try {
var s = object.toString();

if (s.isEmpty()) {
return def;
}

return Double.parseDouble(String.valueOf(object));
} catch (Exception ex) {
return def;
}
}

@Info("Returns the provided snake_case_string in camelCase")
static String snakeCaseToCamelCase(String string) {
if (string == null || string.isEmpty()) {
return string;
}

var s = SNAKE_CASE_SPLIT.split(string, 0);

var sb = new StringBuilder();
var first = true;

for (var value : s) {
if (!value.isEmpty()) {
if (first) {
first = false;
sb.append(value);
} else {
sb.append(Character.toUpperCase(value.charAt(0)));
sb.append(value, 1, value.length());
}
}
}

return sb.toString();
}

@Info("Returns the provided snake_case_string in Title Case")
static String snakeCaseToTitleCase(String string) {
StringJoiner joiner = new StringJoiner(" ");
String[] split = string.split("_");
for (int i = 0; i < split.length; i++) {
String s = split[i];
String titleCase = toTitleCase(s, i == 0);
joiner.add(titleCase);
}
return joiner.toString();
}

@Info("Capitalises the first letter of the string unless it is \"a\", \"an\", \"the\", \"of\", \"on\", \"in\", \"and\", \"or\", \"but\" or \"for\"")
static String toTitleCase(String s) {
return toTitleCase(s, false);
}

@Info("Capitalises the first letter of the string. If ignoreSpecial is true, it will also capitalise articles and prepositions")
static String toTitleCase(String s, boolean ignoreSpecial) {
if (s.isEmpty()) {
return "";
} else if (!ignoreSpecial && ALWAYS_LOWER_CASE.contains(s)) {
return s;
} else if (s.length() == 1) {
return s.toUpperCase();
}

char[] chars = s.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
return new String(chars);
}

static String stripIdForEvent(ResourceLocation id) {
return stripEventName(id.toString());
}

static String getUniqueId(JsonElement json) {
return getUniqueId(json, Function.identity());
}

static <T> String getUniqueId(T input, Function<T, JsonElement> toJson) {
return JsonIO.getJsonHashString(toJson.apply(input));
}

static String stripEventName(String s) {
return s.replaceAll("[/:]", ".").replace('-', '_');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.JSObjectType;
import dev.latvian.mods.kubejs.util.JsonUtils;
import dev.latvian.mods.kubejs.util.ListJS;
import dev.latvian.mods.kubejs.util.MapJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.Context;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -74,7 +72,7 @@ static MutableComponent of(Context cx, @Nullable Object o) {
for (var e1 : a) {
with[i] = e1;

if (with[i] instanceof MapJS || with[i] instanceof ListJS) {
if (with[i] instanceof Map<?, ?> || with[i] instanceof Collection<?>) {
with[i] = of(cx, e1);
}

Expand Down Expand Up @@ -148,7 +146,8 @@ static ClickEvent clickEventOf(Context cx, Object o) {
return ce;
}

var json = MapJS.json(cx, o);
var json = JsonUtils.objectOf(cx, o);

if (json != null) {
var action = GsonHelper.getAsString(json, "action");
var value = GsonHelper.getAsString(json, "value");
Expand Down
Loading

0 comments on commit f1befc4

Please sign in to comment.