I get the command played back to me!

Discussion in 'Plugin Development' started by WestBurst, Aug 15, 2016.

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

    WestBurst

    Hello, I cannot figure out how to make this not play it back. When I type /healplayer I get it back. Please help!

    Code:
    package net.swoq.plugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class HealPlayer extends JavaPlugin {
    
      @Override
      public void onEnable() {
       
      }
       
      @Override
      public void onDisable() {
       
       
      }
       
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
        if (cmd.getName().equalsIgnoreCase("healplayer") && sender instanceof Player) {
         
          Player player = (Player) sender;
           
          int length = args.length;
           
          if (length == 1)  {
             
          boolean playerFound = false;
             
            for (Player playerToHeal : Bukkit.getServer().getOnlinePlayers()) {
              if(playerToHeal.getName().equalsIgnoreCase(args[0])) {
                playerToHeal.setHealth(20.0);
                playerToHeal.sendMessage(ChatColor.GREEN + "You have been healed by " + player.getName()+ "!");
                player.sendMessage(ChatColor.GREEN + "You have healed " + playerToHeal.getName());
                playerFound = true;
                break;
              }
              }  
            if (playerFound == false ) {
               player.sendMessage(ChatColor.RED + args[0] + "Player not Online Or Found. Please try again!");
            }
         
          } else player.sendMessage(ChatColor.RED + "Incorrect arguments!");
             
             return true;
               
             
          }
           
          return false;
           
      }
       
    }
     
    Last edited by a moderator: Aug 15, 2016
  2. @WestBurst
    Whenever you return false in the onCommand method, that's precisely what happens.
     
    N00BHUN73R likes this.
  3. Offline

    N00BHUN73R

    @WestBurst
    PHP:
    import net.md_5.bungee.api.ChatColor;
    Please use the org.bukkit import because if the server doesn't have Spigot, you'll get an error.

    Also, why not just use args.length, why make a separate int. There's not point to it.

    PHP:
     for (Player playerToHeal Bukkit.getServer().getOnlinePlayers()) {
              if(
    playerToHeal.getName().equalsIgnoreCase(args[0])) {
                
    playerToHeal.setHealth(20.0);
                
    playerToHeal.sendMessage(ChatColor.GREEN "You have been healed by " player.getName()+ "!");
                
    player.sendMessage(ChatColor.GREEN "You have healed " playerToHeal.getName());
                
    playerFound true;
                break;
              }
              }  
    This is highly unnecessary.
    Just use Bukkit.getPlayer(args[0]);
    and then check if the player is null, if so, the player is offline!
     
  4. Offline

    WestBurst

    @N00BHUN73R I do have Spigot Installed. But this did not work.

    @AlvinB So, How do I fix this?

    How do I fix this then?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 15, 2016
  5. Offline

    WestBurst

    At the end of the file? Make it say return true on both?
     
  6. Offline

    Marti201

    Yes, returning false sends the command usage from the plugin.yml to the player. You should never return false if the command is correctly used.
    This isn't related to your current problem, but will result in an error if it's ran on a non-spigot server. Import ChatColor from org.bukkit instead.
     
  7. Offline

    Lordloss

    Code:
      @Override
      public void onEnable() {
      
      }
      
      @Override
      public void onDisable() {
      
      }
       
    You dont need this if its empty, you can remove it.

    Code:
    if (playerFound == false ) {
    You can invert an if check with
    Code:
    if (!playerFound)
    No need to compare booleans with =.

    To sum all the stuff up what has been told you and what we have seen from your code, please dont offer other people to make plugins for them, until you really know what you do.
     
Thread Status:
Not open for further replies.

Share This Page