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)
5 hours ago, 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.

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.

 

5 hours ago, diesieben07 said:

Where are you calling sendToServer? Show more code.

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.

5 hours ago, 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.

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
49 minutes ago, diesieben07 said:

What is "on server join", exactly?

Server Join is FMLNetworkEvent.ClientConnectedToServerEvent

 

6 hours ago, diesieben07 said:

Show more code.

Main Mod Class:

Spoiler

package com.jdawg3636.forgeanticheat.init;

import com.jdawg3636.forgeanticheat.proxy.CommonProxy;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;



@Mod( modid = Main.MODID, version = Main.VERSION )
public class Main {
	
    // Variables
	public static final String MODID = "forgeanticheat";
    public static final String VERSION = "1.0";
    static boolean debug = true;
    
    // Getter Methods
    public static boolean getDebugMode() {
    	return debug;
    }
    
    // Network Setup
    public static SimpleNetworkWrapper FACINSTANCE;
    
    // Proxy Setup
    @SidedProxy( clientSide = "com.jdawg3636.forgeanticheat.proxy.ClientProxy", serverSide = "com.jdawg3636.forgeanticheat.proxy.ServerProxy" )
    public static CommonProxy proxy;
    
    @EventHandler
    public void preInit( FMLPreInitializationEvent event ) {
    	proxy.preInit( event );
    }
    
    @EventHandler
    public void init( FMLInitializationEvent event ) {
    	proxy.init( event );
    }
    
    @EventHandler
    public void postInit( FMLPostInitializationEvent event ) {
    	proxy.postInit( event );
    }
    
}

 

EventHandler:

Spoiler

package com.jdawg3636.forgeanticheat.init;

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;

public class EventHandler {
	
	@SubscribeEvent
    public void serverJoin( FMLNetworkEvent.ClientConnectedToServerEvent event ) {
		com.jdawg3636.forgeanticheat.init.Main.proxy.serverJoin( event );
    }
	
	@SubscribeEvent
    public void clientJoin( FMLNetworkEvent.ServerConnectionFromClientEvent event ) {
		com.jdawg3636.forgeanticheat.init.Main.proxy.clientJoin( event );
    }
	
}

 

MyMessage:

Spoiler

package com.jdawg3636.forgeanticheat.network;

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;
		  }
	  }
}

 

CommonProxy:

Spoiler

package com.jdawg3636.forgeanticheat.proxy;

import com.jdawg3636.forgeanticheat.init.EventHandler;
import com.jdawg3636.forgeanticheat.init.Main;
import com.jdawg3636.forgeanticheat.network.MyMessage;
import com.jdawg3636.forgeanticheat.network.MyMessage.MyMessageHandler;

import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.relauncher.Side;



public class CommonProxy {
	public void preInit( FMLPreInitializationEvent event ) {
		System.out.println( "CommonProxy PreInit" );
		// Networking
		Main.FACINSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel( "forgeanticheat" );
		Main.FACINSTANCE.registerMessage(MyMessageHandler.class, MyMessage.class, 0, Side.SERVER );
		
    }

    public void init( FMLInitializationEvent event ) {
    	
    	// Event Handling
    	FMLCommonHandler.instance().bus().register( new EventHandler() );
    }

    public void postInit( FMLPostInitializationEvent event ) {
    	
    	// Debug
    	if ( com.jdawg3636.forgeanticheat.init.Main.getDebugMode() == true ) {
    		String modlist = Loader.instance().getModList().toString();
    		System.out.println( "Mod List: " + modlist );
    		System.out.println( "Mod List Printed to Console" );
    	}
    	
    }
    
    public void serverJoin( FMLNetworkEvent.ClientConnectedToServerEvent event ) {
    	
    }
    
    public void clientJoin( FMLNetworkEvent.ServerConnectionFromClientEvent event ) {
    	
    }
    
}

 

ClientProxy:

Spoiler

package com.jdawg3636.forgeanticheat.proxy;

import com.jdawg3636.forgeanticheat.init.Main;
import com.jdawg3636.forgeanticheat.network.MyMessage;

import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;



public class ClientProxy extends CommonProxy {
	
	String modlist;
	
	public String getModList() {
    	return modlist;
    }
	
	@Override
    public void preInit( FMLPreInitializationEvent event ) {
        super.preInit( event );
    }

    @Override
    public void init( FMLInitializationEvent event ) {
        super.init( event );
    }

    @Override
    public void postInit( FMLPostInitializationEvent event ) {
    	super.postInit( event );
    	modlist = Loader.instance().getModList().toString();
    }
    
    @Override
    public void serverJoin( FMLNetworkEvent.ClientConnectedToServerEvent event ) {
    	super.serverJoin( event );
    	Main.FACINSTANCE.sendToServer( new MyMessage( this.getModList() ) );
    	System.out.println( "SENT LIST TO SERVER!!!" );
    }
    
}

 

ServerProxy:

Spoiler

package com.jdawg3636.forgeanticheat.proxy;

import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;

public class ServerProxy extends CommonProxy {
	@Override
    public void preInit( FMLPreInitializationEvent event ) {
        super.preInit( event );
    }

    @Override
    public void init( FMLInitializationEvent event ) {
        super.init( event );
    }

    @Override
    public void postInit( FMLPostInitializationEvent event ) {
        super.postInit( event );
    }
    
    @Override
    public void clientJoin( FMLNetworkEvent.ServerConnectionFromClientEvent event ) {
    	super.clientJoin( event );
    	System.out.println( "Client Connected" );
    }
}

 

 

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

Posted
9 hours ago, diesieben07 said:

What are you trying to achieve?

Make a basic anticheat mod for server modpacks

On 8/15/2017 at 8:42 AM, diesieben07 said:

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

On 8/15/2017 at 2: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.

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

9 hours ago, diesieben07 said:

Forge already does this.

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.

23 hours ago, jdawg3636 said:

What would be the best method of waiting a tick?

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.