1.8 Not working a command check with console

Discussion in 'Spigot Plugin Development' started by Nighterance, Apr 15, 2017.

  1. Hello guys. So I have a code, where it allows players to admit if they are hacking. I am checking if that is enabled on config.yml and if it is, then it has to do certain commands. But the problem is, that it is not doing what it is supposed to

    Code (Text):

    public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args){
         if(getConfig().getBoolean("admit-cheating", true)){
             Player p = (Player) sender;
         if(cmd.getName().equalsIgnoreCase("/admitcheating") || cmd.getName().equalsIgnoreCase("/ac")) {
             if(sender instanceof Player){
             p.setHealth(20);
             }else{
             sender.sendMessage("As a console, you can't admit, because you are unbannable.");
             }} else {
                    p.sendMessage("Admitting to use cheats is disabled.");
         }}
     
    So In the admit-cheating I have tryed a lot of things - without true, with == true ect, but it still doesn't work.
    I tryed to output value of admit-cheating, but the value already is true.
    Anyone has any idea?
    Thanks!
     
  2. Code (Text):
    if(cmd.getName().equalsIgnoreCase("/admitcheating") || cmd.getName().equalsIgnoreCase("/ac")) {
    Remove the '/' like that:
    Code (Text):
    if(cmd.getName().equalsIgnoreCase("admitcheating") || cmd.getName().equalsIgnoreCase("ac")) {
     
    • Agree Agree x 1
  3. Nice, it was my bad, don't know why I did it, but thats why im just learning :).
    Any idea why if I enter command on console, it outputs error rather than saying that console is not
    allowed to do that command?

    To fix so it outputs even if admitcheating is disabled, i changed order, now it works, but console still brings out error
    Code (Text):

         if(cmd.getName().equalsIgnoreCase("admitcheating") || cmd.getName().equalsIgnoreCase("ac")) {
             Player p = (Player) sender;
            if(getConfig().getBoolean("admit-cheating")){
             if(sender instanceof Player){
             Bukkit.dispatchCommand(Bukkit.getConsoleSender(), getConfig().getString("tempbanonAdmit").replace("%Player%", p.getName()).replace("%Reason%", "Addmited of hacking"));
             }else{
             sender.sendMessage("As a console, you can't admit, because you are unbannable.");
             }} else {
                    p.sendMessage("Admitting to use cheats is disabled.");
         }}
     
     
  4. That's because your're casting sender to a player, then checking if sender is not a player. You must check if the sender is not the player and if it isn't then you should cast the sender to a player
    :)
     
    • Agree Agree x 2
  5. Yea, understood my problem.

    Thanks @Drajsor && @kanoxx
     
  6. No problem, was a pleasure to help! :)