diff --git a/src/main/java/com/ilummc/tlib/annotations/Logger.java b/src/main/java/com/ilummc/tlib/annotations/Logger.java index 1fd7408..da9456a 100644 --- a/src/main/java/com/ilummc/tlib/annotations/Logger.java +++ b/src/main/java/com/ilummc/tlib/annotations/Logger.java @@ -9,7 +9,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) public @interface Logger { - String value() default "[{0}|{1}] {1}"; + String value() default "[{0}|{1}§f] {2}"; int level() default com.ilummc.tlib.logger.TLogger.INFO; diff --git a/src/main/java/me/skymc/taboolib/Main.java b/src/main/java/me/skymc/taboolib/Main.java index 731b886..17591d7 100644 --- a/src/main/java/me/skymc/taboolib/Main.java +++ b/src/main/java/me/skymc/taboolib/Main.java @@ -4,6 +4,8 @@ import com.ilummc.tlib.TLib; import com.ilummc.tlib.annotations.Dependency; import com.ilummc.tlib.inject.TDependencyInjector; import com.ilummc.tlib.resources.TLocale; +import com.ilummc.tlib.util.IO; +import com.ilummc.tlib.util.Strings; import me.skymc.taboolib.anvil.AnvilContainerAPI; import me.skymc.taboolib.bstats.Metrics; import me.skymc.taboolib.commands.TabooLibMainCommand; @@ -49,8 +51,11 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; +import java.nio.charset.Charset; +import java.util.Arrays; import java.util.Random; /** @@ -180,10 +185,21 @@ public class Main extends JavaPlugin implements Listener { if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { new SupportPlaceholder(getInst(), "taboolib").hook(); } + // 载入 SpecialItem 接口 SpecialItem.getInst().loadItems(); // 载入 TLM 接口 TLM.getInst(); + + // 面子工程 + InputStream inputStream = FileUtils.getResource("motd.txt"); + try { + String text = new String(IO.readFully(inputStream), Charset.forName("utf-8")); + if (text != null) { + Arrays.stream(text.split("\n")).forEach(line -> Bukkit.getConsoleSender().sendMessage(Strings.replaceWithOrder(line, getDescription().getVersion()))); + } + } catch (IOException ignored) { + } } }.runTask(this); @@ -324,8 +340,8 @@ public class Main extends JavaPlugin implements Listener { private void registerCommands() { getCommand("language2").setExecutor(new Language2Command()); getCommand("taboolibrarymodule").setExecutor(new TLMCommands()); - getCommand("tabooliblocale").setExecutor(new TabooLibLocaleCommand()); TBaseCommand.registerCommand("taboolib", new TabooLibMainCommand()); + TBaseCommand.registerCommand("tabooliblocale", new TabooLibLocaleCommand()); TBaseCommand.registerCommand("taboolibplugin", new TabooLibPluginMainCommand()); } diff --git a/src/main/java/me/skymc/taboolib/bstats/Metrics.java b/src/main/java/me/skymc/taboolib/bstats/Metrics.java index 8866d16..f8d9a67 100644 --- a/src/main/java/me/skymc/taboolib/bstats/Metrics.java +++ b/src/main/java/me/skymc/taboolib/bstats/Metrics.java @@ -116,26 +116,26 @@ public class Metrics { // Load the data serverUUID = config.getString("serverUuid"); logFailedRequests = config.getBoolean("logFailedRequests", false); - if (config.getBoolean("enabled", true)) { - boolean found = false; - // Search for all other bStats Metrics classes to see if we are the first one - for (Class service : Bukkit.getServicesManager().getKnownServices()) { - try { - // Our identifier :) - service.getField("B_STATS_VERSION"); - // We aren't the first - found = true; - break; - } catch (NoSuchFieldException ignored) { - } - } - // Register our service - Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal); - if (!found) { - // We are the first! - startSubmitting(); + + // ignored config + boolean found = false; + // Search for all other bStats Metrics classes to see if we are the first one + for (Class service : Bukkit.getServicesManager().getKnownServices()) { + try { + // Our identifier :) + service.getField("B_STATS_VERSION"); + // We aren't the first + found = true; + break; + } catch (NoSuchFieldException ignored) { } } + // Register our service + Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal); + if (!found) { + // We are the first! + startSubmitting(); + } } /** diff --git a/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java b/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java index a97cfeb..be3b138 100644 --- a/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java +++ b/src/main/java/me/skymc/taboolib/commands/TabooLibMainCommand.java @@ -11,6 +11,7 @@ import me.skymc.taboolib.commands.internal.type.CommandType; import me.skymc.taboolib.commands.taboolib.*; import me.skymc.taboolib.fileutils.FileUtils; import me.skymc.taboolib.inventory.ItemUtils; +import me.skymc.taboolib.other.NumberUtils; import me.skymc.taboolib.plugin.PluginUtils; import me.skymc.taboolib.update.UpdateTask; import org.bukkit.Bukkit; @@ -710,7 +711,44 @@ public class TabooLibMainCommand extends BaseMainCommand { @CommandRegister(priority = 24) BaseSubCommand getEmptyLine6 = null; - @CommandRegister(priority = 25) + @CommandRegister(priority = 24.1) + BaseSubCommand lagServer = new BaseSubCommand() { + @Override + public String getLabel() { + return "lagServer"; + } + + @Override + public String getDescription() { + return TLocale.asString("COMMANDS.TABOOLIB.LAGSERVER.DESCRIPTION"); + } + + @Override + public CommandArgument[] getArguments() { + return new CommandArgument[] { + new CommandArgument(TLocale.asString("COMMANDS.TABOOLIB.LAGSERVER.ARGUMENTS.0")) + }; + } + + @Override + public void onCommand(CommandSender sender, Command command, String label, String[] args) { + if (NumberUtils.getInteger(args[0]) > 300000) { + TLocale.sendTo(sender, "COMMANDS.TABOOLIB.LAGSERVER.INVALID-TIME"); + } else { + TLocale.sendTo(sender, "COMMANDS.TABOOLIB.LAGSERVER.START"); + try { + Thread.sleep(NumberUtils.getInteger(args[0])); + } catch (Exception ignored) { + } + TLocale.sendTo(sender, "COMMANDS.TABOOLIB.LAGSERVER.STOP"); + } + } + }; + + @CommandRegister(priority = 26) + BaseSubCommand getEmptyLine7 = null; + + @CommandRegister(priority = 27) BaseSubCommand importData = new BaseSubCommand() { @Override @@ -734,7 +772,7 @@ public class TabooLibMainCommand extends BaseMainCommand { } }; - @CommandRegister(priority = 26) + @CommandRegister(priority = 28) BaseSubCommand updatePlugin = new BaseSubCommand() { @Override @@ -782,10 +820,5 @@ public class TabooLibMainCommand extends BaseMainCommand { } }.runTaskAsynchronously(Main.getInst()); } - - @Override - public CommandType getType() { - return CommandType.CONSOLE; - } }; } diff --git a/src/main/java/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java b/src/main/java/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java index f7f4231..b7800d1 100644 --- a/src/main/java/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java +++ b/src/main/java/me/skymc/taboolib/commands/locale/TabooLibLocaleCommand.java @@ -1,11 +1,18 @@ package me.skymc.taboolib.commands.locale; import com.ilummc.tlib.resources.TLocale; +import com.ilummc.tlib.resources.TLocaleLoader; +import me.skymc.taboolib.commands.internal.BaseMainCommand; +import me.skymc.taboolib.commands.internal.BaseSubCommand; +import me.skymc.taboolib.commands.internal.type.CommandArgument; +import me.skymc.taboolib.commands.internal.type.CommandRegister; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import java.util.ArrayList; import java.util.List; @@ -15,57 +22,87 @@ import java.util.stream.IntStream; * @author sky * @since 2018-04-22 14:36:28 */ -public class TabooLibLocaleCommand implements CommandExecutor { +public class TabooLibLocaleCommand extends BaseMainCommand { @Override - public boolean onCommand(CommandSender sender, Command arg1, String label, String[] args) { - if (args.length == 0) { - TLocale.sendTo(sender, "COMMANDS.TLOCALE.HELP", label); - } else if ("send".equalsIgnoreCase(args[0])) { - send(sender, args); - } else if ("reload".equalsIgnoreCase(args[0])) { - reload(sender); - } else { - TLocale.sendTo(sender, "COMMANDS.PARAMETER.UNKNOWN"); - } - return true; + public String getCommandTitle() { + return TLocale.asString("COMMANDS.TLOCALE.COMMAND-TITLE"); } - void send(CommandSender sender, String[] args) { - if (args.length < 3) { - TLocale.sendTo(sender, "COMMANDS.PARAMETER.UNKNOWN"); - return; + @CommandRegister + BaseSubCommand send = new BaseSubCommand() { + + @Override + public String getLabel() { + return "send"; } - long time = System.currentTimeMillis(); - List target = new ArrayList<>(); - if ("all".equalsIgnoreCase(args[1])) { - target.addAll(Bukkit.getOnlinePlayers()); - } else { - Player player = Bukkit.getPlayerExact(args[1]); - if (player == null) { - TLocale.sendTo(sender, "COMMANDS.TLOCALE.INVALID-PLAYER", args[1]); - return; + @Override + public String getDescription() { + return TLocale.asString("COMMANDS.TLOCALE.SEND.DESCRIPTION"); + } + + @Override + public CommandArgument[] getArguments() { + return new CommandArgument[] { + new CommandArgument(TLocale.asString("COMMANDS.TLOCALE.SEND.ARGUMENTS.0")), + new CommandArgument(TLocale.asString("COMMANDS.TLOCALE.SEND.ARGUMENTS.1")), + new CommandArgument(TLocale.asString("COMMANDS.TLOCALE.SEND.ARGUMENTS.2"), false) + }; + } + + @Override + public void onCommand(CommandSender sender, Command command, String label, String[] args) { + long time = System.currentTimeMillis(); + + List target = new ArrayList<>(); + if (args[0].equalsIgnoreCase("all")) { + target.add(Bukkit.getConsoleSender()); + target.addAll(Bukkit.getOnlinePlayers()); + } else if (args[0].equalsIgnoreCase("players")) { + target.addAll(Bukkit.getOnlinePlayers()); + } else if (args[0].equalsIgnoreCase("console")) { + target.add(Bukkit.getConsoleSender()); + } else { + Player player = Bukkit.getPlayerExact(args[0]); + if (player == null) { + TLocale.sendTo(sender, "COMMANDS.TLOCALE.SEND.INVALID-PLAYER", args[0]); + return; + } + target.add(player); + } + + String[] arguments; + if (args.length > 2) { + arguments = new String[args.length - 2]; + IntStream.range(2, args.length).forEach(i -> arguments[i - 2] = args[i]); + } else { + arguments = new String[0]; + } + + /* + * 使用命令发送其他插件文本 + * /tlocale send BlackSKY testPlugin:message + */ + if (args[1].contains(":")) { + String[] path = args[1].split(":"); + Plugin plugin = Bukkit.getPluginManager().getPlugin(path[0]); + if (plugin == null || !TLocaleLoader.isLocaleLoaded(plugin)) { + TLocale.sendTo(sender, "COMMANDS.TLOCALE.SEND.INVALID-PLUGIN", path[0]); + return; + } + if (path.length == 1) { + TLocale.sendTo(sender, "COMMANDS.TLOCALE.SEND.INVALID-PATH", args[1]); + return; + } + target.forEach(x -> TLocaleLoader.sendTo(plugin, path[1], x, arguments)); + } else { + target.forEach(x -> TLocale.sendTo(x, args[1], arguments)); + } + + if (sender instanceof Player && ((Player) sender).getItemInHand().getType().equals(Material.COMMAND)) { + TLocale.sendTo(sender, "COMMANDS.TLOCALE.SEND.SUCCESS-SEND", String.valueOf(System.currentTimeMillis() - time)); } - target.add(player); } - - if (args.length > 3) { - String[] vars = new String[args.length - 3]; - IntStream.range(3, args.length).forEach(i -> vars[i - 3] = args[i]); - target.forEach(x -> TLocale.sendTo(x, args[2], vars)); - } else { - target.forEach(x -> TLocale.sendTo(x, args[2])); - } - - if (sender instanceof Player) { - TLocale.sendTo(sender, "COMMANDS.TLOCALE.SUCCESS-SEND", String.valueOf(System.currentTimeMillis() - time)); - } - } - - void reload(CommandSender sender) { - TLocale.reload(); - TLocale.sendTo(sender, "COMMANDS.TLOCALE.SUCCESS-RELOAD"); - } - + }; } diff --git a/src/main/java/me/skymc/taboolib/nms/NMSUtil19.java b/src/main/java/me/skymc/taboolib/nms/NMSUtil19.java index 7c24b14..95e6be3 100644 --- a/src/main/java/me/skymc/taboolib/nms/NMSUtil19.java +++ b/src/main/java/me/skymc/taboolib/nms/NMSUtil19.java @@ -315,7 +315,7 @@ public class NMSUtil19 { class_WorldServer = fixBukkitClass("net.minecraft.server.WorldServer"); class_EnumSkyBlock = (Class) fixBukkitClass("net.minecraft.server.EnumSkyBlock"); class_EnumSoundCategory = (Class) fixBukkitClass("net.minecraft.server.SoundCategory"); - enum_SoundCategory_PLAYERS = Enum.valueOf(class_EnumSoundCategory, "PLAYERS"); + enum_SoundCategory_PLAYERS = NMSUtils.getEnumSilent(class_EnumSoundCategory, "PLAYERS"); class_EntityPainting = fixBukkitClass("net.minecraft.server.EntityPainting"); class_EntityCreature = fixBukkitClass("net.minecraft.server.EntityCreature"); class_EntityItemFrame = fixBukkitClass("net.minecraft.server.EntityItemFrame"); diff --git a/src/main/java/me/skymc/taboolib/nms/NMSUtils.java b/src/main/java/me/skymc/taboolib/nms/NMSUtils.java index 88995c3..9bddbaa 100644 --- a/src/main/java/me/skymc/taboolib/nms/NMSUtils.java +++ b/src/main/java/me/skymc/taboolib/nms/NMSUtils.java @@ -6,6 +6,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.stream.IntStream; /** * @author Unknown @@ -20,6 +21,14 @@ public class NMSUtils { return name.substring(name.lastIndexOf('.') + 1) + "."; } + public static > T getEnumSilent(Class enumType, String str) { + try { + return Enum.valueOf(enumType, str); + } catch (Exception ignored) { + return null; + } + } + public static Class getClassWithException(String name) throws Exception { return Class.forName(name); } @@ -287,13 +296,13 @@ public class NMSUtils { public static Method getMethodWithException(Class clazz, String name, Class... args) throws Exception { for (Method m : clazz.getDeclaredMethods()) { - if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0 || ClassListEqual(args, m.getParameterTypes()))) { + if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0 || classListEqual(args, m.getParameterTypes()))) { m.setAccessible(true); return m; } } for (Method m : clazz.getMethods()) { - if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0 || ClassListEqual(args, m.getParameterTypes()))) { + if (m.getName().equals(name) && (args.length == 0 && m.getParameterTypes().length == 0 || classListEqual(args, m.getParameterTypes()))) { m.setAccessible(true); return m; } @@ -309,16 +318,8 @@ public class NMSUtils { return null; } - public static boolean ClassListEqual(Class[] l1, Class[] l2) { - if (l1.length != l2.length) { - return false; - } - for (int i = 0; i < l1.length; i++) { - if (l1[i] != l2[i]) { - return false; - } - } - return true; + public static boolean classListEqual(Class[] l1, Class[] l2) { + return l1.length == l2.length && IntStream.range(0, l1.length).noneMatch(i -> l1[i] != l2[i]); } public static Class getInnerClassWithException(Class c, String className) throws Exception { @@ -349,13 +350,13 @@ public class NMSUtils { public static Constructor getConstructor(Class clazz, Class... args) throws Exception { for (Constructor c : clazz.getDeclaredConstructors()) { - if (args.length == 0 && c.getParameterTypes().length == 0 || ClassListEqual(args, c.getParameterTypes())) { + if (args.length == 0 && c.getParameterTypes().length == 0 || classListEqual(args, c.getParameterTypes())) { c.setAccessible(true); return c; } } for (Constructor c : clazz.getConstructors()) { - if (args.length == 0 && c.getParameterTypes().length == 0 || ClassListEqual(args, c.getParameterTypes())) { + if (args.length == 0 && c.getParameterTypes().length == 0 || classListEqual(args, c.getParameterTypes())) { c.setAccessible(true); return c; } diff --git a/src/main/java/me/skymc/taboolib/string/StringUtils.java b/src/main/java/me/skymc/taboolib/string/StringUtils.java index be0359e..20a24d5 100644 --- a/src/main/java/me/skymc/taboolib/string/StringUtils.java +++ b/src/main/java/me/skymc/taboolib/string/StringUtils.java @@ -36,9 +36,13 @@ public class StringUtils { public static double similarDegree(String strA, String strB){ String newStrA = removeSign(max(strA, strB)); String newStrB = removeSign(min(strA, strB)); - int temp = Math.max(newStrA.length(), newStrB.length()); - int temp2 = longestCommonSubstring(newStrA, newStrB).length(); - return temp2 * 1.0 / temp; + try { + int temp = Math.max(newStrA.length(), newStrB.length()); + int temp2 = longestCommonSubstring(newStrA, newStrB).length(); + return temp2 * 1.0 / temp; + } catch (Exception ignored) { + return 0; + } } private static String max(String strA, String strB) { diff --git a/src/main/resources/lang/zh_CN.yml b/src/main/resources/lang/zh_CN.yml index 4ca772c..d1ab8ac 100644 --- a/src/main/resources/lang/zh_CN.yml +++ b/src/main/resources/lang/zh_CN.yml @@ -236,7 +236,7 @@ COMMANDS: UPDATE-NOT-SUPPORT: '&8[&3&lTabooLib&8] &4您的服务器不支持在线更新!' FILE-NOT-FOUND: '&8[&3&lTabooLib&8] &4尚未寻找到插件文件' UPDATE-START: '&8[&3&lTabooLib&8] &7开始下载:&f {0}' - UPDATE-SUCCESS: '&8[&3&lTabooLib&8] &7最新版下载完成, 重启服务器自动更新!' + UPDATE-SUCCESS: '&8[&3&lTabooLib&8] &7最新版下载完成, 服务器即将重启!' JAVASHELL: DESCRIPTION: LOAD: '载入脚本' @@ -273,6 +273,13 @@ COMMANDS: SUCCESS-PREFIX-SET: '&8[&3&lTabooLib&8] &7设置玩家 &f{0} &7的前缀为 &f{1}' SUCCESS-SUFFIX-SET: '&8[&3&lTabooLib&8] &7设置玩家 &f{0} &7的后缀为 &f{1}' SUCCESS-DELETE: '&8[&3&lTabooLib&8] &7删除玩家 &f{0} &7的称号数据' + LAGSERVER: + DESCRIPTION: '休眠主线程' + ARGUMENTS: + 0: '毫秒' + INVALID-TIME: '&8[&3&lTabooLib&8] &4休眠时间不可超过 &c30000 &4毫秒' + START: '&8[&3&lTabooLib&8] &7线程休眠开始.' + STOP: '&8[&3&lTabooLib&8] &7线程休眠结束.' INFO: DESCRIPTION: '查看物品信息' INVALID-ITEM: '&8[&3&lTabooLib&8] &4请手持正确的物品' @@ -455,16 +462,18 @@ COMMANDS: - '&f /{0} reload &6- &e重载语言库' - '' TLOCALE: - INVALID-PLAYER: '&8[&3&lTabooLib&8] &4玩家 &c{0} &4不在线' - SUCCESS-RELOAD: '&8[&3&lTabooLib&8] &7重载完成' - SUCCESS-SEND: '&8[&3&lTabooLib&8] &7信息已发送, 耗时&f: {0}' - HELP: - - '' - - '&b&l----- &3&lTabooLibLoacle Commands &b&l-----' - - '' - - '&f /{0} send &8[&7玩家/ALL&8] &8[&7语言&8] &8<&7变量&8> &6- &e发送语言提示' - - '&f /{0} reload &6- &e重载语言库' - - '' + COMMAND-TITLE: '&e&l----- &6&lTabooLibLoacle Commands &e&l-----' + SEND: + DESCRIPTION: '发送语言提示' + ARGUMENTS: + 0: '目标' + 1: '地址' + 2: '变量' + INVALID-PLAYER: '&8[&3&lTabooLib&8] &4玩家 &c{0} &4不在线' + INVALID-PLUGIN: '&8[&3&lTabooLib&8] &4插件 &c{0} &4不存在或没有使用 Tlocale 语言文件.' + INVALID-PATH: '&8[&3&lTabooLib&8] &4错误的语言文件地址:&c {0}' + SUCCESS-RELOAD: '&8[&3&lTabooLib&8] &7重载完成' + SUCCESS-SEND: '&8[&3&lTabooLib&8] &7信息已发送, 耗时&f: {0}' TPLUGIN: COMMAND-TITLE: '&e&l----- &6&lTabooLibPlugin Commands &e&l-----' LIST: diff --git a/src/main/resources/motd.txt b/src/main/resources/motd.txt new file mode 100644 index 0000000..2d271a9 --- /dev/null +++ b/src/main/resources/motd.txt @@ -0,0 +1,10 @@ +§r +§r +§b________ §b______ §f______§8___________ +§b___ __/§3_____ §b___ /§3_____________§f___ /§8___(_)__ /_ +§b__ / §3_ __ `/§b_ __ \§3 __ \ __ \§f_ / §8__ /__ __ \ +§b_ / §3/ /_/ /§b_ /_/ /§3 /_/ / /_/ /§f /___§8 / _ /_/ / +§b/_/ §3\__,_/ §b/_.___/§3\____/\____/§f/_____/§8_/ /_.___/ +§r + §8version §f{0} +§r \ No newline at end of file