Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.