ISSUED SERVER COMMAND!

Discussion in 'Plugin Development' started by Tivertoni, Mar 23, 2021.

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

    Tivertoni

    Hi in the console it shows me the Error "TivertoniJB issued server command: /heal"

    Here is the Code: Note: The Command dosnt work !
    Code:
    package de.xcoder.plugin.commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class HealCommand implements CommandExecutor {
        int IsSpaming = 0;
           
        public void ResetSpaming() {
            if(IsSpaming >= 4) {
                IsSpaming--;
            }
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
           
            if(sender instanceof Player) {
                Player player = (Player) sender;
                if(player.hasPermission("server.heal")) {           
                    if(args.length == 0) {
                            player.setHealth(20);
                            player.setFoodLevel(20);
                            if(IsSpaming < 4) {
                                player.sendMessage("§aDu wurdest geheilt!");
                            }else 
                                player.sendMessage("§a Please dont spam the command!");
                    }else
                        if(IsSpaming < 4) {
                            player.sendMessage("§4Please use §6/heal");
                        }else 
                            player.sendMessage("§a Please dont spam the command!");               
                }else
                    if(IsSpaming < 4) {
                        player.sendMessage("You dont have enoght permission for this command");
                        System.out.print(player.getName() + "enoght permission for /heal");
                    }else
                        player.sendMessage("§a Please dont spam the command!");               
            }else
                sender.sendMessage("§4The Command cant be run in the console!");
           
            return false;
        }
    }
     
    Last edited by a moderator: Mar 23, 2021
  2. Offline

    CraftCreeper6

  3. Offline

    KarimAKL

    That... Is not an error. That is just the console logging the player using the command.

    Do you have an actual error elsewhere? If your code is not running, are you sure you registered the command?
     
  4. Offline

    Newdel

    Are you sure that it does not work?
     
  5. Offline

    davidclue

    You are returning false at the end of your command, change it to true and it will work.
     
  6. Offline

    Strahan

    As mentioned, that's not an error. In your code though, you are checking for spamming a bunch of times. Since it applies at many stages in the flow, move the check earlier. You are doing that check for every condition after the Player check. You gotta think about your code flow, things like that can be optimized. Also don't embed the color character; use the ChatColor enum. That's why it exists. Negative check and return would help with removing unnecessary indentation as well.

    Example, refactored:
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
      if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "The command can't be run in the console!");
        return true;
      }
    
      Player player = (Player) sender;
      if (IsSpaming > 3) {
        player.sendMessage(ChatColor.GREEN + "Please don't spam the command!");
        return true;
      }
    
      if (!player.hasPermission("server.heal")) {
        player.sendMessage("You don't have enough permission for this command");
        return true;
      }
    
      player.setHealth(20);
      player.setFoodLevel(20);
      player.sendMessage(ChatColor.GREEN + "Du wurdest geheilt!");
      return true;
    }
    PS - your use of a single class scope variable "IsSpaming" makes me nervous. It sounds like you are not accounting for multiple users.

    Returning false doesn't mean the code executed prior fails. All the true/false return means in a command is whether or not Spigot returns to the player the usage information, if such is included in the plugin.yml

    From a command logic standpoint, it's wholly irrelevant.
     
    Last edited: Mar 24, 2021
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page