[WARNING] Unexpected exception while parsing console command

Discussion in 'Plugin Development' started by Roervig2473, Jul 20, 2012.

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

    Roervig2473

    Hey, i'm trying to make a plugin, and 1 of the features should be a console broadcast and a console chat. It should only be able to be used from the console and not by a player. this is what i get when trying to use it:
    Code:text
    1.  
    2. 15:14:06 [INFO] Done (9,374s)! For help, type "help" or "?"
    3. >cc Test
    4. 15:14:57 [WARNING] Unexpected exception while parsing console command
    5. org.bukkit.command.CommandException: Unhandled exception executing command 'cc' in plugin EssentialExtentionForPermissionsEx v0.1
    6. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    7. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    8. at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
    9. at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:475)
    10. at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:612)
    11. at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:581)
    12. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
    13. at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    14. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    15. at org.craftingdreamznetwork.Roervig2473.EEFP.Boot.EssentialExtentionForPermissionsEx.onCommand(EssentialExtentionForPermissionsEx.java:115)
    16. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    17. ... 7 more
    18. >cb test
    19. 15:15:06 [WARNING] Unexpected exception while parsing console command
    20. org.bukkit.command.CommandException: Unhandled exception executing command 'cb' in plugin EssentialExtentionForPermissionsEx v0.1
    21. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    22. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    23. at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
    24. at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:475)
    25. at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:612)
    26. at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:581)
    27. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
    28. at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    29. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    30. at org.craftingdreamznetwork.Roervig2473.EEFP.Boot.EssentialExtentionForPermissionsEx.onCommand(EssentialExtentionForPermissionsEx.java:115)
    31. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    32. ... 7 more
    33.  

    here is what i get from ingame

    Console Broadcast:
    [​IMG]
    Console Chat:
    [​IMG]

    for some reason it wouldnt let me use the img bbcode
    My code is:
    Code:text
    1.  
    2. if(cmd.getName().equalsIgnoreCase("cb"))
    3. {
    4. if (cs instanceof Player)
    5. {
    6. player.sendMessage("§1[§bEEFPermissionsEx§1]§c This command can only be fired from console!");
    7. returnfalse;
    8. }
    9. else
    10. {
    11. Bukkit.broadcastMessage("§e[§6Console Broadcast§e]§9 " + consoleBroadcast);
    12. }
    13. returntrue;
    14. }
    15. if(cmd.getName().equalsIgnoreCase("cc"))
    16. {
    17. Bukkit.broadcastMessage("§d[§5Console Chat§d]§c " + consoleChat);
    18. returntrue;
    19. }
    20. returnfalse;
    21. }
    22.  

    i removed the
    Code:text
    1.  
    2. if (cs instanceof Player)
    3. {
    4. player.sendMessage("§1[§bEEFPermissionsEx§1]§c This command can only be fired from console!");
    5. return false;
    6. }
    7.  

    From the CC command to test if it was that.
     
  2. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    dont cast to a player whitout checking
     
  3. Offline

    Roervig2473

    ?
     
  4. you trying to do (Player)sender, that is what failed, only try to cast when its realy needed, and if its not an player, dont cast but send a message that the console it cant use it
     
  5. Offline

    Roervig2473

    Sorry, i'm kinda new to this. how do i fix it then?
     
  6. if you give the full code and the line where it happend (its hard to count the lines)
     
  7. Offline

    Roervig2473

    Code:text
    1.  
    2. package org.craftingdreamznetwork.Roervig2473.EEFP.Boot;
    3.  
    4. import java.io.BufferedWriter;
    5. import java.io.File;
    6. import java.io.FileWriter;
    7. import java.util.logging.Logger;
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.YamlConfiguration;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.craftingdreamznetwork.Roervig2473.EEFP.Listeners.PlayerListener;
    17. import ru.tehkode.permissions.PermissionManager;
    18. import ru.tehkode.permissions.bukkit.PermissionsEx;
    19.  
    20. public class EssentialExtentionForPermissionsEx extends JavaPlugin
    21. {
    22. public final YamlConfiguration config = new YamlConfiguration();
    23. public final Logger log = Logger.getLogger("Minecraft");
    24. public PermissionManager permissionsex;
    25. private final PlayerListener playerLstnr = new PlayerListener(this);
    26. publicfinal File dir = new File("plugins/EssentialExtentionForPermissionsEx");
    27. public final File yml = new File(dir, "config.yml");
    28.  
    29. @Override
    30. public void onEnable()
    31. {
    32.  
    33. setupThoosePerms();
    34. if (!yml.exists())
    35. {
    36. generateThatConfigFile();
    37. }
    38. cLoad(yml);
    39. PluginManager pm = getServer().getPluginManager();
    40. pm.registerEvents(playerLstnr, this);
    41. Bukkit.broadcastMessage("§1[§bEEFPermissionsEx§1]§9 is now enabled!");
    42.  
    43. }
    44.  
    45. @Override
    46. public void onDisable()
    47. {
    48. log.warning("[EEFPermissionsEx] is now disabled! is the server shuttinf down or did the plugin crash?");
    49. if(config.getBoolean("allow.broadcast.of.plugin.disable", true))
    50. {
    51. Bukkit.broadcastMessage("§1[§bEEFPermissionsEx§1]§9 Got disabled! is the server shutting down or did the plugin crash?");
    52. }
    53. }
    54.  
    55. private void setupThoosePerms()
    56. {
    57. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("PermissionsEx");
    58. permissionsex = PermissionsEx.getPermissionManager();
    59. log.info("[EEFPermissionsEx] Found " + ((PermissionsEx) permissionsPlugin).getDescription().getFullName() + " and it is up and running:D!");
    60. }
    61.  
    62. public void generateThatConfigFile()
    63. {
    64.  
    65. try {
    66. log.info("Config doesnt exist!");
    67. log.info("Generating config!");
    68. log.info("Generating folder!");
    69. log.info("Folder generated:)");
    70. log.info("Opening folder!");
    71. dir.mkdir();
    72. log.info("Folder opended!");
    73. log.info("Getting FileWriter ready!");
    74. FileWriter filestream = new FileWriter(yml);
    75. log.info("FileWrite is up!");
    76. log.info("Getting BufferedWriter ready!");
    77. BufferedWriter out = new BufferedWriter(filestream);
    78. log.info("BufferedWriter is up!");
    79. log.info("Writing the config itself OMFG FINALLY!!!");
    80. out.write("#Here is the config, to config xD\n");
    81. out.write("#Remember its a .yml file!\n");
    82. out.write("#you can read more about yml files at Wikipedia\n");
    83. out.write("#Happy configging:P\n");
    84. out.write("#Best Regards, - Roervig2473\n");
    85. out.write("#The plugin developer\n");
    86. out.write(" \n");
    87. out.write("#The Booleans. only change theese to \"true\" or \"false\" else it will go wrong\n");
    88. out.write(" \n");
    89. out.write("broadcast.staff.join: true\n");
    90. out.write("change.staff.join.message: true\n");
    91. out.write("change.player.join.message: false\n");
    92. out.write("allow.broadcast.of.plugin.disable: true\n");
    93. out.write(" \n");
    94. out.write("#No more booleans for you:)\n");
    95. out.write(" \n");
    96. out.write("#Now to configging the Strings for the plugin:)\n");
    97. out.write("staff.broadcast.join.message.equals: '%STAFFMEMBER% is an <TYPE IN YOUR SERVERNAME HERE!> StaffMember!'\n");
    98. log.info("Config written");
    99. log.info("Closing the FileWriter");
    100. log.info("Closing the BufferedReader!");
    101. out.close();
    102. log.info("FileWriter closed");
    103. log.info("BufferedReader closed");
    104. }
    105. catch (Exception e)
    106. {
    107. log.warning("[EEFPermissionsEx] Failed to create config file!");
    108. log.info("[EEFPermissionsEx]Printing stack trace. Give this to Roervig2473 to get help!");
    109. e.printStackTrace();
    110. }
    111.  
    112. }
    113.  
    114. public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args)
    115. {
    116. Player player = (Player) cs;
    117. String broadcastMessage = getFinalArgs(args, 0);
    118. String consoleBroadcast = getFinalArgs(args, 0);
    119. String consoleChat = getFinalArgs(args, 0);
    120. String playerPrefix = getFinalArgs(args, 1);
    121. if(cmd.getName().equalsIgnoreCase("broadcast"))
    122. {
    123. String finalBroadcast;
    124. if(permissionsex.has(player, "eefpermissionsex.staffbroadcast"))
    125. {
    126. finalBroadcast = "§3[§bStaff Member§3]§2[§a" + player.getDisplayName() + "§2]§f:§5 " + broadcastMessage;
    127. Bukkit.broadcastMessage(finalBroadcast);
    128. returntrue;
    129. }
    130. else
    131. {
    132.  
    133. }
    134. }
    135. if(cmd.getName().equalsIgnoreCase("cb"))
    136. {
    137. if (cs instanceof Player)
    138. {
    139.  
    140. player.sendMessage("§1[§bEEFPermissionsEx§1]§c This command can only be fired from console!");
    141. returnfalse;
    142. }
    143. else
    144. {
    145. Bukkit.broadcastMessage("§e[§6Console Broadcast§e]§9 " + consoleBroadcast);
    146. }
    147. returntrue;
    148. }
    149. if(cmd.getName().equalsIgnoreCase("cc"))
    150. {
    151. Bukkit.broadcastMessage("§d[§5Console Chat§d]§c " + consoleChat);
    152. returntrue;
    153. }
    154. returnfalse;
    155. }
    156.  
    157. public boolean cLoad(File file) {
    158. try {
    159. config.load(file);
    160. returntrue;
    161. } catch (Exception e) {
    162. log.info("[EssentialExtentionForPermissionsEx] Error loading config!");
    163. returnfalse;
    164. }
    165. }
    166.  
    167. public static String getFinalArgs(final String[] args, final int start)
    168. {
    169. final StringBuilder bldr = new StringBuilder();
    170. for (int i = start; i < args.length; i++)
    171. {
    172. if (i != start)
    173. {
    174. bldr.append(" ");
    175. }
    176. bldr.append(args[i]);
    177. }
    178. return bldr.toString();
    179. }
    180.  
    181. }
    182.  
    [/i]
     
  8. the first line of your command excutor, your doing
    Player player = (Player) cs;
    whitout checking is cs is realy an player
     
  9. Offline

    Roervig2473


    so i would do
    Code:JAVA
    1.  
    2.  
    3. if (cs instanceof Player)
    4. {
    5. Player player = (Player) cs;
    6. }
    7.  

    ?
     
  10. actualy, you can do
    Payer player = null;
    if (cs instanceof Player)
    {
    player = (Player) cs;
    }
    and when you need a player ad an function, just make sure player is not null
     
Thread Status:
Not open for further replies.

Share This Page