Skip to content
This repository was archived by the owner on Jun 7, 2022. It is now read-only.

Commit d5dd41b

Browse files
author
Tareko
authored
Merge pull request #51 from NurMarvin/cloudcommandtabcomplete
Adding tab completion to the /cloud command as well as fixing some tr…
2 parents 954b2cc + 4b4ed91 commit d5dd41b

File tree

2 files changed

+126
-2
lines changed

2 files changed

+126
-2
lines changed

cloudnet-api/cloudnet-api-bridge/src/main/java/de/dytanic/cloudnet/bridge/internal/command/proxied/CommandCloud.java

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
package de.dytanic.cloudnet.bridge.internal.command.proxied;
66

7+
import com.google.common.collect.ImmutableList;
78
import de.dytanic.cloudnet.api.CloudAPI;
89
import de.dytanic.cloudnet.bridge.CloudProxy;
10+
import de.dytanic.cloudnet.bridge.internal.util.StringUtil;
911
import de.dytanic.cloudnet.lib.DefaultType;
1012
import de.dytanic.cloudnet.lib.NetworkUtils;
1113
import de.dytanic.cloudnet.lib.database.Database;
@@ -19,6 +21,7 @@
1921
import de.dytanic.cloudnet.lib.server.info.ProxyInfo;
2022
import de.dytanic.cloudnet.lib.server.info.ServerInfo;
2123
import de.dytanic.cloudnet.lib.server.template.Template;
24+
import de.dytanic.cloudnet.lib.service.ServiceId;
2225
import de.dytanic.cloudnet.lib.utility.Acceptable;
2326
import de.dytanic.cloudnet.lib.utility.CollectionWrapper;
2427
import de.dytanic.cloudnet.lib.utility.document.Document;
@@ -31,6 +34,7 @@
3134

3235
import java.util.*;
3336
import java.util.concurrent.TimeUnit;
37+
import java.util.stream.Collectors;
3438

3539
/**
3640
* Created by Tareko on 02.06.2017.
@@ -544,7 +548,7 @@ public boolean isAccepted(Template value)
544548
CloudAPI.getInstance().getPrefix() + "§7/cloud startcs <name> <memory> <priorityStop>",
545549
CloudAPI.getInstance().getPrefix() + "§7/cloud cmds (command server) <server> <command>",
546550
CloudAPI.getInstance().getPrefix() + "§7/cloud cmdp (command proxy) <proxy> <command>",
547-
CloudAPI.getInstance().getPrefix() + "§7/cloud stop <serverId>",
551+
CloudAPI.getInstance().getPrefix() + "§7/cloud stop <server>",
548552
CloudAPI.getInstance().getPrefix() + "§7/cloud stopGroup <group>",
549553
CloudAPI.getInstance().getPrefix() + "§7/cloud ustopGroup <group>",
550554
CloudAPI.getInstance().getPrefix() + "§7/cloud listProxys",
@@ -581,6 +585,103 @@ private boolean checkAsNumber(String input)
581585
@Override
582586
public Iterable<String> onTabComplete(CommandSender commandSender, String[] args)
583587
{
584-
return new LinkedList<>(CloudProxy.getInstance().getCachedServers().keySet());
588+
List<String> tabCompletes = ImmutableList
589+
.of();
590+
switch (args.length)
591+
{
592+
case 1:
593+
{
594+
tabCompletes = ImmutableList
595+
.of("toggle", "setMaxPlayers", "whitelist", "start", "startcs", "cmds", "cmdp", "stop", "stopGroup"
596+
, "ustopGroup", "listProxys", "listOnline", "listServers", "log", "listGroups", "rl", "list"
597+
, "maintenance", "copy", "version", "statistics");
598+
break;
599+
}
600+
case 2:
601+
{
602+
switch (args[0].toLowerCase())
603+
{
604+
case "toggle":
605+
{
606+
tabCompletes = ImmutableList.of("autoslot", "maintenance");
607+
break;
608+
}
609+
case "whitelist":
610+
{
611+
tabCompletes = ImmutableList.of("add", "remove");
612+
break;
613+
}
614+
case "start":
615+
case "stopgroup":
616+
case "ustopgroup":
617+
{
618+
tabCompletes = getProxyAndServerGroups();
619+
break;
620+
}
621+
case "stop":
622+
{
623+
tabCompletes = getProxiesAndServers();
624+
break;
625+
}
626+
case "log":
627+
{
628+
tabCompletes = new LinkedList<>(CloudProxy.getInstance().getCachedServers().keySet());
629+
break;
630+
}
631+
case "maintenance":
632+
{
633+
tabCompletes = new LinkedList<>(CloudAPI.getInstance().getServerGroupMap().keySet());
634+
break;
635+
}
636+
637+
case "cmds":
638+
{
639+
tabCompletes = CloudAPI.getInstance().getServers().stream()
640+
.map(ServerInfo::getServiceId).map(ServiceId::getServerId).collect(Collectors.toList());
641+
break;
642+
}
643+
case "cmdp":
644+
{
645+
tabCompletes = CloudAPI.getInstance().getProxys().stream()
646+
.map(ProxyInfo::getServiceId).map(ServiceId::getServerId).collect(Collectors.toList());
647+
break;
648+
}
649+
650+
}
651+
break;
652+
}
653+
case 3:
654+
{
655+
switch (args[0].toLowerCase())
656+
{
657+
case "whitelist":
658+
{
659+
tabCompletes = CloudAPI.getInstance().getOnlinePlayers()
660+
.stream().map(CloudPlayer::getName).collect(Collectors.toList());
661+
break;
662+
}
663+
}
664+
break;
665+
}
666+
}
667+
return new LinkedList<>(StringUtil.copyPartialMatches(args[args.length-1], tabCompletes, new ArrayList<>(tabCompletes.size())));
668+
}
669+
670+
private LinkedList<String> getProxyAndServerGroups()
671+
{
672+
LinkedList<String> groups = new LinkedList<>(CloudAPI.getInstance().getProxyGroupMap().keySet());
673+
groups.addAll(CloudAPI.getInstance().getServerGroupMap().keySet());
674+
groups.sort(Collections.reverseOrder());
675+
return groups;
676+
}
677+
678+
private LinkedList<String> getProxiesAndServers()
679+
{
680+
LinkedList<String> groups = new LinkedList<>(CloudAPI.getInstance().getProxys().stream()
681+
.map(ProxyInfo::getServiceId).map(ServiceId::getServerId).collect(Collectors.toList()));
682+
groups.addAll(CloudAPI.getInstance().getServers().stream()
683+
.map(ServerInfo::getServiceId).map(ServiceId::getServerId).collect(Collectors.toList()));
684+
groups.sort(Collections.reverseOrder());
685+
return groups;
585686
}
586687
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package de.dytanic.cloudnet.bridge.internal.util;
2+
3+
import java.util.Collection;
4+
5+
public class StringUtil
6+
{
7+
public static <T extends Collection<? super String>> T copyPartialMatches(final String token, final Iterable<String> originals, final T collection) throws UnsupportedOperationException, IllegalArgumentException {
8+
9+
for (String string : originals) {
10+
if (startsWithIgnoreCase(string, token)) {
11+
collection.add(string);
12+
}
13+
}
14+
15+
return collection;
16+
}
17+
public static boolean startsWithIgnoreCase(final String string, final String prefix) throws IllegalArgumentException, NullPointerException {
18+
if (string.length() < prefix.length()) {
19+
return false;
20+
}
21+
return string.regionMatches(true, 0, prefix, 0, prefix.length());
22+
}
23+
}

0 commit comments

Comments
 (0)