13
0
geforkt von Mirrors/Velocity

Introduce a "modular" /velocity command.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-10-25 01:43:11 -04:00
Ursprung a98fcc28fe
Commit c977ddec61

Datei anzeigen

@ -1,5 +1,7 @@
package com.velocitypowered.proxy.command; package com.velocitypowered.proxy.command;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.permission.Tristate;
@ -10,9 +12,74 @@ import net.kyori.text.format.TextColor;
import net.kyori.text.format.TextDecoration; import net.kyori.text.format.TextDecoration;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
public class VelocityCommand implements Command { public class VelocityCommand implements Command {
private final Map<String, Command> subcommands = ImmutableMap.<String, Command>builder()
.put("version", Info.INSTANCE)
.build();
private void usage(CommandSource source) {
String commandText = "/velocity <" + String.join("|", subcommands.keySet()) + ">";
source.sendMessage(TextComponent.of(commandText, TextColor.RED));
}
@Override @Override
public void execute(CommandSource source, String[] args) { public void execute(CommandSource source, String[] args) {
if (args.length == 0) {
usage(source);
return;
}
Command command = subcommands.get(args[0].toLowerCase(Locale.US));
if (command == null) {
usage(source);
return;
}
command.execute(source, Arrays.copyOfRange(args, 1, args.length));
}
@Override
public List<String> suggest(@NonNull CommandSource source, @NonNull String[] currentArgs) {
if (currentArgs.length == 0) {
return ImmutableList.copyOf(subcommands.keySet());
}
if (currentArgs.length == 1) {
return subcommands.keySet().stream()
.filter(name -> name.regionMatches(true, 0, currentArgs[0], 0, currentArgs[0].length()))
.collect(Collectors.toList());
}
Command command = subcommands.get(currentArgs[0].toLowerCase(Locale.US));
if (command == null) {
return ImmutableList.of();
}
return command.suggest(source, Arrays.copyOfRange(currentArgs, 1, currentArgs.length));
}
@Override
public boolean hasPermission(@NonNull CommandSource source, @NonNull String[] args) {
if (args.length == 0) {
return true;
}
Command command = subcommands.get(args[0].toLowerCase(Locale.US));
if (command == null) {
return true;
}
return command.hasPermission(source, Arrays.copyOfRange(args, 1, args.length));
}
private static class Info implements Command {
static final Info INSTANCE = new Info();
private Info() {}
@Override
public void execute(@NonNull CommandSource source, @NonNull String[] args) {
String implVersion = VelocityServer.class.getPackage().getImplementationVersion(); String implVersion = VelocityServer.class.getPackage().getImplementationVersion();
TextComponent velocity = TextComponent.builder("Velocity ") TextComponent velocity = TextComponent.builder("Velocity ")
.decoration(TextDecoration.BOLD, true) .decoration(TextDecoration.BOLD, true)
@ -44,3 +111,4 @@ public class VelocityCommand implements Command {
return source.getPermissionValue("velocity.command.info") != Tristate.FALSE; return source.getPermissionValue("velocity.command.info") != Tristate.FALSE;
} }
} }
}