Jump to content

[1.7.10] wrong data packet sended


deenkayros

Recommended Posts

Hi again,

 

I need to send via packet more data values at same time like this:

PacketDispatcher.sendToServer(new PacketClientStats(1, 2, 3.5F, itemStack));

 

And I noticed that only the first param is correctly sended instead other params have wrong values:

 

PacketClientStats class:

public class PacketClientStats implements IMessage, IMessageHandler<PacketClientStats, IMessage>
{
  int stats = 0;
  int food = 0;
  float heart = 0;
  static ItemStack drill;
  
  public PacketClientStats()
  {
  }

  public PacketClientStats(int stats, int food, float heart, ItemStack drill)
  {
    this.stats = stats;
    this.food = food;
    this.heart = heart;
    
    this.drill = drill;
  }

  public IMessage onMessage(PacketClientStats message, MessageContext ctx)
  {
    System.out.println("stats = " + message.stats);
    System.out.println("food = " + message.food);
    System.out.println("heart = " + message.heart);
    System.out.println("drill = " + message.drill.getDisplayName());
    
    return null;
  }
  
  public void fromBytes(ByteBuf buf)
  {
       this.stats = buf.getInt(0);
       this.food = buf.getInt(0);
       this.heart = buf.getFloat(0);
  }
  
  public void toBytes(ByteBuf buf)
  {
       buf.writeInt(this.stats);
       buf.writeInt(this.food);
       buf.writeFloat(this.heart);
  }
  
}

 

And the "itemStack" param works properly only if it's declared with "static" option ...

 

Link to comment
Share on other sites

Don't declare it static. It "doesn't work" because you don't send the "drill" parameter.

And your fromBytes method is broken, use read***, not get***.

Also do not implement IMessage and IMessageHandler on the same class. And you should use @Override where appropriate.

 

How can I send a "ItemStack" object if it's possible?

Link to comment
Share on other sites

Short answer: No.

Longer answer: It depends. What kind of inventory is it? Why do you think you need to send it? What is your goal, why do you send this packet in the first place?

 

Well, I realized that I don't need to send a packet to the server because the function "updateTick" of my block container (chest) affects both sides...

 

correct me if I'm wrong

 

thank you again ...

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.