Ban Players

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

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

    liam923

    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

    xTrollxDudex

    liam923
    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

    liam923

    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 :)
    BanStuff
    Add to list
    long a = System.getTimeMillis();
    long b = a + (1000*60*60*24);
    for(a >= b){
    System.getTimeMillis();
    }

    Enjoy,
    JHG0

    JHG0
    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

    liam923

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

    Tirelessly

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

    Quantix

    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:
    Code:java
    1. if (!getDataFolder().exists()) {
    2. getDataFolder().mkdir();
    3. }
    4.  
    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):
    Code:java
    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 {
    9. bannedPlayersConfig.save(bannedPlayersFile);
    10. } catch (IOException ex) {
    11. ex.printStackTrace();
    12. }
    13. }
    14. }


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

    Code:java
    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 {
    15. bannedPlayersConfig.save(bannedPlayersFile);
    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

    xTrollxDudex

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

    Tirelessly

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

    xTrollxDudex

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

    Tirelessly

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

Share This Page