Added Velocity 3.0.x support

This commit is contained in:
Alberto Migliorato
2021-09-13 16:28:54 +02:00
parent 38b45d7651
commit 24d97bb3eb
520 changed files with 1043 additions and 1034 deletions
+4 -4
View File
@@ -5,12 +5,12 @@
<parent>
<artifactId>acf-parent</artifactId>
<groupId>co.aikar</groupId>
<version>0.5.0-SNAPSHOT</version>
<version>0.5.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>acf-velocity</artifactId>
<version><!--VERSION-->0.5.0-SNAPSHOT<!--VERSION--></version>
<version><!--VERSION-->0.5.1-SNAPSHOT<!--VERSION--></version>
<name>ACF (Velocity)</name>
@@ -26,13 +26,13 @@
<dependency>
<groupId>co.aikar</groupId>
<artifactId>acf-core</artifactId>
<version><!--VERSION-->0.5.0-SNAPSHOT<!--VERSION--></version>
<version><!--VERSION-->0.5.1-SNAPSHOT<!--VERSION--></version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>3.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
@@ -10,15 +10,15 @@ import java.util.stream.Collectors;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import net.kyori.text.TextComponent;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
public class ACFVelocityUtil {
@SuppressWarnings("deprecation")
public static TextComponent color(String message) {
return LegacyComponentSerializer.legacy().deserialize(message);
return Component.text(message.replace("&", LegacyComponentSerializer.SECTION_CHAR+""));
}
public static Player findPlayerSmart(ProxyServer server, CommandIssuer issuer, String search) {
@@ -25,6 +25,7 @@ package co.aikar.commands;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -34,28 +35,28 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import co.aikar.commands.apachecommonslang.ApacheCommonsLangUtil;
import net.kyori.text.format.TextColor;
import net.kyori.text.format.TextDecoration;
import net.kyori.text.format.TextFormat;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
public class VelocityCommandCompletions extends CommandCompletions<VelocityCommandCompletionContext> {
public VelocityCommandCompletions(ProxyServer server, CommandManager manager) {
super(manager);
registerAsyncCompletion("chatcolors", c -> {
Stream<TextFormat> colors = Stream.of(TextColor.values());
Stream<String> colors = NamedTextColor.NAMES.keys().stream().map((String::toUpperCase));
if (!c.hasConfig("colorsonly")) {
colors = Stream.concat(colors, Stream.of(TextDecoration.values()));
colors = Stream.concat(colors, Arrays.stream(TextDecoration.values())
.map((dec -> dec.name().toUpperCase())));
}
String filter = c.getConfig("filter");
if (filter != null) {
Set<String> filters = Arrays.stream(ACFPatterns.COLON.split(filter)).map(ACFUtil::simplifyString)
.collect(Collectors.toSet());
colors = colors.filter(color -> filters.contains(ACFUtil.simplifyString(color.toString())));
colors = colors.filter(color -> filters.contains(ACFUtil.simplifyString(color)));
}
return colors.map(color -> ACFUtil.simplifyString(color.toString())).collect(Collectors.toList());
return colors.map(color -> ACFUtil.simplifyString(color)).collect(Collectors.toList());
});
registerCompletion("players", c -> {
CommandSource sender = c.getSender();
@@ -31,9 +31,10 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import co.aikar.commands.velocity.contexts.OnlinePlayer;
import net.kyori.text.format.TextColor;
import net.kyori.text.format.TextDecoration;
import net.kyori.text.format.TextFormat;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.format.TextFormat;
import org.jetbrains.annotations.Nullable;
public class VelocityCommandContexts extends CommandContexts<VelocityCommandExecutionContext> {
@@ -53,29 +54,6 @@ public class VelocityCommandContexts extends CommandContexts<VelocityCommandExec
}
return proxiedPlayer;
});
registerContext(TextFormat.class, c -> {
String first = c.popFirstArg();
Stream<TextFormat> colors = Stream.of(TextColor.values());
if (!c.hasFlag("colorsonly")) {
colors = Stream.concat(colors, Stream.of(TextDecoration.values()));
}
String filter = c.getFlagValue("filter", (String) null);
if (filter != null) {
filter = ACFUtil.simplifyString(filter);
String finalFilter = filter;
colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.toString())));
}
TextColor match = ACFUtil.simpleMatch(TextColor.class, first);
if (match == null) {
String valid = colors.map(color -> "<c2>" + ACFUtil.simplifyString(color.toString()) + "</c2>")
.collect(Collectors.joining("<c1>,</c1> "));
throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", valid);
}
return match;
});
}
@Nullable
@@ -31,6 +31,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import net.kyori.adventure.text.format.TextColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,7 +42,6 @@ import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import co.aikar.commands.apachecommonslang.ApacheCommonsExceptionUtil;
import net.kyori.text.format.TextColor;
public class VelocityCommandManager extends
CommandManager<CommandSource, VelocityCommandIssuer, TextColor, VelocityMessageFormatter, VelocityCommandExecutionContext, VelocityConditionContext> {
@@ -56,10 +56,14 @@ public class VelocityCommandManager extends
public VelocityCommandManager(ProxyServer proxy, Object plugin) {
this.proxy = proxy;
this.plugin = proxy.getPluginManager().getPlugin(plugin.getClass().getAnnotation(Plugin.class).id()).get();
this.formatters.put(MessageType.ERROR, defaultFormatter = new VelocityMessageFormatter(TextColor.RED, TextColor.YELLOW, TextColor.RED));
this.formatters.put(MessageType.SYNTAX, new VelocityMessageFormatter(TextColor.YELLOW, TextColor.GREEN, TextColor.WHITE));
this.formatters.put(MessageType.INFO, new VelocityMessageFormatter(TextColor.BLUE, TextColor.DARK_GREEN, TextColor.GREEN));
this.formatters.put(MessageType.HELP, new VelocityMessageFormatter(TextColor.AQUA, TextColor.GREEN, TextColor.YELLOW));
this.formatters.put(MessageType.ERROR, defaultFormatter = new VelocityMessageFormatter(TextColor.fromHexString("#FF5555"),
TextColor.fromHexString("#FFFF55"), TextColor.fromHexString("#FF5555")));
this.formatters.put(MessageType.SYNTAX, new VelocityMessageFormatter(TextColor.fromHexString("#FFFF55"),
TextColor.fromHexString("#55FF55"), TextColor.fromHexString("#FFFFFF")));
this.formatters.put(MessageType.INFO, new VelocityMessageFormatter(TextColor.fromHexString("#5555FF"),
TextColor.fromHexString("#00AA00"), TextColor.fromHexString("#55FF55")));
this.formatters.put(MessageType.HELP, new VelocityMessageFormatter(TextColor.fromHexString("#55FFFF"),
TextColor.fromHexString("#55FF55"), TextColor.fromHexString("#FFFF55")));
getLocales();
@@ -129,7 +133,7 @@ public class VelocityCommandManager extends
if (force) {
proxy.getCommandManager().unregister(commandName);
}
proxy.getCommandManager().register(velocityCommand, commandName);
proxy.getCommandManager().register(commandName, velocityCommand);
}
velocityCommand.isRegistered = true;
registeredCommands.put(commandName, velocityCommand);
@@ -1,17 +1,45 @@
package co.aikar.commands;
import net.kyori.text.format.TextColor;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VelocityMessageFormatter extends MessageFormatter<TextColor> {
private static char COLOR_CHAR = '§';
public VelocityMessageFormatter(TextColor... colors) {
super(colors);
}
@Override
@SuppressWarnings("deprecation")
String format(TextColor color, String message) {
return LegacyComponentSerializer.legacy().serialize(LegacyComponentSerializer.legacy().deserialize(message).color(color));
return hex(message, "#", "");
}
/**
* Colors a String with Hex colors (1.16+ only)
* Usage: pre + hex code + post
*
* @author Elementeral
* @param string The string to color
* @return The colored string
*/
public static String hex(String string, String pre, String post) {
final Pattern hexPattern = Pattern.compile(pre+"([A-Fa-f0-9]{6})"+post);
Matcher matcher = hexPattern.matcher(string);
StringBuffer buffer = new StringBuffer(string.length() + 4 * 8);
while (matcher.find()) {
String group = matcher.group(1);
matcher.appendReplacement(buffer, COLOR_CHAR + "x"
+ COLOR_CHAR + group.charAt(0) + COLOR_CHAR + group.charAt(1)
+ COLOR_CHAR + group.charAt(2) + COLOR_CHAR + group.charAt(3)
+ COLOR_CHAR + group.charAt(4) + COLOR_CHAR + group.charAt(5)
);
}
return matcher.appendTail(buffer).toString();
}
}
@@ -74,12 +74,10 @@ public class VelocityRootCommand implements Command, RootCommand {
return children;
}
@Override
public void execute(CommandSource source, String[] args) {
execute(manager.getCommandIssuer(source), getCommandName(), args);
}
@Override
public List<String> suggest(CommandSource source, String[] args) {
return getTabCompletions(manager.getCommandIssuer(source), getCommandName(), args);
}