From fb34c5416577f2eda93915d44ffd9cf9ca3f9607 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sat, 2 Feb 2019 12:01:48 +0100 Subject: [PATCH] Minor code cleanup in CompositeCommand --- .../api/commands/CompositeCommand.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java index 8aed721d3..920119c15 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -19,6 +19,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.PluginIdentifiableCommand; import org.bukkit.entity.Player; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.addons.Addon; @@ -393,7 +395,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi * @param user the User * @return UUID of player's island owner or null if user has no island */ - protected UUID getOwner(World world, User user) { + @Nullable + protected UUID getOwner(@NonNull World world, @NonNull User user) { return plugin.getIslands().getOwner(world, user.getUniqueId()); } @@ -565,29 +568,31 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi } @Override + @NonNull public List tabComplete(final CommandSender sender, final String alias, final String[] args) { List options = new ArrayList<>(); // Get command object based on args entered so far - CompositeCommand cmd = getCommandFromArgs(args); + CompositeCommand command = getCommandFromArgs(args); // Check for console and permissions - if ((cmd.onlyPlayer && !(sender instanceof Player))) { + if (command.isOnlyPlayer() && !(sender instanceof Player)) { return options; } - if (!cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission()) && !sender.isOp()) { + if (!command.getPermission().isEmpty() && !sender.hasPermission(command.getPermission()) && !sender.isOp()) { return options; } // Add any tab completion from the subcommand - options.addAll(cmd.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElse(new ArrayList<>())); - if (cmd.hasSubCommands()) { - options.addAll(getSubCommandLabels(sender, cmd)); + options.addAll(command.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElse(new ArrayList<>())); + if (command.hasSubCommands()) { + options.addAll(getSubCommandLabels(sender, command)); } String lastArg = args.length != 0 ? args[args.length - 1] : ""; return Util.tabLimit(options, lastArg).stream().sorted().collect(Collectors.toList()); } - private List getSubCommandLabels(CommandSender sender, CompositeCommand cmd) { - return cmd.getSubCommands().values().stream().filter(c -> !c.isOnlyPlayer() || sender.isOp() - || (sender instanceof Player && (c.getPermission().isEmpty() || sender.hasPermission(c.getPermission()))) ) + @NonNull + private List getSubCommandLabels(@NonNull CommandSender sender, @NonNull CompositeCommand command) { + return command.getSubCommands().values().stream() + .filter(cmd -> !cmd.isOnlyPlayer() || sender.isOp() || (sender instanceof Player && (cmd.getPermission().isEmpty() || sender.hasPermission(cmd.getPermission()))) ) .map(CompositeCommand::getLabel).collect(Collectors.toList()); }