Jump to content

[1.7.10] Send custom packets only to players who have the gui open


Recommended Posts

Posted

I'm trying to send GUI-only things (like progress bar update or energy stored) with a custom packet only to player who have the gui open.

 

Obviously I can do this:

for (playersWithGuiOpen)
    PacketHandler.INSTANCE.sendTo(new myCustomMessage(), playerWhoHaveTheGuiOpen);

 

but from my tile I don't know the players, so I decided to use my container:

///////////////Inside myTile/////////////////////
protected void syncMachine() {
    container.sendMessageToCrafters(new MessageTile(this));
    markDirty();
}

/////////////Inside myContainer//////////////
public void sendMessageToCrafters(IMessage message) {
        for (Object crafter : this.crafters) {
            EntityPlayerMP player = (EntityPlayerMP) crafter;
            if (player != null) {
                PacketHandler.INSTANCE.sendTo(message, player);
            }
        }
}

 

But this code works fine with just a player (this.crafters contains just a player even there are three different players with the gui open),  if I use open to LAN feature and I join with other different players, only the last player who opened the gui is updated.

 

Basically I need to sync only the players who have the gui open, so I can't use "PacketHandler.INSTANCE.sendToAllAround" but I need to use "sendTo". The problem is that I don't know the players who need to be updated.

 

I hope I made it clear.  :)

Posted

This should probably be done from your override of

Container#detectAndSendChanges

. Look at how

ContainerFurnace

uses this to send progress updates for the burn/cook time.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

This should probably be done from your override of

Container#detectAndSendChanges

. Look at how

ContainerFurnace

uses this to send progress updates for the burn/cook time.

I fixed my post, I submitted it uncompleted

Posted

Container#crafters

is a list of

ICrafting

objects, it may contain implementations of

ICrafting

other than

EntityPlayerMP

. It's not safe to cast to

EntityPlayerMP

without checking.

 

Every player with the GUI open will have their own

Container

on the client and server, so

Container#crafters

will usually contain 0 players on the client and 1 player on the server.

 

Your

TileEntity

shouldn't interact with your

Container

directly, it certainly shouldn't be storing an instance of it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Container#crafters

is a list of

ICrafting

objects, it may contain implementations of

ICrafting

other than

EntityPlayerMP

. It's not safe to cast to

EntityPlayerMP

without checking.

 

Every player with the GUI open will have their own

Container

on the client and server, so

Container#crafters

will usually contain 0 players on the client and 1 player on the server.

 

Your

TileEntity

shouldn't interact with your

Container

directly, it certainly shouldn't be storing an instance of it.

Ok, so is better if inside Container#detectAndSendChanges I get the message from the tile (if it is changed) and send it to crafter (checking if is a player) ?

Posted

Container#crafters

is a list of

ICrafting

objects, it may contain implementations of

ICrafting

other than

EntityPlayerMP

. It's not safe to cast to

EntityPlayerMP

without checking.

 

Every player with the GUI open will have their own

Container

on the client and server, so

Container#crafters

will usually contain 0 players on the client and 1 player on the server.

 

Your

TileEntity

shouldn't interact with your

Container

directly, it certainly shouldn't be storing an instance of it.

Ok, so is better if inside Container#detectAndSendChanges I get the message from the tile (if it is changed) and send it to crafter (checking if is a player) ?

 

Yes, that should work.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.