Better Data Sync

This commit is contained in:
RayzsYT committed 2024-05-14 15:31:09 +02:00
1 parent 2b899fd6f0
commit c4e0174400
5 files changed
+46 -5

No files matched your search

@@ -36,8 +36,8 @@ public class BukkitLoader extends JavaPlugin {
plugin = this;
logger = getLogger();
Storage.CURRENT_VERSION_NAME = getDescription().getVersion();
Reflection.initialize(getServer());
Storage.CURRENT_VERSION_NAME = getDescription().getVersion();
Storage.load();
MessageTranslator.initialize();
@@ -77,6 +77,7 @@ public class BukkitLoader extends JavaPlugin {
}
public static void synchronizeCommandData(boolean turn, List<String> commands) {
ClientCommunication.sendFeedback();
if (!Storage.BLOCKED_COMMANDS_LIST.containsAll(commands) || !commands.containsAll(Storage.BLOCKED_COMMANDS_LIST))
Storage.BLOCKED_COMMANDS_LIST = commands;
@@ -37,8 +37,8 @@ public class BungeeLoader extends Plugin {
plugin = this;
logger = getLogger();
Storage.CURRENT_VERSION_NAME = getDescription().getVersion();
Reflection.initialize(getProxy());
Storage.CURRENT_VERSION_NAME = getDescription().getVersion();
Storage.load();
MessageTranslator.initialize();
@@ -8,12 +8,14 @@ import java.util.*;
public class ClientCommunication {
private static final Client CLIENT = Reflection.isVelocityServer() ? new VelocityClient() : Reflection.isProxyServer() ? new BungeeClient() : new BukkitClient();
private static final UUID SERVER_ID = UUID.randomUUID();
public static final List<ClientInfo> CLIENTS = new ArrayList<>();
public static void sendInformation(String information) {
CLIENT.sendInformation(information);
}
public static void receiveInformation(String information) {
public static void receiveInformation(String serverName, String information) {
if(!information.contains("::")) return;
String[] args = information.split("::");
@@ -22,18 +24,42 @@ public class ClientCommunication {
if(args.length != 2 || !args[1].equals(Storage.TOKEN_KEY)) return;
synchronizeInformation();
break;
case "received":
if(args.length != 3 || !args[1].equals(Storage.TOKEN_KEY)) return;
String id = args[2];
ClientInfo client = getClientById(id);
if(client == null) {
client = new ClientInfo(id, serverName);
CLIENTS.add(client);
}
else if(!client.getName().equals(serverName)) client.setName(serverName);
if(!client.hasSentFeedback()) {
System.out.println("Feedback from server: " + client.getName());
client.setFeedback(true);
client.syncTime();
Storage.SERVER_DATA_SYNC_COUNT++;
}
break;
case "receive-commands":
if(args.length != 4 || !args[1].equals(Storage.TOKEN_KEY)) return;
BukkitLoader.synchronizeCommandData(Boolean.parseBoolean(args[2]), args[3].equals("§") ? new ArrayList<>() : Arrays.asList(args[3].split(";")));
Storage.LAST_DATA_UPDATE = System.currentTimeMillis();
break;
case "receive-groups":
if(args.length != 3 || !args[1].equals(Storage.TOKEN_KEY)) return;
BukkitLoader.synchronizeGroupData(args[2]);
Storage.LAST_DATA_UPDATE = System.currentTimeMillis();
break;
}
}
public static void synchronizeInformation() {
Storage.LAST_DATA_UPDATE = System.currentTimeMillis();
Storage.SERVER_DATA_SYNC_COUNT = 0;
resetAllFeedbacks();
sendInformation("receive-commands::" + Storage.TOKEN_KEY + "::" + Storage.TURN_BLACKLIST_TO_WHITELIST + "::" + (DataConverter.convertCommandsToString(Storage.BLOCKED_COMMANDS_LIST)));
sendInformation("receive-groups::" + Storage.TOKEN_KEY + "::" + DataConverter.convertGroupsToString());
}
@@ -42,7 +68,19 @@ public class ClientCommunication {
sendInformation("request::" + Storage.TOKEN_KEY);
}
public static void sendFeedback() {
sendInformation("received::" + Storage.TOKEN_KEY + "::" + SERVER_ID);
}
public static Client getClient() {
return CLIENT;
}
public static ClientInfo getClientById(String id) {
return CLIENTS.stream().filter(client -> client.compareId(id)).findFirst().orElse(null);
}
public static void resetAllFeedbacks() {
CLIENTS.forEach(client -> client.setFeedback(false));
}
}
@@ -31,7 +31,7 @@ public class BukkitClient implements Client, PluginMessageListener {
try {
ByteArrayDataInput input = ByteStreams.newDataInput(bytes);
String information = input.readUTF();
Bukkit.getScheduler().scheduleSyncDelayedTask(BukkitLoader.getPlugin(), () -> ClientCommunication.receiveInformation(information));
Bukkit.getScheduler().scheduleSyncDelayedTask(BukkitLoader.getPlugin(), () -> ClientCommunication.receiveInformation("proxy", information));
} catch (Throwable throwable) { throwable.printStackTrace(); }
}
}
@@ -8,6 +8,7 @@ import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.event.PluginMessageEvent;
import org.bukkit.Server;
public class BungeeClient implements Client, Listener {
@@ -36,7 +37,8 @@ public class BungeeClient implements Client, Listener {
try {
DataInputStream input = new DataInputStream(new ByteArrayInputStream(event.getData()));
String information = input.readUTF();
ClientCommunication.receiveInformation(information);
Server server = (Server) event.getSender();
ClientCommunication.receiveInformation(server.getName(), information);
} catch (Throwable throwable) { throwable.printStackTrace(); }
}
}