Can we just do something in here instead of adding @EventBusSubscriber package…
- User
Can we just do something in here instead of adding @EventBusSubscriber
package net.mattias.cobblesnapshot;
import net.mattias.cobblesnapshot.event.CameraUseEvents; import net.mattias.cobblesnapshot.item.ModCreativeModeTabs; import net.mattias.cobblesnapshot.item.ModItems; import org.slf4j.Logger; import com.mojang.logging.LogUtils; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModContainer; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.Mod; import net.neoforged.fml.config.ModConfig; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.server.ServerStartingEvent;
// The value here should match an entry in the META-INF/neoforge.mods.toml file @Mod(CobbleSnapshot.MOD_ID) public class CobbleSnapshot { // Define mod id in a common place for everything to reference public static final String MOD_ID = "cobblesnapshot"; // Directly reference a slf4j logger public static final Logger LOGGER = LogUtils.getLogger();
public CobbleSnapshot(IEventBus modEventBus, ModContainer modContainer) { // Register this class to the NeoForge event bus for server events NeoForge.EVENT_BUS.register(this); // Register items to the mod event bus ModItems.register(modEventBus); ModCreativeModeTabs.register(modEventBus); NeoForge.EVENT_BUS.register(CameraUseEvents.class); // Register config modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); } private void commonSetup(final FMLCommonSetupEvent event) { // Common setup code here } // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public void onServerStarting(ServerStartingEvent event) { // Server starting event code here } // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent @EventBusSubscriber(modid = MOD_ID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) public static class ClientModEvents { @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent event) { // In NeoForge 1.21, we need to modify the ImageFilmItem directly event.enqueueWork(() -> { // We'll use a simpler approach for now - just update the item model // The custom renderer approach is too complex for this stage }); } }}
Assistant
Yes, you absolutely can! The
@EventBusSubscriberannotation is a convenience for automatically registering all static@SubscribeEventmethods within a class. However, you can achieve the same result by manually registering your event listener methods to the correct event bus.Here's how you can modify your
CobbleSnapshotclass andScreenshotKeybindutility class to achieve this, without using@EventBusSubscriberonScreenshotKeybind.1. Update
ScreenshotKeybind.javaFirst, remove the
@EventBusSubscriberannotations fromScreenshotKeybindand its nested class. These methods will no longer be automatically discovered, so they don't need the@SubscribeEventannotation themselves withinScreenshotKeybind. They just become regular static methods that we will register manually.package net.mattias.cobblesnapshot.util; import net.mattias.cobblesnapshot.event.ScreenshotHandler; import net.mattias.cobblesnapshot.item.ModItems; import net.minecraft.client.KeyMapping; import net.minecraft.client.Minecraft; import net.minecraft.world.item.ItemStack; import net.neoforged.neoforge.client.event.InputEvent; import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent; import org.lwjgl.glfw.GLFW; // Removed @Mod.EventBusSubscriber. This is now just a utility class. public class ScreenshotKeybind { public static final KeyMapping TAKE_SNAPSHOT = new KeyMapping( "key.cobblesnapshot.take_snapshot", GLFW.GLFW_KEY_P, "key.categories.misc" ); // This method is now a regular static method. // It will be registered to the MOD event bus from CobbleSnapshot. public static void registerKeybindings(RegisterKeyMappingsEvent event) { event.register(TAKE_SNAPSHOT); } // This method is now a regular static method. // It will be registered to the FORGE event bus from CobbleSnapshot. public static void onKeyInput(InputEvent.Key event) { Minecraft mc = Minecraft.getInstance(); if (TAKE_SNAPSHOT.consumeClick() && mc.player != null) { ItemStack imageFilm = new ItemStack(ModItems.IMAGE_FILM.get()); ScreenshotHandler.captureScreenshot(imageFilm); if (!mc.player.getInventory().add(imageFilm)) { mc.player.drop(imageFilm, false); } } } }2. Update
CobbleSnapshot.javaNow, in your main
CobbleSnapshotclass, you will manually register these static methods to their respective event buses in the constructor. You will also remove theClientModEventsnested class as its purpose is now handled directly.package net.mattias.cobblesnapshot; import net.mattias.cobblesnapshot.event.CameraUseEvents; import net.mattias.cobblesnapshot.item.ModCreativeModeTabs; import net.mattias.cobblesnapshot.item.ModItems; import net.mattias.cobblesnapshot.util.ScreenshotKeybind; // Import your utility class import org.slf4j.Logger; import com.mojang.logging.LogUtils; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModContainer; // Removed the import for EventBusSubscriber if it's no longer used in this class // import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.Mod; import net.neoforged.fml.config.ModConfig; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.server.ServerStartingEvent; @Mod(CobbleSnapshot.MOD_ID) public class CobbleSnapshot { public static final String MOD_ID = "cobblesnapshot"; public static final Logger LOGGER = LogUtils.getLogger(); public CobbleSnapshot(IEventBus modEventBus, ModContainer modContainer) { // Register this class instance to the FORGE event bus for server events (e.g., onServerStarting) NeoForge.EVENT_BUS.register(this); // Register items and creative tabs to the MOD event bus ModItems.register(modEventBus); ModCreativeModeTabs.register(modEventBus); // Register CameraUseEvents class to the FORGE event bus (assuming it uses @EventBusSubscriber or is an instance) // If CameraUseEvents uses @EventBusSubscriber, this line is not strictly needed. // If it's an instance, it would be `NeoForge.EVENT_BUS.register(new CameraUseEvents());` // Assuming CameraUseEvents is correctly setup and this line was intended to register its static handlers: NeoForge.EVENT_BUS.register(CameraUseEvents.class); // --- MANUAL REGISTRATION OF KEYBINDING EVENTS --- // Register the FMLClientSetupEvent handler (on the MOD bus) // This method will be called when the client is setting up (e.g., for general client-side initialization) modEventBus.addListener(this::onClientSetup); // Register the RegisterKeyMappingsEvent handler (on the MOD bus) // This method will be called specifically to register keybindings. modEventBus.addListener(ScreenshotKeybind::registerKeybindings); // Register the InputEvent.Key handler (on the FORGE bus) // This method will be called when keyboard input occurs during gameplay. NeoForge.EVENT_BUS.addListener(ScreenshotKeybind::onKeyInput); // Register config modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC); } private void commonSetup(final FMLCommonSetupEvent event) { // Common setup code here } // This method handles the FMLClientSetupEvent (on the MOD bus) // It replaces the functionality previously in ClientModEvents.onClientSetup private void onClientSetup(final FMLClientSetupEvent event) { event.enqueueWork(() -> { // Any client-side work that needs to happen on the main thread during client setup. // For example, render type registrations, custom item properties, etc. // The original comment indicated this might be for item model updates, but it's empty here. }); } // Example: This method is automatically registered because `NeoForge.EVENT_BUS.register(this);` // was called in the constructor, and this method is an instance method with @SubscribeEvent. @SubscribeEvent public void onServerStarting(ServerStartingEvent event) { // Server starting event code here } // The ClientModEvents static inner class is removed. }Explanation of Changes:
-
ScreenshotKeybind.java:@Mod.EventBusSubscriberannotations are removed. This class is now a plain utility class containing static methods.@SubscribeEventannotations are removed fromregisterKeybindingsandonKeyInputmethods within this class because they are no longer being auto-discovered by anEventBusSubscriber.- These methods remain
public staticso they can be referenced directly fromCobbleSnapshot.
-
CobbleSnapshot.java:- The
ClientModEventsnested static class is removed entirely. - In the
CobbleSnapshotconstructor:modEventBus.addListener(this::onClientSetup);: This registers theonClientSetupinstance method (withinCobbleSnapshot) to handleFMLClientSetupEvent(which fires on the MOD bus).modEventBus.addListener(ScreenshotKeybind::registerKeybindings);: This explicitly registers the staticregisterKeybindingsmethod fromScreenshotKeybindto handleRegisterKeyMappingsEvent(which fires on the MOD bus).NeoForge.EVENT_BUS.addListener(ScreenshotKeybind::onKeyInput);: This explicitly registers the staticonKeyInputmethod fromScreenshotKeybindto handleInputEvent.Key(which fires on the FORGE bus).
- The
This approach gives you more explicit control over which methods are registered to which event bus, while avoiding the use of
@EventBusSubscriberin your utility classes if you prefer.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.