Error: CommandException!

Discussion in 'Plugin Development' started by OhCreative, Apr 4, 2014.

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

    OhCreative

    http://gyazo.com/e68e278d197903040de7661f4609c2eb

    I keep having this error! Is it in the code?

    Code:java
    1. package com.OhCreative.TortureMania;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.potion.PotionEffectType;
    16.  
    17. public class TortureMania extends JavaPlugin implements Listener {
    18. @Override
    19. public void onEnable(){
    20. getLogger().info("Plugin Enabled!");
    21.  
    22. }
    23. @Override
    24. public void onDisable(){
    25. getLogger().info("Plugin Disabled!");
    26. }
    27. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    28. // Uses equalsIgnoreCase() over equals() to accept "ignite" and "IgNiTe."
    29. if (cmd.getName().equalsIgnoreCase("ignite")) {
    30. // Make sure that the player specified exactly one argument (the name of the player to ignite).
    31. if (args.length != 1) {
    32. // When onCommand() returns false, the help message associated with that command is displayed.
    33. return false;
    34. }
    35. Player target = Bukkit.getServer().getPlayer(args[0]);
    36. if (target == null) {
    37. sender.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + args[0] + " is not currently online.");
    38. return true;
    39. }
    40.  
    41. sender.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + "You have set " + ChatColor.AQUA + target.getName() + ChatColor.GOLD + " on fire!" );
    42. target.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + "You have been set on fire by " + ChatColor.AQUA + sender.getName() + "!");
    43.  
    44. target.setFireTicks(600);
    45. return true;
    46.  
    47. }
    48.  
    49. if (cmd.getName().equalsIgnoreCase("crush")) {
    50. // Make sure that the player specified exactly one argument (the name of the player to ignite).
    51. if (args.length != 1) {
    52. // When onCommand() returns false, the help message associated with that command is displayed.
    53. return false;
    54. }
    55. Player target = Bukkit.getServer().getPlayer(args[0]);
    56. if (target == null) {
    57. sender.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.AQUA + args[0] + ChatColor.GOLD + " is not currently online.");
    58. return true;
    59. }
    60.  
    61.  
    62.  
    63. sender.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + " " + ChatColor.GOLD + "You have crushed" + ChatColor.AQUA + args[0] + ChatColor.GOLD + "!");
    64. target.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + " " + ChatColor.GOLD + "You have been crushed by" + ChatColor.AQUA + sender.getName() + ChatColor.GOLD + "!");
    65.  
    66.  
    67.  
    68. target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 4));
    69. Location loc = target.getPlayer().getLocation();
    70. loc.setY(loc.getY() + 5);
    71. Block b = loc.getBlock();
    72. b.setType(Material.ANVIL);
    73.  
    74.  
    75.  
    76. }
    77.  
    78.  
    79. if(cmd.getName().equalsIgnoreCase("rape"))
    80. {
    81. Player player = (Player) sender;
    82. if(args.length == 1)
    83. {
    84. Player target = Bukkit.getPlayer(args[0]);
    85. if(target != null)
    86. {
    87. if(target.getDisplayName().equals(player.getDisplayName()))
    88. {
    89. player.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + "You cannot rape yourself!");
    90. }
    91. else
    92. {
    93. player.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + " You have raped " + ChatColor.DARK_PURPLE + target.getName() + ChatColor.LIGHT_PURPLE + "!");
    94. target.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + "You have been raped by " + ChatColor.DARK_PURPLE + player.getName() + ChatColor.GOLD + "!");
    95. target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1));
    96. target.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 4));
    97. }
    98. if(args[0].equalsIgnoreCase("all"))
    99. {
    100. player.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + " You have raped everyone online!");
    101. for(Player online : Bukkit.getOnlinePlayers())
    102. {
    103. online.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + "You have been raped by " + ChatColor.DARK_PURPLE + player.getName() + ChatColor.GOLD + "!");
    104. online.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 200, 1));
    105. online.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 4));
    106. }
    107. }
    108. }
    109.  
    110. else
    111. {
    112. player.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + "You can only rape players that are online!");
    113. }
    114.  
    115. }
    116. else
    117. {
    118. player.sendMessage(ChatColor.DARK_GRAY + "|" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.RED + ChatColor.BOLD + "T" + ChatColor.WHITE + ChatColor.BOLD + "M" + ChatColor.GRAY + ChatColor.MAGIC + "i" + ChatColor.DARK_GRAY + "|" + ChatColor.GOLD + " Use " + ChatColor.AQUA + "/rape <player>" + ChatColor.GOLD + " or " + ChatColor.AQUA + "/rape all" + ChatColor.GOLD + ".");
    119. }
    120. }
    121.  
    122.  
    123.  
    124.  
    125.  
    126.  
    127. return false;
    128. }
    129.  
    130.  
    131. }
    132.  
     
  2. Offline

    afistofirony

    It seems like you are running the command from console, because the issue is being caused on line 81:

    Code:
    Player player = (Player) sender;
    You need to make sure that you check if the sender is indeed a player before blindly casting him as one:

    Code:
    if (sender instanceof Player) {
        // do stuff here
    }
    However, in this case, it doesn't seem like you really need any specific functionality from the player, so perhaps you can add console support by removing the check and the cast to Player and using 'sender' in the place of 'player.'

    P.S.: What an interesting command.
     
Thread Status:
Not open for further replies.

Share This Page