Skip to content

Instantly share code, notes, and snippets.

@JohnnyJayJay
Last active March 29, 2020 12:45
Show Gist options
  • Save JohnnyJayJay/557e122df5ef9b6af629c213164f8e12 to your computer and use it in GitHub Desktop.
Save JohnnyJayJay/557e122df5ef9b6af629c213164f8e12 to your computer and use it in GitHub Desktop.
import java.util.*;
import org.bukkit.command.*;
public final class DelegatingCommand implements CommandExecutor {
private final Map<String, ? extends CommandExecutor> children;
private final CommandExecutor defaultCase;
public DelegatingCommand(Map<String, ? extends CommandExecutor> children) {
this((s, c, l, a) -> false, children);
}
public DelegatingCommand(CommandExecutor defaultCase, Map<String, ? extends CommandExecutor> children) {
this.children = new HashMap<>(children);
this.defaultCase = defaultCase;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0) {
String childLabel = args[0];
if (children.containsKey(childLabel)) {
return children.get(childLabel).onCommand(sender, command, childLabel, Arrays.copyOfRange(args, 1, args.length));
}
}
return defaultCase.onCommand(sender, command, label, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment