Jump to content

Prefixes


Andrew2070

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I have created a very simple mod that is just supposed to send a message to a player when they join. Upon adding it to a server (that has other mods on it), the following crash message appears: [12:13:01] [main/ERROR] [minecraft/Main]: Failed to start the minecraft server net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [         Epic Mod (epicmod) has failed to load correctly §7java.lang.NoSuchMethodException: net.ed.epicmod.EpicMod.<init>() ]         at net.minecraftforge.fml.ModLoader.waitForTransition(ModLoader.java:243) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at net.minecraftforge.fml.ModLoader.lambda$dispatchAndHandleError$24(ModLoader.java:208) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at java.util.Optional.ifPresent(Optional.java:178) ~[?:?] {re:mixin}         at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:208) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at net.minecraftforge.fml.ModLoader.lambda$gatherAndInitializeMods$14(ModLoader.java:185) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?] {re:mixin,re:computing_frames}         at net.minecraftforge.fml.ModLoader.gatherAndInitializeMods(ModLoader.java:185) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at net.minecraftforge.server.loading.ServerModLoader.load(ServerModLoader.java:32) ~[forge-1.19.2-43.2.14-universal.jar%23552!/:?] {re:classloading}         at net.minecraft.server.Main.main(Main.java:113) ~[server-1.19.2-20220805.130853-srg.jar%23547!/:?] {re:classloading,re:mixin,pl:mixin:A}         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}         at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}         at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}         at net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$launchService$0(CommonServerLaunchHandler.java:29) ~[fmlloader-1.19.2-43.2.14.jar%2367!/:?] {}         at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.Launcher.run(Launcher.java:106) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) [bootstraplauncher-1.1.2.jar:?] {} Why could this be? I have tried using the mod on another forge server with only this mod installed and it works there. Is my mod somehow interfering with other mods? MC version is 1.19.2
    • how to make animated doors?, maybe geckolib, but i don't know how to code it?
    • For crash 1, set max-tick-time to -1 in your server.properties Crash 2 shows a conflict or incompatibility between LuckPerms and the mod boh-0.0.6.1-forge-1.20.1_2.jar
    • Add the crash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here  
    • so my minecraft crashes when opening my world, i played without any troubles for about 5 days and today it started tweaking.. pls help me
  • Topics

×
×
  • Create New...

Important Information

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