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

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

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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