Jump to content

Recommended Posts

Posted (edited)

Hello, I'm trying to set up some basic networking functionality for my mod. I've looked around at various online tutorials, including the forge docs, and I've gotten it set up without errors. However, for some reason, the server does not appear to be receiving the messages.

 

I am running this code on the client (NOTE: getModList() returns a String):

Main.FACINSTANCE.sendToServer( new MyMessage( this.getModList() ) );
System.out.println( "SENT LIST TO SERVER!!!" );

Here is my "MyMessage" class and its embedded handler:

import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class MyMessage implements IMessage {

	  public MyMessage(){}

	  private String text;
	  public MyMessage(String text) {
	    this.text = text;
	  }

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

	  @Override public void fromBytes(ByteBuf buf) {
		  text = ByteBufUtils.readUTF8String(buf);
	  }
	  
	  public static class MyMessageHandler implements IMessageHandler<MyMessage, IMessage> {
		  
		  @Override public IMessage onMessage( MyMessage message, MessageContext ctx ) {
			  System.out.println(String.format("Received %s from %s", message.text, ctx.getServerHandler().playerEntity.getDisplayName()));
	          return null;
		  }
	  }
}

However, on the server end, Handler's onMessage method does not appear to be getting called. Nothing is printed to its console. From my understanding, this method should be called automatically by Forge. If this is not the case, then I need to know how and when to properly call it myself.

 

Here are some smaller snippets of code from other areas just in case they're relevant:

 

Main Mod Class:

public static SimpleNetworkWrapper FACINSTANCE;

CommonProxy preInit():

Main.FACINSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel( "forgeanticheat" );
Main.FACINSTANCE.registerMessage(MyMessageHandler.class, MyMessage.class, 0, Side.SERVER );

 

Please let me know if you need any more information. Thanks in advance! :)

Edited by jdawg3636
Changed title to [SOLVED]

Website: jdawg3636.com
Discord: discord.gg/EKeM7Jz

Posted (edited)
  On 8/15/2017 at 1:42 PM, diesieben07 said:

Why is this in your "CommonProxy"? The idea of a "common proxy" does not make sense, proxies are inherently "sideful" and not "common", they contain side-specific code. Common code goes in your main mod class or other organization classes.

Expand  

Yes, sorry, I should have explained this. "CommonProxy" is code run on both sides. It is essentially just a way to clean up the main mod class. I have tested and can confirm that the code there gets run at the proper time.

 

  On 8/15/2017 at 1:42 PM, diesieben07 said:

Where are you calling sendToServer? Show more code.

Expand  

I am calling it from the ClientProxy on server join. The System.out.println statement is being called at the correct time, so I assume that sendToServer is being called as well.

  On 8/15/2017 at 1:42 PM, diesieben07 said:

As a heads up: This idea of an "anti-cheat" will not work. The client will just lie to the server about which mods are installed.

Expand  

Yes, I am aware that anyone with even a scrap of knowledge of Java could easily break this system. I am designing it purely as a deterrent for obnoxious players on my modded servers. If it stops even one person, then It'll be worth it.

Edited by jdawg3636
Autocorrect being dumb

Website: jdawg3636.com
Discord: discord.gg/EKeM7Jz

Posted
  On 8/15/2017 at 7:49 PM, diesieben07 said:

What is "on server join", exactly?

Expand  

Server Join is FMLNetworkEvent.ClientConnectedToServerEvent

 

  On 8/15/2017 at 1:42 PM, diesieben07 said:

Show more code.

Expand  

Main Mod Class:

  Reveal hidden contents

EventHandler:

  Reveal hidden contents

MyMessage:

  Reveal hidden contents

CommonProxy:

  Reveal hidden contents

ClientProxy:

  Reveal hidden contents

ServerProxy:

  Reveal hidden contents

 

Website: jdawg3636.com
Discord: discord.gg/EKeM7Jz

Posted
  On 8/22/2017 at 1:31 PM, diesieben07 said:

What are you trying to achieve?

Expand  

Make a basic anticheat mod for server modpacks

  On 8/15/2017 at 1:42 PM, diesieben07 said:

This idea of an "anti-cheat" will not work. The client will just lie to the server about which mods are installed.

Expand  
  On 8/15/2017 at 7:24 PM, jdawg3636 said:

Yes, I am aware that anyone with even a scrap of knowledge of Java could easily break this system. I am designing it purely as a deterrent for obnoxious players on my modded servers. If it stops even one person, then It'll be worth it.

Expand  

Also, I have found that the majority of cheaters don't know how Java works. Usually just some obnoxious kid with an Xray mod.

  On 8/22/2017 at 1:31 PM, diesieben07 said:

Forge already does this.

Expand  

How and when would I go about accessing this list? Also I might send other information in the future so I would still like to work this out.

  On 8/21/2017 at 11:29 PM, jdawg3636 said:

What would be the best method of waiting a tick?

Expand  

This is probably a really dumb question but I'm new to this.

Website: jdawg3636.com
Discord: discord.gg/EKeM7Jz

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.