Jump to content

Recommended Posts

Posted

Hi everyone !

 

I have a big problem to send a chat message to all players connected on a server.

The code works perfectly when i send a chat message to the current player, but not when i send it to all players.

 

Here my code (in a GuiScreen subclass), to send it to all players:

 

    @Override
    protected void actionPerformed(GuiButton button) throws IOException
    {
    	if(button==this.boutonValider)
    	{
        	MinecraftServer.getServer().getConfigurationManager().sendChatMsg(new ChatComponentText("The kingdom of "+editKingdomName.getText()+" was created."));
    		mc.thePlayer.closeScreen();
    	}
    }

 

So, in SP mode, i have no problem, the chat message is displayed correctly.

 

But, in multiplayer mode, when i'm connected with the first player: The message don't appear.

When i'm connected with the second player: i'm kicked out of the server with this error:

[11:24:14] [Netty Server IO #4/ERROR] [FML]: NetworkDispatcher exception
java.io.IOException: An existing connection was forcibly closed by the remote host

 

I try others methods, but i didn't succes to send this chat message to all players.

 

I read some classes in the forge source, and i find the method i used, actually.

 

ps: sorry for my english, i'm french :(.

Posted

Ok, so heres your problem:

You are trying to send chat message to all clients from client, but server does not allow it for security reasons. Sp being merged server and client, this can happen. Mp - no. To do this, you have to use packets: button pressed -> send packet to server with all required data -> recieve packet on server and parse data -> perform action (in this case, send chat message).

This model will also work on sp, without requirements of additional code...

Posted

Aaaa... What kind of packet are you using??? There are tutorials on packets that decribe everything about how they work...

 

I don't really know what kind of packet use .... I suppose packet link to the chatMessage, because i wanted to send it to the server, and on the server, send it to all clients.

 

And i will go to read some tutorials on packets, don't worry. I just wanted to know if a simple method/event will be used .....  ;)

 

Anyways, thanks you a lot  ;D.

Posted

Aaaa... What kind of packet are you using??? There are tutorials on packets that decribe everything about how they work...

 

I don't really know what kind of packet use .... I suppose packet link to the chatMessage, because i wanted to send it to the server, and on the server, send it to all clients.

 

And i will go to read some tutorials on packets, don't worry. I just wanted to know if a simple method/event will be used .....  ;)

 

Anyways, thanks you a lot  ;D.

You don't have to send packets from server back to clients, ConfigurationManager.sendChatMsg (what you used before), if used on server, will do all work for you!!!

Posted

You don't have to send packets from server back to clients, ConfigurationManager.sendChatMsg (what you used before), if used on server, will do all work for you!!!

 

Yes, it's what i understand, i just don't express it correctly ^^'.

 

I will send the code here when i will solve this problem  ;).

  • 2 months later...
Posted

Hi everyone !

 

I come back to send the solution:

 

The KingdomAgeMod.class (the main class):

 @EventHandler
    public void preinit(FMLPreInitializationEvent event)
    {
    	System.out.println("Pré-initialisation du mod Kingdom Age ..."); //A french message  

        //Here, the code i added:
    	network = NetworkRegistry.INSTANCE.newSimpleChannel("KingdomAgeChannel"); //We add a channel
    	network.registerMessage(CreateKingdomMessage.Handler.class,  CreateKingdomMessage.class,  0, Side.SERVER); //Here, we register the type of the message we send

 

 

 

In my gui class:

@Override
    protected void actionPerformed(GuiButton button) throws IOException
    {
    	if(button==this.boutonValider)
    	{
    		KingdomAgeMod.network.sendToServer(new CreateKingdomMessage(this.editKingdomName.getText())); // We send the message to the server
    		this.player.closeScreen(); //We close the GUI
    	}
    }

 

 

 

CreateKingdomMessage.class:

public class CreateKingdomMessage implements IMessage{

private String text;

public CreateKingdomMessage()
{

}

public CreateKingdomMessage(String message)
{
	this.text = message;
}

@Override
public void fromBytes(ByteBuf buf) {
	text = ByteBufUtils.readUTF8String(buf);
}

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

public static class Handler implements IMessageHandler<CreateKingdomMessage, IMessage> {
        
        @Override
        public IMessage onMessage(CreateKingdomMessage message, MessageContext ctx) {
        	EntityPlayerMP player = ctx.getServerHandler().playerEntity;
            System.out.println(String.format("Le joueur %s a demandé la création du royaume %s", player.getName(), message.text));
            MinecraftServer.getServer().getConfigurationManager().sendChatMsg(new ChatComponentText("The kingdom of "+message.text+" was created by "+player.getName()+"."));
        	return null;
        	}
}

}

 

So, thanks to everyone who helped me ! ^^

 

I will advance my mod (with a friend), and i expect to reach the beta version before September ! I will probably create a new topic to show you this mod ^^.

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.