From 6f843e0da933d7f7917703548d2e5992f4b4addc Mon Sep 17 00:00:00 2001 From: Ars Paradox <152275094+Iteranya@users.noreply.github.com> Date: Sun, 3 Nov 2024 20:06:52 +0700 Subject: [PATCH] Added a little confirmation that it failed to run the thing that it tries to run For Debugging --- .../mobtalkerredux/DemoCommand.java | 55 +++++++++++++++++++ .../mobtalkerredux/MobTalkerRedux.java | 48 +++++----------- 2 files changed, 68 insertions(+), 35 deletions(-) create mode 100644 src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java diff --git a/src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java b/src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java new file mode 100644 index 0000000..d02eb45 --- /dev/null +++ b/src/main/java/org/arsparadox/mobtalkerredux/DemoCommand.java @@ -0,0 +1,55 @@ +package org.arsparadox.mobtalkerredux; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import net.minecraft.client.Minecraft; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; +import org.arsparadox.mobtalkerredux.vn.controller.VisualNovelEngine; +import org.arsparadox.mobtalkerredux.vn.model.ScriptLoader; +import org.arsparadox.mobtalkerredux.vn.view.DialogueScreen; + +import java.io.IOException; + +public class DemoCommand { + public static void register(CommandDispatcher dispatcher) { + dispatcher.register(Commands.literal("mob_talker") + // Basic command: /demo + .executes(context -> { + if (context.getSource().getEntity() instanceof ServerPlayer player) { + Minecraft.getInstance().execute(() -> { + try { + Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadDemo()))); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + ); + } + return 1; + }) + // Command with argument: /demo + .then(Commands.argument("scriptFile", StringArgumentType.word()) + .executes(context -> { + String name = StringArgumentType.getString(context, "scriptFile"); + if (context.getSource().getEntity() instanceof ServerPlayer player) { + Minecraft.getInstance().execute(() -> { + try { + player.sendSystemMessage(Component.literal("Trying to load the file config/mobtalkerredux/"+name)); + Minecraft.getInstance().setScreen(new DialogueScreen(new VisualNovelEngine(ScriptLoader.loadScript(name)))); + + } catch (IOException e) { + player.sendSystemMessage(Component.literal("Failed to find the file config/mobtalkerredux/"+name)); + throw new RuntimeException(e); + + } + } + ); + } + return 1; + })) + ); + } +} diff --git a/src/main/java/org/arsparadox/mobtalkerredux/MobTalkerRedux.java b/src/main/java/org/arsparadox/mobtalkerredux/MobTalkerRedux.java index b20af04..2978ecf 100644 --- a/src/main/java/org/arsparadox/mobtalkerredux/MobTalkerRedux.java +++ b/src/main/java/org/arsparadox/mobtalkerredux/MobTalkerRedux.java @@ -1,11 +1,9 @@ package org.arsparadox.mobtalkerredux; -import com.google.gson.JsonObject; -import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.BuildCreativeModeTabContentsEvent; +import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -17,20 +15,15 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.RegistryObject; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.stream.Collectors; -// The value here should match an entry in the META-INF/mods.toml file @Mod("mobtalkerredux") public class MobTalkerRedux { - - // Directly reference a slf4j logger public static final Logger LOGGER = LogManager.getLogger(); public static final String MODID = "mobtalkerredux"; - private static JsonObject dialogData; public MobTalkerRedux() { // Register the setup method for modloading @@ -44,48 +37,41 @@ public MobTalkerRedux() { // Register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); + // Register for command registration + MinecraftForge.EVENT_BUS.register(RegistryEvents.class); } private void setup(final FMLCommonSetupEvent event) { - // Some preinit code LOGGER.info("HELLO FROM PREINIT"); - } private void enqueueIMC(final InterModEnqueueEvent event) { - // Some example code to dispatch IMC to another mod - InterModComms.sendTo(MobTalkerRedux.MODID, "helloworld", () -> { + InterModComms.sendTo(MODID, "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world"; }); } private void processIMC(final InterModProcessEvent event) { - // Some example code to receive and process InterModComms from other mods LOGGER.info("Got IMC {}", event.getIMCStream(). map(m->m.messageSupplier().get()). collect(Collectors.toList())); } - // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public void onServerStarting(ServerStartingEvent event) { - // Do something when the server starts LOGGER.info("HELLO from server starting"); } - // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD - // Event bus for receiving Registry Events) - @Mod.EventBusSubscriber(modid = "mobtalkerredux", bus = Mod.EventBusSubscriber.Bus.MOD) - public class RegistryEvents { - - - - public static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, "mobtalkerredux"); - public static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, "mobtalkerredux"); + public static class RegistryEvents { + public static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); + public static final DeferredRegister ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID); - // Example of a registered block and item - public static final RegistryObject HELLO_WORLD_ITEM = ITEMS.register("hello_world_item",HelloWorldItem::new); + // Move command registration to Forge event bus + @SubscribeEvent + public static void onRegisterCommands(RegisterCommandsEvent event) { + DemoCommand.register(event.getDispatcher()); + } public static void register(IEventBus eventBus) { BLOCKS.register(eventBus); @@ -96,13 +82,5 @@ public static void initialize() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); register(modEventBus); } - - @SubscribeEvent - public void buildContents(BuildCreativeModeTabContentsEvent event) { - // Add to ingredients tab - if (event.getTabKey() == CreativeModeTabs.INGREDIENTS) { - event.accept(HELLO_WORLD_ITEM); - } - } } -} +} \ No newline at end of file