Search in sources :

Example 1 with SubServerData

use of net.ME1312.SubServers.Velocity.Server.SubServerData in project SubServers-2 by ME1312.

the class SubCommand method suggest.

/**
 * Suggest command arguments
 *
 * @param invocation Command invocation
 * @return The validator's response and list of possible arguments
 */
@SuppressWarnings("unchecked")
public List<String> suggest(Invocation invocation) {
    CommandSource sender = invocation.source();
    String[] args = invocation.arguments();
    String Last = (args.length > 0) ? args[args.length - 1] : "";
    String last = Last.toLowerCase();
    ServerInfo pcs = (sender instanceof Player) ? ((Player) sender).getCurrentServer().map(ServerConnection::getServerInfo).orElse(null) : null;
    ServerData pcsd = plugin.getData(pcs);
    if (plugin.api.getSubDataNetwork()[0] == null) {
        if (sender instanceof ConsoleCommandSource)
            new IllegalStateException("SubData is not connected").printStackTrace();
        return Collections.emptyList();
    } else if (sender instanceof Player && (!permitted.containsKey(((Player) sender).getUniqueId()) || !permitted.get(((Player) sender).getUniqueId()).containsKey(pcs) || !permitted.get(((Player) sender).getUniqueId()).get(pcs).value())) {
        if (permitted.containsKey(((Player) sender).getUniqueId()) && permitted.get(((Player) sender).getUniqueId()).containsKey(pcs) && permitted.get(((Player) sender).getUniqueId()).get(pcs).key() == null) {
        // do nothing
        } else if (!permitted.containsKey(((Player) sender).getUniqueId()) || !permitted.get(((Player) sender).getUniqueId()).containsKey(pcs) || Calendar.getInstance().getTime().getTime() - permitted.get(((Player) sender).getUniqueId()).get(pcs).key() >= TimeUnit.MINUTES.toMillis(1)) {
            if (pcsd == null || pcsd.getSubData()[0] == null) {
                HashMap<ServerInfo, Pair<Long, Boolean>> map = (permitted.containsKey(((Player) sender).getUniqueId())) ? permitted.get(((Player) sender).getUniqueId()) : new HashMap<ServerInfo, Pair<Long, Boolean>>();
                map.put(pcs, new ContainedPair<>(Calendar.getInstance().getTime().getTime(), false));
                permitted.put(((Player) sender).getUniqueId(), map);
            } else {
                HashMap<ServerInfo, Pair<Long, Boolean>> map = (permitted.containsKey(((Player) sender).getUniqueId())) ? permitted.get(((Player) sender).getUniqueId()) : new HashMap<ServerInfo, Pair<Long, Boolean>>();
                map.put(pcs, new ContainedPair<>(null, false));
                permitted.put(((Player) sender).getUniqueId(), map);
                ((SubDataSender) pcsd.getSubData()[0]).sendPacket(new PacketCheckPermission(((Player) sender).getUniqueId(), "subservers.command", result -> {
                    map.put(pcs, new ContainedPair<>(Calendar.getInstance().getTime().getTime(), result));
                }));
            }
        }
        return Collections.emptyList();
    } else if (args.length <= 1) {
        List<String> cmds = new ArrayList<>();
        cmds.addAll(Arrays.asList("help", "list", "info", "status", "version", "start", "restart", "stop", "kill", "terminate", "cmd", "command", "create", "update", "upgrade", "restore"));
        if (!(sender instanceof Player))
            cmds.addAll(Arrays.asList("remove", "delete"));
        updateCache();
        List<String> list = new ArrayList<String>();
        for (String cmd : cmds) {
            if (cmd.startsWith(last))
                list.add(Last + cmd.substring(last.length()));
        }
        return list;
    } else {
        if (args[0].equals("info") || args[0].equals("status")) {
            Supplier<Collection<String>> getPlayers = () -> {
                LinkedList<String> names = new LinkedList<String>();
                for (Player player : ExProxy.getInstance().getAllPlayers()) names.add(player.getGameProfile().getName());
                for (CachedPlayer player : plugin.api.getRemotePlayers().values()) if (!names.contains(player.getName()))
                    names.add(player.getName());
                Collections.sort(names);
                return names;
            };
            updateCache();
            if (args.length == 2) {
                List<String> list = new ArrayList<String>();
                List<String> subcommands = new ArrayList<String>();
                subcommands.add("proxy");
                subcommands.add("host");
                subcommands.add("group");
                subcommands.add("server");
                subcommands.add("subserver");
                subcommands.add("player");
                for (String command : subcommands) {
                    if (!list.contains(command) && command.toLowerCase().startsWith(last))
                        list.add(Last + command.substring(last.length()));
                }
                Proxy master = proxyMasterCache;
                if (master != null && !list.contains(master.getName()) && master.getName().toLowerCase().startsWith(last))
                    list.add(Last + master.getName().substring(last.length()));
                for (Proxy proxy : proxyCache.values()) {
                    if (!list.contains(proxy.getName()) && proxy.getName().toLowerCase().startsWith(last))
                        list.add(Last + proxy.getName().substring(last.length()));
                }
                for (Host host : hostCache.values()) {
                    if (!list.contains(host.getName()) && host.getName().toLowerCase().startsWith(last))
                        list.add(Last + host.getName().substring(last.length()));
                }
                for (String group : groupCache.keySet()) {
                    if (!list.contains(group) && group.toLowerCase().startsWith(last))
                        list.add(Last + group.substring(last.length()));
                }
                for (ServerData server : plugin.servers.values()) {
                    if (!list.contains(server.getName()) && server.getName().toLowerCase().startsWith(last))
                        list.add(Last + server.getName().substring(last.length()));
                }
                for (String player : getPlayers.get()) {
                    if (!list.contains(player) && player.toLowerCase().startsWith(last))
                        list.add(Last + player.substring(last.length()));
                }
                return list;
            } else if (args.length == 3) {
                List<String> list = new ArrayList<String>();
                switch(args[1].toLowerCase()) {
                    case "p":
                    case "proxy":
                        Proxy master = proxyMasterCache;
                        if (master != null && master.getName().toLowerCase().startsWith(last))
                            list.add(Last + master.getName().substring(last.length()));
                        for (Proxy proxy : proxyCache.values()) {
                            if (!list.contains(proxy.getName()) && proxy.getName().toLowerCase().startsWith(last))
                                list.add(Last + proxy.getName().substring(last.length()));
                        }
                        break;
                    case "h":
                    case "host":
                        for (Host host : hostCache.values()) {
                            if (host.getName().toLowerCase().startsWith(last))
                                list.add(Last + host.getName().substring(last.length()));
                        }
                        break;
                    case "g":
                    case "group":
                        for (String group : groupCache.keySet()) {
                            if (group.toLowerCase().startsWith(last))
                                list.add(Last + group.substring(last.length()));
                        }
                        break;
                    case "s":
                    case "server":
                    case "subserver":
                        for (ServerData server : plugin.servers.values()) {
                            if ((!args[1].equalsIgnoreCase("subserver") || server instanceof SubServerData) && server.getName().toLowerCase().startsWith(last))
                                list.add(Last + server.getName().substring(last.length()));
                        }
                        break;
                    case "u":
                    case "user":
                    case "player":
                        for (String player : getPlayers.get()) {
                            if (player.toLowerCase().startsWith(last))
                                list.add(Last + player.substring(last.length()));
                        }
                        break;
                }
                return list;
            } else {
                return Collections.emptyList();
            }
        } else if (args[0].equals("start") || args[0].equals("restart") || args[0].equals("stop") || args[0].equals("kill") || args[0].equals("terminate") || args[0].equals("cmd") || args[0].equals("command") || args[0].equals("update") || args[0].equals("upgrade") || args[0].equals("remove") || args[0].equals("del") || args[0].equals("delete")) {
            List<String> list = new ArrayList<String>();
            boolean mode = !args[0].equals("cmd") && !args[0].equals("command");
            RawServerSelection select = selectRawServers(null, args, 1, mode);
            if (select.last != null) {
                if (last.startsWith("::")) {
                    Map<String, Host> hosts = hostCache;
                    if (hosts.size() > 0) {
                        if (Arrays.binarySearch(select.selection, "::*") < 0 && "::*".startsWith(last))
                            list.add("::*");
                        if (sender instanceof Player && Arrays.binarySearch(select.selection, "::.") < 0 && "::.".startsWith(last))
                            list.add("::.");
                        for (Host host : hosts.values()) {
                            String name = "::" + host.getName();
                            if (Arrays.binarySearch(select.selection, name.toLowerCase()) < 0 && name.toLowerCase().startsWith(last))
                                list.add(Last + name.substring(last.length()));
                        }
                    }
                    return list;
                } else if (last.startsWith(":")) {
                    Map<String, List<Server>> groups = groupCache;
                    if (groups.size() > 0) {
                        if (Arrays.binarySearch(select.selection, ":*") < 0 && ":*".startsWith(last))
                            list.add(":*");
                        if (sender instanceof Player && Arrays.binarySearch(select.selection, ":.") < 0 && ":.".startsWith(last))
                            list.add(":.");
                        for (String group : groups.keySet()) {
                            group = ":" + group;
                            if (Arrays.binarySearch(select.selection, group.toLowerCase()) < 0 && group.toLowerCase().startsWith(last))
                                list.add(Last + group.substring(last.length()));
                        }
                    }
                    return list;
                } else {
                    Map<ServerInfo, ServerData> subservers = plugin.servers;
                    if (subservers.size() > 0) {
                        if (Arrays.binarySearch(select.selection, "*") < 0 && "*".startsWith(last))
                            list.add("*");
                        if (sender instanceof Player && Arrays.binarySearch(select.selection, ".") < 0 && ".".startsWith(last))
                            list.add(".");
                        for (ServerData server : subservers.values()) {
                            if ((!mode || server instanceof SubServerData) && Arrays.binarySearch(select.selection, server.getName().toLowerCase()) < 0 && server.getName().toLowerCase().startsWith(last))
                                list.add(Last + server.getName().substring(last.length()));
                        }
                    }
                    return list;
                }
            } else if (args[0].equals("cmd") || args[0].equals("command")) {
                if (select.args.length == 3) {
                    return Collections.singletonList("<Command>");
                } else {
                    return Collections.singletonList("[Args...]");
                }
            } else if (args[0].equals("update") || args[0].equals("upgrade")) {
                if (select.args.length == 3) {
                    return Arrays.asList("[Template]", "[Version]");
                } else if (select.args.length == 4) {
                    return Collections.singletonList("<Version>");
                }
            }
            return Collections.emptyList();
        } else if (args[0].equals("create")) {
            updateCache();
            if (args.length == 2) {
                return Collections.singletonList("<Name>");
            } else if (args.length == 3) {
                List<String> list = new ArrayList<String>();
                for (Host host : hostCache.values()) {
                    if (host.getName().toLowerCase().startsWith(last))
                        list.add(Last + host.getName().substring(last.length()));
                }
                return list;
            } else if (args.length == 4) {
                List<String> list = new ArrayList<String>();
                Map<String, Host> hosts = hostCache;
                if (!hosts.containsKey(args[2].toLowerCase())) {
                    list.add("<Template>");
                } else {
                    for (SubCreator.ServerTemplate template : hosts.get(args[2].toLowerCase()).getCreator().getTemplates().values()) {
                        if (template.getName().toLowerCase().startsWith(last))
                            list.add(Last + template.getName().substring(last.length()));
                    }
                }
                return list;
            } else if (args.length == 5) {
                return Collections.singletonList("[Version]");
            } else if (args.length == 6) {
                if (last.length() > 0) {
                    if (!Try.all.run(() -> Integer.parseInt(last)) || Integer.parseInt(last) <= 0 || Integer.parseInt(last) > 65535) {
                        return Collections.emptyList();
                    }
                }
                return Collections.singletonList("[Port]");
            } else {
                return Collections.emptyList();
            }
        } else if (args[0].equals("restore")) {
            if (args.length == 2) {
                return Collections.singletonList("<Subserver>");
            } else {
                return Collections.emptyList();
            }
        } else if (sender instanceof Player && (args[0].equals("tp") || args[0].equals("teleport"))) {
            if (args.length == 2 || args.length == 3) {
                List<String> list = new ArrayList<String>();
                if (args.length == 2) {
                    list.add("@p");
                    list.add("@a");
                    list.add("@r");
                    list.add("@s");
                    List<UUID> used = new ArrayList<UUID>();
                    Optional<ServerConnection> server = ((Player) sender).getCurrentServer();
                    if (server.isPresent()) {
                        for (Player player : server.get().getServer().getPlayersConnected()) {
                            if (player.getGameProfile().getName().toLowerCase().startsWith(last))
                                list.add(Last + player.getGameProfile().getName().substring(last.length()));
                            used.add(player.getUniqueId());
                        }
                        for (CachedPlayer player : SubAPI.getInstance().getRemotePlayers(server.get().getServerInfo()).values()) {
                            if (!used.contains(player.getUniqueId())) {
                                if (player.getName().toLowerCase().startsWith(last))
                                    list.add(Last + player.getName().substring(last.length()));
                                used.add(player.getUniqueId());
                            }
                        }
                    }
                }
                for (ServerData server : plugin.servers.values()) {
                    if (server.getName().toLowerCase().startsWith(last))
                        list.add(Last + server.getName().substring(last.length()));
                }
                return list;
            } else {
                return Collections.emptyList();
            }
        } else {
            return Collections.emptyList();
        }
    }
}
Also used : Platform(net.ME1312.Galaxi.Library.Platform) SubDataSender(net.ME1312.SubData.Client.SubDataSender) ServerConnection(com.velocitypowered.api.proxy.ServerConnection) java.util(java.util) SimpleCommand(com.velocitypowered.api.command.SimpleCommand) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) ChatColor(net.ME1312.SubServers.Velocity.Library.Compatibility.ChatColor) URL(java.net.URL) ClickEvent(net.kyori.adventure.text.event.ClickEvent) Supplier(java.util.function.Supplier) SubDataClient(net.ME1312.SubData.Client.SubDataClient) ServerData(net.ME1312.SubServers.Velocity.Server.ServerData) API(net.ME1312.SubServers.Client.Common.Network.API) Charset(java.nio.charset.Charset) AsyncConsolidator(net.ME1312.Galaxi.Library.AsyncConsolidator) Util(net.ME1312.Galaxi.Library.Util) Version(net.ME1312.Galaxi.Library.Version.Version) Try(net.ME1312.Galaxi.Library.Try) Gson(com.google.gson.Gson) Component(net.kyori.adventure.text.Component) Player(com.velocitypowered.api.proxy.Player) PacketCreateServer(net.ME1312.SubServers.Client.Common.Network.Packet.PacketCreateServer) PacketInExRunEvent(net.ME1312.SubServers.Velocity.Network.Packet.PacketInExRunEvent) CachedPlayer(net.ME1312.SubServers.Velocity.Server.CachedPlayer) TextComponent(net.kyori.adventure.text.TextComponent) RawCommand(com.velocitypowered.api.command.RawCommand) ConsoleCommandSource(com.velocitypowered.api.proxy.ConsoleCommandSource) Value(net.ME1312.Galaxi.Library.Container.Value) PacketUpdateServer(net.ME1312.SubServers.Client.Common.Network.Packet.PacketUpdateServer) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) InputStreamReader(java.io.InputStreamReader) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) InvocationTargetException(java.lang.reflect.InvocationTargetException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) Container(net.ME1312.Galaxi.Library.Container.Container) ObjectMap(net.ME1312.Galaxi.Library.Map.ObjectMap) ContainedPair(net.ME1312.Galaxi.Library.Container.ContainedPair) PacketCheckPermission(net.ME1312.SubServers.Velocity.Network.Packet.PacketCheckPermission) BufferedReader(java.io.BufferedReader) Pair(net.ME1312.Galaxi.Library.Container.Pair) CommandSource(com.velocitypowered.api.command.CommandSource) HoverEvent(net.kyori.adventure.text.event.HoverEvent) CachedPlayer(net.ME1312.SubServers.Velocity.Server.CachedPlayer) ServerInfo(com.velocitypowered.api.proxy.server.ServerInfo) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) ServerData(net.ME1312.SubServers.Velocity.Server.ServerData) ContainedPair(net.ME1312.Galaxi.Library.Container.ContainedPair) ConsoleCommandSource(com.velocitypowered.api.proxy.ConsoleCommandSource) ContainedPair(net.ME1312.Galaxi.Library.Container.ContainedPair) Pair(net.ME1312.Galaxi.Library.Container.Pair) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) Player(com.velocitypowered.api.proxy.Player) CachedPlayer(net.ME1312.SubServers.Velocity.Server.CachedPlayer) ServerConnection(com.velocitypowered.api.proxy.ServerConnection) ConsoleCommandSource(com.velocitypowered.api.proxy.ConsoleCommandSource) CommandSource(com.velocitypowered.api.command.CommandSource) SubDataSender(net.ME1312.SubData.Client.SubDataSender) PacketCheckPermission(net.ME1312.SubServers.Velocity.Network.Packet.PacketCheckPermission) ObjectMap(net.ME1312.Galaxi.Library.Map.ObjectMap)

Example 2 with SubServerData

use of net.ME1312.SubServers.Velocity.Server.SubServerData in project SubServers-2 by ME1312.

the class SubCommand method selectRawServers.

private RawServerSelection selectRawServers(CommandSource sender, String[] rargs, int index, boolean mode) {
    LinkedList<Component> msgs = new LinkedList<Component>();
    LinkedList<String> args = new LinkedList<String>();
    LinkedList<String> selection = new LinkedList<>();
    LinkedList<ServerData> servers = new LinkedList<ServerData>();
    String last = null;
    updateCache();
    int i = 0;
    while (i < index) {
        args.add(rargs[i]);
        ++i;
    }
    Map<String, Host> hostMap = null;
    Map<String, List<Server>> groupMap = null;
    StringBuilder completed = new StringBuilder();
    for (boolean run = true; run && i < rargs.length; i++) {
        String current = last = rargs[i];
        completed.append(current);
        if (current.endsWith(",")) {
            current = current.substring(0, current.length() - 1);
            completed.append(' ');
        } else
            run = false;
        selection.add(current.toLowerCase());
        if (current.length() > 0) {
            LinkedList<ServerData> select = new LinkedList<ServerData>();
            if (current.startsWith("::") && current.length() > 2) {
                current = current.substring(2);
                if (hostMap == null)
                    hostMap = hostCache;
                if (current.equals("*")) {
                    for (Host host : hostMap.values()) {
                        for (SubServer server : host.getSubServers().values()) {
                            ExProxy.getInstance().getServer(server.getName()).map(RegisteredServer::getServerInfo).map(plugin::getData).ifPresent(select::add);
                        }
                    }
                } else {
                    Host host = hostMap.getOrDefault(current.toLowerCase(), null);
                    if (host != null) {
                        for (SubServer server : host.getSubServers().values()) {
                            ExProxy.getInstance().getServer(server.getName()).map(RegisteredServer::getServerInfo).map(plugin::getData).ifPresent(select::add);
                        }
                        if (select.size() <= 0) {
                            Component msg = Component.text("SubServers > There are no " + ((mode) ? "sub" : "") + "servers on host: " + host.getName());
                            if (sender != null)
                                sender.sendMessage(msg);
                            msgs.add(msg);
                        }
                    } else {
                        Component msg = Component.text("SubServers > There is no host with name: " + current);
                        if (sender != null)
                            sender.sendMessage(msg);
                        msgs.add(msg);
                    }
                }
            } else if (current.startsWith(":") && current.length() > 1) {
                current = current.substring(1);
                if (groupMap == null)
                    groupMap = groupCache;
                if (current.equals("*")) {
                    for (List<Server> group : groupMap.values()) for (Server server : group) {
                        if (!mode || server instanceof SubServer) {
                            ExProxy.getInstance().getServer(server.getName()).map(RegisteredServer::getServerInfo).map(plugin::getData).ifPresent(select::add);
                        }
                    }
                } else {
                    Map.Entry<String, List<Server>> group = null;
                    for (Map.Entry<String, List<Server>> entry : groupMap.entrySet()) if (current.equalsIgnoreCase(entry.getKey())) {
                        group = entry;
                        break;
                    }
                    if (group != null) {
                        for (Server server : group.getValue()) {
                            if (!mode || server instanceof SubServer) {
                                ExProxy.getInstance().getServer(server.getName()).map(RegisteredServer::getServerInfo).map(plugin::getData).ifPresent(select::add);
                            }
                        }
                        if (select.size() <= 0) {
                            Component msg = Component.text("SubServers > There are no " + ((mode) ? "sub" : "") + "servers in group: " + group.getKey());
                            if (sender != null)
                                sender.sendMessage(msg);
                            msgs.add(msg);
                        }
                    } else {
                        Component msg = Component.text("SubServers > There is no group with name: " + current);
                        if (sender != null)
                            sender.sendMessage(msg);
                        msgs.add(msg);
                    }
                }
            } else {
                if (current.equals("*")) {
                    for (ServerData server : plugin.servers.values()) {
                        if (!mode || server instanceof SubServerData)
                            select.add(server);
                    }
                } else {
                    ServerData server = ExProxy.getInstance().getServer(current).map(RegisteredServer::getServerInfo).map(plugin::getData).orElse(null);
                    if (server != null) {
                        select.add(server);
                    } else {
                        Component msg = Component.text("SubServers > There is no " + ((mode) ? "sub" : "") + "server with name: " + current);
                        if (sender != null)
                            sender.sendMessage(msg);
                        msgs.add(msg);
                    }
                }
            }
            for (ServerData server : select) {
                if (!servers.contains(server))
                    servers.add(server);
            }
        }
    }
    args.add(completed.toString());
    while (i < rargs.length) {
        args.add(rargs[i]);
        last = null;
        i++;
    }
    LinkedList<SubServerData> subservers = new LinkedList<SubServerData>();
    for (ServerData server : servers) if (server instanceof SubServerData)
        subservers.add((SubServerData) server);
    if ((!mode && servers.size() <= 0) || (mode && subservers.size() <= 0)) {
        Component msg = Component.text("SubServers > No " + ((mode) ? "sub" : "") + "servers were selected");
        if (sender != null)
            sender.sendMessage(msg);
        msgs.add(msg);
    }
    return new RawServerSelection(msgs, selection, servers, subservers, args, last);
}
Also used : SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) PacketCreateServer(net.ME1312.SubServers.Client.Common.Network.Packet.PacketCreateServer) PacketUpdateServer(net.ME1312.SubServers.Client.Common.Network.Packet.PacketUpdateServer) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) ServerData(net.ME1312.SubServers.Velocity.Server.ServerData) Component(net.kyori.adventure.text.Component) TextComponent(net.kyori.adventure.text.TextComponent)

Example 3 with SubServerData

use of net.ME1312.SubServers.Velocity.Server.SubServerData in project SubServers-2 by ME1312.

the class ExProxy method validate.

@Subscribe(order = PostOrder.LAST)
public void validate(ServerPreConnectEvent e) {
    if (e.getPlayer().isActive()) {
        ServerData server = getData(e.getOriginalServer().getServerInfo());
        if (server == null || !server.canAccess(e.getPlayer())) {
            if (!e.getPlayer().getCurrentServer().isPresent() || fallback.containsKey(e.getPlayer().getUniqueId())) {
                if (!fallback.containsKey(e.getPlayer().getUniqueId()) || fallback.get(e.getPlayer().getUniqueId()).names.contains(e.getOriginalServer().getServerInfo().getName())) {
                    Component text = ChatColor.convertColor(api.getLang("SubServers", "Bungee.Restricted"));
                    KickedFromServerEvent kick = new KickedFromServerEvent(e.getPlayer(), e.getOriginalServer(), text, true, KickedFromServerEvent.DisconnectPlayer.create(text));
                    fallback(kick);
                    if (e.getPlayer().getCurrentServer().isPresent())
                        e.setResult(ServerPreConnectEvent.ServerResult.denied());
                    if (kick.getResult() instanceof KickedFromServerEvent.DisconnectPlayer)
                        e.getPlayer().disconnect(text);
                    else if (kick.getResult() instanceof KickedFromServerEvent.RedirectPlayer)
                        e.getPlayer().createConnectionRequest(((KickedFromServerEvent.RedirectPlayer) kick.getResult()).getServer()).fireAndForget();
                }
            } else {
                e.getPlayer().sendMessage(ChatColor.convertColor(api.getLang("SubServers", "Bungee.Restricted")));
                e.setResult(ServerPreConnectEvent.ServerResult.denied());
            }
        } else if (e.getPlayer().getCurrentServer().isPresent() && !fallback.containsKey(e.getPlayer().getUniqueId()) && server instanceof SubServerData && !((SubServerData) server).isRunning()) {
            e.getPlayer().sendMessage(ChatColor.convertColor(api.getLang("SubServers", "Bungee.Server.Offline")));
            e.setResult(ServerPreConnectEvent.ServerResult.denied());
        }
        if (fallback.containsKey(e.getPlayer().getUniqueId())) {
            FallbackState state = fallback.get(e.getPlayer().getUniqueId());
            if (state.names.contains(e.getOriginalServer().getServerInfo().getName())) {
                state.remove(e.getOriginalServer().getServerInfo().getName());
            } else if (e.getPlayer().getCurrentServer().isPresent()) {
                fallback.remove(e.getPlayer().getUniqueId());
            }
        }
    } else {
        e.setResult(ServerPreConnectEvent.ServerResult.denied());
    }
}
Also used : KickedFromServerEvent(com.velocitypowered.api.event.player.KickedFromServerEvent) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) PacketDisconnectPlayer(net.ME1312.SubServers.Client.Common.Network.Packet.PacketDisconnectPlayer) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) ServerData(net.ME1312.SubServers.Velocity.Server.ServerData) FallbackState(net.ME1312.SubServers.Velocity.Library.Fallback.FallbackState) Component(net.kyori.adventure.text.Component) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 4 with SubServerData

use of net.ME1312.SubServers.Velocity.Server.SubServerData in project SubServers-2 by ME1312.

the class ExProxy method merge.

public Boolean merge(net.ME1312.SubServers.Client.Common.Network.API.Server server) {
    ServerData current = proxy.getServer(server.getName()).map(RegisteredServer::getServerInfo).map(this::getData).orElse(null);
    if (server instanceof net.ME1312.SubServers.Client.Common.Network.API.SubServer || !(current instanceof SubServerData)) {
        if (current == null || !current.getSignature().equals(server.getSignature())) {
            ServerData data;
            if (server instanceof net.ME1312.SubServers.Client.Common.Network.API.SubServer) {
                data = new SubServerData(server.getSignature(), server.getName(), server.getDisplayName(), server.getAddress(), getSubDataAsMap(server), server.getMotd(), server.isHidden(), server.isRestricted(), server.getWhitelist(), ((net.ME1312.SubServers.Client.Common.Network.API.SubServer) server).isRunning());
            } else {
                data = new ServerData(server.getSignature(), server.getName(), server.getDisplayName(), server.getAddress(), getSubDataAsMap(server), server.getMotd(), server.isHidden(), server.isRestricted(), server.getWhitelist());
            }
            ServerData old = servers.put(data.get(), data);
            if (old != null && proxy.getServer(data.getName()).isPresent())
                proxy.unregisterServer(old.get());
            proxy.registerServer(data.get());
            Logger.get("SubServers").info("Added " + ((server instanceof net.ME1312.SubServers.Client.Common.Network.API.SubServer) ? "Sub" : "") + "Server: " + server.getName());
            return true;
        } else {
            if (server instanceof net.ME1312.SubServers.Client.Common.Network.API.SubServer) {
                if (((net.ME1312.SubServers.Client.Common.Network.API.SubServer) server).isRunning() != ((SubServerData) current).isRunning())
                    ((SubServerData) current).setRunning(((net.ME1312.SubServers.Client.Common.Network.API.SubServer) server).isRunning());
            }
            if (!server.getMotd().equals(current.getMotd()))
                current.setMotd(server.getMotd());
            if (server.isHidden() != current.isHidden())
                current.setHidden(server.isHidden());
            if (server.isRestricted() != current.isRestricted())
                current.setRestricted(server.isRestricted());
            if (!server.getDisplayName().equals(current.getDisplayName()))
                current.setDisplayName(server.getDisplayName());
            Logger.get("SubServers").info("Re-added " + ((server instanceof net.ME1312.SubServers.Client.Common.Network.API.SubServer) ? "Sub" : "") + "Server: " + server.getName());
            return false;
        }
    }
    return null;
}
Also used : SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) RegisteredServer(com.velocitypowered.api.proxy.server.RegisteredServer) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) ServerData(net.ME1312.SubServers.Velocity.Server.ServerData) DataClient(net.ME1312.SubData.Client.DataClient) SubDataClient(net.ME1312.SubData.Client.SubDataClient)

Example 5 with SubServerData

use of net.ME1312.SubServers.Velocity.Server.SubServerData in project SubServers-2 by ME1312.

the class ExProxy method add.

@Subscribe(order = PostOrder.FIRST)
public void add(SubAddServerEvent e) {
    api.getServer(e.getServer(), server -> {
        if (server != null) {
            ServerData data;
            if (server instanceof net.ME1312.SubServers.Client.Common.Network.API.SubServer) {
                data = new SubServerData(server.getSignature(), server.getName(), server.getDisplayName(), server.getAddress(), getSubDataAsMap(server), server.getMotd(), server.isHidden(), server.isRestricted(), server.getWhitelist(), ((net.ME1312.SubServers.Client.Common.Network.API.SubServer) server).isRunning());
                Logger.get("SubServers").info("Added SubServer: " + e.getServer());
            } else {
                data = new ServerData(server.getSignature(), server.getName(), server.getDisplayName(), server.getAddress(), getSubDataAsMap(server), server.getMotd(), server.isHidden(), server.isRestricted(), server.getWhitelist());
                Logger.get("SubServers").info("Added Server: " + e.getServer());
            }
            ServerData old = servers.put(data.get(), data);
            if (old != null && proxy.getServer(data.getName()).isPresent())
                proxy.unregisterServer(old.get());
            proxy.registerServer(data.get());
        } else
            System.out.println("PacketDownloadServerInfo(" + e.getServer() + ") returned with an invalid response");
    });
}
Also used : SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) SubServerData(net.ME1312.SubServers.Velocity.Server.SubServerData) ServerData(net.ME1312.SubServers.Velocity.Server.ServerData) DataClient(net.ME1312.SubData.Client.DataClient) SubDataClient(net.ME1312.SubData.Client.SubDataClient) Subscribe(com.velocitypowered.api.event.Subscribe)

Aggregations

ServerData (net.ME1312.SubServers.Velocity.Server.ServerData)6 SubServerData (net.ME1312.SubServers.Velocity.Server.SubServerData)6 RegisteredServer (com.velocitypowered.api.proxy.server.RegisteredServer)4 Subscribe (com.velocitypowered.api.event.Subscribe)3 SubDataClient (net.ME1312.SubData.Client.SubDataClient)3 Component (net.kyori.adventure.text.Component)3 DataClient (net.ME1312.SubData.Client.DataClient)2 PacketCreateServer (net.ME1312.SubServers.Client.Common.Network.Packet.PacketCreateServer)2 PacketUpdateServer (net.ME1312.SubServers.Client.Common.Network.Packet.PacketUpdateServer)2 TextComponent (net.kyori.adventure.text.TextComponent)2 Gson (com.google.gson.Gson)1 CommandSource (com.velocitypowered.api.command.CommandSource)1 RawCommand (com.velocitypowered.api.command.RawCommand)1 SimpleCommand (com.velocitypowered.api.command.SimpleCommand)1 KickedFromServerEvent (com.velocitypowered.api.event.player.KickedFromServerEvent)1 ConsoleCommandSource (com.velocitypowered.api.proxy.ConsoleCommandSource)1 Player (com.velocitypowered.api.proxy.Player)1 ServerConnection (com.velocitypowered.api.proxy.ServerConnection)1 ServerInfo (com.velocitypowered.api.proxy.server.ServerInfo)1 ServerPing (com.velocitypowered.api.proxy.server.ServerPing)1