Andrew2070 Posted May 28, 2017 Posted May 28, 2017 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.
Choonster Posted May 28, 2017 Posted May 28, 2017 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.
Andrew2070 Posted May 28, 2017 Author Posted May 28, 2017 Could you go into more detail, I haven't done much with Chat other than commands.
Kokkie Posted May 28, 2017 Posted May 28, 2017 It doesn't have anything to do with the chat. Use the EntityJoinWorldEvent and get the EntityPlayer from it and use EntityPlayer#addPrefix. Classes: 94 Lines of code: 12173 Other files: 206 Github repo: https://github.com/KokkieBeer/DeGeweldigeMod
Choonster Posted May 28, 2017 Posted May 28, 2017 On 5/28/2017 at 9:03 AM, Andrew2070 said: Could you go into more detail, I haven't done much with Chat other than commands. Expand 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.
Andrew2070 Posted May 28, 2017 Author Posted May 28, 2017 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);
Choonster Posted May 28, 2017 Posted May 28, 2017 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.
Andrew2070 Posted May 28, 2017 Author Posted May 28, 2017 Added the subscribe event annotation, tried changing to static and non static. Now my game crashes whenever i chat.
Choonster Posted May 28, 2017 Posted May 28, 2017 (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 May 28, 2017 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.
Andrew2070 Posted May 28, 2017 Author Posted May 28, 2017 Mc Version 1.7.10: Here is the crash report: https://hastebin.com/tewevevide.pas Also i just recently started modding so I don't understand what you mean by: EntityPlayer#addPrefix or at least ServerChatEvent#setComponent?
Ugdhar Posted May 28, 2017 Posted May 28, 2017 You should update, Minecraft 1.7.10 is no longer supported here.
Choonster Posted May 28, 2017 Posted May 28, 2017 (edited) On 5/28/2017 at 1:28 PM, Andrew2070 said: EntityPlayer#addPrefix or at least ServerChatEvent#setComponent? Expand 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. On 5/28/2017 at 1:28 PM, Andrew2070 said: Mc Version 1.7.10: Expand I suspected that may have been the case. As Ugdhar said, 1.7.10 is no longer supported here. Edited May 28, 2017 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.
Recommended Posts