Skip to content

Commit

Permalink
Big Break: Change the Command Completion Handler signature to match C…
Browse files Browse the repository at this point in the history
…ontext

For consistency, make Completion Handlers be implemented the same way Context Handlers are.

Previously when we added the Context parameter, we left the original signature in place and simply tacked , c on the end.

This made migration easier than changing all completion handlers bodies.

But since 0.5.0 is a big migration already, let's just get it over with.

See changes to Bukkit default completions and the example plugin for migration example.
  • Loading branch information
aikar committed Jun 15, 2017
1 parent 14a1c33 commit 15b149d
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
import java.util.stream.Stream;

@SuppressWarnings("WeakerAccess")
public class BukkitCommandCompletions extends CommandCompletions<CommandSender, BukkitCommandCompletionContext> {
public class BukkitCommandCompletions extends CommandCompletions<BukkitCommandCompletionContext> {
public BukkitCommandCompletions(BukkitCommandManager manager) {
super(manager);
registerCompletion("mobs", (sender, config, input, c) -> {
registerCompletion("mobs", c -> {
final Stream<String> normal = Stream.of(EntityType.values())
.map(entityType -> ACFUtil.simplifyString(entityType.getName()));
return normal.collect(Collectors.toList());
});
registerCompletion("chatcolors", (sender, config, input, c) -> {
registerCompletion("chatcolors", c -> {
Stream<ChatColor> colors = Stream.of(ChatColor.values());
if (c.hasConfig("colorsonly")) {
colors = colors.filter(color -> color.ordinal() <= 0xF);
Expand All @@ -62,19 +62,20 @@ public BukkitCommandCompletions(BukkitCommandManager manager) {

return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList());
});
registerCompletion("worlds", (sender, config, input, c) -> (
registerCompletion("worlds", c -> (
Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList())
));

registerCompletion("players", (sender, config, input, c) -> {
registerCompletion("players", c -> {
CommandSender sender = c.getSender();
Validate.notNull(sender, "Sender cannot be null");

Player senderPlayer = sender instanceof Player ? (Player) sender : null;

ArrayList<String> matchedPlayers = new ArrayList<String>();
for (Player player : Bukkit.getOnlinePlayers()) {
String name = player.getName();
if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, input)) {
if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, c.getInput())) {
matchedPlayers.add(name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public synchronized CommandContexts<BukkitCommandExecutionContext> getCommandCon
}

@Override
public synchronized CommandCompletions<CommandSender, BukkitCommandCompletionContext> getCommandCompletions() {
public synchronized CommandCompletions<BukkitCommandCompletionContext> getCommandCompletions() {
if (this.completions == null) {
this.completions = new BukkitCommandCompletions(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BungeeCommandCompletions extends CommandCompletions<CommandSender, BungeeCommandCompletionContext> {
public class BungeeCommandCompletions extends CommandCompletions<BungeeCommandCompletionContext> {

public BungeeCommandCompletions(CommandManager manager) {
super(manager);
registerCompletion("chatcolors", (sender, config, input, c) -> {
registerCompletion("chatcolors", c -> {
Stream<ChatColor> colors = Stream.of(ChatColor.values());
if (c.hasConfig("colorsonly")) {
colors = colors.filter(color -> color.ordinal() <= 0xF);
Expand All @@ -54,8 +54,10 @@ public BungeeCommandCompletions(CommandManager manager) {

return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList());
});
registerCompletion("players", (sender, config, input, c) -> {
registerCompletion("players", c -> {
CommandSender sender = c.getSender();
ACFBungeeUtil.validate(sender, "Sender cannot be null");
String input = c.getInput();

ArrayList<String> matchedPlayers = new ArrayList<>();
for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public synchronized CommandContexts<BungeeCommandExecutionContext> getCommandCon
}

@Override
public synchronized CommandCompletions<CommandSender, BungeeCommandCompletionContext> getCommandCompletions() {
public synchronized CommandCompletions<BungeeCommandCompletionContext> getCommandCompletions() {
if (this.completions == null) {
this.completions = new BungeeCommandCompletions(this);
}
Expand All @@ -69,7 +69,6 @@ public synchronized CommandCompletions<CommandSender, BungeeCommandCompletionCon

@Override
public void registerCommand(BaseCommand command) {
final String plugin = this.plugin.getDescription().getName().toLowerCase();
command.onRegister(this);
for (Map.Entry<String, RootCommand> entry : command.registeredCommands.entrySet()) {
String key = entry.getKey().toLowerCase();
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/java/co/aikar/commands/CommandCompletions.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@


@SuppressWarnings({"WeakerAccess", "UnusedReturnValue"})
public class CommandCompletions <I, C extends CommandCompletionContext> {
public class CommandCompletions <C extends CommandCompletionContext> {
private final CommandManager manager;
private Map<String, CommandCompletionHandler> completionMap = new HashMap<>();

public CommandCompletions(CommandManager manager) {
this.manager = manager;
registerCompletion("range", (sender, config, input, c) -> {
registerCompletion("range", (c) -> {
String config = c.getConfig();
if (config == null) {
return ImmutableList.of();
}
Expand All @@ -59,10 +60,10 @@ public CommandCompletions(CommandManager manager) {
}
return IntStream.rangeClosed(start, end).mapToObj(Integer::toString).collect(Collectors.toList());
});
registerCompletion("timeunits", (sender, config, input, c) -> ImmutableList.of("minutes", "hours", "days", "weeks", "months", "years"));
registerCompletion("timeunits", (c) -> ImmutableList.of("minutes", "hours", "days", "weeks", "months", "years"));
}

public CommandCompletionHandler registerCompletion(String id, CommandCompletionHandler<I, C> handler) {
public CommandCompletionHandler registerCompletion(String id, CommandCompletionHandler<C> handler) {
return this.completionMap.put("@" + id.toLowerCase(), handler);
}

Expand Down Expand Up @@ -96,7 +97,7 @@ List<String> getCompletionValues(RegisteredCommand command, CommandIssuer sender

try {
//noinspection unchecked
Collection<String> completions = handler.getCompletions(sender.getIssuer(), config, input, context);
Collection<String> completions = handler.getCompletions(context);
if (completions != null) {
allCompletions.addAll(completions);
continue;
Expand All @@ -120,8 +121,8 @@ List<String> getCompletionValues(RegisteredCommand command, CommandIssuer sender
return allCompletions;
}

public interface CommandCompletionHandler <I, C extends CommandCompletionContext> {
Collection<String> getCompletions(I sender, String config, String input, C context) throws InvalidCommandArgument;
public interface CommandCompletionHandler <C extends CommandCompletionContext> {
Collection<String> getCompletions(C context) throws InvalidCommandArgument;
}

}
2 changes: 1 addition & 1 deletion core/src/main/java/co/aikar/commands/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class CommandManager {
* Gets the command completions manager
* @return Command Completions
*/
public abstract CommandCompletions<?, ?> getCommandCompletions();
public abstract CommandCompletions<?> getCommandCompletions();

/**
* Lets you add custom string replacements that can be applied to annotation values,
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/java/co/aikar/acfexample/ACFExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void registerCommands() {
commandManager.getCommandReplacements().addReplacements("test", "foobar", "%foo", "barbaz");
commandManager.getCommandReplacements().addReplacement("testcmd", "test4|foobar|barbaz");
commandManager.getCommandContexts().registerContext(SomeObject.class, SomeObject.getContextResolver());
commandManager.getCommandCompletions().registerCompletion("test", (sender, config, input, c) -> (
commandManager.getCommandCompletions().registerCompletion("test", c -> (
Lists.newArrayList("foo", "bar", "baz")
));
commandManager.registerCommand(new SomeCommand());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public synchronized CommandContexts<BukkitCommandExecutionContext> getCommandCon
}

@Override
public synchronized CommandCompletions<CommandSender, BukkitCommandCompletionContext> getCommandCompletions() {
public synchronized CommandCompletions<BukkitCommandCompletionContext> getCommandCompletions() {
if (this.completions == null) {
this.completions = new PaperCommandCompletions(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@

package co.aikar.commands;

import org.spongepowered.api.command.CommandSource;

@SuppressWarnings("WeakerAccess")
public class SpongeCommandCompletions extends CommandCompletions<CommandSource, SpongeCommandCompletionContext> {
public class SpongeCommandCompletions extends CommandCompletions<SpongeCommandCompletionContext> {

public SpongeCommandCompletions(final SpongeCommandManager manager) {
super(manager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public synchronized CommandContexts<SpongeCommandExecutionContext> getCommandCon
}

@Override
public synchronized CommandCompletions<CommandSource, SpongeCommandCompletionContext> getCommandCompletions() {
public synchronized CommandCompletions<SpongeCommandCompletionContext> getCommandCompletions() {
if (this.completions == null) {
this.completions = new SpongeCommandCompletions(this);
}
Expand Down

0 comments on commit 15b149d

Please sign in to comment.