Command.java 文件源码

java
阅读 24 收藏 0 点赞 0 评论 0

项目:StartupCommands 作者:
/**
 * Static method for removing a command from the configuration.
 * @param plugin The StartupCommands instance.
 * @param removeStr The String of the command to remove. If it is an Integer
 * the command will be removed by index, otherwise, the method looks for a matching
 * command String.
 * @return removeStr The command String of the removed command.
 * @throws IllegalArgumentException if the command doesn't exist, if the index provided 
 * is less than zero or greater than commands.size() - 1, or if the configuration file 
 * cannot be saved.
 */
public static String removeCommand(StartupCommands plugin, String removeStr) {
    FileConfiguration config = plugin.getConfig();

    // Find the command String if removeStr is an Integer
    if (StartupCommands.isInteger(removeStr)) {
        List<Command> commands = plugin.getCommands();
        int index = Integer.parseInt(removeStr) - 1;

        // Ensure index is valid
        if (index < 0 || index > commands.size() - 1) {
            throw new IllegalArgumentException("Index must be greater than 0 and less than the number of startup commands.");
        }

        removeStr = plugin.getCommands().remove(index).getCommand();
    }

    if (config.contains("commands." + removeStr)) {
        config.set("commands." + removeStr, null);

        // Try to save configuration
        try {
            config.save(plugin.getDataFolder() + File.separator + "config.yml");
        } catch (IOException e) {
            throw new IllegalArgumentException("Could not save configuration file.");
        }

        return removeStr;
    } else {
        throw new IllegalArgumentException("Could not identify command to remove by " + removeStr + ".");
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号