Jump to content

[1.19] Invalid type when registering packet


WhatIsABlock

Recommended Posts

Hello,

I am attempting to register my packet:

public class SyncStatusToClientPacket{
    // Server -> Client message packet to sync the players' status of the tutorial

    public int tAccessed;
    public int tCrafted;
    public boolean hAccessed;
    public boolean hCrafted;

    public SyncStatusToClientPacket(int tAccessed, int tCrafted, boolean hAccessed, boolean hCrafted){
        this.tAccessed = tAccessed;
        this.tCrafted = tCrafted;
        this.hAccessed = hAccessed;
        this.hCrafted = hCrafted;
    }

    public SyncStatusToClientPacket(FriendlyByteBuf buffer){
        decoder(buffer);
    }
  
    public static void encoder(SyncStatusToClientPacket msg, FriendlyByteBuf buffer){
        buffer.writeInt(msg.tAccessed);
        buffer.writeInt(msg.tCrafted);
        buffer.writeBoolean(msg.hAccessed);
        buffer.writeBoolean(msg.hCrafted);
    }
    
    public static SyncStatusToClientPacket decoder(FriendlyByteBuf buffer){
        return new SyncStatusToClientPacket(buffer);
    }
  
  	// Sending status to client
    public static void handle(SyncStatusToClientPacket msg, Supplier<NetworkEvent.Context> supplier){
        NetworkEvent.Context ctx = supplier.get();
        ctx.enqueueWork(() -> {
            DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ModClientPacketHandler.handleSyncStatusPacket(msg, supplier));
        });
        ctx.setPacketHandled(true);
    }
} 

I am registering it here:

public class Messages {
    private static final String PROTOCOL_VERSION = "1";

    public static SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
        new ResourceLocation(MCAltTutorial.MODID, "modnetwork"), 
        () -> PROTOCOL_VERSION, 
        PROTOCOL_VERSION::equals, 
        PROTOCOL_VERSION::equals);

    private static int packetId = 0;
    private static int id() { return packetId++; }

    public static void register(){

        // Sync from server to client packet message
        INSTANCE.registerMessage(id(), 
            SyncStatusToClientPacket.class, 
            SyncStatusToClientPacket::encoder,
            SyncStatusToClientPacket::decoder,
            SyncStatusToClientPacket::handle);
    }
}

I am getting a compiler error on the line where the method reference for encoder is passed in to registerMessage:

The type SyncStatusToClientPacket does not define encoder(MSG, FriendlyByteBuf) that is applicable here

I have also attempted to satisfy the BiConsumer parameter by passing in a lambda instead:

INSTANCE.registerMessage(id(), 
            SyncStatusToClientPacket.class, 
            (SyncStatusToClientPacket msg, FriendlyByteBuf buf) -> SyncStatusToClientPacket.encoder(msg, buf),
            ...
);

This caused a different error:

Incompatible type specified for lambda expression's parameter msg

Is the SyncStatusToClientPacket.class argument implicitly passed in, or am I missing something?

Link to comment
Share on other sites

Your code compiles fine for me.

Just a guess, but what is your import for Supplier?

It should be from java.util.function, not the one from google or netty. 

Otherwise, check your other imports with the types used by registerMessage().

 

Not related to your problem and just a style thing, but you would normally define the encoder as a class instance method, then the first parameter is implicitly passed as "this".

And methods that not getters are usually named using the verb rather than the noun, e.g.

  
    public void encode(FriendlyByteBuf buffer){
        buffer.writeInt(this.tAccessed);
        buffer.writeInt(this.tCrafted);
        buffer.writeBoolean(this.hAccessed);
        buffer.writeBoolean(this.hCrafted);
    }

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Thank you, you were correct that I had imported the wrong Supplier.

However, after fixing all instances of the wrongly imported Supplier, the "The type SyncStatusToClientPacket does not define encoder(MSG, FriendlyByteBuf)" error persists even after a clean. 

I do not import or use any of the other types used by registerMessage() (BiConsumer, Function)

Link to comment
Share on other sites

Quote

Not related to your problem and just a style thing, but you would normally define the encoder as a class instance method, then the first parameter is implicitly passed as "this".

I experimented and modified the encoder signature to be static:

public static void encoder(SyncStatusToClientPacket message, FriendlyByteBuf buffer){
    buffer.writeInt(message.tAccessed);
    buffer.writeInt(message.tCrafted);
    buffer.writeBoolean(message.hAccessed);
    buffer.writeBoolean(message.hCrafted);
}

which did solve the compiler issue.   I do want this to be an instance method however - I am still somewhat of a beginner to java, any idea why the compiler doesn't complain about this?

----------EDIT---------

I read about static method references vs instance method references.  My final code looks like what warjort posted.  Thanks for the help!

Edited by WhatIsABlock
I read more java documentation
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.



×
×
  • Create New...

Important Information

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