Skip to content

Commit

Permalink
Handle player online check in AsyncPlayerPreLoginEvent
Browse files Browse the repository at this point in the history
Apparently, online players with the same username are kicked before PlayerLoginEvent is called. This commit moves the online player check to AsyncPlayerPreloginEvent so the default behaviour can still be modified

Fixes #642
  • Loading branch information
JeromSar committed May 30, 2015
1 parent a4d8f46 commit c002fbc
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 188 deletions.
4 changes: 2 additions & 2 deletions buildnumber.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Sat May 30 20:10:27 CEST 2015
build.number=1043
#Sat May 30 20:46:17 CEST 2015
build.number=1052
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public boolean run(CommandSender sender, Player sender_p, Command cmd, String co
{
final TFM_Admin admin = TFM_AdminList.getEntry(sender_p);

if (admin == null) {
if (admin == null)
{
playerMsg("Could not find your admin entry! Please notify a developer.", ChatColor.RED);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.block.LeavesDecayEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
Expand Down Expand Up @@ -388,7 +389,7 @@ public void onPlayerTeleport(PlayerTeleportEvent event)
final Player player = event.getPlayer();
final TFM_PlayerData playerdata = TFM_PlayerData.getPlayerData(player);
// Check absolute value to account for negatives
if(Math.abs(event.getTo().getX()) >= MAX_XY_COORD || Math.abs(event.getTo().getZ()) >= MAX_XY_COORD)
if (Math.abs(event.getTo().getX()) >= MAX_XY_COORD || Math.abs(event.getTo().getZ()) >= MAX_XY_COORD)
{
event.setCancelled(true); // illegal position, cancel it
}
Expand Down Expand Up @@ -791,7 +792,7 @@ public void onPlayerJoin(PlayerJoinEvent event)
final TFM_Player playerEntry;
TFM_Log.info("[JOIN] " + TFM_Util.formatPlayer(player) + " joined the game with IP address: " + ip, true);
// Check absolute value to account for negatives
if(Math.abs(player.getLocation().getX()) >= MAX_XY_COORD || Math.abs(player.getLocation().getZ()) >= MAX_XY_COORD)
if (Math.abs(player.getLocation().getX()) >= MAX_XY_COORD || Math.abs(player.getLocation().getZ()) >= MAX_XY_COORD)
{
player.teleport(player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn
}
Expand Down Expand Up @@ -912,25 +913,14 @@ public void run()
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerLogin(PlayerLoginEvent event)
public void onPlayerPreLogin(AsyncPlayerPreLoginEvent event)
{
if (TFM_ConfigEntry.FORCE_IP_ENABLED.getBoolean())
{
final String hostname = event.getHostname().replace("FML", ""); // Forge fix - https://github.com/TotalFreedom/TotalFreedomMod/issues/493
final String connectAddress = TFM_ConfigEntry.SERVER_ADDRESS.getString();
final int connectPort = TotalFreedomMod.server.getPort();

if (!hostname.equalsIgnoreCase(connectAddress + ":" + connectPort) && !hostname.equalsIgnoreCase(connectAddress + ".:" + connectPort))
{
final int forceIpPort = TFM_ConfigEntry.FORCE_IP_PORT.getInteger();
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
TFM_ConfigEntry.FORCE_IP_KICKMSG.getString()
.replace("%address%", TFM_ConfigEntry.SERVER_ADDRESS.getString() + (forceIpPort == DEFAULT_PORT ? "" : ":" + forceIpPort)));
return;
}

}
TFM_ServerInterface.handlePlayerPreLogin(event);
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerLogin(PlayerLoginEvent event)
{
TFM_ServerInterface.handlePlayerLogin(event);
}
}
2 changes: 1 addition & 1 deletion src/me/StevenLawson/TotalFreedomMod/TFM_AdminList.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public static void updateLastLogin(Player player)

public static boolean isSuperAdminSafe(UUID uuid, String ip)
{
if (TotalFreedomMod.server.getOnlineMode())
if (TotalFreedomMod.server.getOnlineMode() && uuid != null)
{
return TFM_AdminList.getSuperUUIDs().contains(uuid);
}
Expand Down
1 change: 1 addition & 0 deletions src/me/StevenLawson/TotalFreedomMod/TFM_PermbanList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class TFM_PermbanList
{

private static final List<String> PERMBANNED_PLAYERS;
private static final List<String> PERMBANNED_IPS;

Expand Down
86 changes: 29 additions & 57 deletions src/me/StevenLawson/TotalFreedomMod/TFM_PlayerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,79 +11,65 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class TFM_PlayerList
{
public class TFM_PlayerList {

private static final Map<UUID, TFM_Player> PLAYER_LIST = new HashMap<UUID, TFM_Player>();

private TFM_PlayerList()
{
private TFM_PlayerList() {
throw new AssertionError();
}

public static Set<TFM_Player> getAllPlayers()
{
public static Set<TFM_Player> getAllPlayers() {
return Collections.unmodifiableSet(Sets.newHashSet(PLAYER_LIST.values()));
}

public static void load()
{
public static void load() {
PLAYER_LIST.clear();

// Load online players
for (Player player : Bukkit.getOnlinePlayers())
{
for (Player player : Bukkit.getOnlinePlayers()) {
getEntry(player);
}

TFM_Log.info("Loaded playerdata for " + PLAYER_LIST.size() + " players");
}

public static void saveAll()
{
for (TFM_Player entry : PLAYER_LIST.values())
{
public static void saveAll() {
for (TFM_Player entry : PLAYER_LIST.values()) {
save(entry);
}
}

// May return null
public static TFM_Player getEntry(UUID uuid)
{
if (PLAYER_LIST.containsKey(uuid))
{
public static TFM_Player getEntry(UUID uuid) {
if (PLAYER_LIST.containsKey(uuid)) {
return PLAYER_LIST.get(uuid);
}

final File configFile = getConfigFile(uuid);

if (!configFile.exists())
{
if (!configFile.exists()) {
return null;
}

final TFM_Player entry = new TFM_Player(uuid, getConfig(uuid));

if (entry.isComplete())
{
if (entry.isComplete()) {
PLAYER_LIST.put(uuid, entry);
return entry;
}
else
{
} else {
TFM_Log.warning("Could not load entry: Entry is not complete!");
configFile.delete();
}

return null;
}

public static TFM_Player getEntry(Player player)
{
public static TFM_Player getEntry(Player player) {
final UUID uuid = TFM_UuidManager.getUniqueId(player);
TFM_Player entry = getEntry(uuid);

if (entry != null)
{
if (entry != null) {
return entry;
}

Expand All @@ -101,12 +87,10 @@ public static TFM_Player getEntry(Player player)
return entry;
}

public static void removeEntry(Player player)
{
public static void removeEntry(Player player) {
final UUID uuid = TFM_UuidManager.getUniqueId(player);

if (!PLAYER_LIST.containsKey(uuid))
{
if (!PLAYER_LIST.containsKey(uuid)) {
return;
}

Expand All @@ -115,20 +99,16 @@ public static void removeEntry(Player player)
PLAYER_LIST.remove(uuid);
}

public static boolean existsEntry(Player player)
{
public static boolean existsEntry(Player player) {
return existsEntry(TFM_UuidManager.getUniqueId(player));
}

public static boolean existsEntry(UUID uuid)
{
public static boolean existsEntry(UUID uuid) {
return getConfigFile(uuid).exists();
}

public static void setUniqueId(TFM_Player entry, UUID newUuid)
{
if (entry.getUniqueId().equals(newUuid))
{
public static void setUniqueId(TFM_Player entry, UUID newUuid) {
if (entry.getUniqueId().equals(newUuid)) {
TFM_Log.warning("Not setting new UUID: UUIDs match!");
return;
}
Expand All @@ -147,44 +127,36 @@ public static void setUniqueId(TFM_Player entry, UUID newUuid)
// Remove old entry
PLAYER_LIST.remove(entry.getUniqueId());
final File oldFile = getConfigFile(entry.getUniqueId());
if (oldFile.exists() && !oldFile.delete())
{
if (oldFile.exists() && !oldFile.delete()) {
TFM_Log.warning("Could not delete config: " + getConfigFile(entry.getUniqueId()).getName());
}
}

public static void purgeAll()
{
for (File file : getConfigFolder().listFiles())
{
public static void purgeAll() {
for (File file : getConfigFolder().listFiles()) {
file.delete();
}

// Load online players
load();
}

public static File getConfigFolder()
{
public static File getConfigFolder() {
return new File(TotalFreedomMod.plugin.getDataFolder(), "players");
}

public static File getConfigFile(UUID uuid)
{
public static File getConfigFile(UUID uuid) {
return new File(getConfigFolder(), uuid + ".yml");
}

public static TFM_Config getConfig(UUID uuid)
{
public static TFM_Config getConfig(UUID uuid) {
final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, getConfigFile(uuid), false);
config.load();
return config;
}

public static void save(TFM_Player entry)
{
if (!entry.isComplete())
{
public static void save(TFM_Player entry) {
if (!entry.isComplete()) {
throw new IllegalArgumentException("Entry is not complete!");
}

Expand Down
65 changes: 45 additions & 20 deletions src/me/StevenLawson/TotalFreedomMod/TFM_ServerInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import java.util.UUID;
import java.util.regex.Pattern;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
import static me.StevenLawson.TotalFreedomMod.Listener.TFM_PlayerListener.DEFAULT_PORT;
import net.minecraft.server.v1_8_R2.EntityPlayer;
import net.minecraft.server.v1_8_R2.MinecraftServer;
import net.minecraft.server.v1_8_R2.PropertyManager;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;

Expand Down Expand Up @@ -62,6 +64,29 @@ public static String getVersion()
return MinecraftServer.getServer().getVersion();
}

public static void handlePlayerPreLogin(AsyncPlayerPreLoginEvent event)
{
final String ip = event.getAddress().getHostAddress().trim();
final boolean isAdmin = TFM_AdminList.isSuperAdminSafe(null, ip);

// 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();
TFM_Sync.playerKick(onlinePlayer, "An admin just logged in with the username you are using.");
}
return;
}
}

public static void handlePlayerLogin(PlayerLoginEvent event)
{
final Server server = TotalFreedomMod.server;
Expand All @@ -70,19 +95,38 @@ public static void handlePlayerLogin(PlayerLoginEvent event)
final String ip = event.getAddress().getHostAddress().trim();
final UUID uuid = TFM_UuidManager.newPlayer(player, ip);

// Perform username checks
// Check username length
if (username.length() < 3 || username.length() > TotalFreedomMod.MAX_USERNAME_LENGTH)
{
event.disallow(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(Result.KICK_OTHER, "Your username contains invalid characters.");
return;
}

// Check force-IP match
if (TFM_ConfigEntry.FORCE_IP_ENABLED.getBoolean())
{
final String hostname = event.getHostname().replace("FML", ""); // Forge fix - https://github.com/TotalFreedom/TotalFreedomMod/issues/493
final String connectAddress = TFM_ConfigEntry.SERVER_ADDRESS.getString();
final int connectPort = TotalFreedomMod.server.getPort();

if (!hostname.equalsIgnoreCase(connectAddress + ":" + connectPort) && !hostname.equalsIgnoreCase(connectAddress + ".:" + connectPort))
{
final int forceIpPort = TFM_ConfigEntry.FORCE_IP_PORT.getInteger();
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
TFM_ConfigEntry.FORCE_IP_KICKMSG.getString()
.replace("%address%", TFM_ConfigEntry.SERVER_ADDRESS.getString() + (forceIpPort == DEFAULT_PORT ? "" : ":" + forceIpPort)));
return;
}

}

// Check if player is admin
// Not safe to use TFM_Util.isSuperAdmin(player) because player.getAddress() will return a null until after player login.
final boolean isAdmin = TFM_AdminList.isSuperAdminSafe(uuid, ip);
Expand All @@ -93,15 +137,6 @@ public static void handlePlayerLogin(PlayerLoginEvent event)
// Force-allow log in
event.allow();

// Kick players with the same name
for (Player onlinePlayer : server.getOnlinePlayers())
{
if (onlinePlayer.getName().equalsIgnoreCase(username))
{
onlinePlayer.kickPlayer("An admin just logged in with the username you are using.");
}
}

int count = server.getOnlinePlayers().size();
if (count >= server.getMaxPlayers())
{
Expand Down Expand Up @@ -151,16 +186,6 @@ public static void handlePlayerLogin(PlayerLoginEvent event)
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())
{
Expand Down
Loading

0 comments on commit c002fbc

Please sign in to comment.