Posted May 31, 201510 yr 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 .
May 31, 201510 yr 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... Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
May 31, 201510 yr Author Ok, thanks a lot, i will test it ! I have just a question: How can i catch the packet on server ?
May 31, 201510 yr Ok, thanks a lot, i will test it ! I have just a question: How can i catch the packet on server ? Aaaa... What kind of packet are you using??? There are tutorials on packets that decribe everything about how they work... Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
May 31, 201510 yr Author 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 .
May 31, 201510 yr 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 . 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!!! Check out my mods: BTAM Armor sets Avoid Exploding Creepers Tools compressor Anti Id Conflict Key bindings overhaul Colourfull blocks Invisi Zones
May 31, 201510 yr Author 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 .
August 3, 201510 yr Author 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.