Skip to content

Commit

Permalink
Add an error checking thread for player sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Aug 23, 2020
1 parent e9c0075 commit 6e60138
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 21 deletions.
Expand Up @@ -150,6 +150,7 @@ public static void updateConfig(File file) throws IOException {
if (i > 0) {
YAMLSection settings = new YAMLSection();
settings.set("Version", ((now.compareTo(was) <= 0)?was:now).toString());
if (updated.getMap("Settings", new YAMLSection()).contains("RemotePlayer-Cache-Interval")) settings.set("RemotePlayer-Cache-Interval", updated.getMap("Settings").getRawString("RemotePlayer-Cache-Interval"));
settings.set("Disabled-Overrides", updated.getMap("Settings", new YAMLSection()).getRawStringList("Disabled-Overrides", Collections.emptyList()));

YAMLSection smart_fallback = new YAMLSection();
Expand Down
Expand Up @@ -64,14 +64,14 @@ public ObjectMap<Integer> send(SubDataClient client) {
public void receive(SubDataClient client, ObjectMap<Integer> data) {
if (client.getHandler() instanceof Proxy) {
ArrayList<RemotePlayer> forward = new ArrayList<RemotePlayer>();
if (data.getBoolean(0x0001) == null) {
for (UUID id : Util.getBackwards(plugin.rPlayerLinkP, (Proxy) client.getHandler())) {
plugin.rPlayerLinkS.remove(id);
plugin.rPlayerLinkP.remove(id);
plugin.rPlayers.remove(id);
}
}
synchronized (plugin.rPlayers) {
if (data.getBoolean(0x0001) == null) {
for (UUID id : Util.getBackwards(plugin.rPlayerLinkP, (Proxy) client.getHandler())) {
plugin.rPlayerLinkS.remove(id);
plugin.rPlayerLinkP.remove(id);
plugin.rPlayers.remove(id);
}
}
if (data.getBoolean(0x0001) != Boolean.FALSE) {
if (data.contains(0x0002)) for (Map<String, Object> object : (List<Map<String, Object>>) data.getObjectList(0x0002)) {
Server server = (object.getOrDefault("server", null) != null)?plugin.api.getServer(object.get("server").toString()):null;
Expand Down Expand Up @@ -99,8 +99,9 @@ public void receive(SubDataClient client, ObjectMap<Integer> data) {
}

if (data.getBoolean(0x0001) == null || forward.size() > 0) {
PacketExSyncPlayer packet = new PacketExSyncPlayer(((Proxy) client.getHandler()).getName(), data.getBoolean(0x0001), forward.toArray(new RemotePlayer[0]));
for (Proxy proxy : SubAPI.getInstance().getProxies().values()) if (proxy.getSubData()[0] != null && proxy != client.getHandler()) {
((SubDataClient) proxy.getSubData()[0]).sendPacket(new PacketExSyncPlayer(((Proxy) client.getHandler()).getName(), data.getBoolean(0x0001), forward.toArray(new RemotePlayer[0])));
((SubDataClient) proxy.getSubData()[0]).sendPacket(packet);
}
}
}
Expand Down
52 changes: 48 additions & 4 deletions SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubProxy.java
Expand Up @@ -33,6 +33,7 @@
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down Expand Up @@ -668,6 +669,46 @@ public void run() {
} catch (Exception e) {}
}
}, 0, TimeUnit.DAYS.toMillis(2));

int interval = config.get().getMap("Settings").getInt("RemotePlayer-Cache-Interval", 300);
int start = interval - new Random().nextInt((interval / 3) + 1);
new Timer("SubServers.Bungee::RemotePlayer_Error_Checking").schedule(new TimerTask() {
@Override
public void run() {
synchronized (rPlayers) {
ArrayList<RemotePlayer> add = new ArrayList<RemotePlayer>();
for (ProxiedPlayer player : getPlayers()) {
if (!rPlayers.containsKey(player.getUniqueId())) { // Add players that don't exist
Logger.get("SubServers").info("RPEC::Add(" + player.getUniqueId() + ")");
RemotePlayer p = new RemotePlayer(player);
rPlayerLinkP.put(player.getUniqueId(), p.getProxy());
rPlayers.put(player.getUniqueId(), p);
if (p.getServer() != null) rPlayerLinkS.put(player.getUniqueId(), p.getServer());
add.add(p);
}
}
ArrayList<RemotePlayer> remove = new ArrayList<RemotePlayer>();
for (UUID player : Util.getBackwards(rPlayerLinkP, mProxy)) { // Remove players that shouldn't exist
if (getPlayer(player) == null) {
Logger.get("SubServers").info("RPEC::Remove(" + player + ")");
remove.add(rPlayers.get(player));
rPlayerLinkS.remove(player);
rPlayerLinkP.remove(player);
rPlayers.remove(player);
}
}
LinkedList<PacketExSyncPlayer> packets = new LinkedList<PacketExSyncPlayer>(); // Compile change data for external proxies
if (add.size() > 0) packets.add(new PacketExSyncPlayer(mProxy.getName(), true, add.toArray(new RemotePlayer[0])));
if (remove.size() > 0) packets.add(new PacketExSyncPlayer(mProxy.getName(), false, remove.toArray(new RemotePlayer[0])));
if (packets.size() > 0) {
PacketExSyncPlayer[] packet = packets.toArray(new PacketExSyncPlayer[0]);
for (Proxy proxy : SubAPI.getInstance().getProxies().values()) if (proxy.getSubData()[0] != null) {
((SubDataClient) proxy.getSubData()[0]).sendPacket(packet);
}
}
}
}
}, TimeUnit.SECONDS.toMillis(start), TimeUnit.SECONDS.toMillis(interval));
}

/**
Expand Down Expand Up @@ -850,8 +891,9 @@ public void ping(ProxyPingEvent e) {

@EventHandler(priority = Byte.MIN_VALUE)
public void login(LoginEvent e) {
ProxyServer.getInstance().getLogger().info("UUID of player " + e.getConnection().getName() + " is " + e.getConnection().getUniqueId());
if (rPlayers.containsKey(e.getConnection().getUniqueId())) {
Logger.get("SubServers").info(e.getConnection().getName() + " connected, but already had a database entry");
Logger.get("SubServers").warning(e.getConnection().getName() + " connected, but already had a database entry");
RemotePlayer player = rPlayers.get(e.getConnection().getUniqueId());
if (player.getProxy() == null || player.getProxy().isMaster()) {
ProxiedPlayer p = getPlayer(player.getUniqueId());
Expand Down Expand Up @@ -962,12 +1004,14 @@ public void resetPlayer(PlayerDisconnectEvent e) {

synchronized (rPlayers) {
if (rPlayers.containsKey(e.getPlayer().getUniqueId()) && (!rPlayerLinkP.containsKey(e.getPlayer().getUniqueId()) || rPlayerLinkP.get(e.getPlayer().getUniqueId()).isMaster())) {
for (Proxy proxy : SubAPI.getInstance().getProxies().values()) if (proxy.getSubData()[0] != null) {
((SubDataClient) proxy.getSubData()[0]).sendPacket(new PacketExSyncPlayer(mProxy.getName(), false, rPlayers.get(e.getPlayer().getUniqueId())));
}
RemotePlayer player = rPlayers.get(e.getPlayer().getUniqueId());
rPlayerLinkS.remove(e.getPlayer().getUniqueId());
rPlayerLinkP.remove(e.getPlayer().getUniqueId());
rPlayers.remove(e.getPlayer().getUniqueId());

for (Proxy proxy : SubAPI.getInstance().getProxies().values()) if (proxy.getSubData()[0] != null) {
((SubDataClient) proxy.getSubData()[0]).sendPacket(new PacketExSyncPlayer(mProxy.getName(), false, player));
}
}
}
}
Expand Down
Expand Up @@ -59,6 +59,7 @@ public static void updateConfig(File file) throws IOException {
settings.set("Show-Addresses", updated.getMap("Settings", new YAMLSection()).getBoolean("Show-Addresses", false));
settings.set("Use-Title-Messages", updated.getMap("Settings", new YAMLSection()).getBoolean("Use-Title-Messages", true));
settings.set("PlaceholderAPI-Ready", updated.getMap("Settings", new YAMLSection()).getBoolean("PlaceholderAPI-Ready", false));
if (updated.getMap("Settings", new YAMLSection()).contains("PlaceholderAPI-Cache-Interval")) settings.set("PlaceholderAPI-Cache-Interval", updated.getMap("Settings").getRawString("PlaceholderAPI-Cache-Interval"));

YAMLSection subdata = new YAMLSection();
if (updated.getMap("Settings", new YAMLSection()).getMap("SubData", new YAMLSection()).contains("Name")) subdata.set("Name", updated.getMap("Settings").getMap("SubData").getRawString("Name"));
Expand Down
56 changes: 55 additions & 1 deletion SubServers.Sync/src/net/ME1312/SubServers/Sync/ExProxy.java
Expand Up @@ -32,6 +32,7 @@
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down Expand Up @@ -237,6 +238,58 @@ public void run() {
} catch (Exception e) {}
}
}, 0, TimeUnit.DAYS.toMillis(2));

int interval = config.get().getMap("Settings").getInt("RemotePlayer-Cache-Interval", 300);
int start = interval - new Random().nextInt((interval / 3) + 1);
new Timer("SubServers.Sync::RemotePlayer_Error_Checking").schedule(new TimerTask() {
@Override
public void run() {
if (api.getSubDataNetwork()[0] != null && !api.getSubDataNetwork()[0].isClosed()) {
api.getProxy(api.getName(), proxy -> {
synchronized (rPlayers) {
ArrayList<RemotePlayer> add = new ArrayList<RemotePlayer>();
for (ProxiedPlayer player : getPlayers()) {
if (!rPlayers.containsKey(player.getUniqueId())) { // Add players that don't exist
Logger.get("SubServers").info("RPEC::Add(" + player.getUniqueId() + ")");
RemotePlayer p = new RemotePlayer(player);
rPlayerLinkP.put(player.getUniqueId(), p.getProxy().toLowerCase());
rPlayers.put(player.getUniqueId(), p);
if (player.getServer().getInfo() instanceof ServerImpl) rPlayerLinkS.put(player.getUniqueId(), (ServerImpl) player.getServer().getInfo());
add.add(p);
}
}
ArrayList<RemotePlayer> remove = new ArrayList<RemotePlayer>();
for (NamedContainer<String, UUID> player : proxy.getPlayers()) { // Remove players that shouldn't exist
if (getPlayer(player.get()) == null) {
Logger.get("SubServers").info("RPEC::Remove(" + player + ")");
remove.add(rPlayers.get(player.get()));
rPlayerLinkS.remove(player.get());
rPlayerLinkP.remove(player.get());
rPlayers.remove(player.get());
}
}
for (UUID player : Util.getBackwards(rPlayerLinkP, api.getName().toLowerCase())) { // Remove players that shouldn't exist (internally)
if (getPlayer(player) == null) {
Logger.get("SubServers").info("RPEC::Internal(" + player + ")");
rPlayerLinkS.remove(player);
rPlayerLinkP.remove(player);
rPlayers.remove(player);
}
}
LinkedList<PacketExSyncPlayer> packets = new LinkedList<PacketExSyncPlayer>(); // Compile change data for external proxies
if (add.size() > 0) packets.add(new PacketExSyncPlayer(true, add.toArray(new RemotePlayer[0])));
if (remove.size() > 0) packets.add(new PacketExSyncPlayer(false, remove.toArray(new RemotePlayer[0])));
if (packets.size() > 0) {
PacketExSyncPlayer[] packet = packets.toArray(new PacketExSyncPlayer[0]);
if (api.getSubDataNetwork()[0] != null) {
((SubDataClient) api.getSubDataNetwork()[0]).sendPacket(packet);
}
}
}
});
}
}
}, TimeUnit.SECONDS.toMillis(start), TimeUnit.SECONDS.toMillis(interval));
}

/**
Expand Down Expand Up @@ -374,8 +427,9 @@ public void ping(ProxyPingEvent e) {

@EventHandler(priority = Byte.MIN_VALUE)
public void login(LoginEvent e) {
ProxyServer.getInstance().getLogger().info("UUID of player " + e.getConnection().getName() + " is " + e.getConnection().getUniqueId());
if (rPlayers.containsKey(e.getConnection().getUniqueId())) {
Logger.get("SubServers").info(e.getConnection().getName() + " connected, but already had a database entry");
Logger.get("SubServers").warning(e.getConnection().getName() + " connected, but already had a database entry");
RemotePlayer player = rPlayers.get(e.getConnection().getUniqueId());
if (player.getProxy() != null && player.getProxy().equalsIgnoreCase(api.getName())) {
ProxiedPlayer p = getPlayer(player.getUniqueId());
Expand Down
Expand Up @@ -74,6 +74,7 @@ public static void updateConfig(File file) throws IOException {
if (i > 0) {
YAMLSection settings = new YAMLSection();
settings.set("Version", ((now.compareTo(was) <= 0)?was:now).toString());
if (updated.getMap("Settings", new YAMLSection()).contains("RemotePlayer-Cache-Interval")) settings.set("RemotePlayer-Cache-Interval", updated.getMap("Settings").getRawString("RemotePlayer-Cache-Interval"));
settings.set("Disabled-Overrides", updated.getMap("Settings", new YAMLSection()).getRawStringList("Disabled-Overrides", Collections.emptyList()));

YAMLSection smart_fallback = new YAMLSection();
Expand Down
Expand Up @@ -56,14 +56,14 @@ public ObjectMap<Integer> send(SubDataSender client) {
@Override
public void receive(SubDataSender client, ObjectMap<Integer> data) {
String proxy = (data.contains(0x0000)?data.getRawString(0x0000).toLowerCase():null);
if (data.getBoolean(0x0001) == null) {
for (UUID id : Util.getBackwards(plugin.rPlayerLinkP, proxy)) {
plugin.rPlayerLinkS.remove(id);
plugin.rPlayerLinkP.remove(id);
plugin.rPlayers.remove(id);
}
}
synchronized (plugin.rPlayers) {
if (data.getBoolean(0x0001) == null) {
for (UUID id : Util.getBackwards(plugin.rPlayerLinkP, proxy)) {
plugin.rPlayerLinkS.remove(id);
plugin.rPlayerLinkP.remove(id);
plugin.rPlayers.remove(id);
}
}
if (data.getBoolean(0x0001) != Boolean.FALSE) {
if (data.contains(0x0002)) for (Map<String, Object> object : (List<Map<String, Object>>) data.getObjectList(0x0002)) {
ServerImpl server = (object.getOrDefault("server", null) != null)?plugin.servers.getOrDefault(object.get("server").toString().toLowerCase(), null):null;
Expand All @@ -78,7 +78,7 @@ public void receive(SubDataSender client, ObjectMap<Integer> data) {
UUID id = UUID.fromString(object.get("id").toString());

// Don't accept removal requests when we're managing players
if ((!plugin.rPlayerLinkP.containsKey(id) || !plugin.rPlayerLinkP.get(id).equalsIgnoreCase(plugin.api.getName()))) {
if ((!plugin.rPlayerLinkP.containsKey(id) || !plugin.rPlayerLinkP.get(id).equalsIgnoreCase(plugin.api.getName().toLowerCase()))) {
plugin.rPlayerLinkS.remove(id);
plugin.rPlayerLinkP.remove(id);
plugin.rPlayers.remove(id);
Expand Down

0 comments on commit 6e60138

Please sign in to comment.