Jump to content

Packet being received as if in a loop


Briggybros

Recommended Posts

I have just started working with Minecraft forge again and I'm trying to set up my mod, however, when sending a packet it gets received repeatedly. I used the CatDany example for reference.

 

This method gets called on a key press:

public void handleKey(EntityPlayer player, int keyId) {
	if(player instanceof EntityClientPlayerMP) {
		System.out.println("Client: Sending key packet");
		IMessage msg = new KeyPacket.KeyMessage(player.getEntityId(), keyId);
		PacketHandler.net.sendToServer(msg);
	}
}

Which is handled by this packet class:

package com.laszloslads.micity.common.packets;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.common.MinecraftForge;

import com.laszloslads.micity.common.MiCity;

import io.netty.buffer.ByteBuf;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;

public class KeyPacket implements
	IMessageHandler<KeyPacket.KeyMessage, IMessage> {

@Override
public IMessage onMessage(KeyPacket.KeyMessage message,
		MessageContext ctx) {
	if(ctx.side.isServer()) {
		System.out.println("Server: Key recieved - " + message.playerId + ":" + message.keyId);
	}
	return message;
}

public static class KeyMessage implements IMessage {
	private int playerId;
	private int keyId;

	public KeyMessage() {
	}

	public KeyMessage(int playerId, int keyId) {
		this.playerId = playerId;
		this.keyId = keyId;
	}

	@Override
	public void fromBytes(ByteBuf buf) {
		this.playerId = buf.readInt();
		this.keyId = buf.readInt();
	}

	@Override
	public void toBytes(ByteBuf buf) {
		buf.writeInt(playerId);
		buf.writeInt(keyId);
	}
}
}

 

The output is:

 

[14:46:47] [Client thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.KeyHandler:handleKey:16]: Client: Sending key packet
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:47] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:48] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:49] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:49] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24
[14:46:49] [server thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.packets.KeyPacket:onMessage:21]: Server: Key recieved - 872:24

at which point I need to close the game to stop it.

 

Why is it that this packet is sent only once by the client but received in duplicate by the server?

Link to comment
Share on other sites

Here:

public class KeyBindings {

KeyHandler keyHandler;

public static KeyBinding[] bindings = {
	new KeyBinding("key.overview", Keyboard.KEY_O, "key.categories.micity")
};
    
    

    public KeyBindings(KeyHandler keyHandler) {
    	
    	this.keyHandler = keyHandler;

    	for(KeyBinding binding : bindings) {
    		ClientRegistry.registerKeyBinding(binding);
    	}
        
        FMLCommonHandler.instance().bus().register(this);
    }
    
    @SubscribeEvent
    public void onKeyInput(InputEvent.KeyInputEvent event) {
            for(KeyBinding binding : bindings) {
            	if(binding.isPressed())
            		keyHandler.handleKey(Minecraft.getMinecraft().thePlayer, binding.getKeyCode());
            }
    }

}

 

But if that were called repeatedly, then the "Client: Sending key packet" would also be printed repeatedly

Link to comment
Share on other sites

Thank you, setting the return to null as the javadoc indicated fixed the issue. How does it show that the packets are registered incorrectly?

Because the "response" message goes back to the client. But the client should not even know how to handle this packet (since it is used to tell the server that the key was pressed) and crash.

What he means by that is that you should register your packets only to the side on which it will be handled, not both (unless it can be handled on both, but 99% of the time it won't be).

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.



×
×
  • Create New...

Important Information

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