diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index dd1cef947..be4917f3f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -59,6 +59,7 @@ import dev.latvian.mods.kubejs.fluid.ThickFluidBuilder; import dev.latvian.mods.kubejs.fluid.ThinFluidBuilder; import dev.latvian.mods.kubejs.item.ArmorMaterialBuilder; +import dev.latvian.mods.kubejs.item.ItemBuilder; import dev.latvian.mods.kubejs.item.ItemEnchantmentsWrapper; import dev.latvian.mods.kubejs.item.ItemPredicate; import dev.latvian.mods.kubejs.item.ItemStackJS; @@ -67,7 +68,6 @@ import dev.latvian.mods.kubejs.item.JukeboxSongBuilder; import dev.latvian.mods.kubejs.item.creativetab.CreativeTabBuilder; import dev.latvian.mods.kubejs.item.custom.ArmorItemBuilder; -import dev.latvian.mods.kubejs.item.custom.BasicItemJS; import dev.latvian.mods.kubejs.item.custom.DiggerItemBuilder; import dev.latvian.mods.kubejs.item.custom.ShearsItemBuilder; import dev.latvian.mods.kubejs.item.custom.SmithingTemplateItemBuilder; @@ -271,7 +271,7 @@ public void registerBuilderTypes(BuilderTypeRegistry registry) { reg.add("carpet", CarpetBlockBuilder.class, CarpetBlockBuilder::new); }); - registry.addDefault(Registries.ITEM, BasicItemJS.Builder.class, BasicItemJS.Builder::new); + registry.addDefault(Registries.ITEM, ItemBuilder.class, ItemBuilder::new); registry.of(Registries.ITEM, reg -> { reg.add("sword", SwordItemBuilder.class, SwordItemBuilder::new); diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJS.java b/src/main/java/dev/latvian/mods/kubejs/KubeJS.java index defa28418..a3ba86263 100644 --- a/src/main/java/dev/latvian/mods/kubejs/KubeJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJS.java @@ -8,65 +8,36 @@ import dev.latvian.mods.kubejs.gui.KubeJSMenus; import dev.latvian.mods.kubejs.holder.KubeJSHolderSets; import dev.latvian.mods.kubejs.ingredient.KubeJSIngredients; -import dev.latvian.mods.kubejs.item.creativetab.CreativeTabCallbackForge; -import dev.latvian.mods.kubejs.item.creativetab.CreativeTabKubeEvent; import dev.latvian.mods.kubejs.item.creativetab.KubeJSCreativeTabs; import dev.latvian.mods.kubejs.level.ruletest.KubeJSRuleTests; import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; import dev.latvian.mods.kubejs.recipe.KubeJSRecipeSerializers; import dev.latvian.mods.kubejs.registry.RegistryType; -import dev.latvian.mods.kubejs.script.ConsoleJS; -import dev.latvian.mods.kubejs.script.ConsoleLine; import dev.latvian.mods.kubejs.script.KubeJSBackgroundThread; import dev.latvian.mods.kubejs.script.PlatformWrapper; -import dev.latvian.mods.kubejs.script.ScriptFileInfo; import dev.latvian.mods.kubejs.script.ScriptManager; -import dev.latvian.mods.kubejs.script.ScriptPack; import dev.latvian.mods.kubejs.script.ScriptType; -import dev.latvian.mods.kubejs.script.ScriptsLoadedEvent; import dev.latvian.mods.kubejs.script.data.KubeFileResourcePack; import dev.latvian.mods.kubejs.util.RecordDefaults; -import dev.latvian.mods.kubejs.util.UtilsJS; -import net.minecraft.Util; import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.PackType; import net.neoforged.api.distmarker.Dist; -import net.neoforged.bus.api.EventPriority; import net.neoforged.bus.api.IEventBus; -import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModContainer; import net.neoforged.fml.ModList; -import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.Mod; -import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; -import net.neoforged.fml.event.lifecycle.FMLLoadCompleteEvent; -import net.neoforged.fml.loading.FMLLoader; -import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.common.NeoForgeMod; -import net.neoforged.neoforge.event.AddPackFindersEvent; -import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; import net.neoforged.neoforgespi.language.IModFileInfo; import net.neoforged.neoforgespi.language.IModInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.IOException; -import java.net.URI; import java.net.URLEncoder; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitOption; import java.nio.file.Files; -import java.nio.file.Path; -import java.time.Duration; import java.util.ArrayList; @Mod(KubeJS.MOD_ID) -@EventBusSubscriber(modid = KubeJS.MOD_ID, bus = EventBusSubscriber.Bus.MOD) public class KubeJS { public static final String MOD_ID = "kubejs"; public static final String MOD_NAME = "KubeJS"; @@ -99,6 +70,7 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable { modEventBus = bus; thisMod = mod; VERSION = mod.getModInfo().getVersion().toString(); + QUERY = "source=kubejs&mc=" + MC_VERSION_NUMBER + "&loader=neoforge&v=" + URLEncoder.encode(mod.getModInfo().getVersion().toString(), StandardCharsets.UTF_8); if (Files.notExists(KubeJSPaths.README)) { try { @@ -127,7 +99,9 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable { RecordDefaults.init(); - if (!PlatformWrapper.isGeneratingData()) { + boolean datagen = PlatformWrapper.isGeneratingData(); + + if (!datagen) { new KubeJSBackgroundThread().start(); // Required to be called this way because ConsoleJS class hasn't been initialized yet ScriptType.STARTUP.console.startCapturingErrors(); @@ -148,11 +122,17 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable { KubeJSPlugins.forEachPlugin(KubeJSPlugin::init); startupScriptManager = new StartupScriptManager(); - startupScriptManager.reload(); + + if (!datagen) { + startupScriptManager.reload(); + } if (dist.isClient()) { clientScriptManager = new ClientScriptManager(); - clientScriptManager.reload(); + + if (!datagen) { + clientScriptManager.reload(); + } } KubeJSPlugins.forEachPlugin(KubeJSPlugin::initStartup); @@ -180,108 +160,4 @@ public KubeJS(IEventBus bus, Dist dist, ModContainer mod) throws Throwable { StartupEvents.INIT.post(ScriptType.STARTUP, KubeStartupEvent.BASIC); // KubeJSRegistries.chunkGenerators().register(new ResourceLocation(KubeJS.MOD_ID, "flat"), () -> KJSFlatLevelSource.CODEC); } - - public static void loadScripts(ScriptPack pack, Path dir, String path) { - if (!path.isEmpty() && !path.endsWith("/")) { - path += "/"; - } - - final var pathPrefix = path; - - try { - for (var file : Files.walk(dir, 10, FileVisitOption.FOLLOW_LINKS).filter(Files::isRegularFile).toList()) { - var fileName = dir.relativize(file).toString().replace(File.separatorChar, '/'); - - if (fileName.endsWith(".js") || fileName.endsWith(".ts") && !fileName.endsWith(".d.ts")) { - pack.info.scripts.add(new ScriptFileInfo(pack.info, file, pathPrefix + fileName)); - } - } - } catch (IOException ex) { - ex.printStackTrace(); - } - } - - public static Path verifyFilePath(Path path) throws IOException { - if (!path.normalize().toAbsolutePath().startsWith(KubeJSPaths.GAMEDIR)) { - throw new IOException("You can't access files outside Minecraft directory!"); - } - - return path; - } - - @SubscribeEvent(priority = EventPriority.LOW) - public static void commonSetup(FMLCommonSetupEvent event) { - } - - @SubscribeEvent(priority = EventPriority.LOW) - public static void creativeTab(BuildCreativeModeTabContentsEvent event) { - var tabId = event.getTabKey().location(); - - if (StartupEvents.MODIFY_CREATIVE_TAB.hasListeners(tabId)) { - StartupEvents.MODIFY_CREATIVE_TAB.post(ScriptType.STARTUP, tabId, new CreativeTabKubeEvent(event.getTab(), event.hasPermissions(), new CreativeTabCallbackForge(event))); - } - } - - @SubscribeEvent(priority = EventPriority.LOW) - public static void loadComplete(FMLLoadCompleteEvent event) { - KubeJSPlugins.forEachPlugin(KubeJSPlugin::afterInit); - NeoForge.EVENT_BUS.post(new ScriptsLoadedEvent()); - StartupEvents.POST_INIT.post(ScriptType.STARTUP, KubeStartupEvent.BASIC); - UtilsJS.postModificationEvents(); - - if (!ConsoleJS.STARTUP.errors.isEmpty()) { - var list = new ArrayList(); - list.add("Startup script errors:"); - - var lines = ConsoleJS.STARTUP.errors.toArray(ConsoleLine.EMPTY_ARRAY); - - for (int i = 0; i < lines.length; i++) { - list.add((i + 1) + ") " + lines[i]); - } - - LOGGER.error(String.join("\n", list)); - - ConsoleJS.STARTUP.flush(true); - - if (FMLLoader.getDist() == Dist.DEDICATED_SERVER || !CommonProperties.get().startupErrorGUI) { - throw new RuntimeException("There were KubeJS startup script syntax errors! See logs/kubejs/startup.log for more info"); - } - } - - ConsoleJS.STARTUP.stopCapturingErrors(); - - QUERY = "source=kubejs&mc=" + MC_VERSION_NUMBER + "&loader=neoforge&v=" + URLEncoder.encode(thisMod.getModInfo().getVersion().toString(), StandardCharsets.UTF_8); - - Util.nonCriticalIoPool().submit(() -> { - try { - var response = HttpClient.newBuilder() - .followRedirects(HttpClient.Redirect.ALWAYS) - .connectTimeout(Duration.ofSeconds(5L)) - .build() - .send(HttpRequest.newBuilder().uri(URI.create("https://v.kubejs.com/update-check?" + QUERY)).GET().build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); - if (response.statusCode() == 200) { - var body = response.body().trim(); - - if (!body.isEmpty()) { - ConsoleJS.STARTUP.info("Update available: " + body); - } - } - } catch (Exception ignored) { - } - }); - } - - @SubscribeEvent(priority = EventPriority.HIGH) - public static void addPacksFirst(AddPackFindersEvent event) { - if (event.getPackType() == PackType.SERVER_DATA) { - // ServerScriptManager.addPacksFirst(event); - } - } - - @SubscribeEvent(priority = EventPriority.LOW) - public static void addPacksLast(AddPackFindersEvent event) { - if (event.getPackType() == PackType.SERVER_DATA) { - // ServerScriptManager.addPacksLast(event); - } - } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java new file mode 100644 index 000000000..154796a98 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java @@ -0,0 +1,117 @@ +package dev.latvian.mods.kubejs; + +import dev.latvian.mods.kubejs.bindings.event.StartupEvents; +import dev.latvian.mods.kubejs.event.KubeStartupEvent; +import dev.latvian.mods.kubejs.item.creativetab.CreativeTabCallbackForge; +import dev.latvian.mods.kubejs.item.creativetab.CreativeTabKubeEvent; +import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; +import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; +import dev.latvian.mods.kubejs.script.ConsoleJS; +import dev.latvian.mods.kubejs.script.ConsoleLine; +import dev.latvian.mods.kubejs.script.ScriptType; +import dev.latvian.mods.kubejs.script.ScriptsLoadedEvent; +import dev.latvian.mods.kubejs.util.UtilsJS; +import net.minecraft.Util; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.EventPriority; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.fml.event.lifecycle.FMLLoadCompleteEvent; +import net.neoforged.fml.loading.FMLLoader; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.ArrayList; + +@EventBusSubscriber(modid = KubeJS.MOD_ID, bus = EventBusSubscriber.Bus.MOD) +public class KubeJSModEventHandler { + /* + @SubscribeEvent(priority = EventPriority.LOW) + public static void commonSetup(FMLCommonSetupEvent event) { + } + */ + + @SubscribeEvent(priority = EventPriority.LOW) + public static void creativeTab(BuildCreativeModeTabContentsEvent event) { + var tabId = event.getTabKey().location(); + + if (StartupEvents.MODIFY_CREATIVE_TAB.hasListeners(tabId)) { + StartupEvents.MODIFY_CREATIVE_TAB.post(ScriptType.STARTUP, tabId, new CreativeTabKubeEvent(event.getTab(), event.hasPermissions(), new CreativeTabCallbackForge(event))); + } + } + + @SubscribeEvent(priority = EventPriority.LOW) + public static void loadComplete(FMLLoadCompleteEvent event) { + event.enqueueWork(KubeJSModEventHandler::loadComplete0); + } + + private static void loadComplete0() { + KubeJSPlugins.forEachPlugin(KubeJSPlugin::afterInit); + NeoForge.EVENT_BUS.post(new ScriptsLoadedEvent()); + StartupEvents.POST_INIT.post(ScriptType.STARTUP, KubeStartupEvent.BASIC); + UtilsJS.postModificationEvents(); + + if (!ConsoleJS.STARTUP.errors.isEmpty()) { + var list = new ArrayList(); + list.add("Startup script errors:"); + + var lines = ConsoleJS.STARTUP.errors.toArray(ConsoleLine.EMPTY_ARRAY); + + for (int i = 0; i < lines.length; i++) { + list.add((i + 1) + ") " + lines[i]); + } + + KubeJS.LOGGER.error(String.join("\n", list)); + + ConsoleJS.STARTUP.flush(true); + + if (FMLLoader.getDist() == Dist.DEDICATED_SERVER || !CommonProperties.get().startupErrorGUI) { + throw new RuntimeException("There were KubeJS startup script syntax errors! See logs/kubejs/startup.log for more info"); + } + } + + ConsoleJS.STARTUP.stopCapturingErrors(); + ConsoleJS.CLIENT.stopCapturingErrors(); + + Util.nonCriticalIoPool().submit(() -> { + try { + var response = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.ALWAYS) + .connectTimeout(Duration.ofSeconds(5L)) + .build() + .send(HttpRequest.newBuilder().uri(URI.create("https://v.kubejs.com/update-check?" + KubeJS.QUERY)).GET().build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); + if (response.statusCode() == 200) { + var body = response.body().trim(); + + if (!body.isEmpty()) { + ConsoleJS.STARTUP.info("Update available: " + body); + } + } + } catch (Exception ignored) { + } + }); + } + + /* + @SubscribeEvent(priority = EventPriority.HIGH) + public static void addPacksFirst(AddPackFindersEvent event) { + if (event.getPackType() == PackType.SERVER_DATA) { + // ServerScriptManager.addPacksFirst(event); + } + } + + @SubscribeEvent(priority = EventPriority.LOW) + public static void addPacksLast(AddPackFindersEvent event) { + if (event.getPackType() == PackType.SERVER_DATA) { + // ServerScriptManager.addPacksLast(event); + } + } + + */ +} diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java b/src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java index 4f3710b30..765080cf8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJSPaths.java @@ -4,6 +4,7 @@ import net.neoforged.fml.loading.FMLPaths; import org.apache.commons.lang3.mutable.MutableBoolean; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -58,4 +59,12 @@ static Path get(PackType type) { static Path getLocalDevProperties() { return CommonProperties.get().saveDevPropertiesInConfig ? CONFIG_DEV_PROPERTIES : LOCAL_DEV_PROPERTIES; } + + static Path verifyFilePath(Path path) throws IOException { + if (!path.normalize().toAbsolutePath().startsWith(GAMEDIR)) { + throw new IOException("You can't access files outside Minecraft directory!"); + } + + return path; + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java b/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java index a0581688c..c969293e1 100644 --- a/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJSTypeWrappers.java @@ -186,12 +186,12 @@ private static UniformFloat parseFloatBounds(Map m) { static Path pathOf(Object o) { try { if (o instanceof Path) { - return KubeJS.verifyFilePath((Path) o); + return KubeJSPaths.verifyFilePath((Path) o); } else if (o == null || o.toString().isEmpty()) { return null; } - return KubeJS.verifyFilePath(KubeJSPaths.GAMEDIR.resolve(o.toString())); + return KubeJSPaths.verifyFilePath(KubeJSPaths.GAMEDIR.resolve(o.toString())); } catch (Exception ex) { return null; } @@ -201,12 +201,12 @@ static Path pathOf(Object o) { static File fileOf(Object o) { try { if (o instanceof File) { - return KubeJS.verifyFilePath(((File) o).toPath()).toFile(); + return KubeJSPaths.verifyFilePath(((File) o).toPath()).toFile(); } else if (o == null || o.toString().isEmpty()) { return null; } - return KubeJS.verifyFilePath(KubeJSPaths.GAMEDIR.resolve(o.toString())).toFile(); + return KubeJSPaths.verifyFilePath(KubeJSPaths.GAMEDIR.resolve(o.toString())).toFile(); } catch (Exception ex) { return null; } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java index f913c72dc..f2cc1dc57 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java @@ -1,8 +1,6 @@ package dev.latvian.mods.kubejs.block; import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; -import dev.latvian.mods.kubejs.item.ItemBuilder; -import dev.latvian.mods.kubejs.item.custom.BasicItemJS; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.resources.ResourceLocation; @@ -15,9 +13,9 @@ import org.jetbrains.annotations.Nullable; public class SeedItemBuilder extends BlockItemBuilder { - public static class SeedItemJS extends BasicItemJS implements SpecialPlantable { - public SeedItemJS(ItemBuilder p) { - super(p); + public static class SeedItemJS extends Item implements SpecialPlantable { + public SeedItemJS(SeedItemBuilder b) { + super(b.createItemProperties()); } @Override @@ -46,7 +44,6 @@ public String getTranslationKeyGroup() { @Override public Item createObject() { - // return new ItemNameBlockItem(blockBuilder.get(), createItemProperties()); return new SeedItemJS(this); } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java index 889a8f9d0..20f77aae3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java @@ -14,13 +14,10 @@ import dev.latvian.mods.kubejs.block.callbacks.EntityFallenOnBlockCallbackJS; import dev.latvian.mods.kubejs.block.callbacks.EntitySteppedOnBlockCallbackJS; import dev.latvian.mods.kubejs.block.entity.BlockEntityJS; -import dev.latvian.mods.kubejs.core.BlockKJS; import dev.latvian.mods.kubejs.level.BlockContainerJS; import dev.latvian.mods.kubejs.script.ScriptType; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvent; @@ -60,7 +57,7 @@ import java.util.Optional; import java.util.function.Consumer; -public class BasicBlockJS extends Block implements BlockKJS, SimpleWaterloggedBlock { +public class BasicBlockJS extends Block implements SimpleWaterloggedBlock { public static class Builder extends BlockBuilder { public Builder(ResourceLocation i) { super(i); @@ -114,15 +111,6 @@ public BasicBlockJS(BlockBuilder p) { return blockBuilder; } - @Override - public MutableComponent getName() { - if (blockBuilder.displayName != null && blockBuilder.formattedDisplayName) { - return Component.literal("").append(blockBuilder.displayName); - } - - return super.getName(); - } - @Override @Deprecated public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { diff --git a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSClientEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java similarity index 99% rename from src/main/java/dev/latvian/mods/kubejs/client/KubeJSClientEventHandler.java rename to src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java index e2abc96e9..44e3fb5be 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSClientEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java @@ -50,7 +50,7 @@ import java.util.LinkedHashMap; @EventBusSubscriber(modid = KubeJS.MOD_ID, value = Dist.CLIENT) -public class KubeJSClientEventHandler { +public class KubeJSGameClientEventHandler { @SubscribeEvent public static void debugInfo(CustomizeGuiOverlayEvent.DebugText event) { var mc = Minecraft.getInstance(); @@ -371,7 +371,7 @@ public static void guiPostInit(ScreenEvent.Init.Post event) { @SubscribeEvent(priority = EventPriority.LOW) public static void openScreenEvent(ScreenEvent.Opening event) { - var s = KubeJSClientEventHandler.setScreen(event.getScreen()); + var s = KubeJSGameClientEventHandler.setScreen(event.getScreen()); if (s != null && event.getScreen() != s) { event.setNewScreen(s); diff --git a/src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java similarity index 91% rename from src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java rename to src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java index 1495e0c8e..c3c7d034e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSNeoForgeClient.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSModClientEventHandler.java @@ -1,4 +1,4 @@ -package dev.latvian.mods.kubejs.neoforge; +package dev.latvian.mods.kubejs.client; import com.mojang.blaze3d.platform.InputConstants; import com.mojang.blaze3d.vertex.DefaultVertexFormat; @@ -6,13 +6,6 @@ import dev.latvian.mods.kubejs.bindings.event.ClientEvents; import dev.latvian.mods.kubejs.bindings.event.ItemEvents; import dev.latvian.mods.kubejs.block.BlockBuilder; -import dev.latvian.mods.kubejs.client.BlockEntityRendererRegistryKubeEvent; -import dev.latvian.mods.kubejs.client.BlockTintFunctionWrapper; -import dev.latvian.mods.kubejs.client.EntityRendererRegistryKubeEvent; -import dev.latvian.mods.kubejs.client.ItemTintFunctionWrapper; -import dev.latvian.mods.kubejs.client.KubeJSClient; -import dev.latvian.mods.kubejs.client.KubeJSResourcePackFinder; -import dev.latvian.mods.kubejs.client.MenuScreenRegistryKubeEvent; import dev.latvian.mods.kubejs.fluid.FluidBlockBuilder; import dev.latvian.mods.kubejs.fluid.FluidBucketItemBuilder; import dev.latvian.mods.kubejs.fluid.FluidBuilder; @@ -51,11 +44,11 @@ import java.util.List; @EventBusSubscriber(modid = KubeJS.MOD_ID, value = Dist.CLIENT, bus = EventBusSubscriber.Bus.MOD) -public class KubeJSNeoForgeClient { +public class KubeJSModClientEventHandler { @SubscribeEvent(priority = EventPriority.LOW) public static void setupClient(FMLClientSetupEvent event) { KubeJS.PROXY = new KubeJSClient(); - event.enqueueWork(KubeJSNeoForgeClient::setupClient0); + event.enqueueWork(KubeJSModClientEventHandler::setupClient0); } @SubscribeEvent diff --git a/src/main/java/dev/latvian/mods/kubejs/client/editor/EditorCallback.java b/src/main/java/dev/latvian/mods/kubejs/client/editor/EditorCallback.java new file mode 100644 index 000000000..5a5620558 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/client/editor/EditorCallback.java @@ -0,0 +1,5 @@ +package dev.latvian.mods.kubejs.client.editor; + +public interface EditorCallback { + void callback(T value, boolean success); +} diff --git a/src/main/java/dev/latvian/mods/kubejs/client/editor/SelectItemStackScreen.java b/src/main/java/dev/latvian/mods/kubejs/client/editor/SelectItemStackScreen.java new file mode 100644 index 000000000..8e6b0e105 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/client/editor/SelectItemStackScreen.java @@ -0,0 +1,19 @@ +package dev.latvian.mods.kubejs.client.editor; + +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import net.minecraft.world.item.ItemStack; + +public class SelectItemStackScreen extends Screen { + public final EditorCallback callback; + + protected SelectItemStackScreen(EditorCallback callback) { + super(Component.literal("Select Item")); + this.callback = callback; + } + + @Override + protected void init() { + super.init(); + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java index fc073d3fc..20d0da175 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/MinecraftClientKJS.java @@ -138,7 +138,10 @@ public interface MinecraftClientKJS extends MinecraftEnvironmentKJS { @HideFromJS default void kjs$afterResourcesLoaded(boolean reload) { - ConsoleJS.CLIENT.stopCapturingErrors(); + if (reload) { + ConsoleJS.CLIENT.stopCapturingErrors(); + } + ConsoleJS.CLIENT.info("Client resource reload complete!"); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java index d391eb22f..b2e61c65e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/BlockMixin.java @@ -4,6 +4,8 @@ import dev.latvian.mods.kubejs.core.BlockKJS; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.core.Holder; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.block.Block; import org.jetbrains.annotations.Nullable; @@ -13,6 +15,9 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Block.class) @RemapPrefixForJS("kjs$") @@ -61,4 +66,11 @@ public abstract class BlockMixin implements BlockKJS { @Accessor("descriptionId") @Mutable public abstract void kjs$setNameKey(String key); + + @Inject(method = "getName", at = @At("HEAD"), cancellable = true) + private void kjs$getName(CallbackInfoReturnable cir) { + if (kjs$blockBuilder != null && kjs$blockBuilder.displayName != null && kjs$blockBuilder.formattedDisplayName) { + cir.setReturnValue(Component.literal("").append(kjs$blockBuilder.displayName)); + } + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java index cbd8419a2..ef1004ad3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ItemMixin.java @@ -174,6 +174,10 @@ private void getName(ItemStack itemStack, CallbackInfoReturnable ci) if (kjs$itemBuilder != null && kjs$itemBuilder.nameGetter != null) { ci.setReturnValue(kjs$itemBuilder.nameGetter.apply(itemStack)); } + + if (kjs$itemBuilder != null && kjs$itemBuilder.displayName != null && kjs$itemBuilder.formattedDisplayName) { + ci.setReturnValue(kjs$itemBuilder.displayName); + } } @Inject(method = "use", at = @At("HEAD"), cancellable = true) diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java index 774bab26b..a001cbab8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java @@ -43,7 +43,7 @@ @SuppressWarnings({"unused", "UnusedReturnValue"}) @ReturnsSelf -public abstract class ItemBuilder extends BuilderBase { +public class ItemBuilder extends BuilderBase { public record HurtEnemyContext(ItemStack getItem, LivingEntity getTarget, LivingEntity getAttacker) { } @@ -106,6 +106,11 @@ public ItemBuilder(ResourceLocation i) { itemAttributeModifiers = null; } + @Override + public Item createObject() { + return new Item(createItemProperties()); + } + @Override public Item transformObject(Item obj) { obj.kjs$setItemBuilder(this); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/custom/BasicItemJS.java b/src/main/java/dev/latvian/mods/kubejs/item/custom/BasicItemJS.java deleted file mode 100644 index b4a395e53..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/item/custom/BasicItemJS.java +++ /dev/null @@ -1,41 +0,0 @@ -package dev.latvian.mods.kubejs.item.custom; - -import dev.latvian.mods.kubejs.item.ItemBuilder; -import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; - -public class BasicItemJS extends Item { - public static class Builder extends ItemBuilder { - public Builder(ResourceLocation i) { - super(i); - } - - @Override - public Item createObject() { - return new BasicItemJS(this); - } - } - - private final ItemBuilder itemBuilder; - - public BasicItemJS(ItemBuilder p) { - super(p.createItemProperties()); - this.itemBuilder = p; - } - - @Override - public ItemBuilder kjs$getItemBuilder() { - return itemBuilder; - } - - @Override - public Component getName(ItemStack itemStack) { - if (itemBuilder.displayName != null && itemBuilder.formattedDisplayName) { - return itemBuilder.displayName; - } - - return super.getName(itemStack); - } -} \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java b/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java index 92d1481e2..8aac9fac7 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java @@ -519,8 +519,7 @@ public Recipe getOriginalRecipe() { if (originalRecipe == null) { originalRecipe = new MutableObject<>(); try { - // todo: this sucks - var holder = RecipeHelper.fromJson(type.event.registries.json(), type.schemaType.getSerializer(), getOrCreateId(), json, DevProperties.get().logErroringParsedRecipes); + var holder = RecipeHelper.fromJson(type.event.registries.json(), type.schemaType.getSerializer(), getOrCreateId(), originalJson, DevProperties.get().logErroringParsedRecipes); if (holder != null) { originalRecipe.setValue(holder.value()); diff --git a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchema.java b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchema.java index d224e25e6..6b36963c3 100644 --- a/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchema.java +++ b/src/main/java/dev/latvian/mods/kubejs/recipe/schema/RecipeSchema.java @@ -244,11 +244,7 @@ public KubeRecipe deserialize(SourceLine sourceLine, RecipeTypeFunction type, @N r.json = json; r.newRecipe = id == null; r.initValues(id == null); - - if (id != null && DevProperties.get().logRecipeDebug) { - r.originalJson = (JsonObject) JsonUtils.copy(json); - } - + r.originalJson = json == null || id == null ? null : (JsonObject) JsonUtils.copy(json); r.deserialize(false); return r; } diff --git a/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java b/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java index fb2f16c72..24dde375d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java @@ -8,8 +8,11 @@ import dev.latvian.mods.kubejs.util.LogType; import dev.latvian.mods.kubejs.util.RegistryAccessContainer; +import java.io.File; +import java.io.IOException; import java.lang.ref.WeakReference; import java.nio.charset.StandardCharsets; +import java.nio.file.FileVisitOption; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -55,6 +58,26 @@ public void reload() { KubeJSPlugins.forEachPlugin(this, KubeJSPlugin::afterScriptsLoaded); } + public void collectScripts(ScriptPack pack, Path dir, String path) { + if (!path.isEmpty() && !path.endsWith("/")) { + path += "/"; + } + + final var pathPrefix = path; + + try { + for (var file : Files.walk(dir, 10, FileVisitOption.FOLLOW_LINKS).filter(Files::isRegularFile).toList()) { + var fileName = dir.relativize(file).toString().replace(File.separatorChar, '/'); + + if (fileName.endsWith(".js") || fileName.endsWith(".ts") && !fileName.endsWith(".d.ts")) { + pack.info.scripts.add(new ScriptFileInfo(pack.info, file, pathPrefix + fileName)); + } + } + } catch (IOException ex) { + ex.printStackTrace(); + } + } + public void loadPackFromDirectory(Path path, String name, boolean exampleFile) { if (Files.notExists(path)) { if (!exampleFile) { @@ -79,7 +102,7 @@ public void loadPackFromDirectory(Path path, String name, boolean exampleFile) { var pack = new ScriptPack(this, new ScriptPackInfo(path.getFileName().toString(), "")); if (Files.exists(path)) { - KubeJS.loadScripts(pack, path, ""); + collectScripts(pack, path, ""); for (var fileInfo : pack.info.scripts) { loadFile(pack, fileInfo); diff --git a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java index c04454a7c..0b22a758f 100644 --- a/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java +++ b/src/main/java/dev/latvian/mods/kubejs/server/ServerScriptManager.java @@ -165,6 +165,10 @@ public void loadAdditional() { b.createAdditionalObjects(new AdditionalServerRegistryHandler(b.sourceLine, builders)); } + for (var b : builders) { + b.generateData(registriesDataPack); + } + for (var b : builders) { if (b.registryKey == null) { ConsoleJS.SERVER.error("", new KubeRuntimeException("Failed to register object '" + b.id + "' - unknown registry").source(b.sourceLine));