Jump to content

Recommended Posts

Posted

Hi.

 

I have been working on a new protection system mod.

It is essentially a combination of MyTown2 and Factions.

I had a question that i need specifically answered to move on.

 

#1: How do I add prefixes? ex: [Faction_Rank] [Faction] Player: Message

 

Note --> I do have classes for the "Faction" and "Faction_Rank" part.

I just wanted to know how many classes and the methods to add these to chat.

 

If someone could give me a very specific breakdown that would be nice.

(Looking around I don't see any explanation and I would rather learn than just be handed an answer)

(But in this case, i am looking for both an answer and an explanation).

 

Thanks.

Posted

You can use EntityPlayer#addPrefix to add a prefix to a player's display name. You'll probably want to do this when EntityJoinWorldEvent is fired for an EntityPlayer.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted
17 minutes ago, Andrew2070 said:

Could you go into more detail, I haven't done much with Chat other than commands.

 

EntityPlayer#addPrefix takes an ITextComponent, which will be added to the player's display name as a prefix.

 

There are multiple implementations of ITextComponent, but the most commonly used ones are TextComponentTranslation (translates its first argument and then formats it by calling String.format with its other arguments) and TextComponentString (a single string).

 

ITextComponent#getStyle returns the ITextComponent's Style, which can be used to modify the style of the text.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

 

So I don't understand why this won't work:

 

package com.andrew2070.Empires.API.Chat;

import java.util.List;

import com.andrew2070.Empires.entities.Empire.Empire;
import com.andrew2070.Empires.API.commands.CommandsEMP;
import com.andrew2070.Empires.Datasource.EmpiresUniverse;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraftforge.event.ServerChatEvent;

//check player's selected Empire:

public class Prefix {

	public static final Prefix instance = new Prefix();
	
	public static void onServerChatReceivedEvent(ServerChatEvent event) {

		if (event.player != null) {
		
			EntityPlayer player = (EntityPlayer) event.player;

			if (player != null) {

				event.setCanceled(true);

				List players = MinecraftServer.getServer().getConfigurationManager().playerEntityList;

				for (int i = 0; i < + players.size(); i++) {
				
					EntityPlayer target = (EntityPlayer) players.get(i);

					if(target.getDisplayName().contains("Player")) {
					
					String name = player.getDisplayName();
					Empire empire = CommandsEMP.getEmpireFromName(name);
					String chat = "[" + empire + "]" + player.getDisplayName() + event.message;
					target.addChatMessage(new ChatComponentTranslation(chat));
					}
				}
			}
		}
	}


	}
	


	

 

 

What it does is finds players, makes a list of them and then finds each of their "Empire" names.

Then when they chat, it would print [Empire] Player: Message.

 

Isn't working.

 

My main class has this: 

        FMLCommonHandler.instance().bus().register(Prefix.instance);
        MinecraftForge.EVENT_BUS.register(Prefix.instance);

 

Posted

You should read Forge's documentation page on events, it explains that event handler methods must be annotated with @SubscribeEvent and when to use static or non-static event handler methods.

 

The FML event bus has been merged with the Forge event bus, so FMLCommonHandler#bus returns the same EventBus that's stored in the MinecraftForge.EVENT_BUS field. It's also deprecated, with a comment containing this information.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

"It crashes" doesn't really tell us anything. Post the latest code and the crash report.

 

Is there a reason you're intercepting and manually re-sending the chat messages? Why not use EntityPlayer#addPrefix or at least ServerChatEvent#setComponent?

 

What version of Minecraft are you using?

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)
18 minutes ago, Andrew2070 said:

EntityPlayer#addPrefix or at least ServerChatEvent#setComponent?

 

EntityPlayer#addPrefix is the preferred way to add prefixes to a player's name.

 

If you can't do that, use ServerChatEvent#setComponent to replace the chat message instead of cancelling the event and re-sending the message yourself.

 

 

18 minutes ago, Andrew2070 said:

Mc Version 1.7.10:

 

I suspected that may have been the case. As Ugdhar said, 1.7.10 is no longer supported here.

Edited by Choonster

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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