Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

}

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.