Solved /heal not working...

Discussion in 'Plugin Development' started by Brevoort, Oct 20, 2016.

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

    Brevoort

    Code:
    package me.themedieval;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class medfre1 extends JavaPlugin {
        public void OnEnable() {
    
        }
    
        public void onDisable() {
          
          
        }
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("heal")) {
                Player player = (Player) sender;
                player.setHealth(20);
                player.sendMessage(player + ", you have been healed!");
                return true;
              
            } else {
                Player player = (Player) sender;
                player.sendMessage(ChatColor.RED + "What? I do not understand...");
                return false;
            }
          
          
          
        }
    
    }
    
    Okay so I recently tried to make a simple /heal command, but it wont load onto the server.

    I also realize now that the } else { segment of text needs to be changed. But is that causing the issue or what?
     
  2. Offline

    Zombie_Striker

    Follow java naming conventions. The first letter must be capitalized.

    The O has to be lower case. Also, if the onEnable and the onDisable do nothing, remove them.

    Use command.getName() instead of label, as they support alias.

    Don't blindly cast sender to a player. What if a plugin sends a command? What if it's the console? Consoles and plugins are not player, so this would throw an exception. Add an instanceof check before you create player.

    Unless you have some unknown/unlisted command, this will never run. The only time the onCommand will be called is if the command sent has already been registered in the plugin.yml. Remove this.

    Main problem: Are there any errors in the console? Does the plugin list (using command /plugins) contain the plugin? Are you sure you are registering the command in the plugin.yml?
     
  3. Offline

    Brevoort

    Okay well I see I have a lot to fix, however I'm using if(label.equalsIgnoreCase()) for the command
    Thanks for the reply. I see that I have a LOT of stuff to consider/fix.
     
    Last edited: Oct 20, 2016
  4. Offline

    Zombie_Striker

    @Brevoort
    command.getName() returns the command that has been sent. What you want is if(command.getName().equalsIgnoreCase ("heal"))
     
  5. Offline

    Brevoort

    Okay thanks! Finally managed to get it to work w/ your help.

    Actually one quick question sorry.

    So I added a permission to /heal. However when I run /heal when deopped it will give me this in chat:

    No Permission
    /heal


    How could I make it so that /heal isn't put in chat if you type it w/o permission.
     
    Last edited by a moderator: Oct 20, 2016
  6. Offline

    mehboss

    @Brevoort
    I believe a return true; would do the trick. ;)
     
    Last edited: Oct 21, 2016
    Brevoort likes this.
  7. @mehboss
    I think you mean a return true. Return false is precisely what puts it in chat in the first place.
     
    Brevoort and Lordloss like this.
  8. Offline

    mehboss

Thread Status:
Not open for further replies.

Share This Page