Fix players being able to log in multiple times via different bungees

This commit is contained in:
AlexTheCoder 2016-10-16 02:23:53 -04:00 committed by Shaun Bennett
parent 8e7ae7f55f
commit 55b37e0261
2 changed files with 25 additions and 31 deletions

View File

@ -14,14 +14,12 @@ public class PlayerJoinHandler implements CommandCallback
_playerTracker = playerTracker;
}
@SuppressWarnings("deprecation")
@Override
public void run(ServerCommand command)
{
if (command instanceof PlayerJoinCommand)
{
PlayerJoinCommand joinCommand = (PlayerJoinCommand)command;
_playerTracker.kickPlayerIfOnline(UUID.fromString(joinCommand.getUuid()));
}
}

View File

@ -1,17 +1,18 @@
package mineplex.bungee.playerTracker;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import com.google.common.collect.Lists;
import mineplex.serverdata.Region;
import mineplex.serverdata.commands.PlayerJoinCommand;
import mineplex.serverdata.commands.ServerCommandManager;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.data.PlayerStatus;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
@ -20,7 +21,7 @@ import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
public class PlayerTracker implements Listener, Runnable
public class PlayerTracker implements Listener
{
// Default period before status expiry (8 hours)
private static final int DEFAULT_STATUS_TIMEOUT = 60 * 60 * 8;
@ -28,24 +29,29 @@ public class PlayerTracker implements Listener, Runnable
// Repository storing player status' across network.
private DataRepository<PlayerStatus> _repository;
private Set<UUID> _onlineUUIDs = new HashSet<>();
private Plugin _plugin;
private final List<UUID> _ignoreKick = Lists.newArrayList();
public PlayerTracker(Plugin plugin)
{
_plugin = plugin;
_plugin.getProxy().getPluginManager().registerListener(_plugin, this);
_plugin.getProxy().getScheduler().schedule(_plugin, this, 1L, 1L, TimeUnit.MINUTES);
_repository = new RedisDataRepository<PlayerStatus>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
Region.currentRegion(), PlayerStatus.class, "playerStatus");
ServerCommandManager.getInstance().registerCommandType("PlayerJoinCommand", mineplex.serverdata.commands.PlayerJoinCommand.class, new PlayerJoinHandler(this));
ServerCommandManager.getInstance().initializeServer("BUNGEE ENABLE - " + System.currentTimeMillis());
ServerCommandManager.getInstance().registerCommandType(mineplex.serverdata.commands.PlayerJoinCommand.class, new PlayerJoinHandler(this));
System.out.println("Initialized PlayerTracker.");
}
public Plugin getPlugin()
{
return _plugin;
}
@EventHandler
public void playerConnect(final ServerConnectedEvent event)
@ -70,42 +76,32 @@ public class PlayerTracker implements Listener, Runnable
_repository.removeElement(event.getPlayer().getName().toLowerCase());
}
});
_onlineUUIDs.remove(event.getPlayer().getUniqueId());
}
@EventHandler
public void playerConnect(final PostLoginEvent event)
{
_onlineUUIDs.add(event.getPlayer().getUniqueId());
}
@Override
public void run()
{
Set<UUID> onlineUUIDs = new HashSet<>();
for (ProxiedPlayer player : _plugin.getProxy().getPlayers())
{
onlineUUIDs.add(player.getUniqueId());
}
_onlineUUIDs = onlineUUIDs;
_ignoreKick.add(event.getPlayer().getUniqueId());
PlayerJoinCommand command = new PlayerJoinCommand(event.getPlayer().getUniqueId());
command.publish();
}
public boolean isPlayerOnline(UUID uuid)
{
return _onlineUUIDs.contains(uuid);
return _plugin.getProxy().getPlayer(uuid) != null;
}
public void kickPlayerIfOnline(UUID uuid)
{
if (_onlineUUIDs.contains(uuid))
if (_ignoreKick.remove(uuid))
{
return;
}
if (isPlayerOnline(uuid))
{
ProxiedPlayer player = _plugin.getProxy().getPlayer(uuid);
if (player != null)
player.disconnect("You have logged in from another location.");
player.disconnect(new TextComponent("You have logged in from another location."));
}
}
}