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.
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.
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.
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 if (!getDataFolder().exists()) { getDataFolder().mkdir();} File bannedPlayersFile = new File(getDataFolder() + File.separator + "BannedPlayers");if (!bannedPlayersFile.exists()) { try { bannedPlayersFile.createNewFile(); } catch (IOException ex) { ex.printStackTrace(); }} Then, somewhere in your plugin add this method which you will use to ban players (I'd make it static): Code:java public void banPlayer24H(Player player) { long timeBanEnd = System.currentTimeMillis() + (1000 * 60 * 60 * 24); File bannedPlayersFile = new File(getDataFolder() + File.separator + "BannedPlayers"); if (bannedPlayersFile.exists()) { YamlConfiguration bannedPlayersConfig = YamlConfiguration.loadConfiguration(bannedPlayersFile); bannedPlayersConfig.set(player.getName(), timeBanEnd); player.kickPlayer(ChatColor.RED + "Banned for 24 hours!"); try { bannedPlayersConfig.save(bannedPlayersFile); } catch (IOException ex) { ex.printStackTrace(); } }} Last but not least insert this piece of code into your listener class: Code:java @EventHandler (priority = EventPriority.NORMAL)public void onLogin (PlayerLoginEvent event) { Player player = event.getPlayer(); File bannedPlayersFile = new File(getDataFolder() + File.separator + "BannedPlayers"); if (bannedPlayersFile.exists()) { YamlConfiguration bannedPlayersConfig = YamlConfiguration.loadConfiguration(bannedPlayersFile); if (bannedPlayersConfig.contains(player.getName())) { long canLogin = bannedPlayersConfig.getLong(player.getName()); if (canLogin < System.currentTimeMillis()) { event.disallow(Result.KICK_BANNED, "You were banned for 24 hours!"); } else { event.allow(); bannedPlayersConfig.set(player.getName(), null); try { bannedPlayersConfig.save(bannedPlayersFile); } catch (IOException ex) { ex.printStackTrace(); } } } }} 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!