Jump to content

[SOLVED] Synchronizing ItemStack NBT during onItemUse method


Recommended Posts

Posted

I've recently been trying to update my Structure Generation Toolhttps://github.com/coolAlias/StructureGenerationTool to be compatible with multi-player; as such, I've changed to a KeyHandler and storing the Item variables in NBT.

 

Each key press changes a variable in the item's NBT, but this is only client side. I know I can send packets, but I was hoping not to spam packets because the data is only needed when the item is used. I've been trying to send a packet from client to server onItemUse, which works, but it's too late to the party and the Item activates with the data from the previous time instead of the current data.

 

I played around with synchronized variable and wait/notify, but it results in thread lock since the server side is waiting for the information but it can't get it until the onItemUse method is called client side, which obviously doesn't happen while the thread is waiting.

 

Here's my item code with my failed attempts:

 

  Reveal hidden contents

 

And my PacketHandler with custom packet class:

 

  Reveal hidden contents

 

 

Posted

Heh, yeah I know. I was just trying to figure a way around sending so many packets, but I guess there's no help for it in this situation. I was really hoping to be able to just send one packet with all the information, but since the keyboard is handled client side, that's impossible (as far as I know) without editing NBT from there, which isn't good practice. Thanks for the reply anyway.

Posted

The way I have it set up now is to send packets to the server with the key pressed information, but I also directly edit the NBT client side for display purposes only. When the NBT is actually needed for processing, it only uses the NBT stored on the server. Would this be considered acceptable, or should I not edit the NBT at all client side without requesting the data from the server?

 

I'll show you what I mean:

 

  Reveal hidden contents

 

Then in the Item onItemUse, I only get NBT data from the server:

 

  Reveal hidden contents

 

 

(And sorry about spamming the tutorials - I thought they were relevant to the topics, though some were a few days old they didn't look resolved yet, except for one... my bad.)

Posted
  On 9/15/2013 at 10:53 AM, diesieben07 said:

"So many packets"? As in one packet per keypress? What is wrong with that?

I was only trying to think of a way to minimize traffic. Guess it's not a big deal, though.

  Quote

You should never do any calculations client-sided if they affect the game data in any way. If you have to give the player an Item, don't rely on the client to do it. Never trust the client, it never does what you want and always sends wrong data (that's what you have to assume to make your mod save against hacks).

You seem to overcomplicate things. Just send a packet when a key gets pressed, then on the server change the NBT data when the key-pressed packet arrives. That NBT will be automatically synced by minecraft.

Yes, but it won't be synced quickly enough to display the correct values immediately, which is why I did what I did above. As you can tell, I'm no expert in Java, and I couldn't think of an easy way to display the information immediately without changing the client-side NBT. As I mentioned, it's only used for the display, not any actual calculations that affect the world, player or anything else. I only send the key pressed to the server, not the client side NBT data. Is this still bad form? What would you suggest, then, to display the information?

Posted
  On 9/15/2013 at 10:53 AM, diesieben07 said:

  Quote

Heh, yeah I know. I was just trying to figure a way around sending so many packets, but I guess there's no help for it in this situation.

"So many packets"? As in one packet per keypress? What is wrong with that?

 

In regards to this, in the Minecraft I use to play (not develop) which has around 45 mods in it including heavy hitters such as Mo'Creatures, I often see something such as this:

 

"Memory connection overburdened after processing packets with 2500 still to go"

 

Isn't this an artifact of modders spamming too many packets as coolAlias was indicating?

 

 

Note: I'm not blaming Mo'Creatures, just illustrating that the mods I use don't just add a block.

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.