Jump to content

Recommended Posts

Posted (edited)

Hello everyone, fairly new with packets and networking so I just tried to put this together for a test run by using the Forge documentation on SimpleImpl. It did not work when I tried sending and receiving the packet and I got this error. Any hints? Thanks.

public class Main {
 
public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("mechannel");
 
public void init(FMLInitializationEvent event)
    {
        INSTANCE.registerMessage(MyMessageHandler.class, MyMessage.class, 0, Side.CLIENT);
    }
}
 
 
public class MyMessage implements IMessage {
     
      public MyMessage() { }
 
      private int testInt;
      public MyMessage(int testInt) {
        this.setTestInt(testInt);
      }
 
      @Override public void toBytes(ByteBuf buf) {
        buf.writeInt(getTestInt());
      }
 
      @Override public void fromBytes(ByteBuf buf) {  
        setTestInt(buf.readInt());
      }
 
    public int getTestInt() {
        return testInt;
    }
 
    public void setTestInt(int testInt) {
        this.testInt = testInt;
    }
    }
 
 
public class MyMessageHandler implements IMessageHandler<MyMessage, IMessage> {
 
  @Override public IMessage onMessage(MyMessage message, MessageContext ctx) {
     
    int number = message.getTestInt();
    System.out.println(number);
 
    return null;
  }
 
}
 
 
public class Executor {
 Main.INSTANCE.sendToServer(new MyMessage(4));
}

 

Edited by olie304
Posted
1 hour ago, olie304 said:

INSTANCE.registerMessage(MyMessageHandler.class, MyMessage.class, 0, Side.CLIENT);

 

1 hour ago, olie304 said:

Main.INSTANCE.sendToServer(new MyMessage(4));

The Side parameter in registerMessage should be the side which receives the message - but you're sending the packet to the server. Where/when do you actually send it? It should also be surrounded by a world#isRemote check to make sure it's being sent only from the correct side.

Posted
Quote

at cpw.mods.fml.server.FMLServerHandler.getClientToServerNetworkManager

1.7.10 & below are not supported in this forum any longer. Please update to at least 1.8.9, but preferably to 1.11.2.
A moderator will likely lock this thread in the near future.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Posted (edited)
1 hour ago, Jay Avery said:

 

The Side parameter in registerMessage should be the side which receives the message - but you're sending the packet to the server. It should also be surrounded by a world#isRemote check to make sure it's being sent only from the correct side.

Ah thanks.

Quote

Where/when do you actually send it?

I just have an event that runs the command when the player left clicks a block at my executor class.

Main.INSTANCE.sendToServer(new MyMessage(4)); //Sends the integer 4 to the server upon left clicking a block

EDIT: Alright I fixed that stuff up and I get no errors but I don't think it is running because I should be seeing the number 4 in the console. Basically I just 

if (!MinecraftServer.getServer().isDedicatedServer()) //before sending the packet
// changed Side.CLIENT to Side.SERVER in the message register

 

Edited by olie304
  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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