Jump to content

Chat handling


Andrew2070

Recommended Posts

Hi.

 

So i was designing a chat handler that provides a prefix to a user based on their "Empire".

An "Empire" is essentially a "faction" that a player can join, create and invite others to.

Now, what I am having problems with is when a player without a "Empire" chats:

 

#1: Players with Empires get this printed: [Rank] [Empire] playername: message

#2: Players without Empires that chat, crash the game because of the null.

 

Now I know why it crashes,

The code searches a database of Empires using their username to find their empire/rank.

If they do not have one, then it just crashes the game despite my else statements.

 

Here is my revised code:

public class ChatHandler {
	public static final ChatHandler instance = new ChatHandler();
	
	@SubscribeEvent
	
	public void onServerChatReceivedEvent(ServerChatEvent event) {
		
	EntityPlayer player = (EntityPlayer) event.player;

	
	 if (event.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);
			Citizen res = EmpiresUniverse.instance.getOrMakeCitizen(target);
			Empire empire;
			Rank rank;
			
			if (CommandsEMP.getEmpireFromCitizen(res) == null)
			{
				String chat2 = player.getDisplayName() + ": " + event.message;
				target.addChatMessage(new ChatComponentTranslation(chat2));
			}

			empire = CommandsEMP.getEmpireFromCitizen(res);
			rank = CommandsEMP.getRankFromCitizen(res);
           
           //TODO Add herochat support?
           //TODO Fix Crash when empire = null
           //TODO Abbreviate ranks (Leader = [L], Officer = [O], Member = [M])
			
			 String chat = "[" + rank   +"]" + "§6[" + empire + "]" + "§f " + player.getDisplayName() + ": " + event.message;
			 target.addChatMessage(new ChatComponentTranslation(chat));
	
			}

			}

	 	}

	}
	

 

hope someone can tell me what i am doing wrong, and how i can fix it.

 

Edited by Andrew2070
Link to comment
Share on other sites

  • Do not use usernames for identification. They can and will change. Use UUIDs.
  • Your EmpiresUniverse and the CommandsEMP class are very suspicious. Please show them.
  • If you want to change a player's display name you can use PlayerEvent.NameFormat. Don't hack around with the chat events. If you simply want to add a prefix or suffix to a player's name you can use EntityPlayer::addPrefix resp. EntityPlayer::addSuffix.
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am trying to override or disable some vanilla recipes, as part of my mod. All of the methods that I have seen online, so far, do not work. So far, I have tried: Creating a JSON file in the "data/minecraft/recipes" folder, that has air as the ingredient and a barrier as the result This only works for crafting table recipes adding a smelting recipe this way works as intended Editing the ordering to AFTER in the forge dependency inside the "mods.toml" file Found an older post about using the FurnaceRecipe class, that no longer exists From: here So, what is required to disable a vanilla smelting recipe? I know some may say to not do this, but part of the progression of my mod kind of requires disabling some of the furnace recipes. Also, it seems that smelting recipes don't technically have to have the same filename as the block used, r as the result. When overriding crafting recipes, just adding the edited/removed recipe file in the minecraft data folder, under the same name as the original recipe.  Edit: the crafting recipes seem to be cached between worlds? I created a new world and the edited crafting recipes works, but it doesn't work in an already made world?
    • Using a modified version of "Fabulously Optimized" installed a few more mods and now my Hot Bar is invisible... Latest log : https://paste.ee/p/72TSs (No DeBug log txt file.)
    • I know that with this you can return the game folder   FMLPaths.GAMEDIR.get() I want the player to be created in the world where a txt file was created in the folder of the same world. How can I return the path of the world folder that the player is in?
    • [09Jun2023 15:38:56.969] [main/ERROR] [mixin/]: Mixin config dynamiclightsreforged.mixins.json does not specify "minVersion" property [09Jun2023 15:38:57.512] [main/ERROR] [mixin/]: Mixin config itshallnottick.mixins.json does not specify "minVersion" property how do i fix those 2
    • Whoops sorry uploaded wrong logs, here are the correct ones: 09:28:31.876 at java.base/java.lang.Thread.run(Thread.java:833) 09:28:31.876 Caused by: java.lang.UnsupportedOperationException: A mod tried to access the state neighbor table directly. Please report this at https://github.com/malte0811/FerriteCore/issues. As a temporary workaround you can enable "populateNeighborTable" in the FerriteCore config 09:28:31.876 at TRANSFORMER/ferritecore@4.2.2/malte0811.ferritecore.fastmap.table.CrashNeighborTable.crashOnAccess(CrashNeighborTable.java:101) 09:28:31.876 at TRANSFORMER/ferritecore@4.2.2/malte0811.ferritecore.fastmap.table.CrashNeighborTable.rowKeySet(CrashNeighborTable.java:77) 09:28:31.876 at TRANSFORMER/canary@0.2.1/com.abdelaziz.canary.common.state.FastImmutableTable.<init>(FastImmutableTable.java:36) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateHolder.handler$bbj000$postCreateWithTable(StateHolder.java:531) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateHolder.m_61133_(StateHolder.java:1052) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateDefinition.<init>(StateDefinition.java:68) 09:28:31.876 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.state.StateDefinition$Builder.m_61101_(StateDefinition.java:157) 09:28:31.877 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.Block.<init>(Block.java:174) 09:28:31.877 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.AirBlock.<init>(AirBlock.java:12) 09:28:31.877 at TRANSFORMER/minecraft@1.18.2/net.minecraft.world.level.block.Blocks.<clinit>(Blocks.java:40) 09:28:31.877 ... 18 more 09:28:32.435 Process crashed with exit code 1
  • Topics

×
×
  • Create New...

Important Information

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