ChatColor Help!

Discussion in 'Plugin Development' started by HungerCraftNL, Oct 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    HungerCraftNL

    The strings will no be replaced but I don't know why, can someone maybe take a look?

    Main:
    Code:java
    1. package me.Shadow48402.broadcastplus;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.Plugin;
    11. import org.bukkit.plugin.PluginDescriptionFile;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class BroadCastPlus extends JavaPlugin{
    15. public Logger logger;
    16. public static Plugin plugin;
    17.  
    18. public void onEnable(){
    19. if (!getDataFolder().exists()) {
    20. getDataFolder().mkdir();
    21. }
    22. getConfig().options().copyDefaults(true);
    23. saveConfig();
    24.  
    25. PluginDescriptionFile pdfFile = getDescription();
    26. logger.info(pdfFile.getName() + "is enabled.");
    27. }
    28. public void onDisable(){
    29. PluginDescriptionFile pdfFile = getDescription();
    30. logger.info(pdfFile.getName() + " is disabled.");
    31. }
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args){
    33. Player p = (Player) sender;
    34. if(commandlabel.equalsIgnoreCase("bc")){
    35. if(args.length == 0){
    36. p.sendMessage(ChatColor.GRAY + "[BroadCastPlus]" + ChatColor.RED + " You have to add a message!");
    37. } else {
    38. if(p.hasPermission("broadcastplus.bc")){
    39. StringBuilder str = new StringBuilder();
    40. for (int i = 0; i < args.length; i++) {
    41. str.append(args[i] + " ");
    42. }
    43. String bcPrefix = getConfig().getString("Prefix");
    44. bcPrefix = bcPrefix.replaceAll("&", "§");
    45.  
    46. Bukkit.broadcastMessage(bcPrefix + " " + ChatColor.RESET + str);
    47. } else {
    48. String bcPrefix = getConfig().getString("Prefix");
    49. bcPrefix = bcPrefix.replaceAll("&", "§");
    50. p.sendMessage(ChatColor.GRAY + "[BroadCastPlus]" + ChatColor.RED + " You don't have the permissions to do this!");
    51. }
    52. } if(args[0].equalsIgnoreCase("reload")){
    53. reloadConfig();
    54. p.sendMessage(ChatColor.GRAY + "[BroadCastPlus]" + ChatColor.GREEN + " The plugin is reloaded!");
    55. }
    56. }
    57. return false;
    58. }
    59. }[/i]


    Error:
    Code:
    21.10 21:11:04 [Server] INFO ... 15 more
    21.10 21:11:04 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    21.10 21:11:04 [Server] INFO at me.Shadow48402.broadcastplus.BroadCastPlus.onCommand(BroadCastPlus.java:44)
    21.10 21:11:04 [Server] INFO Caused by: java.lang.NullPointerException
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:594)
    21.10 21:11:04 [Server] INFO at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:132)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    21.10 21:11:04 [Server] INFO at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:887)
    21.10 21:11:04 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:976)
    21.10 21:11:04 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:527)
    21.10 21:11:04 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    21.10 21:11:04 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    21.10 21:11:04 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'bc' in plugin BroadCastPlus v0.1
    
     
  2. Offline

    MelkyPie

    HungerCraftNL You could just use this method to enable colors in config
    Code:java
    1. String bcPrefix = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefix"));
     
  3. Offline

    Chinwe

    If 'Prefix' doesn't exist in your config, it bcPrefix will be null, so using .replaceAll() on null will throw that NPE :)
    (instead of replacing all & with §, you should use this (after checking bcPrefix isn't null that is): )
    Code:
    bcPrefix = ChatColor.translateAlternateColorCodes('&', bcPrefix);
     
  4. Offline

    HungerCraftNL

    MelkyPie Chinwe
    But it don't works for the StringBuilder right?

    And even with the new codes ( translateAlternateColorCodes(blabla)) do I've got a error:
    Code:
    21.10 21:37:18 [Server] INFO ... 15 more
    21.10 21:37:18 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    21.10 21:37:18 [Server] INFO at me.Shadow48402.broadcastplus.BroadCastPlus.onCommand(BroadCastPlus.java:43)
    21.10 21:37:18 [Server] INFO Caused by: java.lang.NullPointerException
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:594)
    21.10 21:37:18 [Server] INFO at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:132)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    21.10 21:37:18 [Server] INFO at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:887)
    21.10 21:37:18 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:976)
    21.10 21:37:18 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:527)
    21.10 21:37:18 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    21.10 21:37:18 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    21.10 21:37:18 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'bc' in plugin BroadCastPlus v0.1
    
     
  5. Offline

    MelkyPie

    HungerCraftNL Do you have something like this in config ?
    Code:
    Prefix: &3[RandomPrefix]
    because I can't see any other way why it would be throwing a null pointer exception
     
  6. Offline

    HungerCraftNL

    Code:
    21.10 21:47:16 [Server] INFO ... 15 more
    21.10 21:47:16 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    21.10 21:47:16 [Server] INFO at me.Shadow48402.broadcastplus.BroadCastPlus.onCommand(BroadCastPlus.java:43)
    21.10 21:47:16 [Server] INFO Caused by: java.lang.NullPointerException
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:594)
    21.10 21:47:16 [Server] INFO at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:132)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    21.10 21:47:16 [Server] INFO at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:887)
    21.10 21:47:16 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:976)
    21.10 21:47:16 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:527)
    21.10 21:47:16 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    21.10 21:47:16 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    21.10 21:47:16 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'bc' in plugin BroadCastPlus v0.1
    21.10 21:47:16 [Server] SEVERE null
    21.10 21:47:14 [Server] INFO ... 15 more
    21.10 21:47:14 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    21.10 21:47:14 [Server] INFO at me.Shadow48402.broadcastplus.BroadCastPlus.onCommand(BroadCastPlus.java:51)
    21.10 21:47:14 [Server] INFO Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:594)
    21.10 21:47:14 [Server] INFO at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:132)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    21.10 21:47:14 [Server] INFO at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:887)
    21.10 21:47:14 [Server] INFO at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:976)
    21.10 21:47:14 [Server] INFO at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:527)
    21.10 21:47:14 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    21.10 21:47:14 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    21.10 21:47:14 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'bc' in plugin BroadCastPlus v0.1
    
    43:
    Code:
    String bcPrefix = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefix"));
    51:
    Code:
    } if(args[0].equalsIgnoreCase("reload")){
     
  7. Offline

    Chinwe

    Look at the JavaDocs for getString():
    That's why you're getting the NullPointerException at line 43

    And the ArrayIndexOutOfBoundsException is because you're accessing args[0] without checking that args is at least 1 long :)
     
Thread Status:
Not open for further replies.

Share This Page