2015-11-15 23:32:04 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
2020-08-31 00:13:46 +00:00
|
|
|
import io.papermc.lib.PaperLib;
|
2020-03-10 01:58:45 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-04-15 13:09:23 +00:00
|
|
|
import java.util.UUID;
|
2015-11-15 23:32:04 +00:00
|
|
|
import java.util.regex.Pattern;
|
2022-04-15 13:09:23 +00:00
|
|
|
|
|
|
|
import me.totalfreedom.totalfreedommod.admin.Admin;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
2018-07-31 05:58:50 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.player.FPlayer;
|
2020-06-30 07:25:38 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.player.PlayerData;
|
2020-01-25 06:27:16 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FSync;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import org.bukkit.ChatColor;
|
2019-11-16 21:16:38 +00:00
|
|
|
import org.bukkit.Location;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
|
|
import org.bukkit.event.player.PlayerLoginEvent;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2021-08-10 10:06:41 +00:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2015-11-15 23:32:04 +00:00
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class LoginProcess extends FreedomService
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
public static final int DEFAULT_PORT = 25565;
|
2016-03-02 19:28:01 +00:00
|
|
|
public static final int MIN_USERNAME_LENGTH = 2;
|
|
|
|
public static final int MAX_USERNAME_LENGTH = 20;
|
2015-11-15 23:32:04 +00:00
|
|
|
public static final Pattern USERNAME_REGEX = Pattern.compile("^[\\w\\d_]{3,20}$");
|
2020-12-25 19:46:43 +00:00
|
|
|
private static boolean lockdownEnabled = false;
|
2020-03-10 01:58:45 +00:00
|
|
|
public List<String> TELEPORT_ON_JOIN = new ArrayList<>();
|
|
|
|
public List<String> CLEAR_ON_JOIN = new ArrayList<>();
|
2021-02-25 21:42:30 +00:00
|
|
|
public List<String> CLOWNFISH_TOGGLE = new ArrayList<>();
|
2020-07-22 21:40:58 +00:00
|
|
|
|
2020-12-25 19:46:43 +00:00
|
|
|
public static boolean isLockdownEnabled()
|
|
|
|
{
|
|
|
|
return lockdownEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setLockdownEnabled(boolean lockdownEnabled)
|
|
|
|
{
|
|
|
|
LoginProcess.lockdownEnabled = lockdownEnabled;
|
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStart()
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-07-01 01:51:06 +00:00
|
|
|
public void onStop()
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Banning and Permban checks are their respective services
|
|
|
|
*/
|
|
|
|
@EventHandler(priority = EventPriority.NORMAL)
|
|
|
|
public void onPlayerPreLogin(AsyncPlayerPreLoginEvent event)
|
|
|
|
{
|
2022-04-15 13:09:23 +00:00
|
|
|
final Admin entry = plugin.al.getEntryByUuid(event.getUniqueId());
|
|
|
|
final boolean isAdmin = entry != null && entry.isActive();
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
// Check if the player is already online
|
|
|
|
for (Player onlinePlayer : server.getOnlinePlayers())
|
|
|
|
{
|
2022-04-15 13:09:23 +00:00
|
|
|
if (!onlinePlayer.getUniqueId().equals(event.getUniqueId()))
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
if (isAdmin)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
event.allow();
|
2020-12-04 00:28:53 +00:00
|
|
|
FSync.playerKick(onlinePlayer, "An admin just logged in with the username you are using.");
|
2015-11-15 23:32:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username is already logged into this server.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
|
|
|
public void onPlayerLogin(PlayerLoginEvent event)
|
|
|
|
{
|
|
|
|
final Player player = event.getPlayer();
|
|
|
|
final String username = player.getName();
|
2022-04-15 13:09:23 +00:00
|
|
|
final UUID uuid = player.getUniqueId();
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
// Check username length
|
2016-03-02 19:28:01 +00:00
|
|
|
if (username.length() < MIN_USERNAME_LENGTH || username.length() > MAX_USERNAME_LENGTH)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Your username is an invalid length (must be between 3 and 20 characters long).");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check username characters
|
|
|
|
if (!USERNAME_REGEX.matcher(username).find())
|
|
|
|
{
|
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Your username contains invalid characters.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check force-IP match
|
|
|
|
if (ConfigEntry.FORCE_IP_ENABLED.getBoolean())
|
|
|
|
{
|
|
|
|
final String hostname = event.getHostname().replace("\u0000FML\u0000", ""); // Forge fix - https://github.com/TotalFreedom/TotalFreedomMod/issues/493
|
|
|
|
final String connectAddress = ConfigEntry.SERVER_ADDRESS.getString();
|
|
|
|
final int connectPort = server.getPort();
|
|
|
|
|
|
|
|
if (!hostname.equalsIgnoreCase(connectAddress + ":" + connectPort) && !hostname.equalsIgnoreCase(connectAddress + ".:" + connectPort))
|
|
|
|
{
|
|
|
|
final int forceIpPort = ConfigEntry.FORCE_IP_PORT.getInteger();
|
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
|
|
|
|
ConfigEntry.FORCE_IP_KICKMSG.getString()
|
2018-07-31 06:41:56 +00:00
|
|
|
.replace("%address%", ConfigEntry.SERVER_ADDRESS.getString() + (forceIpPort == DEFAULT_PORT ? "" : ":" + forceIpPort)));
|
2015-11-15 23:32:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation below this point
|
2022-04-15 13:09:23 +00:00
|
|
|
final Admin entry = plugin.al.getEntryByUuid(uuid);
|
|
|
|
if (entry != null && entry.isActive()) // Check if player is admin
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
// Force-allow log in
|
|
|
|
event.allow();
|
|
|
|
|
|
|
|
int count = server.getOnlinePlayers().size();
|
|
|
|
if (count >= server.getMaxPlayers())
|
|
|
|
{
|
|
|
|
for (Player onlinePlayer : server.getOnlinePlayers())
|
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
if (!plugin.al.isAdmin(onlinePlayer))
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
onlinePlayer.kickPlayer("You have been kicked to free up room for an admin.");
|
2015-11-15 23:32:04 +00:00
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < server.getMaxPlayers())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count >= server.getMaxPlayers())
|
|
|
|
{
|
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "The server is full and a player could not be kicked, sorry!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
// Player is not an admin
|
2015-11-15 23:32:04 +00:00
|
|
|
// Server full check
|
|
|
|
if (server.getOnlinePlayers().size() >= server.getMaxPlayers())
|
|
|
|
{
|
2016-03-07 20:32:05 +00:00
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Sorry, but this server is full.");
|
2015-11-15 23:32:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
// Admin-only mode
|
|
|
|
if (ConfigEntry.ADMIN_ONLY_MODE.getBoolean())
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Server is temporarily open to admins only.");
|
2015-11-15 23:32:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lockdown mode
|
2016-03-02 19:28:01 +00:00
|
|
|
if (lockdownEnabled)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Server is currently in lockdown mode.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-07 19:33:58 +00:00
|
|
|
// Whitelist
|
2022-04-15 13:52:06 +00:00
|
|
|
if (server.isWhitelistEnforced() && !player.isWhitelisted())
|
2018-01-07 19:33:58 +00:00
|
|
|
{
|
2022-04-15 13:52:06 +00:00
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "You are not whitelisted on this server.");
|
2018-01-07 19:33:58 +00:00
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
|
|
public void onPlayerJoin(PlayerJoinEvent event)
|
|
|
|
{
|
|
|
|
final Player player = event.getPlayer();
|
2018-07-31 05:58:50 +00:00
|
|
|
final FPlayer fPlayer = plugin.pl.getPlayer(player);
|
2020-06-30 07:25:38 +00:00
|
|
|
final PlayerData playerData = plugin.pl.getData(player);
|
2020-07-22 21:40:58 +00:00
|
|
|
|
2021-08-10 10:06:41 +00:00
|
|
|
// Sends a message to the player if they have never joined before (or simply lack player data).
|
2021-08-10 10:13:23 +00:00
|
|
|
if (!event.getPlayer().hasPlayedBefore() && ConfigEntry.FIRST_JOIN_INFO_ENABLED.getBoolean())
|
2021-08-10 10:06:41 +00:00
|
|
|
{
|
2021-08-10 12:58:04 +00:00
|
|
|
new BukkitRunnable()
|
2021-08-10 10:06:41 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
for (String line : ConfigEntry.FIRST_JOIN_INFO.getStringList())
|
|
|
|
{
|
|
|
|
player.sendMessage(FUtil.colorize(line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.runTaskLater(plugin, 20);
|
|
|
|
}
|
|
|
|
|
2019-12-25 01:05:36 +00:00
|
|
|
player.sendTitle(FUtil.colorize(ConfigEntry.SERVER_LOGIN_TITLE.getString()), FUtil.colorize(ConfigEntry.SERVER_LOGIN_SUBTITLE.getString()), 20, 100, 60);
|
2019-11-02 01:28:07 +00:00
|
|
|
player.setOp(true);
|
|
|
|
|
2020-05-29 10:14:21 +00:00
|
|
|
if (TELEPORT_ON_JOIN.contains(player.getName()) || ConfigEntry.AUTO_TP.getBoolean())
|
2019-11-03 14:10:56 +00:00
|
|
|
{
|
2020-04-08 08:34:08 +00:00
|
|
|
int x = FUtil.randomInteger(-10000, 10000);
|
|
|
|
int z = FUtil.randomInteger(-10000, 10000);
|
2019-11-16 21:16:38 +00:00
|
|
|
int y = player.getWorld().getHighestBlockYAt(x, z);
|
|
|
|
Location location = new Location(player.getLocation().getWorld(), x, y, z);
|
2020-08-31 00:13:46 +00:00
|
|
|
PaperLib.teleportAsync(player, location);
|
2020-03-10 01:58:45 +00:00
|
|
|
player.sendMessage(ChatColor.AQUA + "You have been teleported to a random location automatically.");
|
2019-11-03 14:10:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
|
2022-04-15 13:52:06 +00:00
|
|
|
if (!playerData.getIps().contains(FUtil.getIp(player)))
|
2020-06-30 07:25:38 +00:00
|
|
|
{
|
2020-07-01 01:51:06 +00:00
|
|
|
playerData.addIp(FUtil.getIp(player));
|
2020-06-30 07:25:38 +00:00
|
|
|
plugin.pl.save(playerData);
|
|
|
|
}
|
|
|
|
|
2020-05-29 10:14:21 +00:00
|
|
|
if (CLEAR_ON_JOIN.contains(player.getName()) || ConfigEntry.AUTO_CLEAR.getBoolean())
|
2019-11-18 21:45:18 +00:00
|
|
|
{
|
|
|
|
player.getInventory().clear();
|
|
|
|
player.sendMessage(ChatColor.AQUA + "Your inventory has been cleared automatically.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 02:13:57 +00:00
|
|
|
if (!ConfigEntry.SERVER_TABLIST_HEADER.getString().isEmpty())
|
|
|
|
{
|
|
|
|
player.setPlayerListHeader(FUtil.colorize(ConfigEntry.SERVER_TABLIST_HEADER.getString()).replace("\\n", "\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ConfigEntry.SERVER_TABLIST_FOOTER.getString().isEmpty())
|
|
|
|
{
|
|
|
|
player.setPlayerListFooter(FUtil.colorize(ConfigEntry.SERVER_TABLIST_FOOTER.getString()).replace("\\n", "\n"));
|
|
|
|
}
|
|
|
|
|
2020-12-04 00:28:53 +00:00
|
|
|
if (!plugin.al.isAdmin(player))
|
2018-07-31 05:58:50 +00:00
|
|
|
{
|
2020-07-01 01:51:06 +00:00
|
|
|
String tag = playerData.getTag();
|
|
|
|
if (tag != null)
|
|
|
|
{
|
|
|
|
fPlayer.setTag(FUtil.colorize(tag));
|
|
|
|
}
|
2020-07-22 21:40:58 +00:00
|
|
|
|
2020-06-30 07:25:38 +00:00
|
|
|
int noteCount = playerData.getNotes().size();
|
|
|
|
if (noteCount != 0)
|
2018-07-31 05:58:50 +00:00
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
String noteMessage = "This player has " + noteCount + " admin note" + (noteCount > 1 ? "s" : "") + ".";
|
2020-06-30 07:25:38 +00:00
|
|
|
FLog.info(noteMessage);
|
2021-05-26 09:55:49 +00:00
|
|
|
plugin.al.messageAllAdmins(ChatColor.GOLD + noteMessage);
|
|
|
|
plugin.al.messageAllAdmins(ChatColor.GOLD + "Do " + ChatColor.YELLOW + "/notes " + player.getName() + " list" + ChatColor.GOLD + " to view them.");
|
2018-07-31 05:58:50 +00:00
|
|
|
}
|
2018-01-07 19:33:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
if (ConfigEntry.ADMIN_ONLY_MODE.getBoolean())
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2020-12-04 00:28:53 +00:00
|
|
|
player.sendMessage(ChatColor.RED + "Server is currently closed to non-admins.");
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:01 +00:00
|
|
|
if (lockdownEnabled)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
FUtil.playerMsg(player, "Warning: Server is currenty in lockdown-mode, new players will not be able to join!", ChatColor.RED);
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 21:40:58 +00:00
|
|
|
}.runTaskLater(plugin, 20L);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
2020-07-22 21:40:58 +00:00
|
|
|
}
|