Expose CommandMeta in CommandManager and add a ref to the plugin instance (#544)

This commit is contained in:
Frank van der Heijden
2021-10-31 21:23:35 +01:00
committed by GitHub
parent ac4b4a34ca
commit 922c001b59
4 changed files with 69 additions and 2 deletions

View File

@@ -42,7 +42,9 @@ import com.velocitypowered.proxy.event.VelocityEventManager;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
@@ -50,6 +52,7 @@ import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.lock.qual.GuardedBy;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
public class VelocityCommandManager implements CommandManager {
@@ -61,6 +64,7 @@ public class VelocityCommandManager implements CommandManager {
private final List<CommandRegistrar<?>> registrars;
private final SuggestionsProvider<CommandSource> suggestionsProvider;
private final CommandGraphInjector<CommandSource> injector;
private final Map<String, CommandMeta> commandMetas;
/**
* Constructs a command manager.
@@ -78,6 +82,7 @@ public class VelocityCommandManager implements CommandManager {
new RawCommandRegistrar(root, this.lock.writeLock()));
this.suggestionsProvider = new SuggestionsProvider<>(this.dispatcher, this.lock.readLock());
this.injector = new CommandGraphInjector<>(this.dispatcher, this.lock.readLock());
this.commandMetas = new ConcurrentHashMap<>();
}
public void setAnnounceProxyCommands(boolean announceProxyCommands) {
@@ -137,6 +142,9 @@ public class VelocityCommandManager implements CommandManager {
return false;
}
registrar.register(meta, superInterface.cast(command));
for (String alias : meta.getAliases()) {
commandMetas.put(alias, meta);
}
return true;
}
@@ -148,11 +156,24 @@ public class VelocityCommandManager implements CommandManager {
// The literals of secondary aliases will preserve the children of
// the removed literal in the graph.
dispatcher.getRoot().removeChildByName(alias.toLowerCase(Locale.ENGLISH));
CommandMeta meta = commandMetas.get(alias);
if (meta != null) {
for (String metaAlias : meta.getAliases()) {
commandMetas.remove(metaAlias, meta);
}
}
} finally {
lock.writeLock().unlock();
}
}
@Override
public @Nullable CommandMeta getCommandMeta(String alias) {
Preconditions.checkNotNull(alias, "alias");
return commandMetas.get(alias);
}
/**
* Fires a {@link CommandExecuteEvent}.
*

View File

@@ -30,6 +30,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class VelocityCommandMeta implements CommandMeta {
@@ -37,12 +39,14 @@ public final class VelocityCommandMeta implements CommandMeta {
private final ImmutableSet.Builder<String> aliases;
private final ImmutableList.Builder<CommandNode<CommandSource>> hints;
private @MonotonicNonNull Object plugin;
public Builder(final String alias) {
Preconditions.checkNotNull(alias, "alias");
this.aliases = ImmutableSet.<String>builder()
.add(alias.toLowerCase(Locale.ENGLISH));
this.hints = ImmutableList.builder();
this.plugin = null;
}
@Override
@@ -69,9 +73,16 @@ public final class VelocityCommandMeta implements CommandMeta {
return this;
}
@Override
public CommandMeta.Builder plugin(Object plugin) {
Preconditions.checkNotNull(plugin, "plugin");
this.plugin = plugin;
return this;
}
@Override
public CommandMeta build() {
return new VelocityCommandMeta(this.aliases.build(), this.hints.build());
return new VelocityCommandMeta(this.aliases.build(), this.hints.build(), this.plugin);
}
}
@@ -111,11 +122,16 @@ public final class VelocityCommandMeta implements CommandMeta {
private final Set<String> aliases;
private final List<CommandNode<CommandSource>> hints;
private final Object plugin;
private VelocityCommandMeta(
final Set<String> aliases, final List<CommandNode<CommandSource>> hints) {
final Set<String> aliases,
final List<CommandNode<CommandSource>> hints,
final @Nullable Object plugin
) {
this.aliases = aliases;
this.hints = hints;
this.plugin = plugin;
}
@Override
@@ -128,6 +144,11 @@ public final class VelocityCommandMeta implements CommandMeta {
return this.hints;
}
@Override
public @Nullable Object getPlugin() {
return plugin;
}
@Override
public boolean equals(final Object o) {
if (this == o) {