Simplify and expand tab completion

This commit is contained in:
Jikoo 2020-03-15 09:50:51 -04:00
parent 8bc389496b
commit 23d41cd6c8
7 changed files with 221 additions and 45 deletions

View file

@ -18,10 +18,10 @@ package com.lishid.openinv;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.lishid.openinv.commands.ContainerSettingPluginCommand; import com.lishid.openinv.commands.ContainerSettingCommand;
import com.lishid.openinv.commands.OpenInvPluginCommand; import com.lishid.openinv.commands.OpenInvCommand;
import com.lishid.openinv.commands.SearchEnchantPluginCommand; import com.lishid.openinv.commands.SearchEnchantCommand;
import com.lishid.openinv.commands.SearchInvPluginCommand; import com.lishid.openinv.commands.SearchInvCommand;
import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.internal.IAnySilentContainer;
import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialEnderChest;
import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialInventory;
@ -33,7 +33,6 @@ import com.lishid.openinv.listeners.PlayerListener;
import com.lishid.openinv.listeners.PluginListener; import com.lishid.openinv.listeners.PluginListener;
import com.lishid.openinv.util.Cache; import com.lishid.openinv.util.Cache;
import com.lishid.openinv.util.ConfigUpdater; import com.lishid.openinv.util.ConfigUpdater;
import com.lishid.openinv.util.Function;
import com.lishid.openinv.util.InternalAccessor; import com.lishid.openinv.util.InternalAccessor;
import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.Permissions;
import java.util.HashMap; import java.util.HashMap;
@ -331,20 +330,16 @@ public class OpenInv extends JavaPlugin implements IOpenInv {
pm.registerEvents(new InventoryDragListener(), this); pm.registerEvents(new InventoryDragListener(), this);
// Register commands to their executors // Register commands to their executors
OpenInvPluginCommand openInv = new OpenInvPluginCommand(this); OpenInvCommand openInv = new OpenInvCommand(this);
this.getCommand("openinv").setExecutor(openInv); this.getCommand("openinv").setExecutor(openInv);
this.getCommand("openender").setExecutor(openInv); this.getCommand("openender").setExecutor(openInv);
SearchInvPluginCommand searchInv = new SearchInvPluginCommand(this); SearchInvCommand searchInv = new SearchInvCommand(this);
this.getCommand("searchinv").setExecutor(searchInv); this.getCommand("searchinv").setExecutor(searchInv);
this.getCommand("searchender").setExecutor(searchInv); this.getCommand("searchender").setExecutor(searchInv);
this.getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand(this)); this.getCommand("searchenchant").setExecutor(new SearchEnchantCommand(this));
ContainerSettingPluginCommand settingCommand = new ContainerSettingPluginCommand(this); ContainerSettingCommand settingCommand = new ContainerSettingCommand(this);
PluginCommand command = this.getCommand("silentcontainer"); this.getCommand("silentcontainer").setExecutor(settingCommand);
command.setExecutor(settingCommand); this.getCommand("anycontainer").setExecutor(settingCommand);
command.setTabCompleter(settingCommand);
command = this.getCommand("anycontainer");
command.setExecutor(settingCommand);
command.setTabCompleter(settingCommand);
} else { } else {
this.getLogger().info("Your version of CraftBukkit (" + this.accessor.getVersion() + ") is not supported."); this.getLogger().info("Your version of CraftBukkit (" + this.accessor.getVersion() + ") is not supported.");

View file

@ -17,7 +17,7 @@
package com.lishid.openinv.commands; package com.lishid.openinv.commands;
import com.lishid.openinv.OpenInv; import com.lishid.openinv.OpenInv;
import java.util.ArrayList; import com.lishid.openinv.util.TabCompleter;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -25,16 +25,15 @@ import java.util.function.Function;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class ContainerSettingPluginCommand implements CommandExecutor, TabCompleter { public class ContainerSettingCommand implements TabExecutor {
private final OpenInv plugin; private final OpenInv plugin;
public ContainerSettingPluginCommand(final OpenInv plugin) { public ContainerSettingCommand(final OpenInv plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -78,16 +77,7 @@ public class ContainerSettingPluginCommand implements CommandExecutor, TabComple
return Collections.emptyList(); return Collections.emptyList();
} }
String argument = args[0].toLowerCase(); return TabCompleter.completeString(args[0], new String[] {"check", "on", "off"});
List<String> completions = new ArrayList<>();
for (String subcommand : new String[] {"check", "on", "off"}) {
if (subcommand.startsWith(argument)) {
completions.add(subcommand);
}
}
return completions;
} }
} }

View file

@ -19,22 +19,25 @@ package com.lishid.openinv.commands;
import com.lishid.openinv.OpenInv; import com.lishid.openinv.OpenInv;
import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.ISpecialInventory;
import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.Permissions;
import com.lishid.openinv.util.TabCompleter;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public class OpenInvPluginCommand implements CommandExecutor { public class OpenInvCommand implements TabExecutor {
private final OpenInv plugin; private final OpenInv plugin;
private final HashMap<Player, String> openInvHistory = new HashMap<Player, String>(); private final HashMap<Player, String> openInvHistory = new HashMap<Player, String>();
private final HashMap<Player, String> openEnderHistory = new HashMap<Player, String>(); private final HashMap<Player, String> openEnderHistory = new HashMap<Player, String>();
public OpenInvPluginCommand(final OpenInv plugin) { public OpenInvCommand(final OpenInv plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -73,7 +76,7 @@ public class OpenInvPluginCommand implements CommandExecutor {
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
final OfflinePlayer offlinePlayer = OpenInvPluginCommand.this.plugin.matchPlayer(name); final OfflinePlayer offlinePlayer = OpenInvCommand.this.plugin.matchPlayer(name);
if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) { if (offlinePlayer == null || !offlinePlayer.hasPlayedBefore() && !offlinePlayer.isOnline()) {
player.sendMessage(ChatColor.RED + "Player not found!"); player.sendMessage(ChatColor.RED + "Player not found!");
@ -86,9 +89,9 @@ public class OpenInvPluginCommand implements CommandExecutor {
if (!player.isOnline()) { if (!player.isOnline()) {
return; return;
} }
OpenInvPluginCommand.this.openInventory(player, offlinePlayer, openinv); OpenInvCommand.this.openInventory(player, offlinePlayer, openinv);
} }
}.runTask(OpenInvPluginCommand.this.plugin); }.runTask(OpenInvCommand.this.plugin);
} }
}.runTaskAsynchronously(this.plugin); }.runTaskAsynchronously(this.plugin);
@ -161,4 +164,13 @@ public class OpenInvPluginCommand implements CommandExecutor {
plugin.openInventory(player, inv); plugin.openInventory(player, inv);
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (!command.testPermissionSilent(sender) || args.length != 1) {
return Collections.emptyList();
}
return TabCompleter.completeOnlinePlayer(sender, args[0]);
}
} }

View file

@ -17,10 +17,13 @@
package com.lishid.openinv.commands; package com.lishid.openinv.commands;
import com.lishid.openinv.OpenInv; import com.lishid.openinv.OpenInv;
import com.lishid.openinv.util.TabCompleter;
import java.util.Collections;
import java.util.List;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -33,11 +36,11 @@ import org.bukkit.inventory.meta.ItemMeta;
* *
* @author Jikoo * @author Jikoo
*/ */
public class SearchEnchantPluginCommand implements CommandExecutor { public class SearchEnchantCommand implements TabExecutor {
private final OpenInv plugin; private final OpenInv plugin;
public SearchEnchantPluginCommand(OpenInv plugin) { public SearchEnchantCommand(OpenInv plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -130,4 +133,17 @@ public class SearchEnchantPluginCommand implements CommandExecutor {
return false; return false;
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (!command.testPermissionSilent(sender) || args.length < 1 || args.length > 2) {
return Collections.emptyList();
}
if (args.length == 1) {
return TabCompleter.completeObject(args[0], Enchantment::getName, Enchantment.values());
} else {
return TabCompleter.completeInteger(args[1]);
}
}
} }

View file

@ -17,19 +17,22 @@
package com.lishid.openinv.commands; package com.lishid.openinv.commands;
import com.lishid.openinv.OpenInv; import com.lishid.openinv.OpenInv;
import com.lishid.openinv.util.TabCompleter;
import java.util.Collections;
import java.util.List;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
public class SearchInvPluginCommand implements CommandExecutor { public class SearchInvCommand implements TabExecutor {
private final OpenInv plugin; private final OpenInv plugin;
public SearchInvPluginCommand(OpenInv plugin) { public SearchInvCommand(OpenInv plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -40,8 +43,7 @@ public class SearchInvPluginCommand implements CommandExecutor {
int count = 1; int count = 1;
if (args.length >= 1) { if (args.length >= 1) {
String[] gData = args[0].split(":"); material = Material.getMaterial(args[0]);
material = Material.matchMaterial(gData[0]);
} }
if (args.length >= 2) { if (args.length >= 2) {
@ -77,4 +79,18 @@ public class SearchInvPluginCommand implements CommandExecutor {
return true; return true;
} }
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (args.length < 1 || args.length > 2 || !command.testPermissionSilent(sender)) {
return Collections.emptyList();
}
String argument = args[args.length - 1];
if (args.length == 1) {
return TabCompleter.completeEnum(argument, Material.class);
} else {
return TabCompleter.completeInteger(argument);
}
}
} }

View file

@ -0,0 +1,147 @@
/*
* Copyright (C) 2011-2020 lishid. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lishid.openinv.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Function;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
/**
* Utility class for common tab completions.
*/
public class TabCompleter {
/**
* Offer tab completions for whole numbers.
*
* @param argument the argument to complete
* @return integer options
*/
public static List<String> completeInteger(String argument) {
// Ensure existing argument is actually a number
if (!argument.isEmpty()) {
try {
Integer.parseInt(argument);
} catch (NumberFormatException e) {
return Collections.emptyList();
}
}
List<String> completions = new ArrayList<>(10);
for (int i = 0; i < 10; ++i) {
completions.add(argument + i);
}
return completions;
}
/**
* Offer tab completions for a given Enum.
*
* @param argument the argument to complete
* @param enumClazz the Enum to complete for
* @return the matching Enum values
*/
public static List<String> completeEnum(String argument, Class<? extends Enum<?>> enumClazz) {
argument = argument.toLowerCase(Locale.ENGLISH);
List<String> completions = new ArrayList<>();
for (Enum<?> enumConstant : enumClazz.getEnumConstants()) {
String name = enumConstant.name().toLowerCase();
if (name.startsWith(argument)) {
completions.add(name);
}
}
return completions;
}
/**
* Offer tab completions for a given array of Strings.
*
* @param argument the argument to complete
* @param options the Strings which may be completed
* @return the matching Strings
*/
public static List<String> completeString(String argument, String[] options) {
argument = argument.toLowerCase(Locale.ENGLISH);
List<String> completions = new ArrayList<>();
for (String option : options) {
if (option.startsWith(argument)) {
completions.add(option);
}
}
return completions;
}
/**
* Offer tab completions for visible online Players' names.
*
* @param sender the command's sender
* @param argument the argument to complete
* @return the matching Players' names
*/
public static List<String> completeOnlinePlayer(CommandSender sender, String argument) {
List<String> completions = new ArrayList<>();
Player senderPlayer = sender instanceof Player ? (Player) sender : null;
for (Player player : Bukkit.getOnlinePlayers()) {
if (senderPlayer != null && !senderPlayer.canSee(player)) {
continue;
}
if (StringUtil.startsWithIgnoreCase(player.getName(), argument)) {
completions.add(player.getName());
}
}
return completions;
}
/**
* Offer tab completions for a given array of Objects.
*
* @param argument the argument to complete
* @param converter the Function for converting the Object into a comparable String
* @param options the Objects which may be completed
* @return the matching Strings
*/
public static <T> List<String> completeObject(String argument, Function<T, String> converter, T[] options) {
argument = argument.toLowerCase(Locale.ENGLISH);
List<String> completions = new ArrayList<>();
for (T option : options) {
String optionString = converter.apply(option).toLowerCase();
if (optionString.startsWith(argument)) {
completions.add(optionString);
}
}
return completions;
}
private TabCompleter() {}
}

View file

@ -49,7 +49,7 @@ commands:
permission: OpenInv.search permission: OpenInv.search
description: Searches and lists players having a specific item in their ender chest description: Searches and lists players having a specific item in their ender chest
usage: |- usage: |-
/<command> <item> [MinAmount] - Item is the ID or the Bukkit Material, MinAmount is the minimum amount required /<command> <item> [MinAmount] - Item is the Bukkit Material, MinAmount is the minimum amount required
silentcontainer: silentcontainer:
aliases: [sc, silent, silentchest] aliases: [sc, silent, silentchest]
description: SilentContainer stops sounds and animations when using containers. description: SilentContainer stops sounds and animations when using containers.