Solved Bukkit Plugin cmd Returns Usage

Discussion in 'Plugin Development' started by RcExtract, Mar 29, 2017.

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

    RcExtract

    I am new to this aspect, and I have a problem on developing my plugin. Eclipse did not return errors, but when i tried to use the command i created, it returns the usage from plugin.yml (if usage is null, then it returns nothing). Can someone explain whats going on and solution?

    Here is my code:
    Code:
    package me.rcextract.teleport;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Main extends JavaPlugin{
        @Override
        public void onEnable(){
            getLogger().info("Spawn tool has been enabled!");
        }
        @Override
        public void onDisable(){
            getLogger().info("Spawn tool has been invoked!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, Player target, String[] args)
        {
            if (!(sender instanceof Player)) {
                sender.sendMessage("This plugin is dedicated for players!");
                return true;
            }
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tpa")) {
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
                }
                target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null) {
                    p.sendMessage(ChatColor.RED + "Player " + args[0] + " not found!");
                    return true;
                }
                p.sendMessage(ChatColor.GREEN + "Sent teleport request to " + args[0] + ".");
                p = Bukkit.getServer().getPlayer(args[1]);
                target.sendMessage(ChatColor.YELLOW + args[1] + "sent a teleport request to you.");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("tpaccept")) {
                p.teleport(target);
                target.sendMessage(ChatColor.GREEN + args[1] + " has been successfully teleported to you.");
                p.sendMessage(ChatColor.GREEN + "You have been sucessfully teleported to " + args[0] + ".");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("tpdeny")) {
                p.teleport(target);
                target.sendMessage(ChatColor.YELLOW + "You have successfully denied " + args[1] + "'s teleport request.");
                p.sendMessage(ChatColor.YELLOW + args[0] + " denied your teleport request.");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("setspawn")) {
                String world = p.getLocation().getWorld().getName();
                double x = p.getLocation().getX();
                double y = p.getLocation().getY();
                double z = p.getLocation().getZ();
                getConfig().set("spawn.world", world);
                getConfig().set("spawn.x", x);
                getConfig().set("spawn.y", y);
                getConfig().set("spawn.z", z);
                saveConfig();
                p.sendMessage(ChatColor.GREEN + "Spawn set to coordinates " + ChatColor.YELLOW + x + " " + y + " " + z + ChatColor.GREEN + " in" + ChatColor.YELLOW + world + ChatColor.GREEN + ".");
                return true;
            }
            if (cmd.getName().equalsIgnoreCase("spawn")){
                if (getConfig().getConfigurationSection("spawn") == null) {
                    p.sendMessage(ChatColor.RED + "Spawn has not been set!");
                    return true;
                }
                World w = Bukkit.getServer().getWorld(getConfig().getString("spawn.world"));
                double x = getConfig().getDouble("spawn.x");
                double y = getConfig().getDouble("spawn.y");
                double z = getConfig().getDouble("spawn.z");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage(ChatColor.GREEN + "You have been successfully teleported to the spawn.");
                return true;
            }
            return true;
        }
    }
    
    and here is my plugin.yml:
    Code:
    name: Spawn Tool
    version: 1.0
    main: me.rcextract.teleport.Main
    description: This tool provides simple teleports and spawn features.
    commands:
       tpa:
          usage: /<command> [player]
          description: Send teleport requests to other players.
       tpaccept:
          usage: /<command>
          description: Accept teleport requests.
       tpdeny:
          usage: /<command>
          description: Deny teleport requests.
       setspawn:
          usage: /<command>
          description: Set a spawn point.
       spawn:
          usage: /<command>
          aliases: [tpspawn]
          description: Teleports you to spawn point.
    There should be a configuration file generated upon /setspawn issued.
     
  2. Offline

    yPedx

    1) Do not use getlogger, remove this:
    getLogger().info("Spawn tool has been invoked!");

    2) You never specified args nr 1 (args[0])

    This is how I would do it:
    Code:
    Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tpa")) {
                if (args.length == 0 || !args.length == 1) {
                    p.sendMessage(ChatColor.RED + "Please specify a player!");
                    return true;
    Disclaimer: I haven't tested the !args.length myself, but it should work I guess.
     
  3. Online

    timtower Administrator Administrator Moderator

    @yPedx That won't even compile.
    You forgot to check if the sender is a player in the first place.
    What if the amount of args > 1?
     
  4. Offline

    yPedx

    Oh. I'm a bit messy sometimes :p
     
  5. Offline

    RcExtract

    Actually I watched a tutorial. I tried to understand the code and make my own, but it seems not gone well.

    What is String[] args for? What is the bracket for? And what is the difference between args[0] and [1]? I failed to find related tutorials. Other tutorials just explain briefly.
     
  6. Online

    timtower Administrator Administrator Moderator

    @RcExtract The String[] args is basic Java. It means that it is an array.
    The [0] and [1] are value getters.
     
  7. Offline

    RcExtract

    Do I put inside the bracket of a method to save the result into the args[0]? If yes, what's the difference between variables?
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Offline

    RcExtract

    Thanks. I believe it is not related to the current issue.

    I read a similar closed thread, saying that return true or false is the point why the plugin of that threads creator returns usage when issuing the command. However, that doesn't solve my issue, and I am sure that I didn't miss any returns.
     
  10. Online

    timtower Administrator Administrator Moderator

    @RcExtract It is not, it is related to the question you asked.

    And you do not indeed.
    Remove the target variable from the onCommand, and add an @Override to it.
     
  11. Offline

    RcExtract

    Let me conclude the points i don't understand about the code
    1. Line 13 + 17: @Override
    2. Line 21: Variable Type and characteristics inside the bracket
    3. Line 27: the data type inside the bracket
     
  12. Online

    timtower Administrator Administrator Moderator

    @RcExtract You are learning Java through Bukkit aren't you?
    If so: please go and follow java tutorials till you are familiar with it.
     
  13. Offline

    RcExtract

    No. I just have not been coding Java for a year, and I was not very familiar with it, so I forget many things.

    I do not really understand the official tutorial. I learnt Java by comparing many example codes, and comparing results.

    Btw, ur solution works. Though there are internal errors, at least it runs now.

    Did the @Override on onEnable and onDisable hidden onCommand?
     
  14. Online

    timtower Administrator Administrator Moderator

    @RcExtract Override does not hide anything.
    The target variable in the onCommand was the issue. Bukkit searches for a specific method.
    You didn't have that, that is why it wasn't working.
     
    RcExtract likes this.
Thread Status:
Not open for further replies.

Share This Page