Improve any/silent container commands
* Merged duplicate code * Added on/off parameters - closes #120 * Added tab completion for parameters
This commit is contained in:
		@@ -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.");
 | 
			
		||||
 
 | 
			
		||||
@@ -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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
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<Player, Boolean> getSetting = any ? plugin::getPlayerAnyChestStatus : plugin::getPlayerSilentChestStatus;
 | 
			
		||||
        BiConsumer<OfflinePlayer, Boolean> 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<String> 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<String> completions = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (String subcommand : new String[] {"check", "on", "off"}) {
 | 
			
		||||
            if (subcommand.startsWith(argument)) {
 | 
			
		||||
                completions.add(subcommand);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return completions;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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 <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user