Expose MV's completion and context instances in CommandManager

This commit is contained in:
Ben Woo 2024-11-21 22:28:48 +08:00
parent 0cc1676e6c
commit 8a28bcafec
3 changed files with 20 additions and 13 deletions

View File

@ -35,7 +35,7 @@ import org.mvplugins.multiverse.core.world.MultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager; import org.mvplugins.multiverse.core.world.WorldManager;
@Service @Service
class MVCommandCompletions extends PaperCommandCompletions { public class MVCommandCompletions extends PaperCommandCompletions {
private final MVCommandManager commandManager; private final MVCommandManager commandManager;
private final WorldManager worldManager; private final WorldManager worldManager;
@ -77,6 +77,20 @@ class MVCommandCompletions extends PaperCommandCompletions {
setDefaultCompletion("mvworlds", LoadedMultiverseWorld.class); setDefaultCompletion("mvworlds", LoadedMultiverseWorld.class);
} }
/**
* Shortcut to suggest enums values
*
* @param enumClass The enum class with values
* @return A collection of possible string values
* @param <T> The enum type
*/
public <T extends Enum<T>> Collection<String> suggestEnums(Class<T> enumClass) {
return EnumSet.allOf(enumClass).stream()
.map(Enum::name)
.map(String::toLowerCase)
.toList();
}
private Collection<String> suggestCommands(BukkitCommandCompletionContext context) { private Collection<String> suggestCommands(BukkitCommandCompletionContext context) {
String rootCmdName = context.getConfig(); String rootCmdName = context.getConfig();
if (rootCmdName == null) { if (rootCmdName == null) {
@ -192,11 +206,4 @@ class MVCommandCompletions extends PaperCommandCompletions {
return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action); return world.getStringPropertyHandle().getSuggestedPropertyValue(propertyName, context.getInput(), action);
}).getOrElse(Collections.emptyList()); }).getOrElse(Collections.emptyList());
} }
private <T extends Enum<T>> Collection<String> suggestEnums(Class<T> enumClass) {
return EnumSet.allOf(enumClass).stream()
.map(Enum::name)
.map(String::toLowerCase)
.toList();
}
} }

View File

@ -27,7 +27,7 @@ import org.mvplugins.multiverse.core.world.MultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager; import org.mvplugins.multiverse.core.world.WorldManager;
@Service @Service
class MVCommandContexts extends PaperCommandContexts { public class MVCommandContexts extends PaperCommandContexts {
private final MVCommandManager mvCommandManager; private final MVCommandManager mvCommandManager;
private final DestinationsProvider destinationsProvider; private final DestinationsProvider destinationsProvider;

View File

@ -91,11 +91,11 @@ public class MVCommandManager extends PaperCommandManager {
* @return A not-null {@link CommandContexts}. * @return A not-null {@link CommandContexts}.
*/ */
@Override @Override
public synchronized @NotNull CommandContexts<BukkitCommandExecutionContext> getCommandContexts() { public synchronized @NotNull MVCommandContexts getCommandContexts() {
if (this.contexts == null) { if (this.contexts == null) {
this.contexts = commandContextsProvider.get(); this.contexts = commandContextsProvider.get();
} }
return this.contexts; return (MVCommandContexts) this.contexts;
} }
/** /**
@ -104,11 +104,11 @@ public class MVCommandManager extends PaperCommandManager {
* @return A not-null {@link CommandCompletions}. * @return A not-null {@link CommandCompletions}.
*/ */
@Override @Override
public synchronized @NotNull CommandCompletions<BukkitCommandCompletionContext> getCommandCompletions() { public synchronized @NotNull MVCommandCompletions getCommandCompletions() {
if (this.completions == null) { if (this.completions == null) {
this.completions = commandCompletionsProvider.get(); this.completions = commandCompletionsProvider.get();
} }
return this.completions; return (MVCommandCompletions) this.completions;
} }
/** /**