package net.simpvp.Misc;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.entity.Player;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class NoNewPlayers implements Listener,CommandExecutor {

	/* How many hours a player must have played to be allowed to log in,
	 * a negative value means anybody may log in, 0 means somebody who has
	 * logged in at least once for any amount of time may log in. */
	private int hours_required = -1;

	private String kick_msg = ChatColor.RED + "You have reached SimplicityPvP\n\n"
		+ "We're sorry, the server is currently being swarmed by alts, and therefore new players are temporarily restricted from joining the server.\n\n"
		+ "Please try again later.";

	@EventHandler(priority = EventPriority.HIGH, ignoreCancelled=false)
	public void onPlayerLogin(PlayerLoginEvent event) {

		if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
			return;
		}

		if (hours_required < 0) {
			return;
		}

		/* Read
		 * https://www.spigotmc.org/threads/how-to-use-hasplayedbefore-in-a-playerloginevent.355496/
		 * before making changes to this. */
		Player player = event.getPlayer();
		OfflinePlayer offplayer = Misc.instance.getServer().getOfflinePlayer(player.getUniqueId());

		/* OPs can join unconditionally */
		if (player.isOp()) {
			return;
		}

		/* Whitelisted players can join unconditionally */
		if (player.isWhitelisted()) {
			return;
		}

		int hours = -1;
		if (offplayer.hasPlayedBefore()) {
			int played_ticks = player.getStatistic(Statistic.PLAY_ONE_MINUTE);
			hours = played_ticks / (20 * 60 * 60);
		}

		if (hours < hours_required) {
			event.disallow(PlayerLoginEvent.Result.KICK_OTHER, kick_msg);
			String m = String.format("NoNewPlayers blocking %s. %d < %d.", player.getName(), hours, hours_required);
			Misc.instance.getLogger().info(m);
		}
	}

	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		Player player = null;
		if (sender instanceof Player) {
			player = (Player) sender;
			if (!player.isOp()) {
				player.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
				return true;
			}
		}

		if (args.length == 0) {
			sender.sendMessage("/nonewplayers will prevent players who have less than the desired hours played from being able to join the server. This is used if the server is being raided by someone with many alts, by preventing non-regular players from being able to join. To enable nonewplayers, use /nonewplayers <hours> where hours is the minimum hours somebody must have played to be able to join. Set it to a negative number to allow anybody to join. It is currently set to " + hours_required);
			return true;
		}

		try {
			hours_required = Integer.parseInt(args[0]);
		} catch (Exception e) {
			sender.sendMessage("Invalid integer " + args[0]);
			return true;
		}

		String m = "/nonewplayers hours required set to " + hours_required;
		sender.sendMessage(m);
		Misc.instance.getLogger().info(m);

		return true;
	}

}


@@ -19,16 +19,72 @@
 .Misc;%0A%0A
+import org.bukkit.configuration.file.FileConfiguration;%0A
 import o
@@ -109,24 +109,24 @@
 entHandler;%0A
-
 import org.b
@@ -779,21 +779,16 @@
 required
- = -1
 ;%0A%0A%09priv
@@ -1042,16 +1042,297 @@
 ter.%22;%0A%0A
+%09public NoNewPlayers() %7B%0A%09%09FileConfiguration config = Misc.instance.getConfig();%0A%09%09config.addDefault(%22NoNewPlayers%22, -1);%0A%09%09hours_required = config.getInt(%22NoNewPlayers%22);%0A%09%09Misc.instance.saveConfig();%0A%09%09Misc.instance.getLogger().info(%22NoNewPlayers set to %22 + hours_required);%0A%09%7D%0A%0A
 %09@EventH
@@ -2656,92 +2656,224 @@
 %09%09%09%09
-player.sendMessage(ChatColor.RED + %22You do not have permission to use this command.%22
+String msg = (hours_required %3C 0) ?%0A%09%09%09%09%09%09%22Not currently blocking players from joining%22 :%0A%09%09%09%09%09%09%22Currently blocking players with less than %22 + hours_required + %22 hours played from joining%22;%0A%09%09%09%09player.sendMessage(msg
 );%0A%09
@@ -3589,21 +3589,117 @@
 n true;%0A
-
 %09%09%7D%0A%0A
+%09%09Misc.instance.getConfig().set(%22NoNewPlayers%22, hours_required);%0A%09%09Misc.instance.saveConfig();%0A%0A
 %09%09String

Save NoNewPlayers hours