Jump to content

[1.15.2] FMLCommonSetupEvent not fired


byalexeykh

Recommended Posts

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:
 image.png.5f32a340d75f35bd4373dcf136365aa5.png

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: 

  1. to subscribe to FMLCommonSetupEvent using SubcsribeEvent - same issue.
  2. to add Mod("mymodid"), Mod.EventBusSubscriber(modid = "mymodid") above class declaration - did not help.
  3. 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).
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by byalexeykh
Link to comment
Share on other sites

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 :D

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 by byalexeykh
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.