Solved Adding Console Commands

Discussion in 'Plugin Development' started by XvBaseballkidvX, Jun 22, 2013.

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

    XvBaseballkidvX

    Hello everyone.

    I just have a simple question about adding console commands.
    I tried using:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. if(commandLabel.equalsIgnoreCase("command")){
    3. if(args.length == 0){
    4. if(sender instanceof Player){
    5. //stuff
    6. }else{
    7. //same stuff


    But that sadly didnt work.
    All help is much appreciated, thank you for reading :D
     
  2. Offline

    chasechocolate

    Did you register the "command" command in your plugin.yml?
    Code:
    commands:
      command:
        usage: /command
        description: A random command.
     
  3. Offline

    XvBaseballkidvX

    Yes, but whenever I execute the command in the console I get an error. The command does everything I want it to in game, I just would like it to work in the console as well. Its just a simple Player Cords getting plugin:

    Here is the code for it:

    Code:java
    1. import java.util.logging.Logger;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin{
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13. public static Main plugin;
    14.  
    15. @Override
    16. public void onEnable(){
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    19. }
    20.  
    21. @Override
    22. public void onDisable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    25. }
    26.  
    27.  
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    29. Player player = (Player) sender;
    30. if(commandLabel.equalsIgnoreCase("playercords")){
    31. if(args.length == 0){
    32. if(player.hasPermission("player.cords")){
    33. player.sendMessage(ChatColor.GRAY + "----------------" + ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PlayerCords" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "----------------" );
    34. player.sendMessage(ChatColor.GREEN + "Brings You To This Page:" + ChatColor.RED + " /playercords");
    35. player.sendMessage(ChatColor.GREEN + "To Find the Player's Cords:" + ChatColor.RED + " /playercords <player>");
    36. player.sendMessage(ChatColor.GREEN + "Player Cords Made by:" + ChatColor.RED + " XvPROTECTEDvX");
    37. player.sendMessage(ChatColor.GRAY + "----------------" + ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "PlayerCords" + ChatColor.DARK_GRAY + "]" + ChatColor.GRAY + "----------------" );
    38. }else{
    39. player.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
    40. }
    41. }if(args.length == 1){
    42. if(player.hasPermission("player.locate")){
    43. if(player.getServer().getPlayer(args[0]) != null){
    44. Player targetPlayer = player.getServer().getPlayer(args[0]);
    45. int loc = (int) targetPlayer.getLocation().getX();
    46. int location = (int) targetPlayer.getLocation().getY();
    47. int locs = (int) targetPlayer.getLocation().getZ();
    48. player.sendMessage(ChatColor.GREEN + "Cords: " + ChatColor.GOLD + " X: " + loc + " Y: " + location + " Z: " + locs);
    49. }
    50.  
    51.  
    52. }else{
    53. player.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
    54. }
    55.  
    56.  
    57. return false;
    58. }
    59. }
    60. return false;
    61. }
    62. }
    63.  


    EDIT: I did not try to use the if(sender instanceof Player) because it did not work the first time I tried it.
     
  4. Offline

    ZeusAllMighty11

    Well if you try to send a command to console when you assumed the sender is a player... it's going to throw an error
     
  5. Offline

    XvBaseballkidvX

    I understand that, How would I be able to add console commands though?
     
  6. Offline

    ZeusAllMighty11

    Check if sender is instanceof ConsoleCommandSender, and don't do any casting
     
  7. if ((sender instanceof Player)) return true;
     
  8. Offline

    XvBaseballkidvX

    Thank you everyone for your help, Problem Solved! :D

    Final Code:

    Code:java
    1. if(sender instanceof Player){
    2. //stuff
    3. if(sender instanceof ConsoleCommandSender){
    4. //same stuff
    5.  
    6.  
     
Thread Status:
Not open for further replies.

Share This Page