Last active
March 29, 2020 12:45
-
-
Save JohnnyJayJay/557e122df5ef9b6af629c213164f8e12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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