Jump to content

[1.7.10] [SOLVED]Packet onMessage not firing


freethought78

Recommended Posts

Hi, this is my first post.

 

Been struggling through development of my first mod. I can't seem to figure out why my packets are not sending.

 

Any help would be appreciated.

 

In the main mod class I am creating a reference to the channel for sending packets and registering my packet handlers.

 

Main Class

@Mod(modid = "EpicScavengerQuest", name = "Epic Scavenger Quest", version = "1.7.10 - 0.1")
public class EpicScavengerQuest {



public static SimpleNetworkWrapper network;
@SidedProxy(clientSide ="ft.epicscavengerquest.client.ClientProxy", serverSide = "ft.epicscavengerquest.server.ServerProxy")
public static CommonProxy proxy;


@EventHandler
public void load(FMLInitializationEvent event) {
	proxy.registerRenderInformation();
}

@EventHandler
public void preInit(FMLPreInitializationEvent event)  {
       network = NetworkRegistry.INSTANCE.newSimpleChannel("EpicScavengerQuest");
       network.registerMessage(GetQuest.Handler.class, GetQuest.class, 0, Side.SERVER);
       FMLCommonHandler.instance().bus().register(new ServerLoginHandler());
       FMLCommonHandler.instance().bus().register(new ClientLoginHandler());
}

public EpicScavengerQuest(){

}

}

 

ClientLoginHandler

 

The debug message below is sent correctly but the code from the packet does not execute

 

public class ClientLoginHandler {


public ClientLoginHandler() {

}

@SubscribeEvent
    public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
	System.out.println("This message is being displayed, but the code in the packet below is never executed");
	EpicScavengerQuest.network.sendToServer(new GetQuest(1));
    }
}

 

This is the packet class, no errors are reported, no debug messages are sent, other code is not executed.

 

GetQuest

public class GetQuest implements IMessage {

private int value;

public GetQuest(){	}

    public GetQuest(int quest) {
    	this.value = quest;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
    	this.value = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
    	buf.writeInt(this.value);
    }
    	
    public static class Handler implements IMessageHandler<GetQuest, IMessage> {
        @Override
        public IMessage onMessage(GetQuest message, MessageContext ctx) {
            SendQuest completed = null;
            ctx.getServerHandler().playerEntity.addChatMessage(new ChatComponentText("testing GetQuest"));
            System.out.println("testin");
                        /*
		try {
			completed = new SendQuest(ctx.getServerHandler().playerEntity.getDisplayName(), message.value);
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return completed;
		*/
            return null;
        }
    }
}

 

I've Commented out additional code that is unnecessary to demonstrate the point, but left it in to illustrate what the purpose of the code will be.

onmessage is never called and I'm trying to figure out why. There are no error messages being sent and everything looks correct to me.

 

thanks in advance.

Link to comment
Share on other sites

I took a glympse and, well - try this:

 

Packet:

public class GetQuest implements IMessage, IMessageHandler<GetQuest, IMessage>
{
private int value;

public GetQuest() {}

public GetQuest(int quest) {this.value = quest; System.out.println("BAM");}

@Override
public void toBytes(ByteBuf buffer) {buffer.writeInt(this.value); System.out.println("WRITE");}

@Override
public void fromBytes(ByteBuf buffer) {this.value = buffer.readInt(); System.out.println("READ");}

@Override
public IMessage onMessage(GetQuest message, MessageContext ctx)
{
	System.out.println("PACKET");
	SendQuest completed = null;
            ctx.getServerHandler().playerEntity.addChatMessage(new ChatComponentText("testing GetQuest"));
            System.out.println("testin");
                        /*
		try {
			completed = new SendQuest(ctx.getServerHandler().playerEntity.getDisplayName(), message.value);
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return completed;
		*/
            return null;
}
}

 

In Main:

//replace
       network.registerMessage(GetQuest.Handler.class, GetQuest.class, 0, Side.SERVER);
//with
       network.registerMessage(GetQuest.class, GetQuest.class, 0, Side.SERVER);

 

Also - check if you "network" is not null when you use "network.registerMessage()" (just a thought, you know "safe-side" stuff)

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

here are the changes i made

 

In the mod base class:

 

public void preInit(FMLPreInitializationEvent event)  {

      network = NetworkRegistry.INSTANCE.newSimpleChannel("EpicScavengerQuest");

 

      if (network == null){

      System.out.println("null");

      }else {

      System.out.println("Not Null");

      }

      System.out.println(network.toString());

 

      network.registerMessage(GetQuest.class, GetQuest.class, 0, Side.SERVER);

      network.registerMessage(GetQuest.class, GetQuest.class, 0, Side.CLIENT);

      FMLCommonHandler.instance().bus().register(new ServerLoginHandler());

      FMLCommonHandler.instance().bus().register(new ClientLoginHandler());

}

 

 

in the GetQuest class:

 

public class GetQuest implements IMessage, IMessageHandler<GetQuest, IMessage> {

 

private int value;

 

public GetQuest(){ }

 

    public GetQuest(int quest) {

    this.value = quest;

    }

 

    @Override

    public void fromBytes(ByteBuf buf) {

    this.value = buf.readInt();

    }

 

    @Override

    public void toBytes(ByteBuf buf) {

    buf.writeInt(this.value);

    }

   

   

        @Override

        public IMessage onMessage(GetQuest message, MessageContext ctx) {

            System.out.println("This message never displays");

            ctx.getServerHandler().playerEntity.addChatMessage(new ChatComponentText("neither does this one"));

            return null;

        }

   

}

 

 

results in console include:

 

[20:32:36] [Client thread/INFO]: [ft.epicscavengerquest.common.EpicScavengerQuest:preInit:34]: Not Null

[20:32:36] [Client thread/INFO]: [ft.epicscavengerquest.common.EpicScavengerQuest:preInit:35]: cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper@2223878

 

but none of the debug messages from GetQuest are displayed

 

the last console messages shown are:

 

[20:50:18] [Client thread/INFO]: [Client thread] Client side modded connection established

[20:50:18] [server thread/INFO]: [server thread] Server side modded connection established

[20:50:18] [server thread/INFO]: Player456[local:E:a354cb32] logged in with entity id 335 at (-227.5, 76.26202533778098, 46.5)

[20:50:18] [server thread/INFO]: Player456 joined the game

[20:50:18] [server thread/INFO]: [ft.epicscavengerquest.client.ClientLoginHandler:onPlayerLogin:17]: This message is being displayed, but the code in the packet below is never executed

Link to comment
Share on other sites

Oh right! Gosh I was just doing stuff with those last week.

 

PlayerEvent.PlayerLoggedInEvent

Is a server-side event. It's NEVER launched on Client.

Now let's take a look what packet are you sending:

EpicScavengerQuest.network.sendToServer(new GetQuest(1));

 

As you probably know:

When you join your SP world, a virtual server is created which acts similar to dedicated one. This server is handling packets, synchronization and stuff.

 

So in this situation when you login on SP world, you are making your SERVER-side event, launch a packet that sends data to SERVER (.sendToServer()).

Same goes for dedicated one.

 

Proper way of doing this stuff is:

If you want to send from server to players: .sendToAll() (or just to particulat player ofc).

And in other direction .sendToServer().

 

Hope that will fix your problem.

 

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Ernio, you were absolutely right.

 

I thought that I was detecting the player login on the client side, and that the area that the packet was being called from was on the client side only, as it seemed I was only referencing it from the client proxy, but I was dead wrong. The problem was corrected and for those interested this was my solution:

 

I created a new packet type that is sent from the server to the client that just logged in when the server detected a client logging in.

 

When that packet was received on the client, it fired back a GetQuest packet to the server, which then ran the code.

 

Thank you all for your assistance and sorry for the confusion, +1 thanks to ernio!!

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.



×
×
  • Create New...

Important Information

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