2012-12-09 03:42:29 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod;
|
|
|
|
|
2022-03-21 14:35:31 +00:00
|
|
|
import com.earth2me.essentials.utils.DateUtil;
|
2022-03-20 12:35:43 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.admin.AdminList;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.ban.Ban;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.ban.BanManager;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.ban.PermbanList;
|
2022-03-21 14:29:39 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.bridge.EssentialsBridge;
|
2022-03-20 12:35:43 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
|
2022-03-29 22:01:21 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.discord.bridge.DiscordBridge;
|
2022-03-20 12:35:43 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.player.UUIDManager;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.util.SynchronousUtil;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.util.Utilities;
|
2022-03-29 22:01:21 +00:00
|
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
2022-03-20 12:35:43 +00:00
|
|
|
import net.minecraft.server.v1_8_R3.EntityPlayer;
|
|
|
|
import net.minecraft.server.v1_8_R3.MinecraftServer;
|
|
|
|
import net.minecraft.server.v1_8_R3.PropertyManager;
|
2022-03-21 14:29:39 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2012-12-09 03:42:29 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.entity.Player;
|
2015-05-30 18:51:49 +00:00
|
|
|
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
2012-12-09 03:42:29 +00:00
|
|
|
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
|
|
|
|
2022-03-21 14:35:31 +00:00
|
|
|
import java.lang.management.ManagementFactory;
|
2022-03-20 12:35:43 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import static me.StevenLawson.TotalFreedomMod.listener.PlayerListener.DEFAULT_PORT;
|
|
|
|
|
|
|
|
public class Server
|
2012-12-09 03:42:29 +00:00
|
|
|
{
|
2015-04-26 20:21:26 +00:00
|
|
|
public static final String COMPILE_NMS_VERSION = "v1_8_R2";
|
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();
|
2014-09-24 22:37:12 +00:00
|
|
|
manager.setProperty("online-mode", mode);
|
2014-04-14 19:11:41 +00:00
|
|
|
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;
|
2015-04-26 22:31:54 +00:00
|
|
|
for (EntityPlayer player : MinecraftServer.getServer().getPlayerList().players)
|
|
|
|
{
|
2015-04-26 18:43:37 +00:00
|
|
|
MinecraftServer.getServer().getPlayerList().getWhitelist().remove(player.getProfile());
|
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)
|
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
Log.warning("Could not purge the whitelist!");
|
|
|
|
Log.warning(ex);
|
2014-04-17 16:29:08 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-04-26 18:43:37 +00:00
|
|
|
return MinecraftServer.getServer().getPlayerList().getHasWhitelist();
|
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
|
|
|
}
|
|
|
|
|
2015-05-30 18:51:49 +00:00
|
|
|
public static void handlePlayerPreLogin(AsyncPlayerPreLoginEvent event)
|
|
|
|
{
|
|
|
|
final String ip = event.getAddress().getHostAddress().trim();
|
2022-03-20 12:35:43 +00:00
|
|
|
final boolean isAdmin = AdminList.isSuperAdminSafe(null, ip);
|
2022-03-29 22:01:21 +00:00
|
|
|
TextComponent textComponent = new TextComponent(String.format("%s is attempting to join with IP %s.", event.getName(), Utilities.getFuzzyIp(ip)));
|
|
|
|
textComponent.setColor(ChatColor.GRAY.asBungee());
|
|
|
|
|
|
|
|
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (AdminList.isSuperAdmin(player))
|
|
|
|
{
|
|
|
|
player.spigot().sendMessage(textComponent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscordBridge.transmitAdminMessage(textComponent.getText());
|
2015-05-30 18:51:49 +00:00
|
|
|
|
|
|
|
// Check if the player is already online
|
|
|
|
for (Player onlinePlayer : TotalFreedomMod.server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (!onlinePlayer.getName().equalsIgnoreCase(event.getName()))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username is already logged into this server.");
|
|
|
|
} else {
|
|
|
|
event.allow();
|
2022-03-20 12:35:43 +00:00
|
|
|
SynchronousUtil.playerKick(onlinePlayer, "An admin just logged in with the username you are using.");
|
2015-05-30 18:51:49 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 03:42:29 +00:00
|
|
|
public static void handlePlayerLogin(PlayerLoginEvent event)
|
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
final org.bukkit.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();
|
2013-12-01 12:33:39 +00:00
|
|
|
final String ip = event.getAddress().getHostAddress().trim();
|
2022-03-20 12:35:43 +00:00
|
|
|
final UUID uuid = UUIDManager.newPlayer(player, ip);
|
2012-12-09 03:42:29 +00:00
|
|
|
|
2015-05-30 18:51:49 +00:00
|
|
|
// Check username length
|
2015-02-16 16:00:38 +00:00
|
|
|
if (username.length() < 3 || username.length() > TotalFreedomMod.MAX_USERNAME_LENGTH)
|
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
|
|
|
|
2015-05-30 18:51:49 +00:00
|
|
|
// Check username characters
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-30 18:51:49 +00:00
|
|
|
// Check force-IP match
|
2022-03-20 12:35:43 +00:00
|
|
|
if (ConfigurationEntry.FORCE_IP_ENABLED.getBoolean())
|
2015-05-30 18:51:49 +00:00
|
|
|
{
|
|
|
|
final String hostname = event.getHostname().replace("FML", ""); // Forge fix - https://github.com/TotalFreedom/TotalFreedomMod/issues/493
|
2022-03-20 12:35:43 +00:00
|
|
|
final String connectAddress = ConfigurationEntry.SERVER_ADDRESS.getString();
|
2015-05-30 18:51:49 +00:00
|
|
|
final int connectPort = TotalFreedomMod.server.getPort();
|
|
|
|
|
|
|
|
if (!hostname.equalsIgnoreCase(connectAddress + ":" + connectPort) && !hostname.equalsIgnoreCase(connectAddress + ".:" + connectPort))
|
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
final int forceIpPort = ConfigurationEntry.FORCE_IP_PORT.getInteger();
|
2015-05-30 18:51:49 +00:00
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
|
2022-03-20 12:35:43 +00:00
|
|
|
ConfigurationEntry.FORCE_IP_KICKMSG.getString()
|
|
|
|
.replace("%address%", ConfigurationEntry.SERVER_ADDRESS.getString() + (forceIpPort == DEFAULT_PORT ? "" : ":" + forceIpPort)));
|
2015-05-30 18:51:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
// Check if player is admin
|
2022-03-20 12:35:43 +00:00
|
|
|
// Not safe to use Utilities.isSuperAdmin(player) because player.getAddress() will return a null until after player login.
|
|
|
|
final boolean isAdmin = AdminList.isSuperAdminSafe(uuid, ip);
|
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
|
|
|
{
|
2015-02-16 16:00:38 +00:00
|
|
|
// Force-allow log in
|
2013-12-01 12:33:39 +00:00
|
|
|
event.allow();
|
|
|
|
|
2014-12-21 09:23:50 +00:00
|
|
|
int count = server.getOnlinePlayers().size();
|
2013-12-01 17:21:43 +00:00
|
|
|
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
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
if (!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
|
|
|
}
|
2015-02-16 16:00:38 +00:00
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Player is not an admin
|
|
|
|
// Server full check
|
2014-12-21 09:23:50 +00:00
|
|
|
if (server.getOnlinePlayers().size() >= server.getMaxPlayers())
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_FULL, "Sorry, but this server is full.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Admin-only mode
|
2022-03-20 12:35:43 +00:00
|
|
|
if (ConfigurationEntry.ADMIN_ONLY_MODE.getBoolean())
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whitelist
|
|
|
|
if (isWhitelisted())
|
|
|
|
{
|
|
|
|
if (!getWhitelisted().contains(username.toLowerCase()))
|
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER, "You are not whitelisted on this server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UUID ban
|
2022-03-20 12:35:43 +00:00
|
|
|
if (BanManager.isUuidBanned(uuid))
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
final Ban ban = BanManager.getByUuid(uuid);
|
2014-08-25 11:57:13 +00:00
|
|
|
event.disallow(Result.KICK_OTHER, ban.getKickMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IP ban
|
2022-03-20 12:35:43 +00:00
|
|
|
if (BanManager.isIpBanned(ip))
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
final Ban ban = BanManager.getByIp(ip);
|
2014-08-25 11:57:13 +00:00
|
|
|
event.disallow(Result.KICK_OTHER, ban.getKickMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permbanned IPs
|
2022-03-20 12:35:43 +00:00
|
|
|
for (String testIp : PermbanList.getPermbannedIps())
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
2022-03-20 12:35:43 +00:00
|
|
|
if (Utilities.fuzzyIpMatch(testIp, ip, 4))
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
|
|
|
event.disallow(Result.KICK_OTHER,
|
|
|
|
ChatColor.RED + "Your IP address is permanently banned from this server.\n"
|
|
|
|
+ "Release procedures are available at\n"
|
2022-03-20 12:35:43 +00:00
|
|
|
+ ChatColor.GOLD + ConfigurationEntry.SERVER_PERMBAN_URL.getString());
|
2014-08-25 11:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permbanned usernames
|
2022-03-20 12:35:43 +00:00
|
|
|
for (String testPlayer : PermbanList.getPermbannedPlayers())
|
2014-08-25 11:57:13 +00:00
|
|
|
{
|
|
|
|
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"
|
2022-03-20 12:35:43 +00:00
|
|
|
+ ChatColor.GOLD + ConfigurationEntry.SERVER_PERMBAN_URL.getString());
|
2014-08-25 11:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-01 12:33:39 +00:00
|
|
|
}
|
2013-07-04 16:22:02 +00:00
|
|
|
}
|
2022-03-21 14:29:39 +00:00
|
|
|
|
2022-03-21 14:35:31 +00:00
|
|
|
public static double getTPS() {
|
2022-03-21 14:29:39 +00:00
|
|
|
return EssentialsBridge.getEssentialsPlugin().getTimer().getAverageTPS();
|
|
|
|
}
|
2022-03-21 14:35:31 +00:00
|
|
|
|
|
|
|
public static double getMaxMem() {
|
|
|
|
return Runtime.getRuntime().maxMemory() / 1024f / 1024f;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double getTotalMem() {
|
|
|
|
return Runtime.getRuntime().totalMemory() / 1024f / 1024f;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double getFreeMem() {
|
|
|
|
return Runtime.getRuntime().freeMemory() / 1024f / 1024f;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getUptime() {
|
|
|
|
return DateUtil.formatDateDiff(ManagementFactory.getRuntimeMXBean().getStartTime());
|
|
|
|
}
|
2012-12-09 03:42:29 +00:00
|
|
|
}
|