Maintenance Mode Command

Discussion in 'Plugin Development' started by CodePlaysMinecraft, Jan 19, 2015.

Thread Status:
Not open for further replies.
  1. Hi. Recently for a custom hub plugin I'm working on, I made a "Maintenance Mode" command and it works fine but, I get an error when logging in when maintenance mode is set to true instead of the regular message.

    Code:
    Code:
    package me.CodePlaysMC.TheRealmHub.Commands;
    
    import me.CodePlaysMC.TheRealmHub.Utilities.ChatUtilities;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class Maintenance implements CommandExecutor, Listener {
    
        boolean mMode;
    
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("mm")) {
                    if (player.hasPermission("therealm.mm.perform")) {
                        if (args.length == 0) {
                            player.sendMessage(ChatColor.RED + "/mm <boolean>");
                        } else {
                            if (args[0].equalsIgnoreCase("true")) {
                                mMode = true;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to true.");
                                return true;
                            } else if (args[0].equalsIgnoreCase("false")) {
                                mMode = false;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to false.");
                                return true;
                            }
                        }
                    } else {
                        ChatUtilities
                                .sendMessage(player,
                                        "You do not have permission to perform this command.");
                    }
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You must be an ingame player to perform this command.");
            }
            return true;
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            if (mMode = false) {
                return;
            } else if (mMode = true) {
                for (Player onlinePlayers : Bukkit.getOnlinePlayers()) {
                    if (onlinePlayers.hasPermission("therealm.mm.join")
                            || onlinePlayers.isWhitelisted()) {
                        return;
                    } else {
                        onlinePlayers.kickPlayer(ChatColor.DARK_BLUE + ""
                                + ChatColor.BOLD + "The Realm" + ChatColor.GRAY
                                + " >> " + ChatColor.WHITE + "In " + ChatColor.AQUA
                                + "Maintenance Mode" + ChatColor.WHITE
                                + " right now. Check back later!");
                    }
                }
            }
        }
    }
    Regular kick message: http://gyazo.com/6c2bc5872ccb502c4d62a5b39e1e9e3c
    Error message: http://gyazo.com/48206973873202252a6a91950491c791

    Any help is very much appreciated. :)

    EDIT: Regular kick message appears, but occasionally.
     
  2. Offline

    ShadowLAX

  3. @ShadowLAX Used PlayerLoginEvent and added a disallow() method and now it isn't working. :/

    Code:
    Code:
    package me.CodePlaysMC.TheRealmHub.Commands;
    
    import me.CodePlaysMC.TheRealmHub.Utilities.ChatUtilities;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    
    public class Maintenance implements CommandExecutor, Listener {
    
        boolean mMode;
    
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("mm")) {
                    if (player.hasPermission("therealm.mm.perform")) {
                        if (args.length == 0) {
                            player.sendMessage(ChatColor.RED + "/mm <boolean>");
                        } else {
                            if (args[0].equalsIgnoreCase("true")) {
                                mMode = true;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to true.");
                                return true;
                            } else if (args[0].equalsIgnoreCase("false")) {
                                mMode = false;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to false.");
                                return true;
                            }
                        }
                    } else {
                        ChatUtilities
                                .sendMessage(player,
                                        "You do not have permission to perform this command.");
                    }
                }
            } else {
                sender.sendMessage(ChatColor.RED
                        + "You must be an ingame player to perform this command.");
            }
            return true;
        }
    
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            if (mMode = false) {
                return;
            } else if (mMode = true) {
                if (player.hasPermission("therealm.mm.join")
                        || player.isWhitelisted()) {
                    return;
                } else {
                    disallow(player);
                }
            }
        }
    
        public void disallow(Player player) {
            player.kickPlayer(ChatColor.DARK_BLUE + "" + ChatColor.BOLD
                    + "The Realm" + ChatColor.GRAY + " >> " + ChatColor.WHITE
                    + "In " + ChatColor.AQUA + "Maintenance Mode" + ChatColor.WHITE
                    + " right now. Check back later!");
        }
    }
     
  4. Offline

    1Rogue

  5. @1Rogue *facedesk* Why didn't I think of that. When I do use it though I get a couple of errors. One under event.getPlayer(), "The method getPlayer() is undefined for the type PlayerPreLoginEvent" and a deprecation on the event itself.
     
  6. Offline

    ShadowLAX

    @CodePlaysMinecraft I didn't say to create a disallow method, I said to use the event's disallow method. That's why it doesn't work.
     
  7. Offline

    1Rogue

    Aha, per the javadocs there's an async version: http://jd.bukkit.org/dev/apidocs/org/bukkit/event/player/PlayerPreLoginEvent.html

    Additionally there won't be a player object, the event will provide any information you need such as UUIDs/names
     
  8. @ShadowLAX Sorry, misunderstood it. I fixed it. :)
    @1Rogue I need to get the players permissions and check if they have a specific permission and I don't see that listed, is there another way I could do this? I saw event.getName() but that just returns the player's name. I also tried to implement Player into the method but got an error in the console saying that my plugin was trying to register an invalid EventHandler.
     
    Last edited: Jan 19, 2015
  9. Offline

    1Rogue

    If you need to check permissions then you'll need the player object from a PlayerEvent to avoid using something like Vault. In that case it would be appropriate to use PlayerLoginEvent.
     
  10. Okay, switched it up again, and... Still not working. :/
    Code:
    package me.CodePlaysMC.TheRealmHub.Commands;
    
    import me.CodePlaysMC.TheRealmHub.Utilities.ChatUtilities;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    
    public class Maintenance implements CommandExecutor, Listener {
    
        boolean mMode;
    
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("mm")) {
                    if (player.hasPermission("therealm.mm.perform")) {
                        if (args.length == 0) {
                            player.sendMessage(ChatColor.RED + "/mm <boolean>");
                        } else {
                            if (args[0].equalsIgnoreCase("true")) {
                                mMode = true;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to true.");
                                return true;
                            } else if (args[0].equalsIgnoreCase("false")) {
                                mMode = false;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to false.");
                                return true;
                            }
                        }
                    } else {
                        ChatUtilities
                                .sendMessage(player,
                                        "You do not have permission to perform this command.");
                    }
                }
            } else {
                sender.sendMessage(ChatColor.RED
                        + "You must be an ingame player to perform this command.");
            }
            return true;
        }
    
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            if (mMode == false) {
                return;
            } else if (mMode == true) {
                if (player.hasPermission("therealm.mm.join")
                        || player.isWhitelisted()) {
                    return;
                } else {
                    event.disallow(org.bukkit.event.player.PlayerLoginEvent.Result.KICK_OTHER, ChatColor.DARK_BLUE + "" + ChatColor.BOLD
                            + "The Realm" + ChatColor.GRAY + " >> " + ChatColor.WHITE
                            + "In " + ChatColor.AQUA + "Maintenance Mode" + ChatColor.WHITE
                            + " right now. Check back later!");
                }
            }
        }
    }
     
  11. Offline

    ShadowLAX

    @CodePlaysMinecraft It works fine for me, in order for it to work you have to not be opped and not whitelisted.
     
    1Rogue likes this.
  12. Hm... Weird. I'll try and look over everything again, and I'll get back to you later.
    @ShadowLAX @1Rogue
     
  13. @ShadowLAX @1Rogue Yeah, still not working. I tried redoing the entire class, and still came up empty handed. Maybe it's the way I registered the event and command? Let me give you my main and plugin.yml and see if there's something odd about it.

    Main:
    Code:
    package me.CodePlaysMC.TheRealmHub;
    
    import me.CodePlaysMC.TheRealmHub.Commands.GUI;
    import me.CodePlaysMC.TheRealmHub.Commands.Maintenance;
    import me.CodePlaysMC.TheRealmHub.Inventory.InventoryClick;
    import me.CodePlaysMC.TheRealmHub.Inventory.OpenGUI;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerItemDrop;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerItemPickup;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerJoin;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerQuit;
    import me.CodePlaysMC.TheRealmHub.Scoreboard.Scoreboard;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TheRealmHub extends JavaPlugin {
     
        public void onEnable() {
            PluginManager plManager = Bukkit.getServer().getPluginManager();
         
            plManager.registerEvents(new Scoreboard(this), this);
            plManager.registerEvents(new PlayerJoin(this), this);
            plManager.registerEvents(new PlayerQuit(this), this);
            plManager.registerEvents(new PlayerItemDrop(this), this);
            plManager.registerEvents(new PlayerItemPickup(this), this);
            plManager.registerEvents(new InventoryClick(this), this);
            plManager.registerEvents(new OpenGUI(this), this);
            plManager.registerEvents(new Maintenance(), this);
         
            getCommand("gui").setExecutor(new GUI());
            getCommand("mm").setExecutor(new Maintenance());
         
            getLogger().info("TheRealmHub has been enabled.");
        }
    
        public void onDisable() {
            getLogger().info("TheRealmHub has been disabled.");
        }
    }
    Plugin.yml:
    Code:
    name: TheRealmHub
    main: me.CodePlaysMC.TheRealmHub.TheRealmHub
    version: 2.0.5 BETA
    author: CodePlaysMC
    description: TheRealm Hub Essentials
    commands:
      GUI:
        description: Basic GUI commands.
        usage: /<command>
        aliases: []
        permission:
      GUI help:
        description: Shows all GUI commands.
        usage: /<command> help
        aliases: []
        permission:
      GUI add:
        description: Adds a GUI to the player's inventory.
        usage: /<command> add
        aliases: []
        permission:
      GUI remove:
        description: Remove a GUI from the player's inventory.
        usage: /<command> remove
        aliases: []
        permission:
      GUI info:
        description: Lists the info about the plugin.
        usage: /<command> info
        aliases: []
        permission:
      MM:
        description: Puts the server in maitenence mode.
        usage: /<command> <boolean>
        aliases: [maintenance, maintenancemode]
        permission: therealm.mm.perform
    Ignore all of the "GUI" stuff, that's different. I test the command by being op, and doing "/mm true" and then deopping myself and logging off the server. When I log on I'm still able to join. Note that I was never whitelisted.

    Bump. :)

    Bump. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  14. Offline

    1Rogue

    You're registering two separate Maintenance classes. Either make the mMode variable static or use a single instance for your registering it as a Listener and CommandExecutor (better).
     
  15. Okay, so now I made the variable static and make a different class for PlayerLoginEvent, and now event.getPlayer() is giving me the error, "The method getPlayer() is undefined for the event PlayerLoginEvent". How would I make it so I can implement player? I checked the JavaDocs(http://jd.bukkit.org/rb/apidocs/org/bukkit/event/player/PlayerLoginEvent.html), and I didn't seen anything.

    PlayerLoginEvent.java:
    Code:
    package me.CodePlaysMC.TheRealmHub.Player;
    
    import me.CodePlaysMC.TheRealmHub.PluginListener;
    import me.CodePlaysMC.TheRealmHub.TheRealmHub;
    import me.CodePlaysMC.TheRealmHub.Commands.Maintenance;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    
    public class PlayerLoginEvent extends PluginListener implements Listener {
    
        public PlayerLoginEvent(TheRealmHub pl) {
            super(pl);
        }
    
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            if (Maintenance.mMode == false) {
                return;
            } else if (Maintenance.mMode == true) {
                if (player.hasPermission("therealm.mm.join")
                        || player.isWhitelisted()) {
                    return;
                } else {
                    player.kickPlayer(ChatColor.DARK_BLUE + "" + ChatColor.BOLD
                            + "The Realm" + ChatColor.GRAY + " >> "
                            + ChatColor.WHITE + "In " + ChatColor.AQUA
                            + "Maintenance Mode" + ChatColor.WHITE
                            + " right now. Check back later!");
                }
            }
        }
    }
     
  16. Offline

    1Rogue

    You need to use the fully declared name for the class: org.bukkit.event.player.PlayerLoginEvent, or rename your listener.
     
  17. @1Rogue Fixed it, but it just doesn't want to work the plugin.yml is fine, the class looks fine, I registered the events, I'm not getting any errors, it's the correct event, I'm not getting any stack traces, I'm deopped and not whitelisted when I join, I removed all of my permissions, and it still won't work.
    PlayerLogin.java:
    Code:
    package me.CodePlaysMC.TheRealmHub.Player;
    
    import me.CodePlaysMC.TheRealmHub.PluginListener;
    import me.CodePlaysMC.TheRealmHub.TheRealmHub;
    import me.CodePlaysMC.TheRealmHub.Commands.Maintenance;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    
    public class PlayerLogin extends PluginListener implements Listener {
    
        public PlayerLogin(TheRealmHub pl) {
            super(pl);
        }
    
        @EventHandler
        public void onPlayerLogin(PlayerLoginEvent event) {
            Player player = event.getPlayer();
            if (Maintenance.mMode == false) {
                return;
            } else if (Maintenance.mMode == true) {
                if (player.hasPermission("therealm.mm.join")
                        || player.isWhitelisted()) {
                    return;
                } else {
                    player.kickPlayer(ChatColor.DARK_BLUE + "" + ChatColor.BOLD
                            + "The Realm" + ChatColor.GRAY + " >> "
                            + ChatColor.WHITE + "In " + ChatColor.AQUA
                            + "Maintenance Mode" + ChatColor.WHITE
                            + " right now. Check back later!");
                }
            }
        }
    }
    Maintenance.java:
    Code:
    package me.CodePlaysMC.TheRealmHub.Commands;
    
    import me.CodePlaysMC.TheRealmHub.Utilities.ChatUtilities;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Maintenance implements CommandExecutor {
    
        public static boolean mMode;
    
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("mm")) {
                    if (player.hasPermission("therealm.mm.perform")) {
                        if (args.length == 0) {
                            player.sendMessage(ChatColor.RED + "/mm <boolean>");
                        } else {
                            if (args[0].equalsIgnoreCase("true")) {
                                mMode = true;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to true.");
                                return true;
                            } else if (args[0].equalsIgnoreCase("false")) {
                                mMode = false;
                                ChatUtilities.sendMessage(player,
                                        "Maintenance mode set to false.");
                                return true;
                            }
                        }
                    } else {
                        ChatUtilities
                                .sendMessage(player,
                                        "You do not have permission to perform this command.");
                    }
                }
            } else {
                sender.sendMessage(ChatColor.RED
                        + "You must be an ingame player to perform this command.");
            }
            return true;
        }
    }
    TheRealmHub.java:
    Code:
    package me.CodePlaysMC.TheRealmHub;
    
    import me.CodePlaysMC.TheRealmHub.Commands.GUI;
    import me.CodePlaysMC.TheRealmHub.Commands.Maintenance;
    import me.CodePlaysMC.TheRealmHub.Inventory.InventoryClick;
    import me.CodePlaysMC.TheRealmHub.Inventory.OpenGUI;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerItemDrop;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerItemPickup;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerJoin;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerLogin;
    import me.CodePlaysMC.TheRealmHub.Player.PlayerQuit;
    import me.CodePlaysMC.TheRealmHub.Scoreboard.Scoreboard;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TheRealmHub extends JavaPlugin {
       
        public void onEnable() {
            PluginManager plManager = Bukkit.getServer().getPluginManager();
           
            plManager.registerEvents(new Scoreboard(this), this);
            plManager.registerEvents(new PlayerLogin(this), this);
            plManager.registerEvents(new PlayerJoin(this), this);
            plManager.registerEvents(new PlayerQuit(this), this);
            plManager.registerEvents(new PlayerItemDrop(this), this);
            plManager.registerEvents(new PlayerItemPickup(this), this);
            plManager.registerEvents(new InventoryClick(this), this);
            plManager.registerEvents(new OpenGUI(this), this);
           
            getCommand("gui").setExecutor(new GUI());
            getCommand("mm").setExecutor(new Maintenance());
           
            getLogger().info("TheRealmHub has been enabled.");
        }
    
        public void onDisable() {
            getLogger().info("TheRealmHub has been disabled.");
        }
    }
    This is literally my reaction --> :mad:
    Anyway, if you still care, thanks.
     
  18. Offline

    1Rogue

    Don't use a public static variable, if anything make a boolean toggle in your main class, and as a note you can have more than one method in a listener. Additionally why are you comparing a boolean to another boolean rather than just using the one supplied?
     
Thread Status:
Not open for further replies.

Share This Page