DataConverter -> PacketUtils

This commit is contained in:
RayzsYT committed 2024-05-22 18:51:56 +02:00
1 parent a88de9437a
commit 6c6ee7036e
7 files changed
+237 -230

No files matched your search

@@ -7,7 +7,7 @@ import de.rayzs.pat.api.storage.blacklist.impl.GeneralBlacklist;
import de.rayzs.pat.plugin.BukkitLoader;
import de.rayzs.pat.api.communication.impl.*;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.utils.DataConverter;
import de.rayzs.pat.utils.PacketUtil;
import de.rayzs.pat.utils.ExpireCache;
import de.rayzs.pat.utils.Reflection;
import de.rayzs.pat.utils.group.TinyGroup;
@@ -31,12 +31,12 @@ public class ClientCommunication {
}
public static void sendPacket(ClientInfo clientInfo, Object packet) {
clientInfo.sendBytes(DataConverter.convertToBytes(packet));
clientInfo.sendBytes(PacketUtil.convertToBytes(packet));
}
public static void receiveInformation(String serverName, Object packet) {
if(packet instanceof DataConverter.RequestPacket) {
DataConverter.RequestPacket requestPacket = (DataConverter.RequestPacket) packet;
if(packet instanceof PacketUtil.RequestPacket) {
PacketUtil.RequestPacket requestPacket = (PacketUtil.RequestPacket) packet;
if (!requestPacket.isToken(Storage.TOKEN)) return;
ClientInfo client = getClientByName(serverName);
@@ -51,15 +51,15 @@ public class ClientCommunication {
return;
}
if(packet instanceof DataConverter.BackendDataPacket) {
DataConverter.BackendDataPacket backendDataPacket = (DataConverter.BackendDataPacket) packet;
if(packet instanceof PacketUtil.BackendDataPacket) {
PacketUtil.BackendDataPacket backendDataPacket = (PacketUtil.BackendDataPacket) packet;
if (!backendDataPacket.isToken(Storage.TOKEN)) return;
Storage.SERVER_NAME = backendDataPacket.getServerName();
return;
}
if(packet instanceof DataConverter.FeedbackPacket) {
DataConverter.FeedbackPacket feedbackPacket = (DataConverter.FeedbackPacket) packet;
if(packet instanceof PacketUtil.FeedbackPacket) {
PacketUtil.FeedbackPacket feedbackPacket = (PacketUtil.FeedbackPacket) packet;
if (!feedbackPacket.isToken(Storage.TOKEN)) return;
String serverId = feedbackPacket.getServerId();
@@ -75,18 +75,18 @@ public class ClientCommunication {
client.syncTime();
SERVER_DATA_SYNC_COUNT++;
DataConverter.BackendDataPacket backendDataPacket = new DataConverter.BackendDataPacket(Storage.TOKEN, serverId, serverName);
PacketUtil.BackendDataPacket backendDataPacket = new PacketUtil.BackendDataPacket(Storage.TOKEN, serverId, serverName);
sendPacket(client, backendDataPacket);
return;
}
if(packet instanceof DataConverter.PacketBundle) {
DataConverter.PacketBundle packetBundle = (DataConverter.PacketBundle) packet;
if(packet instanceof PacketUtil.PacketBundle) {
PacketUtil.PacketBundle packetBundle = (PacketUtil.PacketBundle) packet;
if(!packetBundle.isToken(Storage.TOKEN) || !packetBundle.isId(SERVER_ID.toString())) return;
DataConverter.CommandsPacket commandsPacket = packetBundle.getCommandsPacket();
DataConverter.GroupsPacket groupsPacket = packetBundle.getGroupsPacket();
DataConverter.UnknownCommandPacket unknownCommandPacket = packetBundle.getUnknownCommandPacket();
PacketUtil.CommandsPacket commandsPacket = packetBundle.getCommandsPacket();
PacketUtil.GroupsPacket groupsPacket = packetBundle.getGroupsPacket();
PacketUtil.UnknownCommandPacket unknownCommandPacket = packetBundle.getUnknownCommandPacket();
LAST_BUKKIT_SYNC = System.currentTimeMillis();
BukkitLoader.synchronizeCommandData(commandsPacket, unknownCommandPacket);
@@ -113,9 +113,9 @@ public class ClientCommunication {
if(SERVER_DATA_SYNC_COUNT <= 0) SERVER_DATA_SYNC_COUNT = 0;
}
DataConverter.CommandsPacket commandsPacket = new DataConverter.CommandsPacket();
DataConverter.GroupsPacket groupsPacket;
DataConverter.PacketBundle bundle;
PacketUtil.CommandsPacket commandsPacket = new PacketUtil.CommandsPacket();
PacketUtil.GroupsPacket groupsPacket;
PacketUtil.PacketBundle bundle;
List<String> commands = new ArrayList<>(Storage.Blacklist.getBlacklist().getCommands());
if(clientInfo != null && clientInfo.getName() != null) {
@@ -129,24 +129,24 @@ public class ClientCommunication {
List<TinyGroup> newGroupList = new ArrayList<>();
groups.forEach(oldGroup -> newGroupList.add(GroupManager.convertToTinyGroup(oldGroup.getGroupName(), oldGroup.getCommands(serverName))));
groupsPacket = new DataConverter.GroupsPacket(newGroupList);
groupsPacket = new PacketUtil.GroupsPacket(newGroupList);
commandsPacket.setTurnBlacklistToWhitelist(Storage.ConfigSections.Settings.TURN_BLACKLIST_TO_WHITELIST.ENABLED);
commandsPacket.setCommands(commands);
bundle = new DataConverter.PacketBundle(Storage.TOKEN, serverId, commandsPacket, groupsPacket);
clientInfo.sendBytes(DataConverter.convertToBytes(bundle));
bundle = new PacketUtil.PacketBundle(Storage.TOKEN, serverId, commandsPacket, groupsPacket);
clientInfo.sendBytes(PacketUtil.convertToBytes(bundle));
}
public static void sendRequest() {
if(System.currentTimeMillis() - LAST_SENT_REQUEST >= 5000) {
LAST_SENT_REQUEST = System.currentTimeMillis();
sendPacket(new DataConverter.RequestPacket(Storage.TOKEN, SERVER_ID.toString()));
sendPacket(new PacketUtil.RequestPacket(Storage.TOKEN, SERVER_ID.toString()));
}
}
public static void sendFeedback() {
sendPacket(new DataConverter.FeedbackPacket(Storage.TOKEN, SERVER_ID.toString()));
sendPacket(new PacketUtil.FeedbackPacket(Storage.TOKEN, SERVER_ID.toString()));
}
public static Client getClient() {
@@ -1,8 +1,7 @@
package de.rayzs.pat.api.communication.impl;
import de.rayzs.pat.utils.DataConverter;
import de.rayzs.pat.utils.PacketUtil;
import org.bukkit.*;
import com.google.common.io.*;
import org.bukkit.entity.Player;
import de.rayzs.pat.plugin.BukkitLoader;
import de.rayzs.pat.api.communication.*;
@@ -19,7 +18,7 @@ public class BukkitClient implements Client, PluginMessageListener {
@Override
public void send(Object packet) {
try { SERVER.sendPluginMessage(BukkitLoader.getPlugin(), CHANNEL_NAME, DataConverter.convertToBytes(packet));
try { SERVER.sendPluginMessage(BukkitLoader.getPlugin(), CHANNEL_NAME, PacketUtil.convertToBytes(packet));
} catch (Throwable throwable) { throwable.printStackTrace(); }
}
@@ -27,8 +26,8 @@ public class BukkitClient implements Client, PluginMessageListener {
public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
if(!channel.equals(CHANNEL_NAME)) return;
try {
Object packetObj = DataConverter.buildFromBytes(bytes);
if(!DataConverter.isPacket(packetObj)) return;
Object packetObj = PacketUtil.buildFromBytes(bytes);
if(!PacketUtil.isPacket(packetObj)) return;
Bukkit.getScheduler().scheduleSyncDelayedTask(BukkitLoader.getPlugin(), () -> ClientCommunication.receiveInformation("proxy", packetObj));
} catch (Throwable throwable) { throwable.printStackTrace(); }
@@ -1,8 +1,6 @@
package de.rayzs.pat.api.communication.impl;
import java.io.*;
import de.rayzs.pat.utils.DataConverter;
import de.rayzs.pat.utils.PacketUtil;
import net.md_5.bungee.api.ProxyServer;
import de.rayzs.pat.plugin.BungeeLoader;
import de.rayzs.pat.api.communication.*;
@@ -24,7 +22,7 @@ public class BungeeClient implements Client, Listener {
@Override
public void send(Object packet) {
for(ServerInfo serverInfo : ProxyServer.getInstance().getServers().values()) {
try { serverInfo.sendData(CHANNEL_NAME, DataConverter.convertToBytes(packet));
try { serverInfo.sendData(CHANNEL_NAME, PacketUtil.convertToBytes(packet));
} catch (Throwable throwable) { throwable.printStackTrace(); }
}
}
@@ -33,8 +31,8 @@ public class BungeeClient implements Client, Listener {
public void onQueryReceive(PluginMessageEvent event) {
if (!event.getTag().equalsIgnoreCase(CHANNEL_NAME)) return;
try {
Object packetObj = DataConverter.buildFromBytes(event.getData());
if(!DataConverter.isPacket(packetObj)) return;
Object packetObj = PacketUtil.buildFromBytes(event.getData());
if(!PacketUtil.isPacket(packetObj)) return;
Server server = (Server) event.getSender();
ClientCommunication.receiveInformation(server.getInfo().getName(), packetObj);
@@ -9,7 +9,7 @@ import com.velocitypowered.api.proxy.server.RegisteredServer;
import de.rayzs.pat.plugin.VelocityLoader;
import de.rayzs.pat.api.communication.Client;
import de.rayzs.pat.api.communication.ClientCommunication;
import de.rayzs.pat.utils.DataConverter;
import de.rayzs.pat.utils.PacketUtil;
public class VelocityClient implements Client {
@@ -24,7 +24,7 @@ public class VelocityClient implements Client {
@Override
public void send(Object packet) {
for (RegisteredServer registeredServer : SERVER.getAllServers()) {
try { registeredServer.sendPluginMessage(IDENTIFIER, DataConverter.convertToBytes(packet));
try { registeredServer.sendPluginMessage(IDENTIFIER, PacketUtil.convertToBytes(packet));
} catch (Throwable throwable) { throwable.printStackTrace(); }
}
}
@@ -32,8 +32,8 @@ public class VelocityClient implements Client {
@Subscribe
public void onQueryReceive(PluginMessageEvent event) {
if (event.getIdentifier() != IDENTIFIER) return;
Object packetObj = DataConverter.buildFromBytes(event.getData());
if(!DataConverter.isPacket(packetObj)) return;
Object packetObj = PacketUtil.buildFromBytes(event.getData());
if(!PacketUtil.isPacket(packetObj)) return;
ServerConnection server = (ServerConnection) event.getSource();
ClientCommunication.receiveInformation(server.getServerInfo().getName(), packetObj);
@@ -1,5 +1,6 @@
package de.rayzs.pat.plugin.process;
import de.rayzs.pat.api.communication.BackendUpdater;
import de.rayzs.pat.api.communication.client.ClientInfo;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.api.storage.blacklist.BlacklistCreator;
@@ -73,6 +74,7 @@ public class CommandProcess {
CustomServerBrand.initialize();
GroupManager.clearAllGroups();
GroupManager.initialize();
handleReload();
if(!backend) handleChange();
sender.sendMessage(Storage.ConfigSections.Messages.RELOAD.DONE);
@@ -568,4 +570,11 @@ public class CommandProcess {
if (Reflection.getMinor() >= 18) BukkitAntiTabListener.updateCommands();
}
}
private static void handleReload() {
if(!Reflection.isProxyServer()) {
BackendUpdater.stop();
BackendUpdater.start();
}
}
}
@@ -1,192 +1,192 @@
package de.rayzs.pat.utils;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.utils.configuration.helper.MultipleMessagesHelper;
import de.rayzs.pat.utils.group.TinyGroup;
import java.io.*;
import java.util.List;
public class DataConverter {
/*
Code originally from:
https://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array
*/
public static byte[] convertToBytes(Object obj) {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
try (ObjectOutputStream outputStream = new ObjectOutputStream(arrayOutputStream)) {
outputStream.writeObject(obj);
outputStream.flush();
return arrayOutputStream.toByteArray();
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
public static Object buildFromBytes(byte[] bytes) {
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
try (ObjectInput input = new ObjectInputStream(arrayInputStream)) {
return input.readObject();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static boolean isPacket(Object object) {
return object instanceof CommunicationPacket;
}
public interface CommunicationPacket {}
public static class BackendDataPacket implements CommunicationPacket, Serializable {
private final String proxyToken, serverName, serverId;
public BackendDataPacket(String proxyToken, String serverId, String serverName) {
this.proxyToken = proxyToken;
this.serverId = serverId;
this.serverName = serverName;
}
public String getServerId() {
return serverId;
}
public String getServerName() {
return serverName;
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
}
public static class FeedbackPacket implements CommunicationPacket, Serializable {
private final String proxyToken, serverId;
public FeedbackPacket(String proxyToken, String serverId) {
this.proxyToken = proxyToken;
this.serverId = serverId;
}
public String getServerId() {
return serverId;
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
}
public static class RequestPacket implements CommunicationPacket, Serializable {
private final String proxyToken, serverId;
public RequestPacket(String proxyToken, String serverId) {
this.proxyToken = proxyToken;
this.serverId = serverId;
}
public String getServerId() {
return serverId;
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
}
public static class PacketBundle implements CommunicationPacket, Serializable {
private final CommandsPacket commandsPacket;
private final GroupsPacket groupsPacket;
private final UnknownCommandPacket unknownCommandPacket;
private final String proxyToken, serverId;
public PacketBundle(String proxyToken, String serverId, CommandsPacket commandsPacket, GroupsPacket groupsPacket) {
this.proxyToken = proxyToken;
this.serverId = serverId;
this.commandsPacket = commandsPacket;
this.groupsPacket = groupsPacket;
this.unknownCommandPacket = new UnknownCommandPacket();
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
public boolean isId(String id) {
return idIgnored() || serverId.equals(id);
}
public boolean idIgnored() {
return serverId == null;
}
public CommandsPacket getCommandsPacket() {
return commandsPacket;
}
public GroupsPacket getGroupsPacket() {
return groupsPacket;
}
public UnknownCommandPacket getUnknownCommandPacket() { return unknownCommandPacket; }
}
public static class UnknownCommandPacket implements Serializable {
private final MultipleMessagesHelper message;
private final boolean enabled;
public UnknownCommandPacket() {
enabled = Storage.ConfigSections.Settings.CUSTOM_UNKNOWN_COMMAND.ENABLED;
message = Storage.ConfigSections.Settings.CUSTOM_UNKNOWN_COMMAND.MESSAGE;
}
public MultipleMessagesHelper getMessage() {
return message;
}
public boolean isEnabled() {
return enabled;
}
}
public static class CommandsPacket implements Serializable {
private List<String> commands;
private boolean turnBlacklistToWhitelist;
public void setCommands(List<String> commands) {
this.commands = commands;
}
public void setTurnBlacklistToWhitelist(boolean turnBlacklistToWhitelist) {
this.turnBlacklistToWhitelist = turnBlacklistToWhitelist;
}
public boolean turnBlacklistToWhitelistEnabled() {
return turnBlacklistToWhitelist;
}
public List<String> getCommands() {
return commands;
}
}
public static class GroupsPacket implements Serializable {
private final List<TinyGroup> groups;
public GroupsPacket(List<TinyGroup> groups) {
this.groups = groups;
}
public List<TinyGroup> getGroups() {
return groups;
}
}
}
package de.rayzs.pat.utils;
import de.rayzs.pat.api.storage.Storage;
import de.rayzs.pat.utils.configuration.helper.MultipleMessagesHelper;
import de.rayzs.pat.utils.group.TinyGroup;
import java.io.*;
import java.util.List;
public class PacketUtil {
/*
Code originally from:
https://stackoverflow.com/questions/2836646/java-serializable-object-to-byte-array
*/
public static byte[] convertToBytes(Object obj) {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
try (ObjectOutputStream outputStream = new ObjectOutputStream(arrayOutputStream)) {
outputStream.writeObject(obj);
outputStream.flush();
return arrayOutputStream.toByteArray();
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
public static Object buildFromBytes(byte[] bytes) {
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
try (ObjectInput input = new ObjectInputStream(arrayInputStream)) {
return input.readObject();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static boolean isPacket(Object object) {
return object instanceof CommunicationPacket;
}
public interface CommunicationPacket {}
public static class BackendDataPacket implements CommunicationPacket, Serializable {
private final String proxyToken, serverName, serverId;
public BackendDataPacket(String proxyToken, String serverId, String serverName) {
this.proxyToken = proxyToken;
this.serverId = serverId;
this.serverName = serverName;
}
public String getServerId() {
return serverId;
}
public String getServerName() {
return serverName;
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
}
public static class FeedbackPacket implements CommunicationPacket, Serializable {
private final String proxyToken, serverId;
public FeedbackPacket(String proxyToken, String serverId) {
this.proxyToken = proxyToken;
this.serverId = serverId;
}
public String getServerId() {
return serverId;
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
}
public static class RequestPacket implements CommunicationPacket, Serializable {
private final String proxyToken, serverId;
public RequestPacket(String proxyToken, String serverId) {
this.proxyToken = proxyToken;
this.serverId = serverId;
}
public String getServerId() {
return serverId;
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
}
public static class PacketBundle implements CommunicationPacket, Serializable {
private final CommandsPacket commandsPacket;
private final GroupsPacket groupsPacket;
private final UnknownCommandPacket unknownCommandPacket;
private final String proxyToken, serverId;
public PacketBundle(String proxyToken, String serverId, CommandsPacket commandsPacket, GroupsPacket groupsPacket) {
this.proxyToken = proxyToken;
this.serverId = serverId;
this.commandsPacket = commandsPacket;
this.groupsPacket = groupsPacket;
this.unknownCommandPacket = new UnknownCommandPacket();
}
public boolean isToken(String token) {
return proxyToken.equals(token);
}
public boolean isId(String id) {
return idIgnored() || serverId.equals(id);
}
public boolean idIgnored() {
return serverId == null;
}
public CommandsPacket getCommandsPacket() {
return commandsPacket;
}
public GroupsPacket getGroupsPacket() {
return groupsPacket;
}
public UnknownCommandPacket getUnknownCommandPacket() { return unknownCommandPacket; }
}
public static class UnknownCommandPacket implements Serializable {
private final MultipleMessagesHelper message;
private final boolean enabled;
public UnknownCommandPacket() {
enabled = Storage.ConfigSections.Settings.CUSTOM_UNKNOWN_COMMAND.ENABLED;
message = Storage.ConfigSections.Settings.CUSTOM_UNKNOWN_COMMAND.MESSAGE;
}
public MultipleMessagesHelper getMessage() {
return message;
}
public boolean isEnabled() {
return enabled;
}
}
public static class CommandsPacket implements Serializable {
private List<String> commands;
private boolean turnBlacklistToWhitelist;
public void setCommands(List<String> commands) {
this.commands = commands;
}
public void setTurnBlacklistToWhitelist(boolean turnBlacklistToWhitelist) {
this.turnBlacklistToWhitelist = turnBlacklistToWhitelist;
}
public boolean turnBlacklistToWhitelistEnabled() {
return turnBlacklistToWhitelist;
}
public List<String> getCommands() {
return commands;
}
}
public static class GroupsPacket implements Serializable {
private final List<TinyGroup> groups;
public GroupsPacket(List<TinyGroup> groups) {
this.groups = groups;
}
public List<TinyGroup> getGroups() {
return groups;
}
}
}