Coloured Message throwing errors?

Discussion in 'Plugin Development' started by CD3, Oct 14, 2013.

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

    CD3

    From what I can see in my Stack traces, trying to send a line of colored text to a player in chat using the ChatColor method throws an error. If somemone could tell me what Im doing wrong, and how to fix it, that would be stellar!
    The class throwing the problem and my log is below.

    The Class throwing the error:
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.command.ConsoleCommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.Plugin;
    9.  
    10. import me.crazeddude.bukkit.punisher.MAIN.PunisherLogger;
    11.  
    12.  
    13. import me.crazeddude.bukkit.punisher.MAIN.Punisher;
    14. import me.crazeddude.bukkit.punisher.util.CheckForAction;
    15. import me.crazeddude.bukkit.punisher.util.CheckRules;
    16. import me.crazeddude.bukkit.punisher.util.Config;
    17. import me.crazeddude.bukkit.punisher.util.StrikeChecker;
    18.  
    19. @SuppressWarnings("unused")
    20. public class PunishExecutor2 implements CommandExecutor{
    21. private Punisher plugin;
    22. public PunishExecutor2(Punisher punisher) {
    23. // TODO Auto-generated constructor stub
    24. }
    25. @Override
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27. if (cmd.getLabel().equalsIgnoreCase("Punish")){
    28. sender.sendMessage("a");
    29. String hp2 = " (For examples, use /Punish ex)";
    30. if (Config.getConfig().getStringList("Dev-tools.De-Bug_Mode").contains("general")){sender.sendMessage("DEBUG: Got Past Label check.");}
    31. if (sender.hasPermission(cmd.getPermission())|!(sender instanceof Player)){
    32. if (Config.getConfig().getStringList("Dev-tools.De-Bug_Mode").contains("general")){sender.sendMessage("DEBUG: got past initial perm check.");}
    33. if (args.length == 1){
    34. if (Config.getConfig().getStringList("Dev-tools.De-Bug_Mode").contains("general")){sender.sendMessage("DEBUG: Got Past Args check for 1 arg!");}
    35. if (args[0].equalsIgnoreCase("Reload")){
    36. if (Config.getConfig().getStringList("Dev-tools.De-Bug_Mode").contains("general")){sender.sendMessage("DEBUG: got past arg 1 RELOAD check. Now in RELOAD code block.");}
    37. if(sender.hasPermission(cmd.getPermission() + ".reload")){
    38. if (Config.getConfig().getStringList("Dev-tools.De-Bug_Mode").contains("general")){sender.sendMessage("DEBUG: Passed RELOAD Permission check!");}
    39. Config.reloadConfig();
    40. }
    41. // ---------------DONE EX
    42. }else if(args[0].equalsIgnoreCase("ex")){
    43. if (Config.getConfig().getStringList("Dev-tools.De-Bug_Mode").contains("general")){sender.sendMessage("DEBUG: Now In EX Code Block.");}
    44. sender.sendMessage(ChatColor.AQUA + "==============Punisher Examples:==============");
    45. sender.sendMessage(ChatColor.AQUA + "Reasons that require a Player/Number input are listed in" + ChatColor.RED + " Red " + ChatColor.AQUA + "Instead of" + ChatColor.GREEN + " Green" + ChatColor.AQUA + ".");
    46. sender.sendMessage(ChatColor.GOLD + "Possible Reasons/Actions:");
    47. sender.sendMessage(ChatColor.GREEN + "Reload,Ex," + ChatColor.RED + "Lookup,Remove,Add,List,Warn," + CheckRules.listtoString(Config.getConfig().getStringList("Rules.rulelist"), ","));
    48. }
    49. }else if (args.length >= 2){
    50. // sender.sendMessage("DEBUG: Failed check for 1 arg, got past >=2 arg check!");
    51. if (args[0].equalsIgnoreCase("Lookup")){
    52. // sender.sendMessage("DEBUG: Now In Lookup Code Block, bast LOOKUP Arg check.");
    53. if (sender.hasPermission(cmd.getPermission() + ".lookup")){
    54. Player target = Bukkit.getServer().getPlayer(args[1]);
    55. // sender.sendMessage("DEBUG: Past Lookup Perm Check!");
    56. sender.sendMessage(ChatColor.GREEN + "Player " + ChatColor.GOLD + target.getName() + ChatColor.GREEN + " Currently Has " + ChatColor.GOLD + Integer.toString(StrikeChecker.getStrikes(target.getName())) + ChatColor.GREEN + " Strikes.");
    57. plugin.log.info("Player " + sender.getName().toString() + " looked up strikes on player " + target.getName() + ".");
    58. }else{
    59. // sender.sendMessage("DEBUG: FAILED Lookup Perm Check!");
    60. sender.sendMessage(cmd.getPermissionMessage());
    61. }
    62. }else if (args[0].equalsIgnoreCase("Remove")){
    63. // sender.sendMessage("DEBUG: Passed Remove arg check 1");
    64. if (sender.hasPermission(cmd.getPermission() + ".remove")){
    65. // sender.sendMessage("DEBUG:pASSED remove Permission Check");
    66. }else{
    67. sender.sendMessage(cmd.getPermissionMessage());
    68. }
    69. // ADD - DONE.
    70. }else if (args[0].equalsIgnoreCase("add")){
    71. // sender.sendMessage("DEBUG: In Add Block");
    72. if (sender.hasPermission(cmd.getPermission() + ".add")){
    73. // sender.sendMessage("DEBUG:passed Add Perm Check");
    74. int a = StrikeChecker.getStrikes(args[0].toString());
    75. int max = Config.getConfig().getInt("Actions.maxPoints");
    76. int b = 0;
    77. try {
    78. b = Integer.parseInt(args[1].toString());
    79. } catch (NumberFormatException e) {
    80. sender.sendMessage(ChatColor.RED + "Please Use a Positive Whole number" );
    81. }
    82. if (b >= max) {
    83. sender.sendMessage(ChatColor.RED + "Number MUST be a Whole, Positive Number.");
    84. }else if (b < 1) {
    85. sender.sendMessage("Value Must be above 0.");
    86. }else{
    87. int c = a + b;
    88. String d = CheckForAction.checkForActs(c);
    89. if (c >= max){
    90. Player target = Bukkit.getServer().getPlayer(args[0].toString());
    91. if (!(target.isBanned())) {
    92. try{
    93. target.kickPlayer(ChatColor.AQUA + "You have Been Banned For Breaching the Server's Rules." + "\n" + ChatColor.RED + "Rules Breached: (FUNCTION STILL IN DEVELOPMENT.)");
    94. target.setBanned(true);
    95. sender.sendMessage(ChatColor.GREEN + "Player " + ChatColor.GOLD + target.getName().toString() + ChatColor.GREEN + " Has Been Kicked. (I forgot the function to ban.)");
    96. }catch (Exception e){
    97. e.printStackTrace();
    98. }
    99. }
    100. else if (target.isBanned()){
    101. sender.sendMessage(ChatColor.RED + "Player " + ChatColor.GOLD + target.getName().toString() + ChatColor.RED + "Is Already Banned.");
    102. }
    103. }
    104. }
    105. }else{
    106. sender.sendMessage(cmd.getPermissionMessage() + hp2);
    107. }
    108. }else if (args[0].equalsIgnoreCase("List")){
    109. // sender.sendMessage("DEBUG: In List Block");
    110. if (sender.hasPermission(cmd.getPermission() + ".list")){
    111. // sender.sendMessage("DEBUG: Passed List Perm Check");
    112. sender.sendMessage(ChatColor.AQUA + "The List Feature is not yet implemented.");
    113. }
    114.  
    115. }else if (args[0].equalsIgnoreCase("warn")){
    116. // sender.sendMessage("DEBUG: In Warn Block");
    117. sender.sendMessage(ChatColor.AQUA + "The Warn Feature is not yet implemented.");
    118. }else{
    119. sender.sendMessage(ChatColor.RED + "Invalid Action.");
    120. }
    121. }else{
    122. // sender.sendMessage("DEBUG: Failed Arg Length Check.");
    123. sender.sendMessage(ChatColor.DARK_RED + "It's Done Like This:");
    124. sender.sendMessage(ChatColor.RED + cmd.getUsage() + hp2);
    125. }
    126. }else{
    127. sender.sendMessage(ChatColor.RED + "Permission " + cmd.getPermission() + " Missing");
    128. }
    129. }
    130. return false;
    131. }
    132. }
    133.  





    the Log:

    2013-10-14 11:26:26 [INFO] Starting minecraft server version 1.6.2
    2013-10-14 11:26:26 [INFO] Loading properties
    2013-10-14 11:26:26 [INFO] Default game type: SURVIVAL
    2013-10-14 11:26:26 [INFO] Generating keypair
    2013-10-14 11:26:27 [INFO] Starting Minecraft server on *:25565
    2013-10-14 11:26:27 [INFO] This server is running CraftBukkit version git-Bukkit-1.6.2-R1.0-b2879jnks (MC: 1.6.2) (Implementing API version 1.6.2-R1.0)
    2013-10-14 11:26:27 [INFO] [Punisher] Loading Punisher v0.0.0.1.00
    2013-10-14 11:26:27 [INFO] [Punisher] Enabling Punisher v0.0.0.1.00
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b=============================================================================================
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b=================================Punisher LOAD START=========================================
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b=============================================================================================
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b=================================LOADING COMMANDS============================================
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �bLoading The Punish Command...
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �bLoading The Howclose Command...
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �bCommands Loaded Successfully!
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b=============================================================================================
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �b======================================LOADING DATA===========================================
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �bVerifying Config...
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �bConfig Verified!
    2013-10-14 11:26:27 [INFO] �a[�6Punisher�a]�f: �bConfig.yml Found!
    2013-10-14 11:26:27 [INFO] Preparing level "world"
    2013-10-14 11:26:27 [INFO] Preparing start region for level 0 (Seed: 8575415723044091288)
    2013-10-14 11:26:28 [INFO] Preparing start region for level 1 (Seed: -4956242592001794844)
    2013-10-14 11:26:28 [INFO] Preparing start region for level 2 (Seed: -4956242592001794844)
    2013-10-14 11:26:28 [INFO] Server permissions file permissions.yml is empty, ignoring it
    2013-10-14 11:26:28 [INFO] Done (1.368s)! For help, type "help" or "?"
    2013-10-14 11:26:37 [INFO] You Must be a Player to use this command.
    2013-10-14 11:26:37 [INFO] /Howclose
    2013-10-14 11:26:41 [WARNING] Unexpected exception while parsing console command "punish"
    org.bukkit.command.CommandException: Unhandled exception executing command 'punish' in plugin Punisher v0.0.0.1.00
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServer.java:523)
    at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchServerCommand(CraftServer.java:512)
    at net.minecraft.server.v1_6_R2.DedicatedServer.ar(DedicatedServer.java:262)
    at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:227)
    at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
    at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
    at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R2.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.crazeddude.bukkit.punishSystem.commands.PunishExecutor3.onCommand(PunishExecutor3.java:30)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 8 more
    2013-10-14 11:26:47 [INFO] CONSOLE: Stopping the server..
    2013-10-14 11:26:47 [INFO] Stopping server
    2013-10-14 11:26:47 [INFO] [Punisher] Disabling Punisher v0.0.0.1.00
    2013-10-14 11:26:47 [INFO] �a[�6Punisher�a]�f: �bSaving Config...
    2013-10-14 11:26:47 [INFO] Saving players
    2013-10-14 11:26:47 [WARNING] DSCT: Socket closed
    2013-10-14 11:26:47 [INFO] Saving worlds
    2013-10-14 11:26:47 [INFO] Saving chunks for level 'world'/Overworld
    2013-10-14 11:26:47 [INFO] Closing listening thread
    2013-10-14 11:26:47 [INFO] Saving chunks for level 'world_nether'/Nether
    2013-10-14 11:26:47 [INFO] Saving chunks for level 'world_the_end'/The End
    2013-10-14 11:26:47 [INFO] Stopping server
     
  2. Offline

    TeeePeee

    You're casting the ColoredConsoleSender to a Player in the class PunishExecutor3 line 30.
     
  3. Offline

    Nateb1121

    Somewhere, not sure where yet, you're trying to cast sender to a player without making sure it's first a player. I can see you have some "instanceOf Player" statements in there, but somewhere you're missing one. The error shows line 30, but since you have comments and it's a runtime error that line won't be exact. Look around line 30.


    I would also like to point out this line this is somethi:
    Code:
     if (sender.hasPermission(cmd.getPermission())|!(sender instanceof Player)){
    Remember, a single | is a bit-wise operator, you need to use ||.
     
  4. Offline

    MrSparkzz

    Line 30 of PunishExecutor3 class.
    EDIT: Ninja'd by TeeePeee ;)

    It's not looking for an instanceof. In his code he's casting ((ColoredConsoleSender) player).

    He provided the wrong class file, that's why you're confused and not finding anything on line 30.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  5. Offline

    Nateb1121


    Ah, true, very true. Dangers of using Eclipse auto complete. He's also using a bit-wise inclusive or on line 31.
     
  6. Offline

    MrSparkzz

    It doesn't matter, he provided the wrong class. So looking at the class he gave, and trying to decipher a stack trace which states it's in a different class, is not going to work very well.
     
  7. Offline

    TeeePeee

    ^ This :)
     
  8. Offline

    CD3


    That class is never called in runtime though...
     
  9. Offline

    MrSparkzz

    Maybe it's a ghost? But still. You should fix it.
     
  10. Offline

    CD3


    Thanks for your help!! :D however, to help solve this issue, I would like to mention that PunishExecutor3 is never called in runtime...
     
  11. Offline

    MrSparkzz

    As I said
     
  12. Offline

    CD3

    would it help to delete PunishExecutor3? it serves no purpose in runtime, as its a fragment of buggy code I completely re-wrote.
     
  13. Offline

    TeeePeee

    From your log, it appears that you type /Howclose from your console, which I'm assuming is meant to return how close a player is from being banned. You must not perform a check before you run that command, because you are trying to cast the console to a player ((Player)sender) when it's not applicable. Humor me and add an if statement before you cast stating if(sender instanceof Player) and see if that fixes it.
     
  14. Offline

    MrSparkzz

    CD3
    Yeah sure, delete it. But if there's something in that class you need, I'd suggest putting it in a notepad file or something, maybe pastebin or pastie?

    If you delete it and you get errors in your code, then somewhere in your valid code, it was calling that class. Which is what I assume is happening.
     
  15. Offline

    CD3

    k, doing that now.
     
  16. Offline

    TeeePeee

    I can't edit my posts so I'm forced to repost (silly bukkit forums). If you're not using PunishExecutor3 but haven't remove its instantiation in your main plugin class (getCommand("howclose").setExecutor(new PunishExecutor3())) it will still be listening for commands and react accordingly...
     
    MrSparkzz likes this.
  17. Offline

    CD3

    the Howclose command is a seprate command, and works.
    this command is /punish, and it the Executor class is set to the class I gave.
     
  18. Offline

    TeeePeee

    Ah silly me, misread the server log for the command name :3.

    Regardless, punish is being called in PunishExecutor3 and it's not checking if the sender is a player before casting. The computer only does exactly what you tell it to :D
     
  19. Offline

    CD3

    ok WTH is going on...

    I ran it with the changes, and this happened on Enable:

    Code:
    12:10:43 [INFO] ?a[?6Punisher?a]?f: ?bLoading The Punish Command...
    12:10:43 [SEVERE] Error occurred while enabling Punisher v0.0.0.1.00 (Is it up to date?)
    java.lang.NoClassDefFoundError: me/crazeddude/bukkit/punishSystem/commands/PunishExecutor3
        at me.crazeddude.bukkit.punisher.MAIN.Punisher.onEnable(Punisher.java:54)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.<init>(CraftServer.java:218)
        at net.minecraft.server.v1_6_R2.PlayerList.<init>(PlayerList.java:56)
        at net.minecraft.server.v1_6_R2.DedicatedPlayerList.<init>(SourceFile:11)
        at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:106)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Caused by: java.lang.ClassNotFoundException: me.crazeddude.bukkit.punishSystem.commands.PunishExecutor3
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 12 more
    
    when the line eclipse said this :
    Code:java
    1. this.getCommand("Punish").setExecutor(new me.crazeddude.bukkit.punishSystem.commands.PunishExecutor2(this));


    thats true
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  20. Offline

    MrSparkzz

    Line 54 of your onEnable();

    I'm guessing that's the line Eclipse highlighted?
     
  21. Offline

    TeeePeee

    The ClassLoader sometimes gets confused when you reload after updating the plugin jar.

    Just try stopping and restarting the server.
     
  22. Offline

    CD3

    yes, it is.

    But if it was getting confused, it should happen consistently, should it?

    making a github repository for this.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  23. Offline

    TeeePeee

    Consistently if you reload after making changes to the plugin jar in a class that's being used. For example, if you were to run a scheduled task, update the runnable and export the jar, your console would throw an error every time that class executed.
     
  24. Offline

    MrSparkzz

    TeeePeee
    Always remember to quote/reply or tag a player when replying.

    CD3
    Well, I've never really experienced any plugins doing that, but it is possible. Try again? If it happens consistently then there's probably another problem hanging around.
     
  25. Offline

    CD3

    ok... The weird part is I cant see Colored console sender in any of the classes.
     
  26. Offline

    MrSparkzz

    CD3
    Well it said it was in the PunishExecutor3 class... but you said you deleted it right?

    Are you still getting the same stacktraceeven after shutting the server down and starting it again?
     
  27. Offline

    CD3

    yes, I am. Maybe renameing 2 to 3 will work?
     
  28. Offline

    MrSparkzz

    CD3
    That might just make more problems than it solves... Link to the gitHub repo?
     
  29. Offline

    CD3

    yes.

    Github is up

    https://github.com/EpicraftMC/punisher

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  30. Offline

    MrSparkzz

    CD3
    Line 49 of your onEnable() method calls the PunishExecutor3 class.
     
    TeeePeee likes this.
Thread Status:
Not open for further replies.

Share This Page