Jump to content

Requesting assistance with packet implementation.(SOLVED)


Cyan

Recommended Posts

Hey,

 

So I'll just go ahead and admit that I am not the best when it comes to sending things from one side to the other, but at the moment I am attempting to create a GUI with a renaming feature, that is, you type the name into the gui, and the name(which is attached to a datawatcher), will update to the new name.

 

Whiiich is where the problem naturally is.

 

So in the GUI, I have the packet being sent to the server when the key is typed:

 

public class GuiTestStatus extends GuiScreen
{


public static final ResourceLocation testlisttexture = new ResourceLocation("test", "textures/gui/guitestlist.png");
    private int guiImageWidth = 256;
    private int guiImageHeight = 199;
    public EntityTest testScreen;
    public GuiScreen prevScreen;


    GuiTextField renameField;

   public GuiTestStatus(EntityTest entity, GuiScreen screen) 
   {
 testScreen=entity;
 prevScreen=screen;
 renameField = new GuiTextField(fontRendererObj, -1, -1, 0, 0);

   }
   



    
public void initGui() 
{    
	this.buttonList.clear();
        int x = (this.width - this.guiImageWidth) / 2;
        int y = (this.height - this.guiImageHeight) / 2;
        renameField = new GuiTextField(fontRendererObj, 120, 172, 200, 20);
        renameField.setMaxStringLength(12);
        
        this.buttonList.add(new GuiButton(0, x + this.guiImageWidth - 102, y + this.guiImageHeight - 22, 100, 20, "Close"));
}


protected void actionPerformed(GuiButton guibutton)
{



if(guibutton.id==0) 
{
	mc.displayGuiScreen(null);
    }
    

	mc.displayGuiScreen(null);

}

public void drawScreen(int i, int j, float f)
{   
	Minecraft.getMinecraft().renderEngine.bindTexture(testlisttexture);
    int x=width/2-128;
	int y=height/2-128;
	//drawRect(0, 0, width, height, 0xff002A2A );
	 drawTexturedModalRect(x, y, 0, 0, 256, 500);
    super.drawScreen(i, j, f);

   
    {
    	
    easyString(new StringBuilder().append("Creature : ").append(testScreen.getNickname()).toString(),85,40);
	easyString(new StringBuilder().append("Tamer : ").append(testScreen.getOwnerName()).toString(),85,50);
	easyString(new StringBuilder().append("Current Exp : ").append(testScreen.getExp()).toString(),85,60);
		easyString(new StringBuilder().append("Level : ").append(testScreen.getLevel()).toString(),85,70);
		easyString(new StringBuilder().append("HP : ").append(testScreen.getHealth()).append("/").append(testScreen.getMaxHealth()).toString(),85,85);
		easyString(new StringBuilder().append("EG : ").append(testScreen.getEnergy()).append("/").append(testScreen.getMaxEnergy()).toString(),85,95);

	easyString(new StringBuilder().append("Strength : ").append(testScreen.getLevel()/5+testScreen.getAttack()).toString(),85,115);
	easyString(new StringBuilder().append("Vitality    : ").append(testScreen.getDefense()).toString(),85,125);
	easyString(new StringBuilder().append("Brains    : ").append(testScreen.getBrains()).toString(),85,135);
	easyString(new StringBuilder().append("Weight : ").append(testScreen.getWeight()).toString(),240,115);
	easyString(new StringBuilder().append("Age    : ").append(testScreen.getTestAge()).toString(),240,125);

	easyString(new StringBuilder().append(testScreen.getNeededExp()).append(" exp for next lvl").toString(),240,60);
	easyString(new StringBuilder().append("Type      : ").append(testScreen.getType()).toString(),240,85);
	easyString(new StringBuilder().append("Attribute : ").append(testScreen.getAttribute()).toString(),240,95);
	easyString(new StringBuilder().append("Element   : ").append(testScreen.getElement()).toString(),240,105);
    }
    
    easyString("Nickname", 185, 160);
	renameField.drawTextBox();
    			
    		}

protected void keyTyped(char c, int i)
{

    renameField.textboxKeyTyped(c, i);
    testScreen.setNickname(renameField.getText());
   testmod.ModBase.testmod.snw.sendToServer(new NicknameMessage(testScreen.getNickname()));


    
   
    super.keyTyped(c, i);
}

protected void mouseClicked(int i, int j, int k)
{
    super.mouseClicked(i, j, k);
    renameField.mouseClicked(i, j, k);
}

public void easyString(String text, int x, int y) {
	drawString(fontRendererObj, text, x, y, 0xffffff);
}

    public boolean doesGuiPauseGame()
    {
        return false;
    }
}

 

 

 

Which triggers fine, other than the fact that it does not send the updated name to the server. GRANTED I may need to do something else in NicknameMessage#OnMessage, but if that is not the case, I don't have the slightest clue where I have went wrong.

 

NicknameMessage, which is more or less the basic string message from the tutorials.

public class NicknameMessage implements IMessage {
   
    private String name;

    public NicknameMessage() { }

    public NicknameMessage(String name) {
        this.text = name;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        name = ByteBufUtils.readUTF8String(buf); 
    }

    @Override
    public void toBytes(ByteBuf buf) {
        ByteBufUtils.writeUTF8String(buf, name);
    }

    public static class Handler implements IMessageHandler<NicknameMessage, IMessage> {
       
        @Override
        public IMessage onMessage(NicknameMessage message, MessageContext ctx) {
            System.out.println(String.format("Received %s from %s", message.name, ctx.getServerHandler().playerEntity.getDisplayName()));
            return null;
        }
    }
}

 

 

Again, sorry if this is something simple, but packets is pretty much a new territory for me.

 

Thanks for any help you can provide,

Cyan

Link to comment
Share on other sites

Well, your IMessageHandler implementation does nothing, except print something to STDOUT. You need to set the data into the Entity.

 

I figured the handler probably had nothing to do with it, but I guess I just thought I would double check I had my understandings of it right :P

 

 

Well I mean, setNickname() is setting the new text into the entity already, or am I just misunderstanding what you meant?

Link to comment
Share on other sites

Yep so I totally was misunderstanding what you posted about the onMessage, but I went back to the tutorial and saw where I was being stupid(several hours later).

 

I need to call the nickname during onMessage, and I feel like a total idiot for reading over the part about it being receiving the packet  :( but anyway,

 

So I guess my question would be how to best make a call to the entity file without getting into a bunch of trouble with static variables?

 

ALSO should mention this is for 1.7.2, in case anything new was added for these type of situations in 1.7.10

 

 

Link to comment
Share on other sites

What kind of Entity is it? If it's a TileEntity, send the x/y/zCoord in the message, and you can then get the tile entity from the player's world object; if it's an Entity entity, send the entity.getEntityId() in the message, and then use the player's world object again to getEntityByID(id).

 

I was under the impression that I would need to use the setNickname() method from my entity file in the message. But if I understand correctly, you are saying that I only need to make sure that the recieved message is going to the entity?(via the entity's ID)

 

In hindsight, I feel like that's what diesieben07 was trying to tell me from the beginning in a lot less words.

 

Link to comment
Share on other sites

Hopefully what im going to say helps clarify what they mean.

 

the IMessageHandler is what the server gets (I only know how to use server side based packets like the example) so you send the entities coords (be it tile or what coolAlias said)

 

you can then use

"int x = message.x, y = message.y, z = message.z;"

"ctx.getServerHandler().playerEntity.getEntityWorld().getTileEntity(x, y, z);"

 

just as an example. so you'd probably send your new nickname() and the entities coords, have the server search for the entity and then use your setNickname(message.nickName)

If I understand the general concept of what you're doing.

 

I also hope I didn't just dish out a heap of rubbish, but I'm sure coolAlias and/or diesieben07 will clarify

Link to comment
Share on other sites

Alright, so let's see if I got any closer.

 

I added  an integer to be sent in the message, and have sent the id along with the new nickname: testmod.ModBase.testmod.snw.sendToServer(new NicknameMessage(testScreen.getNickname(), testScreen.getEntityId()));

 

And then my packet is now looking as follows:

 


	public class NicknameMessage implements IMessage {
   
    private String text;
    private int entityid;
  

    public NicknameMessage() { }

    public NicknameMessage(String text, int entityid) {
        this.text = text;
        this.entityid = entityid;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        text = ByteBufUtils.readUTF8String(buf);
       entityid = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        ByteBufUtils.writeUTF8String(buf, text);
        buf.writeInt(entityid);
    }

    public static class Handler implements IMessageHandler<NicknameMessage, IMessage> {
       
        @Override
        public IMessage onMessage(NicknameMessage message, MessageContext ctx) {
        	
      
     
        	
        	ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.entityid);
        //	setNickname(message.text);
                //      Still not entirely sure how to reference the entityfile here without a static variable
        	
        	

            return null;
        }
    }
}

 

 

This may not even be close to what was meant by sending the entityID/using the world to get the ID, but its kinda how it came together in my head.

 

STILL not sure how I am going to make a reference to the setNickname function though. I'm sure that I am just overlooking a simple way to do it.

Link to comment
Share on other sites

Can you just show us whatever your TestScreen class is? Is it an Entity? Is that where the setNickname method resides?

 

It's really very simple:

1. Send the entity ID and the name (CHECK - provided you got the correct entity ID)

2. In onMessage, get the instance of your entity from the world, which we told you how to do

3. Cast the entity to your custom class and call its setNickname method

 

Much of this is just plain old basic Java; step 2 is:

Entity entity = ctx.getServerHandler().playerEntity.worldObj.getEntityByID(message.entityid);

Link to comment
Share on other sites

I feel like such an idiot. Didn't even occur to me to cast it to entity. Such an obvious thing.

 

Yep. That solved the problem. All hail CoolAlias(and the others who have been patient enough to help me)

Thank you all

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.