Jump to content

Recommended Posts

Posted

I am currently setting up some networking to synchronize data between Clients and Servers, and I've been running into a problem where I got an invalid discriminator byte on my channel.

I've fixed that by "shifting" the data written to the PacketBuffer in my encoding method by one byte.

To know what exactly what I'm talking about, you can take a look at this and that class, they contain the important stuff (Mod's main class and the Message class, respectively). The packet is sent off here . (An Item class' onItemRightClick method)

 

Or you can open the spoiler:

 
 
 
 
 
 
 
 
Spoiler

In the mod's main class (io.github.kehek.mobfighters.Mobfighters)


private static int MSG_ID = 0;
// CHANNEL is a SimpleChannel instance named "mobfighters:main" and a simple version cheking of "1".equals("1") and a Supplier<String> returning "1"
public void doCommonSetup(FMLCommonSetupEvent event)
{                               //Originally MSG_ID++, doesn't change the outcome in any notable ways though
    MobfightersNetworkHandler.CHANNEL.registerMessage(++MSG_ID, FightStartPacket.class, FightStartPacket::encode, FightStartPacket::decode, FightStartPacket::handle);
}

In the message class (io.github.keheck.mobfighters.util.network.FightStartPacket)


// Offset the data by one byte, to get a valid discriminator at index 0
// Was previously not present, and caused invalid discriminator problems
private static final int off = 1;
public static void encode(FightStartPacket packet, PacketBuffer buffer)
{
    for(int i = 0; i < 4; i++)
    {
        // Put the least significant bits in the buffer
        boolean putLeast = i%2 == 0;

        // Put the player in the buffer
        if(i < 2)
        {
            if(putLeast)
                buffer.setLong(i*Long.BYTES+off, packet.playerUUID.getLeastSignificantBits());
            else
                buffer.setLong(i*Long.BYTES+off, packet.playerUUID.getMostSignificantBits());
        }
        // Put the enemy in the buffer
        else
        {
            if(putLeast)
                buffer.setLong(i*Long.BYTES+off, packet.enemyUUID.getLeastSignificantBits());
            else
                buffer.setLong(i*Long.BYTES+off, packet.enemyUUID.getMostSignificantBits());
        }
    }
    buffer.setInt(Long.BYTES*4+off, packet.fightID);
    buffer.setBoolean(Long.BYTES*4 + Integer.BYTES+off, packet.enemyWild);
}

In the item's class (io.github.keheck.mobfighters.item.FightInitiatorItem)


@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn)
{
    // Disclaimer: Simplified for readability
    if(!worldIn.isRemote())
    {
        ServerPlayerEntity serverPlayer = (ServerPlayerEntity)playerIn;

        // I know, there are convenience-methods for sending packets to clients.
        MobfightersNetworkHandler.CHANNEL.sendTo
                (
                        new FightStartPacket(1, playerIn, playerIn),
                        serverPlayer.connection.getNetworkManager(),
                        NetworkDirection.PLAY_TO_CLIENT
                );
    }

    return super.onItemRightClick(worldIn, playerIn, handIn);
}

 

 

It seems like the discriminator is somehow tied to the id of the Message registered on the channel but I would like some confirmation/correction on that theory.

What is the discriminator actually used for?

Posted

So if I understood correctly, the first byte of a packet payload is always(?) the id of the message in the specific channel. It is to correctly identify the message that the payload needs to be decoded into, since there is no (more efficient) way to store the message type itself into the payload, correct?

Posted
3 minutes ago, diesieben07 said:

You could use newEventChannel and write your own implementation, if you need it.

When would it be better to use that instead of a SimpleChannel? Like, what are some example cases you could name?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • UPDATE: this seems to be an Arch-specific issue. Switching to Ubuntu server fixed EVERYTHING.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
  • Topics

×
×
  • Create New...

Important Information

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