Skip to content

Commit

Permalink
Updated Rhino, moved mixins out of common package, rewrote how server…
Browse files Browse the repository at this point in the history
… scripts are stored. Everything is *still* broken
  • Loading branch information
LatvianModder committed May 25, 2024

Unverified

The key that signed this doesn't have usage flags that allow signing.
1 parent 7486b4e commit 62c5c79
Showing 92 changed files with 505 additions and 300 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -9,9 +9,9 @@ mod_author=LatvianModder
curseforge_id=238086
modrinth_id=umyGl7zF
minecraft_version=1.20.6
neoforge_version=20.6.75-beta
rhino_version=2006.2.4-build.12
min_rhino_version=2006.2.4-build.12
neoforge_version=20.6.77-beta
rhino_version=2006.2.4-build.13
min_rhino_version=2006.2.4-build.13
architectury_version=12.1.2
rei_version=15.0.728
#rei_comp_version=9.0.16
142 changes: 136 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSComponents.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,161 @@
package dev.latvian.mods.kubejs;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.rhino.Context;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.TagParser;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.registries.DeferredRegister;

public interface KubeJSComponents {
DeferredRegister<DataComponentType<?>> DATA_COMPONENTS = DeferredRegister.create(Registries.DATA_COMPONENT_TYPE, KubeJS.MOD_ID);
DynamicCommandExceptionType ERROR_UNKNOWN_COMPONENT = new DynamicCommandExceptionType((object) -> Component.translatableEscape("arguments.item.component.unknown", object));
Dynamic2CommandExceptionType ERROR_MALFORMED_COMPONENT = new Dynamic2CommandExceptionType((object, object2) -> Component.translatableEscape("arguments.item.component.malformed", object, object2));
SimpleCommandExceptionType ERROR_EXPECTED_COMPONENT = new SimpleCommandExceptionType(Component.translatable("arguments.item.component.expected"));

static void init() {
}

static DataComponentMap readMap(RegistryOps<Tag> registryOps, StringReader reader) throws CommandSyntaxException {
var builder = DataComponentMap.builder();

reader.expect('[');

while (reader.canRead() && reader.peek() != ']') {
reader.skipWhitespace();
var dataComponentType = readComponentType(reader);

reader.skipWhitespace();
reader.expect('=');
reader.skipWhitespace();
int i = reader.getCursor();
var dataResult = dataComponentType.codecOrThrow().parse(registryOps, new TagParser(reader).readValue());
builder.set(dataComponentType, UtilsJS.cast(dataResult.getOrThrow((string) -> {
reader.setCursor(i);
return ERROR_MALFORMED_COMPONENT.createWithContext(reader, dataComponentType.toString(), string);
})));

reader.skipWhitespace();
if (!reader.canRead() || reader.peek() != ',') {
break;
}

reader.skip();
reader.skipWhitespace();
if (!reader.canRead()) {
throw ERROR_EXPECTED_COMPONENT.createWithContext(reader);
}
}

reader.expect(']');

return builder.build();
}

static DataComponentType<?> readComponentType(StringReader stringReader) throws CommandSyntaxException {
if (!stringReader.canRead()) {
throw ERROR_EXPECTED_COMPONENT.createWithContext(stringReader);
}

int i = stringReader.getCursor();
ResourceLocation resourceLocation = ResourceLocation.read(stringReader);
DataComponentType<?> dataComponentType = BuiltInRegistries.DATA_COMPONENT_TYPE.get(resourceLocation);
if (dataComponentType != null && !dataComponentType.isTransient()) {
return dataComponentType;
} else {
stringReader.setCursor(i);
throw ERROR_UNKNOWN_COMPONENT.createWithContext(stringReader, resourceLocation);
}
}

static DataComponentPatch readPatch(RegistryOps<Tag> registryOps, StringReader reader) throws CommandSyntaxException {
var patch = DataComponentPatch.builder();

return patch.build();
}

static DataComponentMap mapOf(Context cx, Object o) {
return null;
try {
return readMap(((KubeJSContext) cx).getRegistries().createSerializationContext(NbtOps.INSTANCE), new StringReader(o.toString()));
} catch (CommandSyntaxException ex) {
throw new RuntimeException(ex);
}
}

static DataComponentPatch patchOf(Context cx, Object o) {
return null;
try {
return readPatch(((KubeJSContext) cx).getRegistries().createSerializationContext(NbtOps.INSTANCE), new StringReader(o.toString()));
} catch (CommandSyntaxException ex) {
throw new RuntimeException(ex);
}
}

static String mapToString(Context cx, DataComponentMap map) {
return "[]";
static StringBuilder mapToString(StringBuilder builder, HolderLookup.Provider registries, DataComponentMap map) {
var dynamicOps = registries.createSerializationContext(NbtOps.INSTANCE);
builder.append('[');

boolean first = true;

for (var comp : map) {
if (first) {
first = false;
} else {
builder.append(',');
}

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

if (id != null && !optional.isEmpty()) {
builder.append(id).append('=').append(optional.get());
}
}

builder.append(']');
return builder;
}

static String patchToString(Context cx, DataComponentPatch patch) {
return "[]";
static StringBuilder patchToString(StringBuilder builder, HolderLookup.Provider registries, DataComponentPatch patch) {
var dynamicOps = registries.createSerializationContext(NbtOps.INSTANCE);
builder.append('[');

boolean first = true;

for (var comp : patch.entrySet()) {
if (first) {
first = false;
} else {
builder.append(',');
}

var id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(comp.getKey());

if (id != null) {
if (comp.getValue().isPresent()) {
var value = comp.getKey().codecOrThrow().encodeStart(dynamicOps, UtilsJS.cast(comp.getValue().get())).result().get();
builder.append(id).append('=').append(value);
} else {
builder.append('!').append(id).append("={}");
}
}
}

builder.append(']');
return builder;
}
}
14 changes: 10 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/bindings/ItemWrapper.java
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.JsonUtils;
import net.minecraft.Util;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.ResourceLocation;
@@ -44,22 +45,27 @@ static ItemStack of(ItemStack in, int count) {
}

@Info("Returns an ItemStack of the input, with the specified NBT data")
static ItemStack of(ItemStack in, DataComponentPatch components) {
static ItemStack of(ItemStack in, DataComponentMap components) {
return in.kjs$withComponents(components);
}

@Info("Returns an ItemStack of the input, with the specified count and NBT data")
static ItemStack of(ItemStack in, int count, DataComponentPatch components) {
static ItemStack of(ItemStack in, int count, DataComponentMap components) {
var is = in.kjs$withComponents(components);
is.setCount(count);
return is;
}

@Info("Returns an ItemStack of the input, with the specified NBT data")
static ItemStack withComponents(ItemStack in, DataComponentPatch components) {
@Info("Returns an ItemStack of the input, with the specified components")
static ItemStack withComponents(ItemStack in, DataComponentMap components) {
return in.kjs$withComponents(components);
}

@Info("Returns an ItemStack of the input, with the specified components")
static ItemStack withComponentPatch(ItemStack in, DataComponentPatch components) {
return in.kjs$withComponentPatch(components);
}

@Info("Get a list of most items in the game. Items not in a creative tab are ignored")
static List<ItemStack> getList() {
return ItemStackJS.getList();
Original file line number Diff line number Diff line change
@@ -80,23 +80,24 @@ private void itemTooltip(ItemStack stack, List<Component> lines, Item.TooltipCon

if (advanced && ClientProperties.get().getShowTagNames() && Screen.hasShiftDown()) {
var addToTempTags = (Consumer<TagKey<?>>) tag -> tempTagNames.computeIfAbsent(tag.location(), TagInstance::new).registries.add(tag.registry());
var cx = ScriptType.CLIENT.manager.get().contextFactory.enter();

Tags.byItemStack(stack).forEach(addToTempTags);
Tags.byItemStack(cx, stack).forEach(addToTempTags);

if (stack.getItem() instanceof BlockItem item) {
Tags.byBlock(item.getBlock()).forEach(addToTempTags);
Tags.byBlock(cx, item.getBlock()).forEach(addToTempTags);
}

if (stack.getItem() instanceof BucketItem bucket) {
Fluid fluid = FluidBucketHooks.getFluid(bucket);

if (fluid != Fluids.EMPTY) {
Tags.byFluid(fluid).forEach(addToTempTags);
Tags.byFluid(cx, fluid).forEach(addToTempTags);
}
}

if (stack.getItem() instanceof SpawnEggItem item) {
Tags.byEntityType(item.getType(stack)).forEach(addToTempTags);
Tags.byEntityType(cx, item.getType(stack)).forEach(addToTempTags);
}

for (var instance : tempTagNames.values()) {
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
import dev.latvian.mods.kubejs.core.ReloadableServerResourcesKJS;
import dev.latvian.mods.kubejs.core.WithPersistentData;
import dev.latvian.mods.kubejs.event.EventGroups;
import dev.latvian.mods.kubejs.event.EventResult;
@@ -28,7 +29,6 @@
import dev.latvian.mods.kubejs.script.data.ExportablePackResources;
import dev.latvian.mods.kubejs.server.CustomCommandKubeEvent;
import dev.latvian.mods.kubejs.server.DataExport;
import dev.latvian.mods.kubejs.server.ServerScriptManager;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
@@ -344,7 +344,7 @@ private static int dumpEvents(CommandSourceStack source) {
builder.append("\n\n");
}

var scriptManager = ScriptType.SERVER.manager.get();
var scriptManager = ((ReloadableServerResourcesKJS) source.getServer().getServerResources().managers()).kjs$getServerScriptManager();
var cx = (KubeJSContext) scriptManager.contextFactory.enter();

var members = JavaMembers.lookupClass(cx, cx.topLevelScope, eventType, null, false);
@@ -535,7 +535,7 @@ private static int hand(ServerPlayer player, InteractionHand hand) {

// item info
// id
player.sendSystemMessage(copy(ItemStackJS.toItemString(stack), ChatFormatting.GREEN, "Item ID"));
player.sendSystemMessage(copy(ItemStackJS.toItemString(player.server.registryAccess(), stack), ChatFormatting.GREEN, "Item ID"));
// item tags
var itemTags = holder.tags().toList();
for (var tag : itemTags) {
@@ -596,7 +596,8 @@ private static int hotbar(ServerPlayer player) {
}

private static int dump(List<ItemStack> stacks, ServerPlayer player, String name) {
var dump = stacks.stream().filter(is -> !is.isEmpty()).map(ItemStackJS::toItemString).toList();
var registries = player.server.registryAccess();
var dump = stacks.stream().filter(is -> !is.isEmpty()).map(is -> ItemStackJS.toItemString(registries, is)).toList();
player.sendSystemMessage(copy(dump.toString(), ChatFormatting.WHITE, name + " Item List"));
return 1;
}
@@ -669,7 +670,8 @@ private static int reloadStartup(CommandSourceStack source) {
}

private static int reloadServer(CommandSourceStack source) {
ServerScriptManager.instance.reload(source.getServer().kjs$getReloadableResources().resourceManager());
var resources = source.getServer().getServerResources();
((ReloadableServerResourcesKJS) resources.managers()).kjs$getServerScriptManager().reload(resources.resourceManager());
source.sendSuccess(() -> Component.literal("Done! To reload recipes, tags, loot tables and other datapack things, run ")
.append(Component.literal("'/reload'")
.kjs$clickRunCommand("/reload")
Loading

0 comments on commit 62c5c79

Please sign in to comment.