2012-12-09 03:42:29 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod;
|
|
|
|
|
2014-04-14 19:11:41 +00:00
|
|
|
import java.util.Arrays;
|
2013-08-24 19:35:09 +00:00
|
|
|
import java.util.List;
|
2014-04-14 19:11:41 +00:00
|
|
|
import java.util.UUID;
|
2012-12-09 03:42:29 +00:00
|
|
|
import java.util.regex.Pattern;
|
2014-04-21 21:09:10 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
|
2014-04-14 19:11:41 +00:00
|
|
|
import net.minecraft.server.v1_7_R3.MinecraftServer;
|
|
|
|
import net.minecraft.server.v1_7_R3.PropertyManager;
|
2012-12-09 03:42:29 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.player.PlayerLoginEvent;
|
2013-12-01 12:33:39 +00:00
|
|
|
import org.bukkit.event.player.PlayerLoginEvent.Result;
|
2012-12-09 03:42:29 +00:00
|
|
|
|
|
|
|
public class TFM_ServerInterface
|
|
|
|
{
|
2014-05-04 22:01:57 +00:00
|
|
|
public static final String COMPILE_NMS_VERSION = "v1_7_R3";
|
2014-04-23 17:30:11 +00:00
|
|
|
public static final Pattern USERNAME_REGEX = Pattern.compile("^[\\w\\d_]{3,20}$");
|
2012-12-09 03:42:29 +00:00
|
|
|
|
|
|
|
public static void setOnlineMode(boolean mode)
|
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
final PropertyManager manager = MinecraftServer.getServer().getPropertyManager();
|
|
|
|
manager.a("online-mode", mode);
|
|
|
|
manager.savePropertiesFile();
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 19:11:41 +00:00
|
|
|
public static int purgeWhitelist()
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
String[] whitelisted = MinecraftServer.getServer().getPlayerList().getWhitelisted();
|
|
|
|
int size = whitelisted.length;
|
|
|
|
for (String player : MinecraftServer.getServer().getPlayerList().getWhitelist().getEntries())
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
MinecraftServer.getServer().getPlayerList().getWhitelist().remove(player);
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
2014-04-17 16:29:08 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MinecraftServer.getServer().getPlayerList().getWhitelist().save();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
TFM_Log.warning("Could not purge the whitelist!");
|
|
|
|
TFM_Log.warning(ex);
|
|
|
|
}
|
2014-04-14 19:11:41 +00:00
|
|
|
return size;
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 19:11:41 +00:00
|
|
|
public static boolean isWhitelisted()
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
return MinecraftServer.getServer().getPlayerList().hasWhitelist;
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 19:11:41 +00:00
|
|
|
public static List<?> getWhitelisted()
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
return Arrays.asList(MinecraftServer.getServer().getPlayerList().getWhitelisted());
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-14 19:11:41 +00:00
|
|
|
public static String getVersion()
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
return MinecraftServer.getServer().getVersion();
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void handlePlayerLogin(PlayerLoginEvent event)
|
|
|
|
{
|
2013-12-01 12:33:39 +00:00
|
|
|
final Server server = TotalFreedomMod.server;
|
2013-08-14 14:01:42 +00:00
|
|
|
final Player player = event.getPlayer();
|
2013-08-25 16:32:01 +00:00
|
|
|
final String username = player.getName();
|
2014-08-02 14:27:43 +00:00
|
|
|
final UUID uuid = TFM_Util.getUniqueId(username);
|
2013-12-01 12:33:39 +00:00
|
|
|
final String ip = event.getAddress().getHostAddress().trim();
|
2012-12-09 03:42:29 +00:00
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
// Perform username checks
|
2014-04-23 17:30:11 +00:00
|
|
|
if (username.length() < 3 || username.length() > 20)
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-23 17:30:11 +00:00
|
|
|
event.disallow(Result.KICK_OTHER, "Your username is an invalid length (must be between 3 and 20 characters long).");
|
2012-12-09 03:42:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-14 19:11:41 +00:00
|
|
|
|
2014-04-23 17:30:11 +00:00
|
|
|
if (!USERNAME_REGEX.matcher(username).find())
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-08-25 11:57:13 +00:00
|
|
|
event.disallow(Result.KICK_OTHER, "Your username contains invalid characters.");
|
2012-12-09 03:42:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
// Check if player is admin
|
|
|
|
// Not safe to use TFM_Util.isSuperAdmin for player logging in because player.getAddress() will return a null until after player login.
|
2014-05-16 13:39:40 +00:00
|
|
|
final boolean isAdmin;
|
2012-12-09 03:42:29 +00:00
|
|
|
if (server.getOnlineMode())
|
|
|
|
{
|
2014-04-21 15:45:32 +00:00
|
|
|
isAdmin = TFM_AdminList.getSuperUUIDs().contains(uuid);
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-21 21:09:10 +00:00
|
|
|
final TFM_Admin admin = TFM_AdminList.getEntryByIp(ip);
|
|
|
|
isAdmin = admin != null && admin.isActivated();
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 12:33:39 +00:00
|
|
|
// Validation below this point
|
2014-08-25 11:57:13 +00:00
|
|
|
if (isAdmin) // Player is superadmin
|
2013-07-02 18:31:22 +00:00
|
|
|
{
|
2014-08-25 11:57:13 +00:00
|
|
|
// force-allow log in
|
2013-12-01 12:33:39 +00:00
|
|
|
event.allow();
|
|
|
|
|
2014-04-14 19:11:41 +00:00
|
|
|
for (Player onlinePlayer : server.getOnlinePlayers())
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
if (onlinePlayer.getName().equalsIgnoreCase(username))
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
onlinePlayer.kickPlayer("An admin just logged in with the username you are using.");
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 18:31:22 +00:00
|
|
|
|
2013-12-01 17:21:43 +00:00
|
|
|
int count = server.getOnlinePlayers().length;
|
|
|
|
if (count >= server.getMaxPlayers())
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
for (Player onlinePlayer : server.getOnlinePlayers())
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
if (!TFM_AdminList.isSuperAdmin(onlinePlayer))
|
2013-12-01 12:33:39 +00:00
|
|
|
{
|
2014-04-14 19:11:41 +00:00
|
|
|
onlinePlayer.kickPlayer("You have been kicked to free up room for an admin.");
|
2013-12-01 17:21:43 +00:00
|
|
|
count--;
|
2013-12-01 12:33:39 +00:00
|
|
|
}
|
|
|
|
|
2013-12-01 17:21:43 +00:00
|
|
|
if (count < server.getMaxPlayers())
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2013-07-11 22:21:23 +00:00
|
|
|
break;
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 18:47:36 +00:00
|
|
|
|
2013-12-01 17:21:43 +00:00
|
|
|
if (count >= server.getMaxPlayers())
|
2013-08-12 18:47:36 +00:00
|
|
|
{
|
2014-08-25 11:57:13 +00:00
|
|
|
event.disallow(Result.KICK_OTHER, "The server is full and a player could not be kicked, sorry!");
|
2013-12-01 12:33:39 +00:00
|
|
|
return;
|
2013-08-12 18:47:36 +00:00
|
|
|
}
|
2014-08-25 11:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Player is not an admin
|
2013-12-01 12:33:39 +00:00
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
// Server full check
|
|
|
|
if (server.getOnlinePlayers().length >= server.getMaxPlayers())
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_FULL, "Sorry, but this server is full.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admin-only mode
|
|
|
|
if (TFM_ConfigEntry.ADMIN_ONLY_MODE.getBoolean())
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER, "Server is temporarily open to admins only.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lockdown mode
|
|
|
|
if (TotalFreedomMod.lockdownEnabled)
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER, "Server is currently in lockdown mode.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Username already logged in
|
|
|
|
for (Player onlinePlayer : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (onlinePlayer.getName().equalsIgnoreCase(username))
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER, "Your username is already logged into this server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whitelist
|
|
|
|
if (isWhitelisted())
|
|
|
|
{
|
|
|
|
if (!getWhitelisted().contains(username.toLowerCase()))
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER, "You are not whitelisted on this server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UUID ban
|
|
|
|
if (TFM_BanManager.isUuidBanned(uuid))
|
|
|
|
{
|
|
|
|
final TFM_Ban ban = TFM_BanManager.getByUuid(uuid);
|
|
|
|
event.disallow(Result.KICK_OTHER, ban.getKickMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IP ban
|
|
|
|
if (TFM_BanManager.isIpBanned(ip))
|
|
|
|
{
|
|
|
|
final TFM_Ban ban = TFM_BanManager.getByIp(ip);
|
|
|
|
event.disallow(Result.KICK_OTHER, ban.getKickMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permbanned IPs
|
|
|
|
for (String testIp : TFM_PermbanList.getPermbannedIps())
|
|
|
|
{
|
|
|
|
if (TFM_Util.fuzzyIpMatch(testIp, ip, 4))
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER,
|
|
|
|
ChatColor.RED + "Your IP address is permanently banned from this server.\n"
|
|
|
|
+ "Release procedures are available at\n"
|
|
|
|
+ ChatColor.GOLD + TFM_ConfigEntry.SERVER_PERMBAN_URL.getString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permbanned usernames
|
|
|
|
for (String testPlayer : TFM_PermbanList.getPermbannedPlayers())
|
|
|
|
{
|
|
|
|
if (testPlayer.equalsIgnoreCase(username))
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER,
|
|
|
|
ChatColor.RED + "Your username is permanently banned from this server.\n"
|
|
|
|
+ "Release procedures are available at\n"
|
|
|
|
+ ChatColor.GOLD + TFM_ConfigEntry.SERVER_PERMBAN_URL.getString());
|
|
|
|
return;
|
|
|
|
}
|
2013-12-01 12:33:39 +00:00
|
|
|
}
|
2013-07-04 16:22:02 +00:00
|
|
|
}
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|