feat: fixup command aliases on legacy Paper

dependabot/gradle/org.projectlombok-lombok-1.18.34
William 6 months ago
parent b59b376eb6
commit cffb5b0020
No known key found for this signature in database

@ -24,6 +24,8 @@ package net.william278.uniform;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
import static net.william278.uniform.BaseCommand.greedyString; import static net.william278.uniform.BaseCommand.greedyString;
public class ExampleCommand extends Command { public class ExampleCommand extends Command {
@ -31,6 +33,7 @@ public class ExampleCommand extends Command {
public ExampleCommand() { public ExampleCommand() {
super("example"); super("example");
setDescription("An example command for Uniform"); setDescription("An example command for Uniform");
setAliases(List.of("helloworld"));
} }
@Override @Override

@ -23,6 +23,7 @@ package net.william278.uniform.paper;
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent; import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
import com.google.common.collect.Sets;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode; import com.mojang.brigadier.tree.LiteralCommandNode;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -35,6 +36,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Set; import java.util.Set;
@SuppressWarnings({"removal", "deprecation", "UnstableApiUsage"}) @SuppressWarnings({"removal", "deprecation", "UnstableApiUsage"})
@ -73,10 +75,20 @@ public class LegacyPaperCommand extends BaseCommand<BukkitBrigadierCommandSource
@EventHandler @EventHandler
public void commandRegisterEvent(CommandRegisteredEvent<BukkitBrigadierCommandSource> event) { public void commandRegisterEvent(CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
commands.forEach(command -> { commands.forEach(command -> {
// Register root command
final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build(); final LiteralCommandNode<BukkitBrigadierCommandSource> built = command.build();
event.getRoot().addChild(built); event.getRoot().addChild(built);
command.getAliases().forEach(alias -> event.getRoot().addChild(
LiteralArgumentBuilder.<BukkitBrigadierCommandSource>literal(alias).redirect(built).build() // Register aliases
final Set<String> aliases = Sets.newHashSet(command.getAliases());
aliases.add("%s:%s".formatted(
plugin.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""),
command.getName())
);
aliases.forEach(alias -> event.getRoot().addChild(
LiteralArgumentBuilder.<BukkitBrigadierCommandSource>literal(alias)
.requires(built.getRequirement()).executes(built.getCommand()).redirect(built)
.build()
)); ));
}); });
commands.clear(); commands.clear();

Loading…
Cancel
Save