Posted June 19, 20205 yr What im trying to achieve: Add newSimpleChannel and custom messages for client-server communication. Main problem: When im trying to send packets from client to server using this code: NetworkHandler.INSTANCE.sendToServer(msg); I get null pointer exception. msg is not null, there is a corresponding check for this, then probably INSTANCE is null here. That's what debugger tells me: Which is pretty strange considering that I assigning value to INSTANCE in FMLCommonSetupEvent: public class NetworkHandler { public NetworkHandler(){ FMLJavaModLoadingContext.get().getModEventBus().addListener(this::commonSetup); } private void commonSetup(FMLCommonSetupEvent event){ System.out.println("[ACS] Initiating NetworkHandler"); INSTANCE = NetworkRegistry.newSimpleChannel( new ResourceLocation("advancedcombatsystem", "ACSchannel"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); // Registering messages INSTANCE.registerMessage(SEND_DAMAGE_TO_ID, MessageSendDamageTo.class, MessageSendDamageTo::encode, MessageSendDamageTo::decode, ServerMessagesHandler::onSendDamageToReceived ); } } Based on this, I conclude that this event is not fired. Additional info: I've tried: to subscribe to FMLCommonSetupEvent using SubcsribeEvent - same issue. to add Mod("mymodid"), Mod.EventBusSubscriber(modid = "mymodid") above class declaration - did not help. to call "init" function (commonSetup in code above) within same event (FMLCommonSetupEvent) in another class, where there is other events that works well - same issue (FMLCommonSetupEvent does not start there either).
June 19, 20205 yr Author 4 minutes ago, diesieben07 said: Where do you call this constructor? It needs to be called? I thought it worked like EVENT_BUS registration: @Mod("advancedcombatsystem") public class ACSInputHandler { public ACSInputHandler(){ MinecraftForge.EVENT_BUS.register(this); } In that case, where should I call it? If I understand correctly, that constructor must be called before the event is executed. But the event occurs when the game starts and before it is impossible to do anything, as I understand it.
June 19, 20205 yr Author 53 minutes ago, diesieben07 said: Code doesn't get executed by magic 😮 53 minutes ago, diesieben07 said: If you don't tell anything to run the code then, surprise, it won't run. What about automatic registration like here? @Mod("randommod") public class randomClass { public randomClass(){ MinecraftForge.EVENT_BUS.register(this); } } There is no word in the documentation that the constructor with FMLModLoadingContext.get().getModEventBus().registerListener(); needs to be called manually. Anyway, this code does not work either: @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = "advancedcombatsystem") public class NetworkHandler { @SubscribeEvent public void commonSetup(FMLCommonSetupEvent event){ System.out.println("[ACS] Initiating NetworkHandler"); INSTANCE = NetworkRegistry.newSimpleChannel( new ResourceLocation("advancedcombatsystem", "ACSchannel"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); // Registering messages INSTANCE.registerMessage(SEND_DAMAGE_TO_ID, MessageSendDamageTo.class, MessageSendDamageTo::encode, MessageSendDamageTo::decode, ServerMessagesHandler::onSendDamageToReceived ); } } Edited June 19, 20205 yr by byalexeykh
June 19, 20205 yr Author 4 hours ago, diesieben07 said: Nothing is automatic here, except that FML will of course instantiate your @Mod class Nothing is automatic here, just forge executing constructor automatically... good contradictions 4 hours ago, diesieben07 said: Basic Java and programming knowledge It's not about "programming knowledge", its about forge knowlege. How was I supposed to know that in one case this would allow forge to run constructor, and in which case it should be done manually? 4 hours ago, diesieben07 said: static event handler methods are registered ^SOLUTION^, yeah, now everything works fine, thanks Edited June 19, 20205 yr by byalexeykh
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.