Jump to content

[solved] Grab and sync TileEntity via SimpleNetworkWrapper


Recommended Posts

Posted

So I'm working with a custom block/gui and have some buttons on the gui that are changing a value stored in a TileEntity. Unless I am mistaken the button actions and therefore the value changes corresponding to pressing them occur on client side only. I have, then, set up a packet to be sent to the server so this value can be updated globally and other players "see" the updated values. I am using the SimpleNetworkWrapper so I am simply sending a string. I imagine I will be sending a constructed string containing an id for the TileEntity and the updated value, and then grabbing that TileEntity by that id on the server/packet receive? Should I be using hashCode? And what list/map am I looking it up in? Or should I be approaching this some other way... thanks for any pointers! (I mean tips, not programmatic 'pointers' lol - bad programming joke)

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Yeah, it sure does. I'd be happy to share code, but I have no errors so to speak..

 

Its a block, TileEntity, Container, and gui. The setup displays the player inventory, the 12-slot 'inventory' of the container, the 'value' in question (currently held by the TileEntity) and buttons to change the value. I have a blank string packet sent to server upon button click.

I'll need help, and I'll give help. Just ask, you know I will!

Posted

..so at the end of the day I know the button clicks happen privately on the client, but I need them to adjust the value on the server so any other player would see the updated value when opening the gui. Maybe I misunderstand what automatically gets synced to the server, and maybe I am storing the value in the wrong spot (I have options. Container, gui, TileEntity...) but I can't find a solution and its a hard topic to google!

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Thanks for the quick responses! I should clarify that when I say 'button' I mean a clickable gui control. And then my follow-up question is such: my packet handling method currently has no objects to access aside from the packet class and the message text. That is why I imagined I would be sending an ID of sorts in the string, and then looking that ID up in a static server list. Does that make sense?

 

Something like...

       protected void actionPerformed(GuiButton button) {

	        	++chest.Cost;
            IShop.network.sendToServer(new ChangeCost(chest.hashCode() + ":" + chest.Cost));
            break;

 

public static class Handler implements IMessageHandler<ChangeCost, IMessage> {
        
        @Override
        public IMessage onMessage(ChangeCost message, MessageContext ctx) {
        	
        	String[] incoming = message.text.split(":");
            
            if(incoming != null && incoming.length == 2){
            	int hash = Integer.parseInt(incoming[0]);
            	int cost = Integer.parseInt(incoming[1]);

       // lookup chest # here and adjust value of 'cost'

I'll need help, and I'll give help. Just ask, you know I will!

Posted

this is extra nice because I can shorten the message to one character - no numbers - and do all the logic on the server side! whenever I find my code getting long and myself inventing solutions there is usually a simpler already available approach.and it usually gets pointed out to me by you. Haha at this point you have virtually written half of my mods and I'm sure I'm not the only one on this forum with that experience. :D

I'll need help, and I'll give help. Just ask, you know I will!

Posted

right that is basically my point. I guess I could make 2 separate packet classes as there are two different buttons. I was using one packet for both buttons; the character would be either a plus or a minus - or whatever. An increment button and a decrement button. i 100% see your point although in this scenario cheating isn't really possible but I will still take that advice!

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Okay.. Setting up the custom packets and sending safe information to the server upon button click, I'm good. But that's as far as I can get! Ugh.

 

I have a few issues. Ultimately I'm not sure how to send that update back to the client (and others who may open this container.) I've played with many methods and approaches, such as simply reversing the process; but there seems to be no direct access to the player through the client side of the handler to grab the open container from. Also, upon closing the container, the value seems to reset on the server side - I am logging the server-side value every time the server receives the packet. I'm writing the value to NBT in the generic writeToNBT method, which doesn't seem to affect it.

 

Again, I'm sure there's some simple mechanism already in place for updating custom values in a TileEntity and then propogating that update back to nearby clients; maybe through a custom packet and sendToAllAround()? Any tutorials I've found near this topic are deprecated which is frustrating!

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Update: of course, the moment I posted I solved the issue of getting the info back to the client... at least I think.

 

 

player.worldObj.markBlockForUpdate(container.tileEntity.xCoord, container.tileEntity.yCoord, container.tileEntity.zCoord);
        	
        	return new SendCost(container.tileEntity.Cost); 

 

 

public static class Handler implements IMessageHandler<SendCost, IMessage> {
        
        @Override
        public IMessage onMessage(SendCost message, MessageContext ctx) {
        	
        	
        	EntityPlayer player = Minecraft.getMinecraft().thePlayer;
        	ContainerShop container = (ContainerShop)player.openContainer;     
        	
        	container.tileEntity.Cost = message.value;

 

But the value is still resetting upon close.

I'll need help, and I'll give help. Just ask, you know I will!

Posted

TileEntity

 

  Reveal hidden contents

 

 

there are other variables in this class such as the contents that save just fine when closing the chest, but I cant find anywhere they are referenced that the 'Cost' is not... could it be associated to it being updated within the network handler? Like its another instance of the TileEntity or something?

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Thats actually exactly what I have done. The server side thread prints out the value immediately after incrementing in the server packet handler, and I can watch it climb with each bitton click. I can also watch the client reflect the change visually on the gui, which is only adjusted in the SendCost handler as shown above, which is the return value of successfully handling the server packet. Then I close the chest, reopen, and its at 0. My current guesses are that Coin gets reinitialized somehow or I am changing a temporary instance/clone of the TileEntity? I tried MarkDirty, markforUpdate. .. im lost for words!

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Well when I get at my computer I will post my container, gui, and packets; TileEntity is above. Surely the issue is there or missing from there. Maybe I have a lost line of code that reinitialized Coin somewhere I am overlooking or something. Are there any classes that could be related that I am not thinking of?

I'll need help, and I'll give help. Just ask, you know I will!

Posted

Sure enough, I had a lost tileEntity,Cost = 0; initialization in my Container of all places from an earlier approach! Search and replace caused the issue, and search fixed the issue! Thanks again for your continued help!

I'll need help, and I'll give help. Just ask, you know I will!

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.