The command doesn't exist?

Discussion in 'Plugin Development' started by TheMinecraftKnight, Jan 7, 2017.

Thread Status:
Not open for further replies.
  1. Once again, making my plugin... It isn't working.
    When typing /hint - it doesn't seem to exist. Everything else is working 100% fine.
    Any ideas?
    Here's my code:
    PHP:
    package com.knightzmc.basics;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class 
    KnightzMCRandomTP extends JavaPlugin {

        public 
    String bold ChatColor.BOLD "";

        @
    SuppressWarnings("deprecation")
        public 
    boolean onCommand(CommandSender theSenderCommand cmdString commandLabelString[] args) {
                
    Player player = (PlayertheSender;
                if (
    cmd.getName().equalsIgnoreCase("randomtp")) {
                    
    int randomx = (int) (Math.random() * 100000);
                    
    int randomz = (int) (Math.random() * 100000);
                    
    int randomy player.getWorld().getHighestBlockYAt(randomxrandomz);

                    
    Location randomLocation = new Location(player.getWorld(), randomxrandomyrandomz);
                    
    player.teleport(randomLocation);
                    
    player.sendMessage(ChatColor.GOLD "You were teleported to X: " ChatColor.GREEN randomx
                            
    ChatColor.GOLD " Y: " ChatColor.GREEN randomy ChatColor.GOLD " Z: "
                            
    ChatColor.GREEN randomz);
                }

                if (
    cmd.getName().equalsIgnoreCase("heal")) {

                    if (
    player.hasPermission("knightzmc.heal"))
                        
    player.setHealth(20);
                    else
                        
    player.sendMessage(
                                
    ChatColor.GOLD "Sorry, you don't have permission to use " ChatColor.GREEN "/heal");
                }

                
    // Superheal Command
                
    if (cmd.getName().equalsIgnoreCase("superheal")) {
                    if (
    player.hasPermission("knightzmc.superheal")) {
                        if (
    args.length == 1) {
                            if (
    args[0].equalsIgnoreCase("on")) {
                                
    player.setMaxHealth(1500);
                                
    player.setHealth(1500);
                                
    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION1000000255));
                            } else if (
    args[0].equalsIgnoreCase("off")) {
                                
    player.setMaxHealth(20);
                                
    player.setHealth(20);
                                
    player.removePotionEffect(PotionEffectType.REGENERATION);
                            } else
                                
    player.sendMessage(ChatColor.GOLD "Usage: " ChatColor.GREEN "/superheal on/off");
                        } else
                            
    player.sendMessage(ChatColor.GOLD "Usage: " ChatColor.GREEN "/superheal on/off");
                    }

                    else
                        
    player.sendMessage(ChatColor.GOLD "Sorry, you don't have permission to use " ChatColor.GREEN
                                
    commandLabel);
                }
               
                
    //hint command
                
    if (cmd.getName().equalsIgnoreCase("hint")) {
                    
    int hintnumber = (int) (Math.random() * 1);
                    if (
    hintnumber == 0) {
                        
    player.sendMessage(ChatColor.GOLD bold "Hint 1:" ChatColor.AQUA bold "Manual Labor is good for you");
                    }
                    if (
    hintnumber == 1) {player.sendMessage(ChatColor.GOLD bold "Hint 2:" ChatColor.AQUA bold "You shouldn't be afraid of Ender Pearls. Or cobwebs.");
                    }
                }

            return 
    true;
        }
    }
    And my plugin.yml
    Code:
    name: KnightzMCBasics
    version: 1.1
    main: com.knightzmc.basics.KnightzMCRandomTP
    description: A collection of basic commands made for KnightzMC
    commands:
      randomtp:
        description: Teleports you to a random location in the current world you are in.
      heal:
        description: Heals you up to full health!
        permission: knightzmc.heal
      superheal:
        description: Heals you up to full health - and makes your max health 40!
        permission: knightzmc.superheal
      hint:
        description: This command totally won't help you find collectables
    Any help would be greatly appreciated :)
     
  2. Offline

    ipodtouch0218

    You need a "usage" tag for each command in your plugin.yml
    Code:
      randomtp:
        description: Teleports you to a random location in the current world you are in.
        usage: /<command>
     
  3. I've updated my plugin.yml to this
    Code:
    name: KnightzMCBasics
    version: 1.1
    main: com.knightzmc.basics.KnightzMCRandomTP
    description: A collection of basic commands made for KnightzMC
    commands:
      randomtp:
        description: Teleports you to a random location in the current world you are in.
        usage: /randomtp
      heal:
        description: Heals you up to full health!
        permission: knightzmc.heal
        usage: /heal
      superheal:
        description: Heals you up to full health - and makes your max health 40!
        permission: knightzmc.superheal
        usage: /superheal on/off
      hint:
        description: This command totally won''t help you find collectables
        usage: /hint
    It's still not working. I think its more likely to be something in the java?
     
  4. No, it just says Enabling KnightzMCBasics...
     
  5. Offline

    CrazyLukeHD

    You don't need any usage tags for your commands. I don't use them, and my plugin works fine. I don't even know why you would need them apart from people wanting to know how to use a command.

    The problem is that you are repeating "if" statements on each command:
    Code:
    if (cmd.getName().equalsIgnoreCase("command")) {
    Keep the first "if" statement as it is, but replace all of the other "if" statements which have that code with "else if" statements.

    Just for a tip, if you want you can insert this code right in between the onCommand boolean and "Player player = (Player) theSender" so it detects if the sender of the command is console or not (Sending colored messages to console will create errors because it isn't supported), like this:
    Code:
    if (!(theSender instanceof Player)) {
        theSender.sendMessage("You must be a player to execute this command!")
    }
    else {
        // Anything further than these brackets are to be put in here
    }
     
    Last edited: Jan 8, 2017
  6. Offline

    Zombie_Striker

    Exactly.
    It would be better if you returned false instead. You don't have to worry about encapsulation that way.
     
Thread Status:
Not open for further replies.

Share This Page