Jump to content

Packet Transfering Between Sides 1.12.2


DoctorOne

Recommended Posts

First of all, I am new to Forge API(3-4 Hours) but I have some experiences with Java.
So here is my purpose: I want to make a mod that checks all the mods client has at the folder and sends the list to Server. Than server kicks if anyone changed the mods or added any new the mods.
My Question is: How do I send message to Server.

I followed this tutorial and checked this one. They helped quite a lot. But I can't send the data pack to Server. Client receives the pack and according to debug I have done, its also sending the packet but Server is not reading it.

 

I don't know if I am doing something wrong, is there a way to force Server to read the message?


Here is the code:

 

Initialization(in-it)

@EventHandler
	public static void init(FMLInitializationEvent event) {
		if(SIDE == Side.CLIENT) {
			network.registerMessage(MyMessageHandler.class, MyMessage.class, PACKET_ID, Side.CLIENT);
			System.out.println("LOOK ITS CLIENT!");
		}
		if(SIDE == Side.SERVER) {
			network.registerMessage(MyMessageHandler.class, MyMessage.class, PACKET_ID, Side.SERVER);
			System.out.println("LOOK ITS SERVER!");
		}
		MinecraftForge.EVENT_BUS.register(ServerSideJoin.class);

 


My Message.Class

public class MyMessage implements IMessage {
	  public MyMessage(){}

	  public CharSequence toSend;
	  public MyMessage(String toSend) {
	    this.toSend = toSend;
	    Main.PACKET_ID++;
	  }

	  @Override public void toBytes(ByteBuf buf) {
			System.out.println("TO BYTESS!!!!!!!!!");
		    buf.writeCharSequence(toSend, Charset.defaultCharset());
		    registerMessage();
	  }

	  @Override public void fromBytes(ByteBuf buf) {
		  System.out.println("FROM BYTESS!!!!!!!!!");
		  toSend = buf.readCharSequence(buf.readableBytes(), Charset.defaultCharset());
		  registerMessage();
	  }
	  
	  public void registerMessage() {
		  if(Main.SIDE == Side.CLIENT) {
			  	Main.PACKET_ID++;
		    	Main.network.registerMessage(MyMessageHandler.class, MyMessage.class, Main.PACKET_ID, Side.CLIENT);
				System.out.println("LOOK ITS CLIENT!");
			}
			if(Main.SIDE == Side.SERVER) {
				Main.PACKET_ID++;
				Main.network.registerMessage(MyMessageHandler.class, MyMessage.class, Main.PACKET_ID, Side.SERVER);
				System.out.println("LOOK ITS SERVER!");
			}
	  }
	}


Handler

public class MyMessageHandler implements IMessageHandler<MyMessage, IMessage> {
// Do note that the default constructor is required, but implicitly defined in this case
	public MyMessageHandler() {
		// TODO Auto-generated constructor stub
	}
	@Override 
	public IMessage onMessage(MyMessage message, MessageContext ctx) {
		System.out.println("RECEIVED!!!");
		System.out.println("THE MESSAGE : " + message.toSend);
		if(Main.SIDE == Side.CLIENT) {
			Main.network.sendToServer(new MyMessage("MESSAGE FROM CLIENT!!!!"));
		}
	 return null;
	}

}


When Player Joins, Server Sends the Packet

public class ServerSideJoin {
	@SubscribeEvent
	public static void onPlayerJoin(EntityJoinWorldEvent event) {
		if(event.getEntity() instanceof EntityPlayerMP) {
			Main.network.sendTo(new MyMessage("THE MESSAGE FROM SERVER"), (EntityPlayerMP) event.getEntity());
		}
	}
}


Thx

Edited by DoctorOne
Question corrected.
Link to comment
Share on other sites

2 hours ago, diesieben07 said:

You can't. The client cannot be trusted.

 

  • Packets must always be registered on both sides. The last parameter to registerMessage defines the direction (server to client or client to server) the packet goes. But the call to registerMessage must happen on both sides and the exact same way. 
  • You can't immediately do things in onMessage, as that is running on the network thread. Read the warnings in the packet documentation.
Quote

You can't. The client cannot be trusted.

Yeah.. I know that much, but I wanna do what I can. And if client doesn't send the message or sends inappropriate message,  I am thinking "kick the client" anyway.
So whole "if else" is unnecessary. Because it has to be registered both sides. 
And I can't use the "onMessage" becuz Minecraft just doesn't work like that. Also I can't send messages with "ClientConnectedToServerEvent" since its too early for that.
So I need to use an event like, walking and kind of that things. Hopefully those work. Thx!

Link to comment
Share on other sites

1 hour ago, DoctorOne said:

And if client doesn't send the message or sends inappropriate message,  I am thinking "kick the client" anyway.

No, this is still flawed.

Lets say for instance you have 10 mods on the server. A client then connects with an extra mod which is not allowed as stated in your original post.
The server then asks the client to send back a list of all the installed mods, the client happily does so BUT you only get a list of the 10 mods that you're allowing and the client is let into the server.
The problem here is that the extra mod that was installed to the client has the ability to modify that request in some way, shape or form and only send back what the server wants too see.

This is an example of why the client cannot be trusted.

Edit: I understand you wanna do as much as you can to try and circumvent this issue but it is still flawed

Edited by FearsomePhobia
  • Like 1
Link to comment
Share on other sites

Remember:

The Client is a lying, cheating bastard. Never trust the client for anything. There is no way to insure the integrity of the client, for any message the client sends can be faked.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

4 hours ago, Draco18s said:

Remember:

The Client is a lying, cheating bastard. Never trust the client for anything. There is no way to insure the integrity of the client, for any message the client sends can be faked.

What a bitch. Yeah, I have some experience with that.
You guys are right at that point. But I am thinking this will be the case if I publish the mod publicly. I am running small server, generally kids are playing in. They know how to install the mod but i don't think if they know how to bypass. Well, if they know how to bypass then "You can cheat, but don't get caught!" rule works. Anyway I am still gonna try and make this mod. Thx all of you!

Edited by DoctorOne
Link to comment
Share on other sites

So if anyone needs in the future, below is the way I solved my problem.

	@SubscribeEvent
	public static void mouseEvent(MouseEvent event) {
		if(Main.SIDE == Side.CLIENT && Main.IS_PLAYER_IN_SERVER) {
			Main.IS_PLAYER_IN_SERVER = false;
			Minecraft.getMinecraft().addScheduledTask(() -> {
				Main.network.sendToServer(new MyMessage("Message"));
			    });
		}
	}
	
	@SubscribeEvent
	public static void clientSideJoin(ClientConnectedToServerEvent event) {
		Main.IS_PLAYER_IN_SERVER = true;
	}
	
	@SubscribeEvent
	public static void clientSideDisconnect(ClientDisconnectionFromServerEvent event) {
		Main.IS_PLAYER_IN_SERVER = false;
	}

 

If anyone wonders where I get SIDE from, I get "SIDE" in PreInit:
 

	@EventHandler
	public static void PreInit(FMLPreInitializationEvent event) {
		SIDE = event.getSide();
			
	}

 

Link to comment
Share on other sites

Now I am getting different error, and what is with this
 

Quote

java.lang.NoSuchFieldError: player

The Code is as below

	public IMessage onMessage(MyMessage message, MessageContext ctx) {
		if(Main.SIDE == Side.SERVER) {
			if(Main.SHOULD_KICK) {
				try {
				ctx.getServerHandler().player.getServerWorld().addScheduledTask(()-> { //Thats where error refers, the player object.

 

What could be the possible problem here ? Like how the hell is this not getting player?

 

Link to comment
Share on other sites

19 minutes ago, diesieben07 said:

Post the complete log.

Here

[19:15:38] [Netty Server IO #6/ERROR] [FML]: There was a critical exception handling a packet on channel mkrex
java.lang.NoSuchFieldError: player
	at com.mahmudkocas.realextras.MyMessageHandler.onMessage(MyMessageHandler.java:32) ~[MyMessageHandler.class:?]
	at com.mahmudkocas.realextras.MyMessageHandler.onMessage(MyMessageHandler.java:1) ~[MyMessageHandler.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper.channelRead0(SimpleChannelHandlerWrapper.java:56) ~[SimpleChannelHandlerWrapper.class:?]
	at net.minecraftforge.fml.common.network.simpleimpl.SimpleChannelHandlerWrapper.channelRead0(SimpleChannelHandlerWrapper.java:36) ~[SimpleChannelHandlerWrapper.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) ~[SimpleChannelInboundHandler.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) ~[MessageToMessageDecoder.class:?]
	at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) ~[DefaultChannelPipeline$HeadContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) ~[AbstractChannelHandlerContext.class:?]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) ~[DefaultChannelPipeline.class:?]
	at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:274) ~[EmbeddedChannel.class:?]
	at net.minecraftforge.fml.common.network.internal.FMLProxyPacket.func_148833_a(FMLProxyPacket.java:99) [FMLProxyPacket.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:147) [gw.class:?]
	at net.minecraft.network.NetworkManager.channelRead0(NetworkManager.java:49) [gw.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.handleServerSideCustomPacket(NetworkDispatcher.java:452) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:264) [NetworkDispatcher.class:?]
	at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.channelRead0(NetworkDispatcher.java:72) [NetworkDispatcher.class:?]
	at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105) [SimpleChannelInboundHandler.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:?]
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) [ByteToMessageDecoder.class:?]
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) [ByteToMessageDecoder.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:?]
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) [ByteToMessageDecoder.class:?]
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) [ByteToMessageDecoder.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:?]
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) [ByteToMessageDecoder.class:?]
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) [ByteToMessageDecoder.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:?]
	at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:287) [IdleStateHandler.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) [DefaultChannelPipeline$HeadContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [AbstractChannelHandlerContext.class:?]
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) [DefaultChannelPipeline.class:?]
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) [AbstractNioByteChannel$NioByteUnsafe.class:?]
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:624) [NioEventLoop.class:?]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:559) [NioEventLoop.class:?]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:476) [NioEventLoop.class:?]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) [NioEventLoop.class:?]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [SingleThreadEventExecutor$5.class:?]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]

 

Link to comment
Share on other sites

2 minutes ago, diesieben07 said:

Looks like you are running your mod in the actual game.

How did you build the mod jar file?

Exported from Eclipse

Fuck do I need the gradle build that ? Let me test that

Edited by DoctorOne
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.