Posted June 2, 20178 yr 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 June 2, 20178 yr 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.