Skip to content

Commit

Permalink
Switch from open/close to unlock/lock
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeafCreeper committed Oct 10, 2023
1 parent 15f653c commit f181774
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package group.aelysium.rustyconnector.core.lib.packets;

import com.google.gson.*;
import group.aelysium.rustyconnector.core.lib.packets.variants.CloseServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.LockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.CoordinateRequestQueuePacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.OpenServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.UnlockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.SendPlayerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.ServerPingPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.ServerPingResponsePacket;
Expand Down Expand Up @@ -189,8 +189,8 @@ public GenericPacket buildReceived() {
if (this.type == PacketType.PING_RESPONSE) return new ServerPingResponsePacket(this.protocolVersion, this.rawMessage, this.address, this.origin, this.parameters);
if (this.type == PacketType.SEND_PLAYER) return new SendPlayerPacket(this.protocolVersion, this.rawMessage, this.address, this.origin, this.parameters);
if (this.type == PacketType.COORDINATE_REQUEST_QUEUE) return new CoordinateRequestQueuePacket(this.protocolVersion, this.rawMessage, this.address, this.origin, this.parameters);
if (this.type == PacketType.OPEN_SERVER) return new OpenServerPacket(this.address, this.origin, this.parameters);
if (this.type == PacketType.CLOSE_SERVER) return new CloseServerPacket(this.address, this.origin, this.parameters);
if (this.type == PacketType.UNLOCK_SERVER) return new UnlockServerPacket(this.address, this.origin, this.parameters);
if (this.type == PacketType.LOCK_SERVER) return new LockServerPacket(this.address, this.origin, this.parameters);

throw new IllegalStateException("Invalid RedisMessage type encountered!");
}
Expand All @@ -217,8 +217,8 @@ public GenericPacket buildSendable() {
if(this.type == PacketType.PING_RESPONSE) return new ServerPingResponsePacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.SEND_PLAYER) return new SendPlayerPacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.COORDINATE_REQUEST_QUEUE) return new CoordinateRequestQueuePacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.OPEN_SERVER) return new OpenServerPacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.CLOSE_SERVER) return new CloseServerPacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.UNLOCK_SERVER) return new UnlockServerPacket(this.address, this.origin, this.parameters);
if(this.type == PacketType.LOCK_SERVER) return new LockServerPacket(this.address, this.origin, this.parameters);

throw new IllegalStateException("Invalid RedisMessage type encountered!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public class PacketType {
/**
* `Server > Proxy` | Tells the proxy to open a server.
*/
public static Mapping OPEN_SERVER = new Mapping(400, "OPEN_SERVER");
public static Mapping UNLOCK_SERVER = new Mapping(400, "UNLOCK_SERVER");

/**
* `Server > Proxy` | Tells the proxy to close a server.
*/
public static Mapping CLOSE_SERVER = new Mapping(401, "CLOSE_SERVER");
public static Mapping LOCK_SERVER = new Mapping(401, "LOCK_SERVER");

public static List<Mapping> toList() {
List<Mapping> list = new ArrayList<>();
list.add(PING);
list.add(PING_RESPONSE);
list.add(SEND_PLAYER);
list.add(COORDINATE_REQUEST_QUEUE);
list.add(OPEN_SERVER);
list.add(CLOSE_SERVER);
list.add(UNLOCK_SERVER);
list.add(LOCK_SERVER);

return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@
import java.net.InetSocketAddress;
import java.util.List;

public class CloseServerPacket extends GenericPacket {
public class LockServerPacket extends GenericPacket {
private String serverName;

public String serverName() { return this.serverName; }

public CloseServerPacket(InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(PacketType.CLOSE_SERVER, address, origin);
public LockServerPacket(InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(PacketType.LOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
JsonPrimitive value = entry.getValue();

if (key.equals(OpenServerPacket.ValidParameters.SERVER_NAME)) {
if (key.equals(UnlockServerPacket.ValidParameters.SERVER_NAME)) {
this.serverName = value.getAsString();
}
});
}
public CloseServerPacket(int messageVersion, String rawMessage, InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(messageVersion, rawMessage, PacketType.CLOSE_SERVER, address, origin);
public LockServerPacket(int messageVersion, String rawMessage, InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(messageVersion, rawMessage, PacketType.LOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
JsonPrimitive value = entry.getValue();

if (key.equals(OpenServerPacket.ValidParameters.SERVER_NAME)) {
if (key.equals(UnlockServerPacket.ValidParameters.SERVER_NAME)) {
this.serverName = value.getAsString();
}
});
Expand All @@ -45,7 +45,7 @@ public JsonObject toJSON() {
JsonObject object = super.toJSON();
JsonObject parameters = new JsonObject();

parameters.add(OpenServerPacket.ValidParameters.SERVER_NAME, new JsonPrimitive(this.serverName));
parameters.add(UnlockServerPacket.ValidParameters.SERVER_NAME, new JsonPrimitive(this.serverName));

object.add(MasterValidParameters.PARAMETERS, parameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketOrigin;
import group.aelysium.rustyconnector.core.lib.packets.PacketType;
import group.aelysium.rustyconnector.core.lib.util.ColorMapper;
import io.lettuce.core.KeyValue;
import net.kyori.adventure.text.format.NamedTextColor;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class OpenServerPacket extends GenericPacket {
public class UnlockServerPacket extends GenericPacket {
private String serverName;

public String serverName() { return this.serverName; }

public OpenServerPacket(InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(PacketType.OPEN_SERVER, address, origin);
public UnlockServerPacket(InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(PacketType.UNLOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
Expand All @@ -31,8 +28,8 @@ public OpenServerPacket(InetSocketAddress address, PacketOrigin origin, List<Key
}
});
}
public OpenServerPacket(int messageVersion, String rawMessage, InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(messageVersion, rawMessage, PacketType.OPEN_SERVER, address, origin);
public UnlockServerPacket(int messageVersion, String rawMessage, InetSocketAddress address, PacketOrigin origin, List<KeyValue<String, JsonPrimitive>> parameters) {
super(messageVersion, rawMessage, PacketType.UNLOCK_SERVER, address, origin);

parameters.forEach(entry -> {
String key = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static void create(PaperCommandManager<CommandSender> manager) {
manager.command(messageList(manager));
manager.command(messageGet(manager));
manager.command(send(manager));
manager.command(open(manager));
manager.command(close(manager));
manager.command(unlock(manager));
manager.command(lock(manager));
}

private static Command.Builder<CommandSender> messageGet(PaperCommandManager<CommandSender> manager) {
Expand Down Expand Up @@ -117,18 +117,18 @@ private static Command.Builder<CommandSender> send(PaperCommandManager<CommandSe
}).execute());
}

private static Command.Builder<CommandSender> open(PaperCommandManager<CommandSender> manager) {
private static Command.Builder<CommandSender> unlock(PaperCommandManager<CommandSender> manager) {
Tinder api = Tinder.get();
PluginLogger logger = api.logger();

final Command.Builder<CommandSender> builder = api.commandManager().commandBuilder("rc", "/rc");

return builder.literal("open")
return builder.literal("unlock")
.senderType(ConsoleCommandSender.class)
.handler(context -> manager.taskRecipe().begin(context)
.asynchronous(commandContext -> {
try {
api.services().packetBuilder().openServer();
api.services().packetBuilder().unlockServer();
} catch (NullPointerException e) {
PaperLang.RC_SEND_USAGE.send(logger);
} catch (Exception e) {
Expand All @@ -137,18 +137,18 @@ private static Command.Builder<CommandSender> open(PaperCommandManager<CommandSe
}).execute());
}

private static Command.Builder<CommandSender> close(PaperCommandManager<CommandSender> manager) {
private static Command.Builder<CommandSender> lock(PaperCommandManager<CommandSender> manager) {
Tinder api = Tinder.get();
PluginLogger logger = api.logger();

final Command.Builder<CommandSender> builder = api.commandManager().commandBuilder("rc", "/rc");

return builder.literal("close")
return builder.literal("lock")
.senderType(ConsoleCommandSender.class)
.handler(context -> manager.taskRecipe().begin(context)
.asynchronous(commandContext -> {
try {
api.services().packetBuilder().closeServer();
api.services().packetBuilder().lockServer();
} catch (NullPointerException e) {
PaperLang.RC_SEND_USAGE.send(logger);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketOrigin;
import group.aelysium.rustyconnector.core.lib.packets.PacketType;
import group.aelysium.rustyconnector.core.lib.packets.variants.CloseServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.OpenServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.LockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.UnlockServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.SendPlayerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.ServerPingPacket;
import group.aelysium.rustyconnector.core.lib.lang_messaging.Lang;
Expand Down Expand Up @@ -60,15 +60,15 @@ public void sendToOtherFamily(Player player, String familyName) {
/**
* Tells the proxy to open the server running the command.
*/
public void openServer() {
public void unlockServer() {
Tinder api = Tinder.get();
ServerInfoService serverInfoService = api.services().serverInfo();

OpenServerPacket message = (OpenServerPacket) new GenericPacket.Builder()
.setType(PacketType.OPEN_SERVER)
UnlockServerPacket message = (UnlockServerPacket) new GenericPacket.Builder()
.setType(PacketType.UNLOCK_SERVER)
.setOrigin(PacketOrigin.SERVER)
.setAddress(serverInfoService.address())
.setParameter(OpenServerPacket.ValidParameters.SERVER_NAME, serverInfoService.name())
.setParameter(UnlockServerPacket.ValidParameters.SERVER_NAME, serverInfoService.name())
.buildSendable();

api.flame().backbone().connection().orElseThrow().publish(message);
Expand All @@ -77,15 +77,15 @@ public void openServer() {
/**
* Tells the proxy to close the server running the command.
*/
public void closeServer() {
public void lockServer() {
Tinder api = Tinder.get();
ServerInfoService serverInfoService = api.services().serverInfo();

CloseServerPacket message = (CloseServerPacket) new GenericPacket.Builder()
.setType(PacketType.CLOSE_SERVER)
LockServerPacket message = (LockServerPacket) new GenericPacket.Builder()
.setType(PacketType.LOCK_SERVER)
.setOrigin(PacketOrigin.SERVER)
.setAddress(serverInfoService.address())
.setParameter(OpenServerPacket.ValidParameters.SERVER_NAME, serverInfoService.name())
.setParameter(UnlockServerPacket.ValidParameters.SERVER_NAME, serverInfoService.name())
.buildSendable();

api.flame().backbone().connection().orElseThrow().publish(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import group.aelysium.rustyconnector.core.lib.connectors.storage.StorageConnector;
import group.aelysium.rustyconnector.core.lib.data_transit.DataTransitService;
import group.aelysium.rustyconnector.core.lib.data_transit.cache.MessageCacheService;
import group.aelysium.rustyconnector.core.lib.exception.NoOutputException;
import group.aelysium.rustyconnector.core.lib.hash.AESCryptor;
import group.aelysium.rustyconnector.core.lib.lang_messaging.Lang;
import group.aelysium.rustyconnector.core.lib.packets.PacketHandler;
Expand Down Expand Up @@ -48,8 +47,8 @@
import group.aelysium.rustyconnector.plugin.velocity.lib.load_balancing.LoadBalancingService;
import group.aelysium.rustyconnector.plugin.velocity.lib.magic_link.MagicLinkService;
import group.aelysium.rustyconnector.plugin.velocity.lib.magic_link.handlers.MagicLinkPingHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.CloseServerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.OpenServerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.LockServerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.UnlockServerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.message.handling.SendPlayerHandler;
import group.aelysium.rustyconnector.plugin.velocity.lib.parties.PartyService;
import group.aelysium.rustyconnector.plugin.velocity.lib.parties.config.PartyConfig;
Expand All @@ -68,7 +67,6 @@
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.util.*;
Expand Down Expand Up @@ -358,8 +356,8 @@ public Callable<Runnable> connectors(AESCryptor cryptor, MessageCacheService cac
Map<PacketType.Mapping, PacketHandler> handlers = new HashMap<>();
handlers.put(PacketType.PING, new MagicLinkPingHandler());
handlers.put(PacketType.SEND_PLAYER, new SendPlayerHandler());
handlers.put(PacketType.OPEN_SERVER, new OpenServerHandler());
handlers.put(PacketType.CLOSE_SERVER, new CloseServerHandler());
handlers.put(PacketType.UNLOCK_SERVER, new UnlockServerHandler());
handlers.put(PacketType.LOCK_SERVER, new LockServerHandler());

connectorsService.messengers().forEach(connector -> {
if(connector.connection().isEmpty()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ public void removeServer(PlayerServer server) {
this.closedServers.remove(server);
}

public void openServer(PlayerServer server) {
public void unlockServer(PlayerServer server) {
if (!this.closedServers.contains(server)) return;
this.closedServers.remove(server);
this.loadBalancer.add(server);

this.loadBalancer.completeSort();
}

public void closeServer(PlayerServer server) {
public void lockServer(PlayerServer server) {
if (!this.loadBalancer.dump().contains(server)) return;
this.loadBalancer.remove(server);
this.closedServers.add(server);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
import com.velocitypowered.api.proxy.server.ServerInfo;
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketHandler;
import group.aelysium.rustyconnector.core.lib.packets.variants.OpenServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.LockServerPacket;
import group.aelysium.rustyconnector.plugin.velocity.central.Tinder;
import group.aelysium.rustyconnector.plugin.velocity.lib.family.bases.PlayerFocusedServerFamily;
import group.aelysium.rustyconnector.plugin.velocity.lib.server.PlayerServer;

public class OpenServerHandler extends PacketHandler {
public class LockServerHandler extends PacketHandler {
@Override
public void execute(GenericPacket genericPacket) throws Exception {
OpenServerPacket packet = (OpenServerPacket) genericPacket;
LockServerPacket packet = (LockServerPacket) genericPacket;
Tinder api = Tinder.get();

ServerInfo serverInfo = new ServerInfo(packet.serverName(), packet.address());
PlayerServer server = api.services().serverService().search(serverInfo);

if (server != null) {
PlayerFocusedServerFamily family = (PlayerFocusedServerFamily) server.family();
family.openServer(server);
family.lockServer(server);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
import com.velocitypowered.api.proxy.server.ServerInfo;
import group.aelysium.rustyconnector.core.lib.packets.GenericPacket;
import group.aelysium.rustyconnector.core.lib.packets.PacketHandler;
import group.aelysium.rustyconnector.core.lib.packets.variants.CloseServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.OpenServerPacket;
import group.aelysium.rustyconnector.core.lib.packets.variants.UnlockServerPacket;
import group.aelysium.rustyconnector.plugin.velocity.central.Tinder;
import group.aelysium.rustyconnector.plugin.velocity.lib.family.bases.PlayerFocusedServerFamily;
import group.aelysium.rustyconnector.plugin.velocity.lib.server.PlayerServer;

import java.lang.ref.WeakReference;
import java.util.Objects;
import java.util.Vector;

public class CloseServerHandler extends PacketHandler {
public class UnlockServerHandler extends PacketHandler {
@Override
public void execute(GenericPacket genericPacket) throws Exception {
CloseServerPacket packet = (CloseServerPacket) genericPacket;
UnlockServerPacket packet = (UnlockServerPacket) genericPacket;
Tinder api = Tinder.get();

ServerInfo serverInfo = new ServerInfo(packet.serverName(), packet.address());
PlayerServer server = api.services().serverService().search(serverInfo);

if (server != null) {
PlayerFocusedServerFamily family = (PlayerFocusedServerFamily) server.family();
family.closeServer(server);
family.unlockServer(server);
}
}
}

0 comments on commit f181774

Please sign in to comment.