Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

  • Author

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

  • Author

But if handleKey were being called multiple times then there would be "[14:46:47] [Client thread/INFO] [sTDOUT]: [com.laszloslads.micity.common.KeyHandler:handleKey:16]: Client: Sending key packet" printed to console for every time it is called, which there isn't.

  • Author

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

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).

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.