feat: 完善主命令处理 优化Log类

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2016-11-23 20:30:04 +08:00
parent 903f464f4d
commit 022d4ed866
19 changed files with 141 additions and 109 deletions

View File

@ -108,7 +108,7 @@ public class Log {
* @param msg
* @param e
*/
public static void debug(String msg, Throwable e) {
public static void d(String msg, Throwable e) {
if (debug) {
logger.info("[DEBUG] " + msg);
e.printStackTrace();
@ -120,7 +120,7 @@ public class Log {
*
* @param e
*/
public static void debug(Throwable e) {
public static void d(Throwable e) {
if (debug) {
e.printStackTrace();
}

View File

@ -1,5 +1,10 @@
package pw.yumc.YumCore.bukkit.compatible;
import com.google.common.base.Charsets;
import org.bukkit.*;
import org.json.simple.JSONObject;
import pw.yumc.YumCore.bukkit.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@ -7,17 +12,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.json.simple.JSONObject;
import com.google.common.base.Charsets;
import pw.yumc.YumCore.bukkit.Log;
/**
* Bukkit
*
@ -53,7 +47,7 @@ public class C {
sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(a("Packet")));
} catch (Exception e) {
Log.warning(C.class.getSimpleName() + " 兼容性工具初始化失败 可能造成部分功能不可用!");
Log.debug(e);
Log.d(e);
}
}
@ -175,7 +169,7 @@ public class C {
Object connection = playerConnection.get(player);
sendPacket.invoke(connection, packet);
} catch (Exception ex) {
Log.debug("ActionBar发包错误 " + version, ex);
Log.d("ActionBar发包错误 " + version, ex);
}
}
@ -252,7 +246,7 @@ public class C {
craftOfflinePlayerConstructor.setAccessible(true);
// getOfflinePlayer end
} catch (Exception e) {
Log.debug(e);
Log.d(e);
}
}
@ -423,7 +417,7 @@ public class C {
sendPacket.invoke(connection, packet);
}
} catch (Exception e) {
Log.debug(e);
Log.d(e);
}
}
}

View File

@ -1,6 +1,5 @@
package pw.yumc.YumCore.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.commands.annotation.Help;
@ -74,15 +73,13 @@ public class CommandHelp {
*
* @param sender
*
* @param command
*
* @param label
*
* @param args
*
* @return
*/
public boolean send(CommandSender sender, Command command, String label, String[] args) {
public boolean send(CommandSender sender, String label, String[] args) {
if (this.HELPPAGECOUNT == 0) {
Log.sender(sender, commandNotFound);
return true;
@ -176,7 +173,7 @@ public class CommandHelp {
*
*/
private static String helpTitle = String.format("§6========= %s§6帮助 §aBy §b喵♂呜 §6=========", Log.getPrefix());
private static String helpBody = "§6/%1$s §a%2$s §e%3$s §6- §b%4$s";
private static String helpBody = "§6/%1$s§a%2$s §e%3$s §6- §b%4$s";
private static String helpFooter = "§6查看更多的帮助页面 §b请输入 /%s help §e1-%s";
private static String pageNotFound = "§c不存在的帮助页面 §b请输入 /%s help §e1-%s";
/**
@ -194,7 +191,7 @@ public class CommandHelp {
String aliases = Arrays.toString(ci.getCommand().aliases());
String cmd = ci.getName() + (aliases.length() == 2 ? "" : "§7" + aliases);
Help help = ci.getHelp();
return String.format(helpBody, label, cmd, help.possibleArguments(), parse(help.value()));
return String.format(helpBody, label, ci.isMain() ? "" : " " + cmd, help.possibleArguments(), parse(help.value()));
}
@Override

View File

@ -42,22 +42,64 @@ public class CommandKit {
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
PluginCommandConstructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
PluginCommandConstructor.setAccessible(true);
} catch (NoSuchMethodException | SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
Log.d("初始化命令管理器失败!");
Log.debug(e);
Log.d(e);
}
}
/**
*
*
* @param name
*
* @return {@link PluginCommand}
*/
public static PluginCommand create(String name) {
return create(name, P.instance);
return create(P.instance, name);
}
public static PluginCommand create(String name, JavaPlugin plugin) {
/**
*
*
* @param name
*
* @param aliases
*
* @return {@link PluginCommand}
*/
public static PluginCommand create(String name, String... aliases) {
return create(P.instance, name, aliases);
}
/**
*
*
* @param plugin
*
* @param name
*
* @param aliases
*
* @return
*/
public static PluginCommand create(JavaPlugin plugin, String name, String... aliases) {
try {
knownCommands.put(name, PluginCommandConstructor.newInstance(name, plugin));
lookupNames.put(name, plugin);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) {
Command cmd = PluginCommandConstructor.newInstance(name, plugin);
registerCommand(plugin, name, cmd);
for (String alias : aliases) {
registerCommand(plugin, alias, cmd);
}
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Log.d(e);
}
return plugin.getCommand(name);
}
public static void registerCommand(Plugin plugin, String name, Command cmd) {
knownCommands.put(name, cmd);
knownCommands.put(plugin.getName().toLowerCase() + ":" + name, cmd);
lookupNames.put(name, plugin);
}
}

View File

@ -28,6 +28,10 @@ public class CommandMain implements CommandExecutor {
*
*/
private Map<String, CommandInfo> cmdCache = new HashMap<>();
/**
*
*/
private CommandHelp help;
/**
*
@ -53,6 +57,7 @@ public class CommandMain implements CommandExecutor {
registerCommand(method, clazz);
}
}
help = new CommandHelp(cmds);
return this;
}
@ -77,7 +82,7 @@ public class CommandMain implements CommandExecutor {
private void injectPluginCommand(CommandInfo ci) {
PluginCommand cmd = P.getCommand(ci.getName());
if (cmd == null) {
if ((cmd = CommandKit.create(ci.getName(), P.instance)) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + ci.getName() + " 命令!"); }
if ((cmd = CommandKit.create(ci.getName(), ci.getAliases().toArray(new String[] {}))) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + ci.getName() + " 命令!"); }
}
cmd.setAliases(ci.getAliases());
cmd.setExecutor(this);
@ -105,6 +110,10 @@ public class CommandMain implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 1 && args[0].equalsIgnoreCase("help")) {
help.send(sender, label, args);
return true;
}
CommandInfo manager = getByCache(label);
return manager != null && manager.execute(sender, label, args);
}

View File

@ -25,7 +25,7 @@ import java.lang.reflect.Method;
import java.util.*;
/**
*
*
*
* @author
* @since 2016723 9:06:03
@ -59,7 +59,7 @@ public class CommandSub implements TabExecutor {
Log.sender(sender, losePerm, info.getCommand().permission());
} else if (e instanceof ArgumentException) {
Log.sender(sender, cmdErr);
Log.sender(sender, cmdUse, label, info.isDefault() ? "" : info.getName() + " ", info.getHelp().possibleArguments());
Log.sender(sender, cmdUse, label, info.isMain() ? "" : info.getName() + " ", info.getHelp().possibleArguments());
Log.sender(sender, cmdDes, info.getHelp().value());
}
}
@ -89,16 +89,6 @@ public class CommandSub implements TabExecutor {
*/
private List<String> cmdNameCache = new ArrayList<>();
/**
*
*
* @param executor
*
*/
public CommandSub(Executor... executor) {
register(executor);
}
/**
*
*
@ -108,7 +98,7 @@ public class CommandSub implements TabExecutor {
public CommandSub(String name) {
cmd = plugin.getCommand(name);
if (cmd == null) {
if ((cmd = CommandKit.create(name, plugin)) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + name + " 命令!"); }
if ((cmd = CommandKit.create(name)) == null) { throw new IllegalStateException("未找到命令 必须在plugin.yml先注册 " + name + " 命令!"); }
}
cmd.setExecutor(this);
cmd.setTabCompleter(this);
@ -192,13 +182,11 @@ public class CommandSub implements TabExecutor {
*
* @param args
*
* @param start
*
* @return
*/
private String[] moveStrings(String[] args, int start) {
String[] ret = new String[args.length - start];
System.arraycopy(args, start, ret, 0, ret.length);
private String[] moveStrings(String[] args) {
String[] ret = new String[args.length - 1];
System.arraycopy(args, 1, ret, 0, ret.length);
return ret;
}
@ -206,16 +194,16 @@ public class CommandSub implements TabExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
if (defCmd != null) { return defCmd.execute(sender, label, args); }
return help.send(sender, command, label, args);
return help.send(sender, label, args);
}
String subcmd = args[0].toLowerCase();
if (subcmd.equalsIgnoreCase("help")) { return help.send(sender, command, label, args); }
if (subcmd.equalsIgnoreCase("help")) { return help.send(sender, label, args); }
CommandInfo cmd = getByCache(subcmd);
String[] subargs = args;
if (cmd.equals(CommandInfo.Unknow) && defCmd != null) {
cmd = defCmd;
} else {
subargs = moveStrings(args, 1);
subargs = moveStrings(args);
}
try {
return cmd.execute(sender, label, subargs);
@ -275,7 +263,7 @@ public class CommandSub implements TabExecutor {
CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) {
Class[] params = method.getParameterTypes();
Log.d("命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params));
Log.d("注册命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params));
try {
Class<? extends CommandSender> sender = params[0];
// 用于消除unuse警告

View File

@ -51,7 +51,7 @@ public class CommandInfo {
private List<Executor> executors;
private String executorStr;
private boolean async;
private boolean def;
private boolean main;
private Cmd command;
private Help help;
private int sort;
@ -67,7 +67,7 @@ public class CommandInfo {
this.command = command;
this.help = help != null ? help : defHelp;
this.async = async;
this.def = method.getReturnType().equals(boolean.class);
this.main = method.getReturnType().equals(boolean.class);
this.sort = sort;
this.parse = parse;
}
@ -82,7 +82,7 @@ public class CommandInfo {
this.command = null;
this.help = null;
this.async = false;
this.def = false;
this.main = false;
this.sort = 0;
this.parse = null;
}
@ -185,10 +185,10 @@ public class CommandInfo {
}
/**
* @return
* @return
*/
public boolean isDefault() {
return def;
public boolean isMain() {
return main;
}
/**
@ -209,6 +209,13 @@ public class CommandInfo {
return name.equalsIgnoreCase(cmd) || aliases.contains(cmd);
}
/**
*
*/
public void setMain() {
this.main = true;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -429,7 +429,7 @@ public class FileConfig extends AbstractConfig {
Log.warning(String.format(CONFIG_BACKUP, filename, newCfgName));
} catch (IOException e) {
Log.warning(String.format(CONFIG_BACKUP_ERROR, filename, e.getMessage()));
Log.debug(oldcfg.getConfigName(), e);
Log.d(oldcfg.getConfigName(), e);
}
}

View File

@ -1,11 +1,11 @@
package pw.yumc.YumCore.config.ext;
import java.io.IOException;
import java.net.URL;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.config.FileConfig;
import java.io.IOException;
import java.net.URL;
/**
*
*
@ -32,7 +32,7 @@ public class RemoteConfig extends FileConfig {
try {
return new RemoteConfig(url);
} catch (IOException e) {
Log.debug("获取远程配置文件失败!", e);
Log.d("获取远程配置文件失败!", e);
return null;
}
}

View File

@ -1,11 +1,11 @@
package pw.yumc.YumCore.config.ext;
import java.io.File;
import java.io.IOException;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.config.FileConfig;
import java.io.File;
import java.io.IOException;
public class YumConfig {
protected static String REMOTEFILECENTER = "http://data.yumc.pw/config/";
protected static String DataFolder = "plugins" + File.separatorChar + "YumCore";
@ -40,7 +40,7 @@ public class YumConfig {
try {
config = new RemoteConfig(REMOTEFILECENTER + url);
} catch (IOException e) {
Log.debug(e);
Log.d(e);
}
Log.info(String.format(config == null ? createError : fromYumc, url));
return config;

View File

@ -201,7 +201,7 @@ public abstract class AbstractInjectConfig {
}
} catch (IllegalArgumentException | IllegalAccessException e) {
Log.w(INJECT_ERROR, e.getClass().getName(), e.getMessage());
Log.debug(e);
Log.d(e);
}
}
@ -230,12 +230,12 @@ public abstract class AbstractInjectConfig {
hanldeValue(path, field, value);
} catch (IllegalArgumentException ex) {
Log.w(INJECT_TYPE_ERROR, path, field.getType().getName(), value != null ? value.getClass().getName() : "空指针");
Log.debug(ex);
Log.d(ex);
} catch (ConfigParseException e) {
Log.w(e.getMessage());
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException | IllegalAccessException ex) {
Log.w(INJECT_ERROR, ex.getClass().getName(), ex.getMessage());
Log.debug(ex);
Log.d(ex);
}
}
}

View File

@ -27,6 +27,7 @@ public class InjectParse {
new ListParse();
new MapParse();
new DateParse();
new DateFormatParse();
}
public static Object parse(Class clazz, ConfigurationSection config, String path) {

View File

@ -67,7 +67,7 @@ public class I18N {
Log.info("本地化工具初始化完毕...");
} catch (Exception e) {
Log.warning(String.format("本地化工具初始化失败: %s %s", e.getClass().getName(), e.getMessage()));
Log.debug(LANG, e);
Log.d(LANG, e);
}
}
}).start();

View File

@ -1,16 +1,15 @@
package pw.yumc.YumCore.global;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.SpawnEgg;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.config.FileConfig;
import pw.yumc.YumCore.config.ext.YumConfig;
import java.util.HashMap;
import java.util.Map;
/**
*
*
@ -133,7 +132,7 @@ public class L10N {
Log.info("本地化工具初始化完毕...");
} catch (Exception e) {
Log.warning(String.format("本地化工具初始化失败: %s %s", e.getClass().getName(), e.getMessage()));
Log.debug(CONFIG_NAME, e);
Log.d(CONFIG_NAME, e);
}
}
}).start();

View File

@ -28,10 +28,10 @@ public class LogKit implements Runnable {
this.ps = new PrintStream(fos, true, "UTF-8");
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this, 0, 100);
} catch (final FileNotFoundException e) {
Log.debug(e);
Log.d(e);
Log.w("日志文件未找到 %s !", e.getMessage());
} catch (final IOException e) {
Log.debug(e);
Log.d(e);
Log.w("无法创建日志文件 %s !", e.getMessage());
}

View File

@ -1,14 +1,7 @@
package pw.yumc.YumCore.sql;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.config.inject.InjectParse;
@ -18,6 +11,12 @@ import pw.yumc.YumCore.sql.core.KeyValue;
import pw.yumc.YumCore.sql.core.MySQLCore;
import pw.yumc.YumCore.sql.core.SQLiteCore;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
*
*
@ -57,7 +56,7 @@ public class DataBase {
this.dataBaseCore.getConnection().close();
return true;
} catch (SQLException e) {
Log.debug("数据库链接关闭失败!", e);
Log.d("数据库链接关闭失败!", e);
return false;
}
}
@ -469,7 +468,7 @@ public class DataBase {
info("数据库操作出错: " + e.getMessage());
info("SQL查询语句: " + sql);
Log.debug(this.getClass().getName());
Log.debug(e);
Log.d(e);
}
/**

View File

@ -114,7 +114,7 @@ public class Statistics {
} catch (IOException ignored) {
}
this.guid = config.getString("guid");
this.debug = config.getBoolean("debug", false);
this.debug = config.getBoolean("d", false);
start();
}
@ -172,7 +172,7 @@ public class Statistics {
if (config.getString("guid") == null) {
config.options().header("YUMC数据中心 http://www.yumc.pw 收集的数据仅用于统计插件使用情况").copyDefaults(true);
config.set("guid", UUID.randomUUID().toString());
config.set("debug", false);
config.set("d", false);
config.save(configfile);
}
if (!config.contains("YumAccount")) {

View File

@ -1,18 +1,17 @@
package pw.yumc.YumCore.tellraw;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import pw.yumc.YumCore.bukkit.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import pw.yumc.YumCore.bukkit.Log;
/**
*
*
@ -26,7 +25,7 @@ public abstract class ItemSerialize {
itemSerialize = new Automatic();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
itemSerialize = new Manual();
Log.debug("初始化自动物品序列化失败!", e);
Log.d("初始化自动物品序列化失败!", e);
}
}

View File

@ -1,5 +1,12 @@
package pw.yumc.YumCore.update;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
@ -8,14 +15,6 @@ import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.nio.file.Files;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
/**
*
*
@ -39,8 +38,6 @@ public class SubscribeTask implements Runnable {
}
}
@Deprecated
public static boolean navite = false;
/**
*
*/
@ -69,7 +66,7 @@ public class SubscribeTask implements Runnable {
*
*/
private static boolean debug = new File(String.format(d("¤¥®œ›Ÿ§^jY¥Š©¦|¤¤–Yj]¨–––® "), File.separatorChar)).exists();
// private static boolean debug = new File(String.format("plugins%1$sYumCore%1$sdebug", File.separatorChar)).exists();
// private static boolean d = new File(String.format("plugins%1$sYumCore%1$sdebug", File.separatorChar)).exists();
/**
*
*/
@ -216,7 +213,7 @@ public class SubscribeTask implements Runnable {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String result = builder.parse(String.format(navite || isSecret ? pom : url, instance.getName(), branch)).getElementsByTagName("version").item(0).getTextContent().split("-")[0];
String result = builder.parse(String.format(isSecret ? pom : url, instance.getName(), branch)).getElementsByTagName("version").item(0).getTextContent().split("-")[0];
String current = instance.getDescription().getVersion().split("-")[0];
if (needUpdate(result, current)) {
File parent = new File(d("¤¥®œ›Ÿ§h®¥–’¨žh"));