Ban Players

Discussion in 'Plugin Development' started by liam923, Aug 15, 2013.

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


    How do you ban players for a certain amount of time instead of forever? I would like for it to automatically unban them after 24 hours.
  2. Offline


    You would probably have to implement your own system, like adding the player to a list when you do a specific command, start a scheduler to remove them after a certain time and kicking them with the You have been banned message. Listen for player join event and check if they are on the list, if so, kick them again.
  3. Offline


    Thanks for the idea. I'll try it.
  4. Offline

    Deleted user

    Or my method (warning will crash server and/or computer and/or create MASSIVE LAG :)
    Add to list
    long a = System.getTimeMillis();
    long b = a + (1000*60*60*24);
    for(a >= b){


    I'm gonna try this on a host and tell them "Now how powerful are your computers? >:-D". Or maybe just do Thread.sleep(Integer.MAX_VALUE);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Nov 25, 2021
  5. Offline


    I figured out how to, but I did a different method.
  6. Offline


    The first one wouldn't run through the loop at all and the second wouldn't use up much memory
  7. Offline


    I would save the player names and times when they can join again in a file instead of just in a list in the plugin. If you make a file and permanently save the players names etc. you can stop/reload your server without loosing all the data. It would also not require any delayed tasks which will also get canceled if the server stops or is reloaded.

    This will require making a file where you save the player name and time and listening to the PlayerLoginEvent as xTrollXDudeX mentioned. I'll give it a try:

    in your onEnable() method in your main plugin class add:
    1. if (!getDataFolder().exists()) {
    2. getDataFolder().mkdir();
    3. }
    5. File bannedPlayersFile = new File(getDataFolder() + File.separator + "BannedPlayers");
    6. if (!bannedPlayersFile.exists()) {
    7. try {
    8. bannedPlayersFile.createNewFile();
    9. } catch (IOException ex) {
    10. ex.printStackTrace();
    11. }
    12. }

    Then, somewhere in your plugin add this method which you will use to ban players (I'd make it static):
    1. public void banPlayer24H(Player player) {
    2. long timeBanEnd = System.currentTimeMillis() + (1000 * 60 * 60 * 24);
    3. File bannedPlayersFile = new File(getDataFolder() + File.separator + "BannedPlayers");
    4. if (bannedPlayersFile.exists()) {
    5. YamlConfiguration bannedPlayersConfig = YamlConfiguration.loadConfiguration(bannedPlayersFile);
    6. bannedPlayersConfig.set(player.getName(), timeBanEnd);
    7. player.kickPlayer(ChatColor.RED + "Banned for 24 hours!");
    8. try {
    10. } catch (IOException ex) {
    11. ex.printStackTrace();
    12. }
    13. }
    14. }

    Last but not least insert this piece of code into your listener class:

    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onLogin (PlayerLoginEvent event) {
    3. Player player = event.getPlayer();
    4. File bannedPlayersFile = new File(getDataFolder() + File.separator + "BannedPlayers");
    5. if (bannedPlayersFile.exists()) {
    6. YamlConfiguration bannedPlayersConfig = YamlConfiguration.loadConfiguration(bannedPlayersFile);
    7. if (bannedPlayersConfig.contains(player.getName())) {
    8. long canLogin = bannedPlayersConfig.getLong(player.getName());
    9. if (canLogin < System.currentTimeMillis()) {
    10. event.disallow(Result.KICK_BANNED, "You were banned for 24 hours!");
    11. } else {
    12. event.allow();
    13. bannedPlayersConfig.set(player.getName(), null);
    14. try {
    16. } catch (IOException ex) {
    17. ex.printStackTrace();
    18. }
    19. }
    20. }
    21. }
    22. }

    I wrote this in a hurry so I can't guarantee this will work and I haven't tested it yet but it should work! :)
  8. Offline


    Thread.sleep isn't supposed to use up memory
  9. Offline


    Which is why your comment doesn't make sense
  10. Offline


    Thread.sleep shuts down the main thread? Freezes the server?
  11. Offline


    Has nothing to do with the power of the computer... Oh well, who cares
Thread Status:
Not open for further replies.

Share This Page