use of net.ME1312.SubServers.Velocity.Network.Packet.PacketCheckPermission 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();
}
}
}
use of net.ME1312.SubServers.Velocity.Network.Packet.PacketCheckPermission in project SubServers-2 by ME1312.
the class SubCommand method onTabComplete.
/**
* Suggest command arguments
*
* @param sender Sender
* @param args Arguments
* @return The validator's response and list of possible arguments
*/
@SuppressWarnings("unchecked")
public List<String> onTabComplete(CommandSender sender, String[] args) {
String Last = (args.length > 0) ? args[args.length - 1] : "";
String last = Last.toLowerCase();
if (sender instanceof ProxiedPlayer && (!players.containsKey(((ProxiedPlayer) sender).getUniqueId()) || !players.get(((ProxiedPlayer) sender).getUniqueId()).containsKey(((ProxiedPlayer) sender).getServer().getInfo()) || !players.get(((ProxiedPlayer) sender).getUniqueId()).get(((ProxiedPlayer) sender).getServer().getInfo()).value())) {
if (players.containsKey(((ProxiedPlayer) sender).getUniqueId()) && players.get(((ProxiedPlayer) sender).getUniqueId()).containsKey(((ProxiedPlayer) sender).getServer().getInfo()) && players.get(((ProxiedPlayer) sender).getUniqueId()).get(((ProxiedPlayer) sender).getServer().getInfo()).key() == null) {
// do nothing
} else if (!players.containsKey(((ProxiedPlayer) sender).getUniqueId()) || !players.get(((ProxiedPlayer) sender).getUniqueId()).containsKey(((ProxiedPlayer) sender).getServer().getInfo()) || Calendar.getInstance().getTime().getTime() - players.get(((ProxiedPlayer) sender).getUniqueId()).get(((ProxiedPlayer) sender).getServer().getInfo()).key() >= TimeUnit.MINUTES.toMillis(1)) {
if (!(((ProxiedPlayer) sender).getServer().getInfo() instanceof Server) || ((Server) ((ProxiedPlayer) sender).getServer().getInfo()).getSubData()[0] == null) {
HashMap<ServerInfo, Pair<Long, Boolean>> map = (players.containsKey(((ProxiedPlayer) sender).getUniqueId())) ? players.get(((ProxiedPlayer) sender).getUniqueId()) : new HashMap<ServerInfo, Pair<Long, Boolean>>();
map.put(((ProxiedPlayer) sender).getServer().getInfo(), new ContainedPair<>(Calendar.getInstance().getTime().getTime(), false));
players.put(((ProxiedPlayer) sender).getUniqueId(), map);
} else {
HashMap<ServerInfo, Pair<Long, Boolean>> map = (players.containsKey(((ProxiedPlayer) sender).getUniqueId())) ? players.get(((ProxiedPlayer) sender).getUniqueId()) : new HashMap<ServerInfo, Pair<Long, Boolean>>();
map.put(((ProxiedPlayer) sender).getServer().getInfo(), new ContainedPair<>(null, false));
players.put(((ProxiedPlayer) sender).getUniqueId(), map);
((SubDataClient) ((Server) ((ProxiedPlayer) sender).getServer().getInfo()).getSubData()[0]).sendPacket(new PacketCheckPermission(((ProxiedPlayer) sender).getUniqueId(), "subservers.command", result -> {
map.put(((ProxiedPlayer) sender).getServer().getInfo(), 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 ProxiedPlayer))
cmds.addAll(Arrays.asList("reload", "sudo", "screen", "remove", "delete"));
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 (ProxiedPlayer player : plugin.getPlayers()) names.add(player.getName());
for (RemotePlayer player : plugin.api.getRemotePlayers().values()) if (!names.contains(player.getName()))
names.add(player.getName());
Collections.sort(names);
return names;
};
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 = plugin.api.getMasterProxy();
if (master != null && !list.contains(master.getName()) && master.getName().toLowerCase().startsWith(last))
list.add(Last + master.getName().substring(last.length()));
for (Proxy proxy : plugin.api.getProxies().values()) {
if (!list.contains(proxy.getName()) && proxy.getName().toLowerCase().startsWith(last))
list.add(Last + proxy.getName().substring(last.length()));
}
for (Host host : plugin.api.getHosts().values()) {
if (!list.contains(host.getName()) && host.getName().toLowerCase().startsWith(last))
list.add(Last + host.getName().substring(last.length()));
}
for (String group : plugin.api.getGroups().keySet()) {
if (!list.contains(group) && group.toLowerCase().startsWith(last))
list.add(Last + group.substring(last.length()));
}
for (Server server : plugin.api.getServers().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 = plugin.api.getMasterProxy();
if (master != null && master.getName().toLowerCase().startsWith(last))
list.add(Last + master.getName().substring(last.length()));
for (Proxy proxy : plugin.api.getProxies().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 : plugin.api.getHosts().values()) {
if (host.getName().toLowerCase().startsWith(last))
list.add(Last + host.getName().substring(last.length()));
}
break;
case "g":
case "group":
for (String group : plugin.api.getGroups().keySet()) {
if (group.toLowerCase().startsWith(last))
list.add(Last + group.substring(last.length()));
}
break;
case "s":
case "server":
case "subserver":
for (Server server : plugin.api.getServers().values()) {
if ((!args[1].equalsIgnoreCase("subserver") || server instanceof SubServer) && 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 (!(sender instanceof ProxiedPlayer) && (args[0].equals("reload") || args[0].equals("restore"))) {
if (args[0].equals("reload")) {
List<String> list = new ArrayList<String>(), completes = Arrays.asList("hard", "bungee", "servers", "templates");
if (args.length == 2) {
for (String complete : completes) {
if (complete.toLowerCase().startsWith(last))
list.add(Last + complete.substring(last.length()));
}
return list;
} else {
return Collections.emptyList();
}
} else /* if (args[0].equals("restore")) */
{
if (args.length == 2) {
return Collections.singletonList("<Subserver>");
} 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") || (!(sender instanceof ProxiedPlayer) && (args[0].equals("sudo") || args[0].equals("screen")))) {
List<String> list = new ArrayList<String>();
boolean mode = !args[0].equals("cmd") && !args[0].equals("command");
ServerSelection select = selectServers(null, args, 1, mode);
if (select.last != null) {
if (last.startsWith("::")) {
Map<String, Host> hosts = plugin.api.getHosts();
if (hosts.size() > 0) {
if (Arrays.binarySearch(select.selection, "::*") < 0 && "::*".startsWith(last))
list.add("::*");
if (sender instanceof ProxiedPlayer && 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 = plugin.api.getGroups();
if (groups.size() > 0) {
if (Arrays.binarySearch(select.selection, ":*") < 0 && ":*".startsWith(last))
list.add(":*");
if (sender instanceof ProxiedPlayer && 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<String, Server> servers = (mode) ? (Map<String, Server>) (Map<String, ?>) plugin.api.getSubServers() : plugin.api.getServers();
if (servers.size() > 0) {
if (Arrays.binarySearch(select.selection, "*") < 0 && "*".startsWith(last))
list.add("*");
if (sender instanceof ProxiedPlayer && Arrays.binarySearch(select.selection, ".") < 0 && ".".startsWith(last))
list.add(".");
for (Server server : servers.values()) {
if (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")) {
if (args.length == 2) {
return Collections.singletonList("<Name>");
} else if (args.length == 3) {
List<String> list = new ArrayList<String>();
for (Host host : plugin.api.getHosts().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 = plugin.api.getHosts();
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 ProxiedPlayer && (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>();
for (ProxiedPlayer player : ((ProxiedPlayer) sender).getServer().getInfo().getPlayers()) {
if (player.getName().toLowerCase().startsWith(last))
list.add(Last + player.getName().substring(last.length()));
used.add(player.getUniqueId());
}
if (((ProxiedPlayer) sender).getServer().getInfo() instanceof Server) {
for (RemotePlayer player : ((Server) ((ProxiedPlayer) sender).getServer().getInfo()).getRemotePlayers()) {
if (!used.contains(player.getUniqueId())) {
if (player.getName().toLowerCase().startsWith(last))
list.add(Last + player.getName().substring(last.length()));
used.add(player.getUniqueId());
}
}
}
}
for (Server server : plugin.api.getServers().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();
}
}
}
use of net.ME1312.SubServers.Velocity.Network.Packet.PacketCheckPermission in project SubServers-2 by ME1312.
the class SubCommand method onTabComplete.
/**
* Suggest command arguments
*
* @param sender Sender
* @param args Arguments
* @return The validator's response and list of possible arguments
*/
@SuppressWarnings("unchecked")
public List<String> onTabComplete(CommandSender sender, String[] args) {
String Last = (args.length > 0) ? args[args.length - 1] : "";
String last = Last.toLowerCase();
if (plugin.api.getSubDataNetwork()[0] == null) {
if (sender instanceof ConsoleCommandSender)
new IllegalStateException("SubData is not connected").printStackTrace();
return Collections.emptyList();
} else if (sender instanceof ProxiedPlayer && (!permitted.containsKey(((ProxiedPlayer) sender).getUniqueId()) || !permitted.get(((ProxiedPlayer) sender).getUniqueId()).containsKey(((ProxiedPlayer) sender).getServer().getInfo()) || !permitted.get(((ProxiedPlayer) sender).getUniqueId()).get(((ProxiedPlayer) sender).getServer().getInfo()).value())) {
if (permitted.containsKey(((ProxiedPlayer) sender).getUniqueId()) && permitted.get(((ProxiedPlayer) sender).getUniqueId()).containsKey(((ProxiedPlayer) sender).getServer().getInfo()) && permitted.get(((ProxiedPlayer) sender).getUniqueId()).get(((ProxiedPlayer) sender).getServer().getInfo()).key() == null) {
// do nothing
} else if (!permitted.containsKey(((ProxiedPlayer) sender).getUniqueId()) || !permitted.get(((ProxiedPlayer) sender).getUniqueId()).containsKey(((ProxiedPlayer) sender).getServer().getInfo()) || Calendar.getInstance().getTime().getTime() - permitted.get(((ProxiedPlayer) sender).getUniqueId()).get(((ProxiedPlayer) sender).getServer().getInfo()).key() >= TimeUnit.MINUTES.toMillis(1)) {
if (!(((ProxiedPlayer) sender).getServer().getInfo() instanceof ServerImpl) || ((ServerImpl) ((ProxiedPlayer) sender).getServer().getInfo()).getSubData()[0] == null) {
HashMap<ServerInfo, Pair<Long, Boolean>> map = (permitted.containsKey(((ProxiedPlayer) sender).getUniqueId())) ? permitted.get(((ProxiedPlayer) sender).getUniqueId()) : new HashMap<ServerInfo, Pair<Long, Boolean>>();
map.put(((ProxiedPlayer) sender).getServer().getInfo(), new ContainedPair<>(Calendar.getInstance().getTime().getTime(), false));
permitted.put(((ProxiedPlayer) sender).getUniqueId(), map);
} else {
HashMap<ServerInfo, Pair<Long, Boolean>> map = (permitted.containsKey(((ProxiedPlayer) sender).getUniqueId())) ? permitted.get(((ProxiedPlayer) sender).getUniqueId()) : new HashMap<ServerInfo, Pair<Long, Boolean>>();
map.put(((ProxiedPlayer) sender).getServer().getInfo(), new ContainedPair<>(null, false));
permitted.put(((ProxiedPlayer) sender).getUniqueId(), map);
((SubDataSender) ((ServerImpl) ((ProxiedPlayer) sender).getServer().getInfo()).getSubData()[0]).sendPacket(new PacketCheckPermission(((ProxiedPlayer) sender).getUniqueId(), "subservers.command", result -> {
map.put(((ProxiedPlayer) sender).getServer().getInfo(), 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 ProxiedPlayer))
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 (ProxiedPlayer player : plugin.getPlayers()) names.add(player.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 (ServerImpl 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 (ServerImpl server : plugin.servers.values()) {
if ((!args[1].equalsIgnoreCase("subserver") || server instanceof SubServerImpl) && 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 ProxiedPlayer && 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 ProxiedPlayer && 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<String, ServerImpl> servers = plugin.servers;
if (servers.size() > 0) {
if (Arrays.binarySearch(select.selection, "*") < 0 && "*".startsWith(last))
list.add("*");
if (sender instanceof ProxiedPlayer && Arrays.binarySearch(select.selection, ".") < 0 && ".".startsWith(last))
list.add(".");
for (ServerImpl server : servers.values()) {
if ((!mode || server instanceof SubServerImpl) && 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 ProxiedPlayer && (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>();
for (ProxiedPlayer player : ((ProxiedPlayer) sender).getServer().getInfo().getPlayers()) {
if (player.getName().toLowerCase().startsWith(last))
list.add(Last + player.getName().substring(last.length()));
used.add(player.getUniqueId());
}
if (((ProxiedPlayer) sender).getServer().getInfo() instanceof ServerImpl) {
for (CachedPlayer player : ((ServerImpl) ((ProxiedPlayer) sender).getServer().getInfo()).getRemotePlayers()) {
if (!used.contains(player.getUniqueId())) {
if (player.getName().toLowerCase().startsWith(last))
list.add(Last + player.getName().substring(last.length()));
used.add(player.getUniqueId());
}
}
}
}
for (ServerImpl 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();
}
}
}
Aggregations