[SOLVED] Setting ban message

Discussion in 'Spigot Plugin Development' started by Banjo226, May 1, 2015.

  1. Hi, I've made my ban command, and I want to set an actual ban message. Like right now when the person is banned it sets the ban message as: "You are banned from this server! Reason: Banned by an operator."

    I want it to set the ban message as the first argument.

    Here is my code if needed.
    Code (Java):
            if (cmd.getName().equalsIgnoreCase("ban")) {
                if (sender.hasPermission("altess.ban")) {
                    if (args.length == 0) {
                        sender.sendMessage(ChatColor.DARK_RED + "» " + ChatColor.RED + "Please specify a player!");
                        return true;
                    }
                    if (args.length == 1) {
                        sender.sendMessage(ChatColor.DARK_RED + "» " + ChatColor.RED + "Please provide a reason!");
                    }
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    OfflinePlayer offline = sender.getServer().getOfflinePlayer(args[0]);
                    if (target == null) {
                        offline.setBanned(true);
                        Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "LAW" + ChatColor.DARK_GRAY + "] " + ChatColor.GOLD + offline.getName() + " was banned by " + ChatColor.RED + sender.getName() + ChatColor.GOLD+ " because " + args[1]);
                        return true;
                    }
                    if (target.isOp()) {
                        sender.sendMessage(ChatColor.DARK_RED + "» " + ChatColor.RED + "You can't ban ops!");
                        return true;
                    }
                    target.kickPlayer(ChatColor.DARK_RED + "» " + ChatColor.RED + "You have been banned because " + args[1] + "!");
                    target.setBanned(true);
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "LAW" + ChatColor.DARK_GRAY + "] " + ChatColor.GOLD + target.getName() + " was banned by " + ChatColor.RED + sender.getName() + ChatColor.GOLD+ " because " + args[1]);
                }
            }
    I know you can do
    Code (Java):

        @EventHandler
        public void playerBanLoginEvent(PlayerLoginEvent e) {
            Result r = e.getResult();
           
            if(r == Result.KICK_BANNED) {
                e.setKickMessage("§4Banned because why not... so on");
            }

        }
    But, I want to set the ban message as the first argument in /ban.
     
    #1 Banjo226, May 1, 2015
    Last edited: May 1, 2015
    • Agree Agree x 1
  2. Save the name & reason in a config, on join if in config kick with reason
     
  3. debug it.
    since you're kicking the player and than banning them, some part of it may not be getting called.

    or, instead of banning the player on the spot, you could simply kick them with your custom message, then add the player to a hashmap/arraylist and listen on PlayerLoginEvent, check if the player joining is inside of the banned hashmap/arraylist, and if so then:
    Code (Text):
    event.disallow(PlayerLoginEvent.Result KICK_BANNED, "BAN MESSAGE HERE");
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerLoginEvent.html#disallow(org.bukkit.event.player.PlayerLoginEvent.Result, java.lang.String)
     
  4. Remember to save the list to a file