Jump to content

Editing a player's extended property from within a GUI button


Recommended Posts

Posted

Hello All! So I have a working gui and I'm trying to edit a player's extended entity property I have created when they press a button. Unfortunately the method doesn't take the player as a parameter. I tried using " this.mc.thePlayer" to get the player variable, but that doesn't seem to work. I thought that maybe the issue was that the server didn't recognize the change, because I know that GUIs are client side only. I have a working packet handler and I tried using that to send a packet to the server with the change, but it still doesn't work! I've been struggling with it for awhile now and decided to come here for some advice. Does anyone know a good way to access the player who opened the GUI? Or am I doing something wrong with my packet handling? I'm basically trying to change the variable "activeSpellID" in my magicPlayer class depending on what button is pressed (which right now there is only one). activeSpellID initializes to zero, and should change to 1 when I press this button, but  it refuses to! Anyway, thanks in advance for the help! You guys are always really helpful whenever I come here!

 

Here is my gui button method. There is a bit of an attempt to send a packet in there but I'm not very confident in it, as I havn't sent packets to the server before.

 

public void onActionPerformed(GuiButton button)

{

ByteArrayOutputStream bos = new ByteArrayOutputStream(8);

DataOutputStream outputStream = new DataOutputStream(bos);

 

magicPlayer props= magicPlayer.get(this.mc.thePlayer);

 

this.mc.thePlayer.addChatMessage("This is the correct player"); //checking to see if this.mc.thePlayer is working, which it didn't

 

 

switch(button.id)

{

case 0:

{

props.activeSpellID=1;

}

}

 

Packet250CustomPayload packet = new Packet250CustomPayload("manachannel", bos.toByteArray());

PacketDispatcher.sendPacketToServer(packet);

}

 

 

Here is my packet handler in case you guys need it:

 

public class manaPacketHandler implements IPacketHandler

{

 

public manaPacketHandler() {}

 

@Override

public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)

{

// This is a good place to parse through channels if you have multiple channels

if (packet.channel.equals("manachannel")){

handleExtendedProperties(packet, player);   

 

}

}

 

private void handleExtendedProperties(Packet250CustomPayload packet, Player player)

{

DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));

magicPlayer props = magicPlayer.get((EntityPlayer) player);

 

// Everything we read here should match EXACTLY the order in which we wrote it

// to the output stream in our ExtendedPlayer sync() method.

try {

props.setMaxMana(inputStream.readInt());

props.setCurrentMana(inputStream.readInt());

props.setActiveSpellID(inputStream.readInt());

} catch (IOException e) {

e.printStackTrace();

return;

}

// Just so you can see in the console that it's working:

System.out.println("[PACKET] Mana from packet: " + props.getCurrentMana() + "/" + props.getMaxMana());

System.out.println("[PACKET] Current Active Spell ID: "+props.activeSpellID);

}

}

 

 

All I really need is access to the player variable, so if anyone has a good way to do that I would greatly appreciate the help!

 

Posted

Whoops! The method I meant to use was just "actionPerformed" not "onActionPerformed"! It now adds the chat message but it's not updating the variable, any ideas?

Posted

Yeah, your packet / handling is not set up correctly to handle what you are trying to do, and you don't actually send any data with your packet...

props.activeSpellID=1;

That line is setting the value on the client side, when what you want to be doing is writing that data to the packet so it can be sent to the server. You will need to handle this packet separately from your other packet for extended properties, since the data it contains will be different.

Posted

Ok, thank you! That's kind of what I thought was happening but it's good to get confirmation! And thanks again for those tutorials you put out, they've really helped me a lot! I'll get working on this tonight, thanks for the fast reply! :D

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.

×
×
  • Create New...

Important Information

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