Jump to content

Recommended Posts

Posted

Hi.

 

I'm working on a permissions mod, thus I need to modify chat to include a player's prefix/suffix.
I'm currently using the forge provided ServerChatEvent to accomplish this endeavor.


I set the event cancellation to true, intercepted the message, performed my modifications and created a TextComponent out of it.

However I can't find a method to send a chat message on behalf of the player. Essentially looking to sudo a message from the player to the rest of the server.

Back in 1.7.10 this was possible through the player.addChatMessage() method.


Now in 1.12.2 there is only the player.sendMessage() method, which only sends the intercepted chat message to the player and not to the rest of the server.

Here's my code:

 

package EmpireCoreLib.Events.Player;

import java.awt.TextComponent;
import java.util.List;

import EmpireCoreLib.EmpireCoreLib;
import EmpireCoreLib.Permissions.PermissionsList;
import EmpireCoreLib.Permissions.User;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;


@Mod.EventBusSubscriber(modid = EmpireCoreLib.MODID)
public class PlayerChatEvent {
	@SubscribeEvent
	public static void onPlayerChatEvent(ServerChatEvent event) {
		EntityPlayer player = (EntityPlayer) event.getPlayer();
		if (event.getPlayer() != null) {
			event.setCanceled(true);

					for (User user : PermissionsList.users) {
						
						if (user.getUUID().equals(player.getUniqueID())) {
							String suffix = "";
							String prefix = "";
							String username = player.getName();
							if (user.getPrefix().length() > 0) {
								prefix = "[" + user.getPrefix() + "] ";
							}
							if (user.getSuffix().length() > 0) {
								suffix = " [" + user.getSuffix() + "] ";
							}
							
							String message = prefix + username + suffix + ": " + event.getMessage();
							event.setComponent((new TextComponentString(message)));
							player.sendMessage(new TextComponentString(message));
										
						}
						
					}
					
					
					
				}
		}

}

 

Posted (edited)

If you take a look at the source, in particular tracing back from the chat event constructor, you'll find NetHandlerPlayServer#processChatMessage().  In there, you'll see how the message is normally sent to all players on the server: server.getPlayerList().sendMessage(...).

 

But if you just want to modify a message, do what @diesieben07said above!

Edited by desht

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.