Plugin Commands are Returning Plugin.yml Usage

Discussion in 'Plugin Development' started by Notorious_Park, Aug 31, 2015.

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

    Notorious_Park

    On the plugin I have written, when I do two of the three commands it just returns to me the usage in the plugin.yml

    Main Class:
    Code:
    package com.notoriouspark.pl.insanotimer;
    
    import com.notoriouspark.pl.insanotimer.commands.StopTimerCommand;
    import com.notoriouspark.pl.insanotimer.commands.TimerCommand;
    import com.notoriouspark.pl.insanotimer.commands.UHCInfoCommand;
    import com.notoriouspark.pl.insanotimer.listeners.InventoryClickListener;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class InSanoTimer extends JavaPlugin {
        FileConfiguration config = this.getConfig();
        private String prefix;
        private boolean running = false;
        private int pvp;
        private int meetup;
    
        public void onEnable() {
    
            this.prefix = ChatColor.getByChar(config.getString("Bracket ID")) + "[ " + ChatColor.getByChar(config.getString("Server ID")) + "InSano" + ChatColor.getByChar(config.getString("Bracket ID")) + "] ";
    
            this.getServer().getPluginManager().registerEvents(new InventoryClickListener(this), this);
    
            this.getCommand("starttimer").setExecutor(new TimerCommand(this));
            this.getCommand("stoptimer").setExecutor(new StopTimerCommand(this));
            this.getCommand("uhcinfo").setExecutor(new UHCInfoCommand(this));
    
        }
    
        public FileConfiguration getConfig() {
            return this.config;
        }
    
        public String getPrefix() {
            return this.prefix;
        }
    
        public boolean getRunning() {
            return this.running;
        }
    
        public void setRunning(boolean running) {
            this.running = running;
        }
    
        public int getPvP() {
            return this.pvp;
        }
    
        public void setPvP(int pvp) {
            this.pvp = pvp;
        }
    
        public int getMeetUp() {
            return this.meetup;
        }
    
        public void setMeetUp(int meetup) {
            this.meetup = meetup;
        }
    }
    
    Command Classes:
    Code:
    package com.notoriouspark.pl.insanotimer.commands;
    
    import com.notoriouspark.pl.insanotimer.InSanoTimer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class TimerCommand implements CommandExecutor {
    
        private final InSanoTimer insanotimer;
    
        public TimerCommand(InSanoTimer insanotimer) {
            this.insanotimer = insanotimer;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Bukkit.broadcastMessage("hi!");
            if (!insanotimer.getRunning()) {
                if (args == null) {
                    insanotimer.setRunning(true);
    
                    insanotimer.setPvP(insanotimer.getConfig().getInt("Default PvP"));
                    insanotimer.setMeetUp(insanotimer.getConfig().getInt("Default MeetUp"));
    
                    runGame();
                    return true;
                } else if (args.length == 1) {
                    if (args[0].matches("\\d+")) {
                        insanotimer.setRunning(true);
    
                        insanotimer.setPvP(Integer.parseInt(args[0]));
                        insanotimer.setMeetUp(insanotimer.getConfig().getInt("Default MeetUp"));
    
                        runGame();
                        return true;
                    } else {
                        sender.sendMessage(insanotimer.getPrefix() + "Invalid Syntax.");
                        return false;
                    }
                } else if (args.length == 2) {
                    if (args[0].matches("\\d+") && args[1].matches("\\d+")) {
                        insanotimer.setRunning(true);
    
                        insanotimer.setPvP(Integer.parseInt(args[0]));
                        insanotimer.setMeetUp(Integer.parseInt(args[1]));
    
                        runGame();
                        return true;
                    } else {
                        sender.sendMessage(insanotimer.getPrefix() + "Invalid Syntax.");
                        return false;
                    }
                } else {
                    sender.sendMessage(insanotimer.getPrefix() + "Invalid Syntax.");
                    return false;
                }
            } else {
                sender.sendMessage(insanotimer.getPrefix() + "Timer is already running!");
                return false;
            }
        }
    
        public void runGame() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(insanotimer, new Runnable() {
                @Override
                public void run() {
                    insanotimer.setPvP(insanotimer.getPvP() - 1);
                    insanotimer.setMeetUp(insanotimer.getMeetUp() - 1);
                }
            }, 1200L, 1200L);
        }
    }
    
    Code:
    package com.notoriouspark.pl.insanotimer.commands;
    
    import com.notoriouspark.pl.insanotimer.InSanoTimer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class StopTimerCommand implements CommandExecutor {
    
        private final InSanoTimer insanotimer;
    
        public StopTimerCommand(InSanoTimer insanotimer) {
            this.insanotimer = insanotimer;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (insanotimer.getRunning()) {
                insanotimer.setRunning(false);
    
                Bukkit.broadcastMessage(insanotimer.getPrefix() + "Timer stopped!");
    
                for (Player player : Bukkit.getOnlinePlayers()) {
                    player.playSound(player.getLocation(), Sound.NOTE_BASS, 10, 1);
                }
                return true;
            } else {
                sender.sendMessage(insanotimer.getPrefix() + "No timer running!");
                return false;
            }
        }
    }
    
    Plugin.yml:
    Code:
    name: InSanoTimer
    main: com.notoriouspark.pl.insanotimer.InSanoTimer
    version: 1.0
    author: NotoriousPark
    
    commands:
      starttimer:
        description: Starts a timer
        usage: /<command>
        permission: InSanoTimer.basic
        permission-message: You don't have <permission>
      stoptimer:
        description: Stops a timer
        usage: /<command>
        permission: InSanoTimer.basic
        permission-message: You don't have <permission>
      uhcinfo:
        description: Displays timer
        usage: /<command>
    
    permissions:
      InSanoTimer.basic:
        description: Allows use of the commands in the plugin
        default: op
    Can anyone figure out why this is happening?
     
  2. Offline

    mine-care

    Thats an unrealistic condition.. Args will never be null.

    Does your debug work?
     
  3. Offline

    mythbusterma

    @Notorious_Park

    Returning "false" out of the onCommand(...) method does that.
     
    mine-care likes this.
  4. Offline

    Notorious_Park

    @mine-care
    What do you mean by debug? My plugin does go on the server but the commands aren't working. One of the commands is however. Edit: I just tried changing the args == null to args.length == 0 and it still doesn't work.

    @mythbusterma I have seen other plugins use false and a message and work fine. I have also tried to get rid of all the return false and it still doesn't work.
     
    Last edited: Sep 1, 2015
  5. onCommand boolean {
    if (cmd.getName().equalsIgnoreCase("test") {

    return true;
    }
    return false;
    }

    OP, you could however just return true on both and send a custom msg :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 26, 2017
  6. Offline

    mine-care

    @Notorious_Park https://en.wikipedia.org/wiki/Debug_code
    essentially adding some lines of code to visually represent where the code reaches. Also printing out variables and method returns to cross check everything works

    its not an emperical observation, it is how code works and as you can read on the docs, returning false will be handled by bukkit and send the executor the usage message. Now if you havent specified it in the plugin.yml file it wont be sent....
     
  7. Offline

    Notorious_Park

    @mine-care

    I changed my code to this:
    Code:
    package com.notoriouspark.pl.insanotimer.commands;
    
    import com.notoriouspark.pl.insanotimer.InSanoTimer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class TimerCommand implements CommandExecutor {
    
        private final InSanoTimer insanotimer;
    
        public TimerCommand(InSanoTimer insanotimer) {
            this.insanotimer = insanotimer;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Bukkit.broadcastMessage("Running:  + insanotimer.getRunning()");
    
            if (!insanotimer.getRunning()) {
                if (args.length == 0) {
                    insanotimer.setRunning(true);
    
                    insanotimer.setPvP(insanotimer.getConfig().getInt("Default PvP"));
                    insanotimer.setMeetUp(insanotimer.getConfig().getInt("Default MeetUp"));
    
                    runGame();
                    return true;
                } else if (args.length == 1) {
                    if (args[0].matches("\\d+")) {
                        insanotimer.setRunning(true);
    
                        insanotimer.setPvP(Integer.parseInt(args[0]));
                        insanotimer.setMeetUp(insanotimer.getConfig().getInt("Default MeetUp"));
    
                        runGame();
                        return true;
                    } else {
                        sender.sendMessage(insanotimer.getPrefix() + "Invalid Syntax.");
                        return true;
                    }
                } else if (args.length == 2) {
                    if (args[0].matches("\\d+") && args[1].matches("\\d+")) {
                        insanotimer.setRunning(true);
    
                        insanotimer.setPvP(Integer.parseInt(args[0]));
                        insanotimer.setMeetUp(Integer.parseInt(args[1]));
    
                        runGame();
                        return true;
                    } else {
                        sender.sendMessage(insanotimer.getPrefix() + "Invalid Syntax.");
                        return true;
                    }
                } else {
                    sender.sendMessage(insanotimer.getPrefix() + "Invalid Syntax.");
                    return true;
                }
            } else {
                sender.sendMessage(insanotimer.getPrefix() + "Timer is already running!");
                return true;
            }
        }
    
        public void runGame() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(insanotimer, new Runnable() {
                @Override
                public void run() {
                    insanotimer.setPvP(insanotimer.getPvP() - 1);
                    insanotimer.setMeetUp(insanotimer.getMeetUp() - 1);
                }
            }, 1200L, 1200L);
        }
    }
    
    And removed the usage in the plugin.yml. It does broadcast anything and nothing happens. Do you have any idea why?
     
    Last edited: Sep 1, 2015
  8. Offline

    mine-care

    Is insantorimmer running?
    Also you haven't done as I said, args will never be null so you have a dead if there ^
     
  9. Offline

    Notorious_Park

    I did change it in the code I just forgot to change it on the post. Insanotimer should be false because I set it to false in the main class.
     
  10. Offline

    mine-care

    @Notorious_Park ok do some debuging to verify this, and see to that point the code reaches.
     
  11. Offline

    Notorious_Park

    I did right here, but it never broadcasted:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Bukkit.broadcastMessage("Running:  + insanotimer.getRunning()");
    Whenever I run this command it doesn't return the usage and it returns the message to me that it should return is insanotimer.getRunning() is false:

    Code:
    package com.notoriouspark.pl.insanotimer.commands;
    
    import com.notoriouspark.pl.insanotimer.InSanoTimer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class UHCInfoCommand implements CommandExecutor {
    
        private final InSanoTimer insanotimer;
    
        public UHCInfoCommand(InSanoTimer insanotimer) {
            this.insanotimer = insanotimer;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (insanotimer.getRunning()) {
                openUHCInfo((Player) sender);
            } else {
                sender.sendMessage(insanotimer.getPrefix() + "No timer is running!");
            }
            return false;
        }
    
        public void openUHCInfo(Player player) {
            Inventory uhcinfo = Bukkit.createInventory(null, 9, "UHC Info");
    
            ItemStack pvp = new ItemStack(Material.DIAMOND_SWORD, 1);
            pvp.getItemMeta().setDisplayName("PvP: " + insanotimer.getPvP());
    
            ItemStack meetup = new ItemStack(Material.COMPASS, 1);
            meetup.getItemMeta().setDisplayName("MeetUp: " + insanotimer.getMeetUp());
    
            uhcinfo.setItem(0, pvp);
            uhcinfo.setItem(1, meetup);
    
            player.openInventory(uhcinfo);
        }
    }
     
  12. Offline

    RoboticPlayer

    A) I think it's supposed to be Bukkit.getServer().broadcastMessage(String), but correct me if I'm wrong
    B) You aren't actually adding your insanotime.getRunning(), because you did this:
    Code:
    Bukkit.broadcastMessage("Running: + insanotimer.getRunning()");
    Instead of this:
    Code:
    Bukkit.broadcastMessage("Running: " + insanotimer.getRunning());
    If you have the entire thing in quotes, it will print exactly that. But if you take the insanotimer.getRunning() out of quotation marks, then it will actually print the status of insanotimer.
     
    Last edited: Sep 2, 2015
  13. Offline

    mine-care

    It does the same thing, it doesnt matter which of the two you use. Also B is a good point!

    Remmeber, first check then cast!
     
    Gonmarte likes this.
  14. Offline

    RoboticPlayer

    OK, I wasn't quite sure on that, because I've always used getServer(), and I hadn't seen it as Bukkit.broadcastMessage(String) without getServer() before.
     
  15. Offline

    mine-care

    @henderry2019 Well we cant know everything, dont worry! We all learn things every day.
     
  16. Offline

    Notorious_Park

  17. Offline

    boomboompower

    It should be:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("COMMAND") {
    Bukkit.broadcastMessage("Running:  + insanotimer.getRunning()")
    Then you need to add the command in the plugin.yml
     
    Last edited: Sep 2, 2015
  18. Offline

    Xerox262

    He's using a command executor, unless he is handling many commands with that executor he doesn't need to check if it is the command
     
  19. Offline

    RoboticPlayer

    Actually, it doesn't. It could be that he didn't register the command in the plugin.yml, but you don't have to have and if statement like this
    Code:
    if (cmd.getName().equalsIgnoreCase(String) {
    //Things
    }
    If you have multiple commands in your plugin, then you do, but otherwise no. Running any command from the plugin will call the onCommand method, and if there is only one command, you have no real need to check for which command it was (since it's a 100% chance that the command being executed is the proper one).
     
  20. Offline

    Notorious_Park

    I have multiple commands, but they are all in different classes. I also have them all registered in the plugin.yml
     
  21. Offline

    RoboticPlayer

    Did you remember to register the executors in your onEnable()?
     
  22. Offline

    Notorious_Park

    From my main class:

    Code:
    package com.notoriouspark.pl.insanotimer;
    
    import com.notoriouspark.pl.insanotimer.commands.StopTimerCommand;
    import com.notoriouspark.pl.insanotimer.commands.TimerCommand;
    import com.notoriouspark.pl.insanotimer.commands.UHCInfoCommand;
    import com.notoriouspark.pl.insanotimer.listeners.InventoryClickListener;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class InSanoTimer extends JavaPlugin {
        FileConfiguration config = this.getConfig();
        private String prefix;
        private boolean running = false;
        private int pvp;
        private int meetup;
    
        public void onEnable() {
    
            this.prefix = ChatColor.getByChar(config.getString("Bracket ID")) + "[ " + ChatColor.getByChar(config.getString("Server ID")) + "InSano" + ChatColor.getByChar(config.getString("Bracket ID")) + "] ";
    
            this.getServer().getPluginManager().registerEvents(new InventoryClickListener(this), this);
    
            this.getCommand("starttimer").setExecutor(new TimerCommand(this));
            this.getCommand("stoptimer").setExecutor(new StopTimerCommand(this));
            this.getCommand("uhcinfo").setExecutor(new UHCInfoCommand(this));
    
        }
     
Thread Status:
Not open for further replies.

Share This Page