Skip to content

Commit

Permalink
Updated TJS, fixed release mc version, added more webserver renderers…
Browse files Browse the repository at this point in the history
…, PR cleanup
  • Loading branch information
LatvianModder committed Aug 18, 2024
1 parent 50cdcff commit 26277a6
Show file tree
Hide file tree
Showing 28 changed files with 675 additions and 252 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ unifiedPublishing {
releaseType = "${ENV.RELEASE_TYPE ?: 'release'}"
gameVersions = [
rootProject.minecraft_version,
'1.20'
'1.21'
]
gameLoaders = ["neoforge"]
displayName = "$mod_name NeoForge $project.version"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ neoforge_version=21.1.12
parchment_mc_version=1.21
parchment_mapping_version=2024.07.28
rhino_version=2100.2.5-build.46
tiny_server_version=1.0.0-build.2
tiny_server_version=1.0.0-build.3

architectury_version=13.0.6
rei_version=16.0.744
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,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::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(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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/mods/kubejs/KubeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable {
}

LOGGER.info("Loading vanilla registries...");
RegistryType.init();
RegistryType.Scanner.init();

var pluginTimer = Stopwatch.createStarted();
LOGGER.info("Looking for KubeJS plugins...");
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ static Path dir(Path dir) {
Path PACKICON = CONFIG.resolve("packicon.png");
Path README = DIRECTORY.resolve("README.txt");
Path LOCAL = dir(GAMEDIR.resolve("local").resolve("kubejs"));
Path LOCAL_CACHE = dir(LOCAL.resolve("cache"));
Path LOCAL_WEB_IMG_CACHE = dir(LOCAL_CACHE.resolve("web/img"));
Path LOCAL_DEV_PROPERTIES = LOCAL.resolve("dev.json");
Path EXPORT = dir(LOCAL.resolve("export"));
Path EXPORTED_PACKS = dir(LOCAL.resolve("exported_packs"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static RegistryWrapper<?> of(Context cx, ResourceLocation id) {
return RegistryAccessContainer.of(cx).wrapRegistry(id);
}

public static RegistryAccessContainer access() {
return RegistryAccessContainer.current;
}

public T get(ResourceLocation id) {
return registry.get(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.type.TypeInfo;
import dev.latvian.mods.rhino.util.HideFromJS;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -37,6 +38,7 @@ static SizedIngredient ofTag(TagKey<Item> tag, int count) {
return SizedIngredient.of(tag, count);
}

@HideFromJS
static SizedIngredient wrap(RegistryAccessContainer registries, Object from) {
if (from instanceof SizedIngredient s) {
return s;
Expand All @@ -57,6 +59,7 @@ static SizedIngredient wrap(RegistryAccessContainer registries, Object from) {
return IngredientJS.wrap(registries, from).kjs$asStack();
}

@HideFromJS
static SizedIngredient read(RegistryAccessContainer registries, StringReader reader) throws CommandSyntaxException {
int count = 1;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.latvian.mods.kubejs.block;

public enum BlockRotationType {
NONE,
VERTICAL,
HORIZONTAL,
FACING,
WALL_ATTACHED
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import dev.latvian.mods.kubejs.util.Cast;
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 it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
Expand All @@ -29,6 +28,9 @@

import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -253,23 +255,23 @@ 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) {
static DataComponentMap mapOf(DynamicOps<Tag> ops, Object o) {
try {
return readMap(registries.nbt(), new StringReader(o.toString()));
return readMap(ops, new StringReader(o.toString()));
} catch (CommandSyntaxException ex) {
throw new RuntimeException("Error parsing DataComponentMap from " + o, ex);
}
}

static DataComponentPatch patchOf(RegistryAccessContainer registries, Object o) {
static DataComponentPatch patchOf(DynamicOps<Tag> ops, Object o) {
try {
return readPatch(registries.nbt(), new StringReader(o.toString()));
return readPatch(ops, new StringReader(o.toString()));
} catch (CommandSyntaxException ex) {
throw new RuntimeException("Error parsing DataComponentPatch from " + o, ex);
}
}

static StringBuilder mapToString(StringBuilder builder, DynamicOps<Tag> dynamicOps, DataComponentMap map) {
static StringBuilder mapToString(StringBuilder builder, DynamicOps<Tag> ops, DataComponentMap map) {
builder.append('[');

boolean first = true;
Expand All @@ -282,7 +284,7 @@ static StringBuilder mapToString(StringBuilder builder, DynamicOps<Tag> dynamicO
}

var id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(comp.type());
var optional = comp.encodeValue(dynamicOps).result();
var optional = comp.encodeValue(ops).result();

if (id != null && !optional.isEmpty()) {
builder.append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString()).append('=').append(optional.get());
Expand All @@ -293,7 +295,7 @@ static StringBuilder mapToString(StringBuilder builder, DynamicOps<Tag> dynamicO
return builder;
}

static StringBuilder patchToString(StringBuilder builder, DynamicOps<Tag> dynamicOps, DataComponentPatch patch) {
static StringBuilder patchToString(StringBuilder builder, DynamicOps<Tag> ops, DataComponentPatch patch) {
builder.append('[');

boolean first = true;
Expand All @@ -309,7 +311,7 @@ static StringBuilder patchToString(StringBuilder builder, DynamicOps<Tag> dynami

if (id != null) {
if (comp.getValue().isPresent()) {
var value = comp.getKey().codecOrThrow().encodeStart(dynamicOps, Cast.to(comp.getValue().get())).result().get();
var value = comp.getKey().codecOrThrow().encodeStart(ops, Cast.to(comp.getValue().get())).result().get();
builder.append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString()).append('=').append(value);
} else {
builder.append('!').append(id.getNamespace().equals("minecraft") ? id.getPath() : id.toString());
Expand All @@ -321,22 +323,20 @@ static StringBuilder patchToString(StringBuilder builder, DynamicOps<Tag> dynami
return builder;
}

static void writeVisualComponentsForCache(StringBuilder builder, DynamicOps<?> ops, DataComponentMap map) {
for (var entry : map) {
if (DataComponentWrapper.VISUAL_DIFFERENCE.get().contains(entry.type())) {
builder.append(BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(entry.type()));

if (entry.type().codec() != null) {
if (entry.value() instanceof CharSequence || entry.value() instanceof Number || entry.value() instanceof Boolean || entry.value() instanceof Tag) {
builder.append(entry.value());
} else {
var str = entry.type().codec().encodeStart(ops, Cast.to(entry.value())).result().map(Object::toString).orElse("");
builder.append(str.isEmpty() ? entry.value() : str);
}
} else {
builder.append(entry.value());
}
}
static String urlEncodePatch(DynamicOps<Tag> ops, DataComponentPatch patch) {
var sb = patchToString(new StringBuilder(), ops, patch);
return URLEncoder.encode(sb.substring(1, sb.length() - 1), StandardCharsets.UTF_8);
}

static DataComponentPatch urlDecodePatch(DynamicOps<Tag> ops, String s) {
if (s.isEmpty()) {
return DataComponentPatch.EMPTY;
}

try {
return readPatch(ops, new StringReader("[" + URLDecoder.decode(s, StandardCharsets.UTF_8) + "]"));
} catch (Exception ex) {
return DataComponentPatch.EMPTY;
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ public interface BlockKJS extends BlockBuilderProvider, RegistryObjectKJS<Block>
default Object replaceThisWith(Context cx, Object with) {
return with instanceof Block block ? block : with instanceof BlockState state ? state.getBlock() : cx.jsToJava(with, BlockWrapper.TYPE_INFO);
}

default String getWebIconURL(int size) {
return this instanceof Block block ? block.defaultBlockState().getWebIconURL(size) : "";
}
}
9 changes: 9 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/BlockStateKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dev.latvian.mods.kubejs.block.RandomTickKubeEvent;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.util.ID;
import dev.latvian.mods.kubejs.web.KubeJSLocalWebServer;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -70,4 +72,11 @@ public interface BlockStateKJS extends RegistryObjectKJS<Block>, Replaceable {
default Object replaceThisWith(Context cx, Object with) {
return with instanceof BlockState state ? state : with instanceof Block block ? block.defaultBlockState() : cx.jsToJava(with, BlockWrapper.STATE_TYPE_INFO);
}

default String getWebIconURL(int size) {
var url = "/img/" + size + "/block/" + ID.url(kjs$getIdLocation());


return KubeJSLocalWebServer.getURL(url);
}
}
96 changes: 95 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/core/FluidStackKJS.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,100 @@
package dev.latvian.mods.kubejs.core;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DynamicOps;
import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.component.MutableDataComponentHolderFunctions;
import dev.latvian.mods.kubejs.fluid.FluidLike;
import dev.latvian.mods.kubejs.fluid.FluidWrapper;
import dev.latvian.mods.kubejs.recipe.match.FluidMatch;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
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.kubejs.web.KubeJSLocalWebServer;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.SpecialEquality;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;

public interface FluidStackKJS extends Replaceable, FluidLike, FluidMatch, MutableDataComponentHolderFunctions {
public interface FluidStackKJS extends
Replaceable,
SpecialEquality,
WithCodec,
FluidLike,
FluidMatch,
MutableDataComponentHolderFunctions,
RegistryObjectKJS<Fluid> {
default FluidStack kjs$self() {
return (FluidStack) (Object) this;
}

@Override
default boolean specialEquals(Context cx, Object o, boolean shallow) {
if (o instanceof CharSequence) {
return kjs$getId().equals(ID.string(o.toString()));
} else if (o instanceof FluidStack s) {
return kjs$equalsIgnoringCount(s);
}

return kjs$equalsIgnoringCount(FluidWrapper.wrap(RegistryAccessContainer.of(cx), o));
}

default boolean kjs$equalsIgnoringCount(FluidStack stack) {
var self = kjs$self();

if (self == stack) {
return true;
} else if (self.isEmpty()) {
return stack.isEmpty();
}

return FluidStack.isSameFluidSameComponents(self, stack);
}

@Override
default ResourceKey<Registry<Fluid>> kjs$getRegistryId() {
return Registries.FLUID;
}

@Override
default Registry<Fluid> kjs$getRegistry() {
return BuiltInRegistries.FLUID;
}

@Override
default ResourceLocation kjs$getIdLocation() {
return kjs$self().getFluid().kjs$getIdLocation();
}

@Override
default Holder<Fluid> kjs$asHolder() {
return kjs$self().getFluid().kjs$asHolder();
}

@Override
default ResourceKey<Fluid> kjs$getKey() {
return kjs$self().getFluid().kjs$getKey();
}

@Override
default String kjs$getId() {
return kjs$self().getFluid().kjs$getId();
}

@Override
default String kjs$getMod() {
return kjs$self().getFluid().kjs$getMod();
}

@Override
default int kjs$getAmount() {
return kjs$self().getAmount();
Expand All @@ -36,6 +115,11 @@ public interface FluidStackKJS extends Replaceable, FluidLike, FluidMatch, Mutab
return (FluidLike) (Object) kjs$self().copyWithAmount(amount);
}

@Override
default Codec<?> getCodec(Context cx) {
return FluidStack.CODEC;
}

@Override
default Object replaceThisWith(Context cx, Object with) {
var t = kjs$self();
Expand All @@ -58,4 +142,14 @@ default boolean matches(Context cx, FluidStack s, boolean exact) {
default boolean matches(Context cx, FluidIngredient ingredient, boolean exact) {
return ingredient.test(kjs$self());
}

default String getWebIconURL(DynamicOps<Tag> ops, int size) {
var url = "/img/" + size + "/fluid/" + ID.url(kjs$getIdLocation());

if (!kjs$self().isComponentsPatchEmpty()) {
url += "?components=" + DataComponentWrapper.urlEncodePatch(ops, kjs$self().getComponentsPatch());
}

return KubeJSLocalWebServer.getURL(url);
}
}
Loading

0 comments on commit 26277a6

Please sign in to comment.