<commit_before>package team.unstudio.udpl.command.anno;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import team.unstudio.udpl.command.CommandHelper;
import team.unstudio.udpl.core.UDPLI18n;
import team.unstudio.udpl.i18n.I18n;

import javax.annotation.Nonnull;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Optional;

public class AnnoCommandManager implements CommandExecutor,TabCompleter{
	
	private static final String MESSAGE_UNKNOWN_COMMAND = UDPLI18n.format("message.unknown_command");
	private static final String MESSAGE_NO_PERMISSION = UDPLI18n.format("message.no_permission");
	private static final String MESSAGE_NO_ENOUGH_PARAMETER = UDPLI18n.format("message.no_enough_parameter");
	private static final String MESSAGE_WRONG_SENDER = UDPLI18n.format("message.wrong_sender");
	private static final String MESSAGE_WRONG_PARAMETER = UDPLI18n.format("message.wrong_parameter");
	private static final String MESSAGE_FAILURE = UDPLI18n.format("message.failure");
	
	private final JavaPlugin plugin;
	private final String name;
	private final CommandWrapper defaultHandler;
	
	protected final Map<Class<?>,CommandParameterHandler> parameterHandlers;
	
	private I18n i18n;
	private String usage;
	private String description;
	
	/**
	 * 创建指令管理者
	 * @param name 指令名
	 */
	public AnnoCommandManager(@Nonnull String name){
		this(name,(JavaPlugin) Bukkit.getPluginCommand(name).getPlugin());
	}

	/**
	 * 创建指令管理者
	 * @param name 指令名
	 * @param plugin 插件
	 */
	public AnnoCommandManager(@Nonnull String name, @Nonnull JavaPlugin plugin){
		this(name,plugin,null);
	}
	
	/**
	 * 创建指令管理者
	 * @param name 指令名
	 * @param plugin 插件
	 * @param parameterHandlers 指令参数处理器
	 */
	public AnnoCommandManager(@Nonnull String name, @Nonnull JavaPlugin plugin, Map<Class<?>,CommandParameterHandler> parameterHandlers){
		Validate.notNull(name);
		Validate.notNull(plugin);
		this.name = name;
		this.plugin = plugin;
		this.defaultHandler = new CommandWrapper(name,this,null);
		this.parameterHandlers = initParameter(parameterHandlers);
	}
	
	protected Map<Class<?>,CommandParameterHandler> initParameter(Map<Class<?>,CommandParameterHandler> parameterHandlers){
		Map<Class<?>,CommandParameterHandler> map = Maps.newHashMap();
		
		map.put(boolean.class, new CommandParameterHandler.BooleanHandler());
		map.put(Player.class, new CommandParameterHandler.PlayerHandler());
		map.put(OfflinePlayer.class, new CommandParameterHandler.OfflinePlayerHandler());
		
		if(parameterHandlers != null)
			map.putAll(parameterHandlers);
		
		return ImmutableMap.copyOf(map);
	}
	
	public JavaPlugin getPlugin() {
		return plugin;
	}
	
	public String getName() {
		return name;
	}
	
	public String getUsage() {
		return usage;
	}

	public void setUsage(String usage) {
		this.usage = usage;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public I18n getI18n() {
		return i18n;
	}

	public void setI18n(I18n i18n) {
		this.i18n = i18n;
	}
	
	/**
	 * 注册指令
	 */
	public AnnoCommandManager registerCommand(){
		PluginCommand command = plugin.getCommand(name);
		command.setExecutor(this);
		command.setTabCompleter(this);
		plugin.getLogger().info("Register command \""+name+"\" successful.");
		return this;
	}
	
	/**
	 * 不安全的注册指令
	 */
	public AnnoCommandManager unsafeRegisterCommand(){
		Optional<PluginCommand> command = CommandHelper.unsafeRegisterCommand(name, plugin);
		if(command.isPresent()){
			command.get().setExecutor(this);
			command.get().setTabCompleter(this);
			plugin.getLogger().info("Unsafe register command \""+name+"\" successful.");
		}else{
			plugin.getLogger().warning("Unsafe register command \""+name+"\" failure.");
		}
		return this;
	}
	
	/**
	 * 添加指令
	 */
	public AnnoCommandManager addHandler(Object object) {
		for (Method method : object.getClass().getDeclaredMethods()) {
			team.unstudio.udpl.command.anno.Command annoCommand = method
					.getAnnotation(team.unstudio.udpl.command.anno.Command.class);

			if (annoCommand != null) {
				createCommandWrapper(annoCommand.value()).setCommandMethod(object, annoCommand, method);

				for (Alias annoAlias : method.getAnnotationsByType(Alias.class))
					createCommandWrapper(annoAlias.value()).setCommandMethod(object, annoCommand, method);

				continue;
			}
			
			TabComplete annoTabComplete = method.getAnnotation(TabComplete.class);
			
			if(annoTabComplete != null){
				getCommandWrapper(annoTabComplete.value()).ifPresent(wrapper->wrapper.setTabCompleteMethod(object, annoTabComplete, method));
			}
		}
		return this;
	}
	
	public AnnoCommandManager addHandler(Object... object){
		Arrays.stream(object).forEach(this::addHandler);
		return this;
	}

	protected void onUnknownCommand(CommandSender sender, Command command, String label, String[] args, CommandWrapper handler) {
		if (sender instanceof Player)
			sender.sendMessage(UDPLI18n.format((Player) sender, "message.unknown_command"));
		else
			sender.sendMessage(String.format(MESSAGE_UNKNOWN_COMMAND, label));
	}<commit_msg>Fixed AnnoCommand system's i18n format<commit_after>package team.unstudio.udpl.command.anno;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import team.unstudio.udpl.command.CommandHelper;
import team.unstudio.udpl.core.UDPLI18n;
import team.unstudio.udpl.i18n.I18n;

import javax.annotation.Nonnull;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Optional;

public class AnnoCommandManager implements CommandExecutor,TabCompleter{
	
	private static final String MESSAGE_UNKNOWN_COMMAND = UDPLI18n.format("message.unknown_command");
	private static final String MESSAGE_NO_PERMISSION = UDPLI18n.format("message.no_permission");
	private static final String MESSAGE_NO_ENOUGH_PARAMETER = UDPLI18n.format("message.no_enough_parameter");
	private static final String MESSAGE_WRONG_SENDER = UDPLI18n.format("message.wrong_sender");
	private static final String MESSAGE_WRONG_PARAMETER = UDPLI18n.format("message.wrong_parameter");
	private static final String MESSAGE_FAILURE = UDPLI18n.format("message.failure");
	
	private final JavaPlugin plugin;
	private final String name;
	private final CommandWrapper defaultHandler;
	
	protected final Map<Class<?>,CommandParameterHandler> parameterHandlers;
	
	private I18n i18n;
	private String usage;
	private String description;
	
	/**
	 * 创建指令管理者
	 * @param name 指令名
	 */
	public AnnoCommandManager(@Nonnull String name){
		this(name,(JavaPlugin) Bukkit.getPluginCommand(name).getPlugin());
	}

	/**
	 * 创建指令管理者
	 * @param name 指令名
	 * @param plugin 插件
	 */
	public AnnoCommandManager(@Nonnull String name, @Nonnull JavaPlugin plugin){
		this(name,plugin,null);
	}
	
	/**
	 * 创建指令管理者
	 * @param name 指令名
	 * @param plugin 插件
	 * @param parameterHandlers 指令参数处理器
	 */
	public AnnoCommandManager(@Nonnull String name, @Nonnull JavaPlugin plugin, Map<Class<?>,CommandParameterHandler> parameterHandlers){
		Validate.notNull(name);
		Validate.notNull(plugin);
		this.name = name;
		this.plugin = plugin;
		this.defaultHandler = new CommandWrapper(name,this,null);
		this.parameterHandlers = initParameter(parameterHandlers);
	}
	
	protected Map<Class<?>,CommandParameterHandler> initParameter(Map<Class<?>,CommandParameterHandler> parameterHandlers){
		Map<Class<?>,CommandParameterHandler> map = Maps.newHashMap();
		
		map.put(boolean.class, new CommandParameterHandler.BooleanHandler());
		map.put(Player.class, new CommandParameterHandler.PlayerHandler());
		map.put(OfflinePlayer.class, new CommandParameterHandler.OfflinePlayerHandler());
		
		if(parameterHandlers != null)
			map.putAll(parameterHandlers);
		
		return ImmutableMap.copyOf(map);
	}
	
	public JavaPlugin getPlugin() {
		return plugin;
	}
	
	public String getName() {
		return name;
	}
	
	public String getUsage() {
		return usage;
	}

	public void setUsage(String usage) {
		this.usage = usage;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public I18n getI18n() {
		return i18n;
	}

	public void setI18n(I18n i18n) {
		this.i18n = i18n;
	}
	
	/**
	 * 注册指令
	 */
	public AnnoCommandManager registerCommand(){
		PluginCommand command = plugin.getCommand(name);
		command.setExecutor(this);
		command.setTabCompleter(this);
		plugin.getLogger().info("Register command \""+name+"\" successful.");
		return this;
	}
	
	/**
	 * 不安全的注册指令
	 */
	public AnnoCommandManager unsafeRegisterCommand(){
		Optional<PluginCommand> command = CommandHelper.unsafeRegisterCommand(name, plugin);
		if(command.isPresent()){
			command.get().setExecutor(this);
			command.get().setTabCompleter(this);
			plugin.getLogger().info("Unsafe register command \""+name+"\" successful.");
		}else{
			plugin.getLogger().warning("Unsafe register command \""+name+"\" failure.");
		}
		return this;
	}
	
	/**
	 * 添加指令
	 */
	public AnnoCommandManager addHandler(Object object) {
		for (Method method : object.getClass().getDeclaredMethods()) {
			team.unstudio.udpl.command.anno.Command annoCommand = method
					.getAnnotation(team.unstudio.udpl.command.anno.Command.class);

			if (annoCommand != null) {
				createCommandWrapper(annoCommand.value()).setCommandMethod(object, annoCommand, method);

				for (Alias annoAlias : method.getAnnotationsByType(Alias.class))
					createCommandWrapper(annoAlias.value()).setCommandMethod(object, annoCommand, method);

				continue;
			}
			
			TabComplete annoTabComplete = method.getAnnotation(TabComplete.class);
			
			if(annoTabComplete != null){
				getCommandWrapper(annoTabComplete.value()).ifPresent(wrapper->wrapper.setTabCompleteMethod(object, annoTabComplete, method));
			}
		}
		return this;
	}
	
	public AnnoCommandManager addHandler(Object... object){
		Arrays.stream(object).forEach(this::addHandler);
		return this;
	}

	protected void onUnknownCommand(CommandSender sender, Command command, String label, String[] args, CommandWrapper handler) {
		if (sender instanceof Player)
			sender.sendMessage(UDPLI18n.format((Player) sender, "message.unknown_command", label));
		else
			sender.sendMessage(UDPLI18n.format(MESSAGE_UNKNOWN_COMMAND, label));
	}
{'fraction_non_alphanumeric': 0.1157373440939105, 'fraction_numerical': 0.007703595011005136, 'mean_word_length': 5.274453394706559, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3634441', 'n_tokens_mistral': 3739, 'n_tokens_neox': 3430, 'n_words': 661}