From 4f2665fedcba5e86ed0988e425ecf6c1673921ed Mon Sep 17 00:00:00 2001 From: Jikoo Date: Sat, 1 Feb 2020 08:00:03 -0500 Subject: [PATCH] Improve any/silent container commands * Merged duplicate code * Added on/off parameters - closes #120 * Added tab completion for parameters --- .../main/java/com/lishid/openinv/OpenInv.java | 57 ++---------- .../commands/AnyChestPluginCommand.java | 54 ----------- .../ContainerSettingPluginCommand.java | 93 +++++++++++++++++++ .../commands/SilentChestPluginCommand.java | 52 ----------- plugin/src/main/resources/plugin.yml | 10 +- 5 files changed, 107 insertions(+), 159 deletions(-) delete mode 100644 plugin/src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java create mode 100644 plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingPluginCommand.java delete mode 100644 plugin/src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index b5f8b1d..3703220 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -18,11 +18,10 @@ package com.lishid.openinv; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; -import com.lishid.openinv.commands.AnyChestPluginCommand; +import com.lishid.openinv.commands.ContainerSettingPluginCommand; import com.lishid.openinv.commands.OpenInvPluginCommand; import com.lishid.openinv.commands.SearchEnchantPluginCommand; import com.lishid.openinv.commands.SearchInvPluginCommand; -import com.lishid.openinv.commands.SilentChestPluginCommand; import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; @@ -167,50 +166,6 @@ public class OpenInv extends JavaPlugin implements IOpenInv { return this.accessor.getAnySilentContainer(); } - private int getLevenshteinDistance(final String string1, final String string2) { - if (string1 == null || string2 == null) { - throw new IllegalArgumentException("Strings must not be null"); - } - - if (string1.isEmpty()) { - return string2.length(); - } - if (string2.isEmpty()) { - return string2.length(); - } - if (string1.equals(string2)) { - return 0; - } - - int len1 = string1.length(); - int len2 = string2.length(); - - int[] prevDistances = new int[len1 + 1]; - int[] distances = new int[len1 + 1]; - - for (int i = 0; i <= len1; ++i) { - prevDistances[i] = i; - } - - for (int i = 1; i <= len2; ++i) { - // TODO: include tweaks available in Simmetrics? - char string2char = string2.charAt(i - 1); - distances[0] = i; - - for (int j = 1; j <= len1; ++j) { - int cost = string1.charAt(j - 1) == string2char ? 0 : 1; - - distances[j] = Math.min(Math.min(distances[j - 1] + 1, prevDistances[j] + 1), prevDistances[j - 1] + cost); - } - - int[] swap = prevDistances; - prevDistances = distances; - distances = swap; - } - - return prevDistances[len1]; - } - @Override public boolean getPlayerAnyChestStatus(@NotNull final OfflinePlayer player) { boolean defaultState = false; @@ -389,8 +344,14 @@ public class OpenInv extends JavaPlugin implements IOpenInv { this.getCommand("searchinv").setExecutor(searchInv); this.getCommand("searchender").setExecutor(searchInv); this.getCommand("searchenchant").setExecutor(new SearchEnchantPluginCommand(this)); - this.getCommand("silentchest").setExecutor(new SilentChestPluginCommand(this)); - this.getCommand("anychest").setExecutor(new AnyChestPluginCommand(this)); + ContainerSettingPluginCommand settingCommand = new ContainerSettingPluginCommand(this); + PluginCommand command = this.getCommand("silentcontainer"); + command.setExecutor(settingCommand); + command.setTabCompleter(settingCommand); + command = this.getCommand("anycontainer"); + command.setExecutor(settingCommand); + command.setTabCompleter(settingCommand); + } else { this.getLogger().info("Your version of CraftBukkit (" + this.accessor.getVersion() + ") is not supported."); this.getLogger().info("If this version is a recent release, check for an update."); diff --git a/plugin/src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java deleted file mode 100644 index 683d628..0000000 --- a/plugin/src/main/java/com/lishid/openinv/commands/AnyChestPluginCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2011-2019 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 . - */ - -package com.lishid.openinv.commands; - -import com.lishid.openinv.OpenInv; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class AnyChestPluginCommand implements CommandExecutor { - - private final OpenInv plugin; - - public AnyChestPluginCommand(final OpenInv plugin) { - this.plugin = plugin; - } - - @Override - public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); - return true; - } - - Player player = (Player) sender; - - if (args.length > 0 && args[0].equalsIgnoreCase("check")) { - sender.sendMessage("AnyChest is " + (this.plugin.getPlayerAnyChestStatus(player) ? "ON" : "OFF") + "."); - return true; - } - - this.plugin.setPlayerAnyChestStatus(player, !this.plugin.getPlayerAnyChestStatus(player)); - sender.sendMessage("AnyChest is now " + (this.plugin.getPlayerAnyChestStatus(player) ? "ON" : "OFF") + "."); - - return true; - } - -} diff --git a/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingPluginCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingPluginCommand.java new file mode 100644 index 0000000..e636e38 --- /dev/null +++ b/plugin/src/main/java/com/lishid/openinv/commands/ContainerSettingPluginCommand.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2011-2019 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 . + */ + +package com.lishid.openinv.commands; + +import com.lishid.openinv.OpenInv; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.Function; +import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; + +public class ContainerSettingPluginCommand implements CommandExecutor, TabCompleter { + + private final OpenInv plugin; + + public ContainerSettingPluginCommand(final OpenInv plugin) { + this.plugin = plugin; + } + + @Override + public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "You can't use this from the console."); + return true; + } + + Player player = (Player) sender; + boolean any = command.getName().startsWith("any"); + String commandName = any ? "AnyContainer" : "SilentContainer"; + Function getSetting = any ? plugin::getPlayerAnyChestStatus : plugin::getPlayerSilentChestStatus; + BiConsumer setSetting = any ? plugin::setPlayerAnyChestStatus : plugin::setPlayerSilentChestStatus; + + if (args.length > 0) { + args[0] = args[0].toLowerCase(); + + if (args[0].equals("on")) { + setSetting.accept(player, true); + } else if (args[0].equals("off")) { + setSetting.accept(player, false); + } else if (!args[0].equals("check")) { + // Invalid argument, show usage. + return false; + } + + } else { + setSetting.accept(player, !getSetting.apply(player)); + } + + sender.sendMessage(commandName + " is now " + (getSetting.apply(player) ? "ON" : "OFF") + "."); + + return true; + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (!command.testPermissionSilent(sender) || args.length != 1) { + return Collections.emptyList(); + } + + String argument = args[0].toLowerCase(); + List completions = new ArrayList<>(); + + for (String subcommand : new String[] {"check", "on", "off"}) { + if (subcommand.startsWith(argument)) { + completions.add(subcommand); + } + } + + return completions; + } + +} diff --git a/plugin/src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java b/plugin/src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java deleted file mode 100644 index 001d57f..0000000 --- a/plugin/src/main/java/com/lishid/openinv/commands/SilentChestPluginCommand.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2011-2019 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 . - */ - -package com.lishid.openinv.commands; - -import com.lishid.openinv.OpenInv; -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class SilentChestPluginCommand implements CommandExecutor { - - private final OpenInv plugin; - - public SilentChestPluginCommand(OpenInv plugin) { - this.plugin = plugin; - } - - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You can't use this from the console."); - return true; - } - - Player player = (Player) sender; - - if (args.length > 0 && args[0].equalsIgnoreCase("check")) { - sender.sendMessage("SilentChest is " + (plugin.getPlayerSilentChestStatus(player) ? "ON" : "OFF") + "."); - return true; - } - - plugin.setPlayerSilentChestStatus(player, !plugin.getPlayerSilentChestStatus(player)); - sender.sendMessage("SilentChest is now " + (plugin.getPlayerSilentChestStatus(player) ? "ON" : "OFF") + "."); - - return true; - } -} diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index a2d629c..b6df8df 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -43,7 +43,7 @@ commands: description: Search and list players having a specific item permission: OpenInv.search usage: |- - / [MinAmount] - Item is the ID or the Bukkit Material, MinAmount is the minimum amount required + / [MinAmount] - Item is the Bukkit Material, MinAmount is the minimum amount required searchender: aliases: [se] permission: OpenInv.search @@ -52,16 +52,16 @@ commands: / [MinAmount] - Item is the ID or the Bukkit Material, MinAmount is the minimum amount required silentcontainer: aliases: [sc, silent, silentchest] - description: Toggle SilentContainer function, which stops sounds and animations when using containers. + description: SilentContainer stops sounds and animations when using containers. permission: OpenInv.silent usage: |- - / [Check] - Check or toggle silent chest + / [check|on|off] - Check, toggle, or set SilentContainer anycontainer: aliases: [ac, anychest] - description: Toggle AnyContainer function, which allows opening of blocked containers. + description: AnyContainer allows using blocked containers. permission: OpenInv.anychest usage: |- - / [Check] - Checks or toggle anychest + / [check|on|off] - Check, toggle, or set AnyContainer searchenchant: aliases: [searchenchants] description: Search and list players with a specific enchantment.