diff --git a/build.gradle b/build.gradle index ec2ff19..fe8d070 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = 'me.skymc' -version = '5.24' +version = '5.25' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/resources/__resources__/lang/en_US.yml b/src/main/resources/__resources__/lang/en_US.yml index 2ae707b..4754cda 100644 --- a/src/main/resources/__resources__/lang/en_US.yml +++ b/src/main/resources/__resources__/lang/en_US.yml @@ -19,29 +19,66 @@ MYSQL-HIKARI: CLOSE-FAIL: '&7Plugin &f{0} &7Try to deregister connection used by &f{1} &7plugins.' COMMANDS: - GLOBAL: - ONLY-PLAYER: '&8[&3&lTabooLib&8] &4This is only for player.' - ONLY-STORAGE-SQL: '&8[&3&lTabooLib&8] &4Can only be done when SQL-Mode is enabled.' INTERNAL: + COMMAND-CREATE-FAILED: '&c插件 &7{0} &c的 &7{1} &c命令注册失败: &7{2}' TYPE-ERROR: '&8[&3&lTabooLib&8] &7Command &f{0} &7can only be executed by &f{1} &7.' TYPE-PLAYER: 'Player' TYPE-CONSOLE: 'Console' - ERROR-USAGE: - - '&8[&3&lTabooLib&8] &7Command &f{0} &7' - - '&8[&3&lTabooLib&8] &7Usage:' - - '&8[&3&lTabooLib&8] &7{1}' - ERROR-COMMAND: - - '&8[&3&lTabooLib&8] &7Command &f{0} &7not found' - - '&8[&3&lTabooLib&8] &7Maybe:' - - '&8[&3&lTabooLib&8] &7{1}' - COMMAND-CREATE-FAILED: '&c插件 &7{0} &c的 &7{1} &c命令注册失败: &7{2}' - COMMAND-HELP: ' §f/{0} {1} {2}§6- §e{3}' - COMMAND-HELP-EMPTY: ' §f/{0} {1} {2}' - COMMAND-ARGUMENT: '§7<§8{0}§7>' - COMMAND-ARGUMENT-REQUIRE: '§7[§8{0}§7]' PARAMETER: UNKNOWN: '&8[&3&lTabooLib&8] &4Invalid command.' INSUFFICIENT: '&8[&3&lTabooLib&8] &4Invalid arguments.' + DISPLAY: + CLASSIC: + HELP: '§f/{0} {1} {2} §6- §e{3}' + HELP-EMPTY: '§f/{0} {1} {2}' + ARGUMENT-REQUIRED: '§7[§8{0}§7]' + ARGUMENT-OPTIONAL: '§7<§8{0}§7>' + ERROR-USAGE: + - '&8[&3&l{2}&8] &7Command &f{0} &7is missing parameters' + - '&8[&3&l{2}&8] &7Usage:' + - '&8[&3&l{2}&8] &7{1}' + ERROR-COMMAND: + - '&8[&3&l{2}&8] &7Command &f{0} &7not found' + - '&8[&3&l{2}&8] &7Usage:' + - '&8[&3&l{3}&8] &7{1}' + FLAT: + HELP: '§f/{0} {1} {2} §8- §7{3}' + HELP-EMPTY: '§f/{0} {1} {2}' + ARGUMENT-REQUIRED: '§7[§8{0}§7]' + ARGUMENT-OPTIONAL: '§7<§8{0}§7>' + ERROR-USAGE: + - '&8[&3&l{2}&8] &7Command &f{0} &7is missing parameters' + - '&8[&3&l{2}&8] &7Usage:' + - '&8[&3&l{2}&8] &7{1}' + ERROR-COMMAND: + - '&8[&3&l{2}&8] &7Command &f{0} &7not found' + - '&8[&3&l{2}&8] &7Usage:' + - '&8[&3&l{3}&8] &7{1}' + HEAD: + - ' ' + - ' &f&l{0}&7 v{1}' + - ' ' + - ' &7命令: &f/{2} &8[...]' + - ' &7参数:' + BOTTOM: + - ' ' + PARAMETERS: + ==: JSON + text: + - ' &8- &f<{0}@0>' + - ' &8 &7{1}' + args: + 0: + hover: '{2}' + suggest: '{3}' + PARAMETERS-EMPTY: + ==: JSON + text: + - ' &8- &f<{0}@0>' + args: + 0: + hover: '{2}' + suggest: '{3}' COMMUNICATION: FAILED-LOAD-SETTINGS: '§8[§3§lTabooLibClient§8] &4配置载入失败: {0}' diff --git a/src/main/resources/__resources__/lang/zh_CN.yml b/src/main/resources/__resources__/lang/zh_CN.yml index 1f4de7e..000446b 100644 --- a/src/main/resources/__resources__/lang/zh_CN.yml +++ b/src/main/resources/__resources__/lang/zh_CN.yml @@ -19,29 +19,66 @@ MYSQL-HIKARI: CLOSE-FAIL: '&7插件 &f{0} &7注册的连接池正在被 &f{1} &7个插件使用, 无法注销!' COMMANDS: - GLOBAL: - ONLY-PLAYER: '&8[&3&lTabooLib&8] &4控制台无法这么做' - ONLY-STORAGE-SQL: '&8[&3&lTabooLib&8] &4只有启用数据库储存时才能这么做' INTERNAL: - TYPE-ERROR: '&8[&3&lTabooLib&8] &7指令 &f{0} &7只能由 &f{1} &7执行' + COMMAND-CREATE-FAILED: '&c插件 &7{0} &c的 &7{1} &c命令注册失败: &7{2}' + TYPE-ERROR: '&8[&3&l{2}&8] &7指令 &f{0} &7只能由 &f{1} &7执行' TYPE-PLAYER: '玩家' TYPE-CONSOLE: '控制台' - ERROR-USAGE: - - '&8[&3&lTabooLib&8] &7指令 &f{0} &7参数不足' - - '&8[&3&lTabooLib&8] &7正确用法:' - - '&8[&3&lTabooLib&8] &7{1}' - ERROR-COMMAND: - - '&8[&3&lTabooLib&8] &7指令 &f{0} &7不存在' - - '&8[&3&lTabooLib&8] &7你可能想要:' - - '&8[&3&lTabooLib&8] &7{1}' - COMMAND-CREATE-FAILED: '&c插件 &7{0} &c的 &7{1} &c命令注册失败: &7{2}' - COMMAND-HELP: ' §f/{0} {1} {2}§6- §e{3}' - COMMAND-HELP-EMPTY: ' §f/{0} {1} {2}' - COMMAND-ARGUMENT: '§7<§8{0}§7>' - COMMAND-ARGUMENT-REQUIRE: '§7[§8{0}§7]' PARAMETER: - UNKNOWN: '&8[&3&lTabooLib&8] &4指令错误' + UNKNOWN: '&8[&3&lTabooLib&8] &4b指令错误' INSUFFICIENT: '&8[&3&lTabooLib&8] &4参数不足' + DISPLAY: + CLASSIC: + HELP: '§f/{0} {1} {2} §6- §e{3}' + HELP-EMPTY: '§f/{0} {1} {2}' + ARGUMENT-REQUIRED: '§7[§8{0}§7]' + ARGUMENT-OPTIONAL: '§7<§8{0}§7>' + ERROR-USAGE: + - '&8[&3&l{2}&8] &7指令 &f{0} &7参数不足' + - '&8[&3&l{2}&8] &7正确用法:' + - '&8[&3&l{2}&8] &7{1}' + ERROR-COMMAND: + - '&8[&3&l{2}&8] &7指令 &f{0} &7不存在' + - '&8[&3&l{2}&8] &7你可能想要:' + - '&8[&3&l{3}&8] &7{1}' + FLAT: + HELP: '§f/{0} {1} {2} §8- §7{3}' + HELP-EMPTY: '§f/{0} {1} {2}' + ARGUMENT-REQUIRED: '§7[§8{0}§7]' + ARGUMENT-OPTIONAL: '§7<§8{0}§7>' + ERROR-USAGE: + - '&8[&3&l{2}&8] &7指令 &f{0} &7参数不足' + - '&8[&3&l{2}&8] &7正确用法:' + - '&8[&3&l{2}&8] &7{1}' + ERROR-COMMAND: + - '&8[&3&l{2}&8] &7指令 &f{0} &7不存在' + - '&8[&3&l{2}&8] &7你可能想要:' + - '&8[&3&l{2}&8] &7{1}' + HEAD: + - ' ' + - ' &f&l{0}&7 v{1}' + - ' ' + - ' &7命令: &f/{2} &8[...]' + - ' &7参数:' + BOTTOM: + - ' ' + PARAMETERS: + ==: JSON + text: + - ' &8- &f<{0}@0>' + - ' &8 &7{1}' + args: + 0: + hover: '{2}' + suggest: '{3}' + PARAMETERS-EMPTY: + ==: JSON + text: + - ' &8- &f<{0}@0>' + args: + 0: + hover: '{2}' + suggest: '{3}' COMMUNICATION: FAILED-LOAD-SETTINGS: '§8[§3§lTabooLibClient§8] &4配置载入失败: {0}' diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/Argument.java b/src/main/scala/io/izzel/taboolib/module/command/base/Argument.java index 1537bd7..992d5f0 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/Argument.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/Argument.java @@ -1,7 +1,5 @@ package io.izzel.taboolib.module.command.base; -import io.izzel.taboolib.module.locale.TLocale; - import java.util.Arrays; import java.util.Objects; @@ -48,11 +46,6 @@ public class Argument { this.tab = tab; } - @Override - public String toString() { - return required ? TLocale.asString("COMMANDS.INTERNAL.COMMAND-ARGUMENT-REQUIRE", name) : TLocale.asString("COMMANDS.INTERNAL.COMMAND-ARGUMENT", name); - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java b/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java index 7db8c25..f4a8a24 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/BaseMainCommand.java @@ -4,7 +4,8 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLibAPI; -import io.izzel.taboolib.Version; +import io.izzel.taboolib.module.command.base.display.DisplayBase; +import io.izzel.taboolib.module.command.base.display.DisplayFlat; import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.util.ArrayUtil; import io.izzel.taboolib.util.Ref; @@ -17,7 +18,10 @@ import org.bukkit.scheduler.BukkitRunnable; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; @@ -27,6 +31,7 @@ import java.util.stream.Collectors; */ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { + private DisplayBase display = new DisplayFlat(); private PluginCommand registerCommand; private List> linkClasses = new CopyOnWriteArrayList<>(); private List subCommands = new CopyOnWriteArrayList<>(); @@ -95,7 +100,7 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { } } - private static BaseSubCommand buildSubCommand(BaseMainCommand baseMainCommand, Method method) { + public static BaseSubCommand buildSubCommand(BaseMainCommand baseMainCommand, Method method) { return new BaseSubCommand() { @Override public void onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -108,47 +113,24 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { }; } - public void setRegisterCommand(PluginCommand registerCommand) { - this.registerCommand = registerCommand; - } - - public PluginCommand getRegisterCommand() { - return registerCommand; - } - - public List> getLinkClasses() { - return linkClasses; - } - - public List getSubCommands() { - return subCommands; - } - public void registerSubCommand(BaseSubCommand subCommand) { - if (subCommand != null) { - Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null"); - Preconditions.checkArgument(subCommand.getArguments() != null, "Command arguments can not be null"); - } - subCommands.add(subCommand); + Preconditions.checkNotNull(subCommand); + Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null"); + Preconditions.checkArgument(subCommand.getArguments() != null, "Command arguments can not be null"); + subCommands.add(subCommand.mainCommand(this)); } public void onCommandHelp(CommandSender sender, Command command, String label, String[] args) { - sender.sendMessage(getEmptyLine()); - sender.sendMessage(getCommandTitle()); - sender.sendMessage(getEmptyLine()); - subCommands.stream().filter(subCommands -> !hideInHelp(subCommands) && hasPermission(sender, subCommands)).map(subCommand -> subCommand == null ? getEmptyLine() : subCommand.getCommandString(label)).forEach(sender::sendMessage); - sender.sendMessage(getEmptyLine()); - } - - public String getCommandTitle() { - return "§e§l----- §6§l" + registerCommand.getPlugin().getName() + " Commands §e§l-----"; + display.displayHead(sender, this, label); + subCommands.forEach(s -> display.displayParameters(sender, s, label)); + display.displayBottom(sender, this, label); } @Override - public List onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { + public List onTabComplete(CommandSender sender, Command command, String s, String[] args) { if (args.length == 1) { List label = Lists.newArrayList(); - subCommands.stream().filter(subCommand -> !hideInHelp(subCommand) && hasPermission(commandSender, subCommand)).forEach(l -> { + subCommands.stream().filter(subCommand -> subCommand != null && !subCommand.hideInHelp() && subCommand.hasPermission(sender)).forEach(l -> { label.add(l.getLabel()); label.addAll(Lists.newArrayList(l.getAliases())); }); @@ -172,18 +154,18 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { onCommandHelp(sender, command, label, args); } else { for (BaseSubCommand subCommand : subCommands) { - if (subCommand == null || !(args[0].equalsIgnoreCase(subCommand.getLabel()) || java.util.Arrays.stream(subCommand.getAliases()).anyMatch(args[0]::equalsIgnoreCase)) || !hasPermission(sender, subCommand)) { + if (subCommand == null || !(args[0].equalsIgnoreCase(subCommand.getLabel()) || java.util.Arrays.stream(subCommand.getAliases()).anyMatch(args[0]::equalsIgnoreCase)) || !subCommand.hasPermission(sender)) { continue; } if (!isConfirmType(sender, subCommand.getType())) { - TLocale.sendTo(sender, "COMMANDS.INTERNAL.TYPE-ERROR", args[0], TLocale.asString("COMMANDS.INTERNAL.TYPE-" + subCommand.getType())); + TLocale.sendTo(sender, "COMMANDS.INTERNAL.TYPE-ERROR", args[0], TLocale.asString("COMMANDS.INTERNAL.TYPE-" + subCommand.getType()), registerCommand.getPlugin().getName()); return true; } String[] subCommandArgs = removeFirst(args); if (subCommand.isParameterConform(subCommandArgs)) { subCommand.onCommand(sender, command, label, subCommand.ignoredLabel() ? subCommandArgs : args); } else { - TLocale.sendTo(sender, "COMMANDS.INTERNAL.ERROR-USAGE", args[0], subCommand.getCommandString(label)); + display.displayErrorUsage(sender, this, args[0], subCommand.getCommandString(sender, label)); } return true; } @@ -191,9 +173,9 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { @Override public void run() { - List commandCompute = subCommands.stream().filter(x -> x != null && hasPermission(sender, x)).sorted((b, a) -> Double.compare(Strings.similarDegree(args[0], a.getLabel()), Strings.similarDegree(args[0], b.getLabel()))).collect(Collectors.toList()); + List commandCompute = subCommands.stream().filter(x -> x != null && x.hasPermission(sender)).sorted((b, a) -> Double.compare(Strings.similarDegree(args[0], a.getLabel()), Strings.similarDegree(args[0], b.getLabel()))).collect(Collectors.toList()); if (commandCompute.size() > 0) { - TLocale.sendTo(sender, "COMMANDS.INTERNAL.ERROR-COMMAND", args[0], commandCompute.get(0).getCommandString(label).trim()); + display.displayErrorCommand(sender, BaseMainCommand.this, args[0], commandCompute.get(0).getCommandString(sender, label)); } } }.runTaskAsynchronously(TabooLib.getPlugin()); @@ -203,24 +185,48 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { @Override public String toString() { - return "registerCommand=" + "BaseMainCommand{" + registerCommand + ", linkClasses=" + linkClasses + ", subCommands=" + subCommands + '}'; + return "BaseMainCommand{" + + "display=" + display + + ", registerCommand=" + registerCommand + + ", linkClasses=" + linkClasses + + ", subCommands=" + subCommands + + '}'; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof BaseMainCommand)) { - return false; - } - BaseMainCommand that = (BaseMainCommand) o; - return Objects.equals(getLinkClasses(), that.getLinkClasses()) && Objects.equals(getRegisterCommand(), that.getRegisterCommand()) && Objects.equals(getSubCommands(), that.getSubCommands()); + // ********************************* + // + // Getter and Setter + // + // ********************************* + + public String getCommandTitle() { + return "§e§l----- §6§l" + registerCommand.getPlugin().getName() + " Commands §e§l-----"; } - @Override - public int hashCode() { - return Objects.hash(getRegisterCommand(), getLinkClasses(), getSubCommands()); + public void setRegisterCommand(PluginCommand registerCommand) { + Preconditions.checkNotNull(registerCommand); + this.registerCommand = registerCommand; + } + + public PluginCommand getRegisterCommand() { + return registerCommand; + } + + public List> getLinkClasses() { + return linkClasses; + } + + public List getSubCommands() { + return subCommands; + } + + public DisplayBase getDisplay() { + return display; + } + + public void setDisplay(DisplayBase display) { + Preconditions.checkNotNull(display); + this.display = display; } // ********************************* @@ -229,10 +235,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { // // ********************************* - private String getEmptyLine() { - return Version.isAfter(Version.v1_8) ? "" : "~"; - } - private boolean isConfirmType(CommandSender sender, CommandType commandType) { return commandType == CommandType.ALL || (sender instanceof Player && commandType == CommandType.PLAYER) @@ -254,14 +256,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { } } - private boolean hideInHelp(BaseSubCommand baseSubCommand) { - return baseSubCommand != null && baseSubCommand.hideInHelp(); - } - - private boolean hasPermission(CommandSender sender, BaseSubCommand baseSubCommand) { - return baseSubCommand == null || Strings.isEmpty(baseSubCommand.getPermission()) || sender.hasPermission(baseSubCommand.getPermission()); - } - private String[] removeFirst(String[] args) { if (args.length <= 1) { return new String[0]; diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java b/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java index a3716a5..78383e0 100644 --- a/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java +++ b/src/main/scala/io/izzel/taboolib/module/command/base/BaseSubCommand.java @@ -1,12 +1,10 @@ package io.izzel.taboolib.module.command.base; -import io.izzel.taboolib.module.locale.TLocale; import io.izzel.taboolib.util.Strings; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import java.util.Arrays; -import java.util.stream.Collectors; import java.util.stream.IntStream; /** @@ -18,9 +16,14 @@ public abstract class BaseSubCommand { private String label; private boolean player; private SubCommand annotation; + private BaseMainCommand mainCommand; abstract public void onCommand(CommandSender sender, Command command, String label, String[] args); + public BaseMainCommand getMainCommand() { + return mainCommand; + } + public String getLabel() { return label; } @@ -61,8 +64,12 @@ public abstract class BaseSubCommand { return IntStream.range(0, getArguments().length).noneMatch(i -> getArguments()[i].isRequired() && (args == null || args.length <= i)); } - public String getCommandString(String label) { - return TLocale.asString(Strings.isEmpty(getDescription()) ? "COMMANDS.INTERNAL.COMMAND-HELP-EMPTY" : "COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription()); + public String getCommandString(CommandSender sender, String label) { + return mainCommand.getDisplay().displayHelp(sender, this, label); + } + + public boolean hasPermission(CommandSender sender) { + return Strings.isBlank(getPermission()) || sender.hasPermission(getPermission()); } protected BaseSubCommand label(String label) { @@ -79,4 +86,9 @@ public abstract class BaseSubCommand { this.annotation = annotation; return this; } + + protected BaseSubCommand mainCommand(BaseMainCommand mainCommand) { + this.mainCommand = mainCommand; + return this; + } } \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayBase.java b/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayBase.java new file mode 100644 index 0000000..b8d6387 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayBase.java @@ -0,0 +1,24 @@ +package io.izzel.taboolib.module.command.base.display; + +import io.izzel.taboolib.module.command.base.BaseMainCommand; +import io.izzel.taboolib.module.command.base.BaseSubCommand; +import org.bukkit.command.CommandSender; + +/** + * @Author sky + * @Since 2020-04-04 16:14 + */ +public abstract class DisplayBase { + + abstract public void displayHead(CommandSender sender, BaseMainCommand baseMainCommand, String label); + + abstract public void displayBottom(CommandSender sender, BaseMainCommand baseMainCommand, String label); + + abstract public void displayParameters(CommandSender sender, BaseSubCommand baseSubCommand, String label); + + abstract public void displayErrorUsage(CommandSender sender, BaseMainCommand baseMainCommand, String label, String help); + + abstract public void displayErrorCommand(CommandSender sender, BaseMainCommand baseMainCommand, String label, String help); + + abstract public String displayHelp(CommandSender sender, BaseSubCommand baseSubCommand, String label); +} diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayClassic.java b/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayClassic.java new file mode 100644 index 0000000..70c8b72 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayClassic.java @@ -0,0 +1,62 @@ +package io.izzel.taboolib.module.command.base.display; + +import io.izzel.taboolib.module.command.base.Argument; +import io.izzel.taboolib.module.command.base.BaseMainCommand; +import io.izzel.taboolib.module.command.base.BaseSubCommand; +import io.izzel.taboolib.module.locale.TLocale; +import io.izzel.taboolib.util.Strings; +import org.bukkit.command.CommandSender; + +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2020-04-04 16:17 + */ +public class DisplayClassic extends DisplayBase { + + @Override + public void displayHead(CommandSender sender, BaseMainCommand main, String label) { + sender.sendMessage(" "); + sender.sendMessage(main.getCommandTitle()); + sender.sendMessage(" "); + } + + @Override + public void displayBottom(CommandSender sender, BaseMainCommand main, String label) { + sender.sendMessage(" "); + } + + @Override + public void displayParameters(CommandSender sender, BaseSubCommand sub, String label) { + if (sub == null) { + sender.sendMessage(" "); + } else if (!sub.hideInHelp() && sub.hasPermission(sender)) { + sender.sendMessage(" " + displayHelp(sender, sub, label)); + } + } + + @Override + public void displayErrorUsage(CommandSender sender, BaseMainCommand main, String label, String help) { + TLocale.sendTo(sender, "COMMANDS.DISPLAY.CLASSIC.ERROR-USAGE", label, help, main.getRegisterCommand().getPlugin().getName()); + } + + @Override + public void displayErrorCommand(CommandSender sender, BaseMainCommand main, String label, String help) { + TLocale.sendTo(sender, "COMMANDS.DISPLAY.CLASSIC.ERROR-COMMAND", label, help, main.getRegisterCommand().getPlugin().getName()); + } + + @Override + public String displayHelp(CommandSender sender, BaseSubCommand sub, String label) { + if (Strings.nonBlack(sub.getDescription())) { + return TLocale.asString("COMMANDS.DISPLAY.CLASSIC.HELP", label, sub.getLabel(), Arrays.stream(sub.getArguments()).map(a -> argument(sub, a)).collect(Collectors.joining(" ")), sub.getDescription()); + } else { + return TLocale.asString("COMMANDS.DISPLAY.CLASSIC.HELP-EMPTY", label, sub.getLabel(), Arrays.stream(sub.getArguments()).map(a -> argument(sub, a)).collect(Collectors.joining(" "))); + } + } + + public String argument(BaseSubCommand subCommand, Argument argument) { + return argument.isRequired() ? TLocale.asString("COMMANDS.DISPLAY.CLASSIC.ARGUMENT-REQUIRED", argument.getName()) : TLocale.asString("COMMANDS.DISPLAY.CLASSIC.ARGUMENT-OPTIONAL", argument.getName()); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayFlat.java b/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayFlat.java new file mode 100644 index 0000000..81a3908 --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/command/base/display/DisplayFlat.java @@ -0,0 +1,63 @@ +package io.izzel.taboolib.module.command.base.display; + +import io.izzel.taboolib.module.command.base.Argument; +import io.izzel.taboolib.module.command.base.BaseMainCommand; +import io.izzel.taboolib.module.command.base.BaseSubCommand; +import io.izzel.taboolib.module.locale.TLocale; +import io.izzel.taboolib.util.Strings; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; + +import java.util.Arrays; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2020-04-04 16:18 + */ +public class DisplayFlat extends DisplayBase { + + @Override + public void displayHead(CommandSender sender, BaseMainCommand main, String label) { + Plugin plugin = main.getRegisterCommand().getPlugin(); + TLocale.sendTo(sender, "COMMANDS.DISPLAY.FLAT.HEAD", plugin.getName(), plugin.getDescription().getVersion(), main.getRegisterCommand().getName()); + } + + @Override + public void displayBottom(CommandSender sender, BaseMainCommand main, String label) { + Plugin plugin = main.getRegisterCommand().getPlugin(); + TLocale.sendTo(sender, "COMMANDS.DISPLAY.FLAT.BOTTOM", plugin.getName(), plugin.getDescription().getVersion(), main.getRegisterCommand().getName()); + } + + @Override + public void displayParameters(CommandSender sender, BaseSubCommand sub, String label) { + if (Strings.nonBlack(sub.getDescription())) { + TLocale.sendTo(sender, "COMMANDS.DISPLAY.FLAT.PARAMETERS", sub.getLabel(), sub.getDescription(), displayHelp(sender, sub, label), "/" + label + " " + sub.getLabel() + " "); + } else { + TLocale.sendTo(sender, "COMMANDS.DISPLAY.FLAT.PARAMETERS-EMPTY", sub.getLabel(), sub.getDescription(), displayHelp(sender, sub, label), "/" + label + " " + sub.getLabel() + " "); + } + } + + @Override + public void displayErrorUsage(CommandSender sender, BaseMainCommand main, String label, String help) { + TLocale.sendTo(sender, "COMMANDS.DISPLAY.FLAT.ERROR-USAGE", label, help, main.getRegisterCommand().getPlugin().getName()); + } + + @Override + public void displayErrorCommand(CommandSender sender, BaseMainCommand main, String label, String help) { + TLocale.sendTo(sender, "COMMANDS.DISPLAY.FLAT.ERROR-COMMAND", label, help, main.getRegisterCommand().getPlugin().getName()); + } + + @Override + public String displayHelp(CommandSender sender, BaseSubCommand sub, String label) { + if (Strings.nonBlack(sub.getDescription())) { + return TLocale.asString("COMMANDS.DISPLAY.FLAT.HELP", label, sub.getLabel(), Arrays.stream(sub.getArguments()).map(a -> argument(sub, a)).collect(Collectors.joining(" ")), sub.getDescription()); + } else { + return TLocale.asString("COMMANDS.DISPLAY.FLAT.HELP-EMPTY", label, sub.getLabel(), Arrays.stream(sub.getArguments()).map(a -> argument(sub, a)).collect(Collectors.joining(" "))); + } + } + + public String argument(BaseSubCommand subCommand, Argument argument) { + return argument.isRequired() ? TLocale.asString("COMMANDS.DISPLAY.FLAT.ARGUMENT-REQUIRED", argument.getName()) : TLocale.asString("COMMANDS.DISPLAY.FLAT.ARGUMENT-OPTIONAL", argument.getName()); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/i18n/I18n.java b/src/main/scala/io/izzel/taboolib/module/i18n/I18n.java new file mode 100644 index 0000000..0c1605c --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/i18n/I18n.java @@ -0,0 +1,43 @@ +package io.izzel.taboolib.module.i18n; + +import com.google.common.collect.Maps; +import io.izzel.taboolib.Version; +import io.izzel.taboolib.module.i18n.version.I18n20w14a; +import io.izzel.taboolib.module.i18n.version.I18nOrigin; +import io.izzel.taboolib.module.inject.TFunction; + +import java.util.Map; + +/** + * @Author sky + * @Since 2020-04-04 19:33 + */ +public class I18n { + + public static final Map VERSION = Maps.newHashMap(); + + private static I18nBase base; + + static { + VERSION.put(Version.v1_7, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_8, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_9, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_10, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_11, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_12, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_13, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_14, I18nOrigin.INSTANCE); + VERSION.put(Version.v1_15, I18n20w14a.INSTANCE); + VERSION.put(Version.v1_16, I18n20w14a.INSTANCE); + } + + @TFunction.Init + static void init() { + base = VERSION.getOrDefault(Version.getCurrentVersion(), I18nOrigin.INSTANCE); + base.init(); + } + + public static I18nBase get() { + return base; + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/i18n/I18nBase.java b/src/main/scala/io/izzel/taboolib/module/i18n/I18nBase.java new file mode 100644 index 0000000..c1cdb5b --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/i18n/I18nBase.java @@ -0,0 +1,19 @@ +package io.izzel.taboolib.module.i18n; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +/** + * @Author sky + * @Since 2020-04-04 19:42 + */ +public abstract class I18nBase { + + abstract public void init(); + + abstract public String getName(Player player, Entity entity); + + abstract public String getName(Player player, ItemStack itemStack); + +} diff --git a/src/main/scala/io/izzel/taboolib/module/i18n/version/I18n20w14a.java b/src/main/scala/io/izzel/taboolib/module/i18n/version/I18n20w14a.java new file mode 100644 index 0000000..7f5938a --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/i18n/version/I18n20w14a.java @@ -0,0 +1,92 @@ +package io.izzel.taboolib.module.i18n.version; + +import com.google.common.collect.Maps; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import io.izzel.taboolib.TabooLib; +import io.izzel.taboolib.module.i18n.I18nBase; +import io.izzel.taboolib.module.nms.NMS; +import io.izzel.taboolib.util.Files; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Map; + +/** + * @Author sky + * @Since 2020-04-04 19:51 + */ +public class I18n20w14a extends I18nBase { + + public static final I18n20w14a INSTANCE = new I18n20w14a(); + + // 20w14a + public static final String[][] LOCALE = { + { + "zh_cn", "5797b76621b4b335527cda6f903a1bb3d9b7ffa3" + }, + { + "zh_tw", "71be70e62f4cf7e98c6bcb32d57d7ef66614853b" + }, + { + "en_gb", "0f679e66f50f1dea4749cac3882cf5c6857bbfc3", + } + }; + + private File folder = new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n/v2/20w14a"); + private Map cache = Maps.newHashMap(); + + @Override + public void init() { + if (folder.exists() && folder.isDirectory()) { + load(); + } else { + long time = System.currentTimeMillis(); + System.out.println("[TabooLib] Loading Assets..."); + try { + for (String[] locale : LOCALE) { + Files.toFile(Files.readFromURL("https://resources.download.minecraft.net/" + locale[1].substring(0, 2) + "/" + locale[1], StandardCharsets.UTF_8, "{}"), Files.file(folder, locale[0])); + } + load(); + System.out.println("[TabooLib] Loading Successfully. (" + (System.currentTimeMillis() - time + "ms)")); + } catch (Throwable ignored) { + System.out.println("[TabooLib] Loading Failed. (" + (System.currentTimeMillis() - time + "ms)")); + } + } + } + + @Override + public String getName(Player player, Entity entity) { + JsonObject locale = cache.get(player == null ? "zh_cn" : player.getLocale()); + if (locale == null) { + locale = cache.get("en_gb"); + } + if (locale == null) { + return "[ERROR LOCALE]"; + } + JsonElement element = locale.get(NMS.handle().getName(entity)); + return element == null ? entity.getName() : element.getAsString(); + } + + @Override + public String getName(Player player, ItemStack itemStack) { + JsonObject locale = cache.get(player == null ? "zh_cn" : player.getLocale()); + if (locale == null) { + locale = cache.get("en_gb"); + } + if (locale == null) { + return "[ERROR LOCALE]"; + } + JsonElement element = locale.get(NMS.handle().getName(itemStack)); + return element == null ? itemStack.getType().name().toLowerCase().replace("_", "") : element.getAsString(); + } + + public void load() { + Arrays.stream(folder.listFiles()).forEach(listFile -> cache.put(listFile.getName(), new JsonParser().parse(Files.readFromFile(listFile)).getAsJsonObject())); + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/i18n/version/I18nOrigin.java b/src/main/scala/io/izzel/taboolib/module/i18n/version/I18nOrigin.java new file mode 100644 index 0000000..0ebc50a --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/module/i18n/version/I18nOrigin.java @@ -0,0 +1,89 @@ +package io.izzel.taboolib.module.i18n.version; + +import io.izzel.taboolib.TabooLib; +import io.izzel.taboolib.Version; +import io.izzel.taboolib.module.i18n.I18nBase; +import io.izzel.taboolib.module.locale.TLocaleLoader; +import io.izzel.taboolib.module.nms.NMS; +import io.izzel.taboolib.module.nms.nbt.NBTCompound; +import io.izzel.taboolib.util.Files; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SpawnEggMeta; +import org.bukkit.plugin.Plugin; + +import java.io.File; + +/** + * @Author sky + * @Since 2020-04-04 19:44 + */ +public class I18nOrigin extends I18nBase { + + public static final I18nOrigin INSTANCE = new I18nOrigin(); + + private static FileConfiguration lang; + private static boolean released; + + @Override + public void init() { + File localeFile = getLocaleFile(TabooLib.getPlugin()); + if (localeFile == null) { + lang = new YamlConfiguration(); + } else { + lang = Files.load(localeFile); + } + if (lang.getInt("version") < 3 && !released) { + released = true; + Files.deepDelete(new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n")); + init(); + } + } + + @Override + public String getName(Player player, Entity entity) { + return entity == null ? "-" : lang.getString(NMS.handle().getName(entity).replace(".", "_"), entity.getName()); + } + + @Override + public String getName(Player player, ItemStack itemStack) { + if (itemStack == null) { + return "-"; + } + ItemMeta itemMeta = itemStack.getItemMeta(); + if (itemMeta instanceof BookMeta && ((BookMeta) itemMeta).getTitle() != null) { + return ((BookMeta) itemMeta).getTitle(); + } + if (!Version.isAfter(Version.v1_11)) { + if (itemStack.getType().name().equals("MONSTER_EGG")) { + NBTCompound nbtCompound = NMS.handle().loadNBT(itemStack); + if (nbtCompound.containsKey("EntityTag")) { + return lang.getString("item_monsterPlacer_name") + " " + lang.getString("entity_" + nbtCompound.get("EntityTag").asCompound().get("id").asString() + "_name"); + } + return lang.getString("item_monsterPlacer_name"); + } + } else if (!Version.isAfter(Version.v1_13)) { + if (itemMeta instanceof SpawnEggMeta) { + String spawnEggType = lang.getString("entity_" + ((SpawnEggMeta) itemMeta).getSpawnedType().getEntityClass().getSimpleName().replace(".", "_") + "_name"); + if (spawnEggType != null) { + return lang.getString(NMS.handle().getName(itemStack).replace(".", "_"), itemStack.getType().name().toLowerCase().replace("_", "")) + " " + spawnEggType; + } + } + } + return lang.getString(NMS.handle().getName(itemStack).replace(".", "_"), itemStack.getType().name().toLowerCase().replace("_", "")); + } + + private File getLocaleFile(Plugin plugin) { + TLocaleLoader.getLocalePriority(plugin).forEach(localeName -> Files.releaseResource(plugin, "simpleI18n/" + getVersion() + "/" + localeName + ".yml", false)); + return TLocaleLoader.getLocalePriority(plugin).stream().map(localeName -> new File("plugins/TabooLib/simpleI18n/" + getVersion() + "/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null); + } + + private String getVersion() { + return Version.isAfter(Version.v1_13) ? "high" : "low"; + } +} diff --git a/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java b/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java index 5e23749..c3f80a6 100644 --- a/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java +++ b/src/main/scala/io/izzel/taboolib/module/lite/SimpleI18n.java @@ -1,22 +1,11 @@ package io.izzel.taboolib.module.lite; -import io.izzel.taboolib.TabooLib; -import io.izzel.taboolib.Version; -import io.izzel.taboolib.module.locale.TLocaleLoader; -import io.izzel.taboolib.module.inject.TFunction; -import io.izzel.taboolib.module.nms.NMS; -import io.izzel.taboolib.module.nms.nbt.NBTCompound; -import io.izzel.taboolib.util.Files; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; +import io.izzel.taboolib.module.i18n.I18n; import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.SpawnEggMeta; -import org.bukkit.plugin.Plugin; -import java.io.File; import java.util.Optional; /** @@ -25,24 +14,6 @@ import java.util.Optional; */ public class SimpleI18n { - private static FileConfiguration lang; - private static boolean released; - - @TFunction.Init - static void init() { - File localeFile = getLocaleFile(TabooLib.getPlugin()); - if (localeFile == null) { - lang = new YamlConfiguration(); - } else { - lang = Files.load(localeFile); - } - if (lang.getInt("version") < 3 && !released) { - released = true; - Files.deepDelete(new File(TabooLib.getPlugin().getDataFolder(), "simpleI18n")); - init(); - } - } - public static String getCustomName(Entity entity) { return entity != null ? Optional.ofNullable(entity.getCustomName()).orElse(getName(entity)) : getName(entity); } @@ -56,42 +27,18 @@ public class SimpleI18n { } public static String getName(Entity entity) { - return entity == null ? "-" : lang.getString(NMS.handle().getName(entity).replace(".", "_"), entity.getName()); + return I18n.get().getName(null, entity); } public static String getName(ItemStack item) { - if (item == null) { - return "-"; - } - ItemMeta itemMeta = item.getItemMeta(); - if (itemMeta instanceof BookMeta && ((BookMeta) itemMeta).getTitle() != null) { - return ((BookMeta) itemMeta).getTitle(); - } - if (!Version.isAfter(Version.v1_11)) { - if (item.getType().name().equals("MONSTER_EGG")) { - NBTCompound nbtCompound = NMS.handle().loadNBT(item); - if (nbtCompound.containsKey("EntityTag")) { - return lang.getString("item_monsterPlacer_name") + " " + lang.getString("entity_" + nbtCompound.get("EntityTag").asCompound().get("id").asString() + "_name"); - } - return lang.getString("item_monsterPlacer_name"); - } - } else if (!Version.isAfter(Version.v1_13)) { - if (itemMeta instanceof SpawnEggMeta) { - String spawnEggType = lang.getString("entity_" + ((SpawnEggMeta) itemMeta).getSpawnedType().getEntityClass().getSimpleName().replace(".", "_") + "_name"); - if (spawnEggType != null) { - return lang.getString(NMS.handle().getName(item).replace(".", "_"), item.getType().name().toLowerCase().replace("_", "")) + " " + spawnEggType; - } - } - } - return lang.getString(NMS.handle().getName(item).replace(".", "_"), item.getType().name().toLowerCase().replace("_", "")); + return I18n.get().getName(null, item); } - private static File getLocaleFile(Plugin plugin) { - TLocaleLoader.getLocalePriority(plugin).forEach(localeName -> Files.releaseResource(plugin, "simpleI18n/" + getVersion() + "/" + localeName + ".yml", false)); - return TLocaleLoader.getLocalePriority(plugin).stream().map(localeName -> new File("plugins/TabooLib/simpleI18n/" + getVersion() + "/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null); + public static String getName(Player player, Entity entity) { + return I18n.get().getName(player, entity); } - private static String getVersion() { - return Version.isAfter(Version.v1_13) ? "high" : "low"; + public static String getName(Player player, ItemStack item) { + return I18n.get().getName(player, item); } } diff --git a/src/main/scala/io/izzel/taboolib/module/locale/TLocaleLoader.java b/src/main/scala/io/izzel/taboolib/module/locale/TLocaleLoader.java index 17a3610..75edf62 100644 --- a/src/main/scala/io/izzel/taboolib/module/locale/TLocaleLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/locale/TLocaleLoader.java @@ -2,6 +2,7 @@ package io.izzel.taboolib.module.locale; import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLibAPI; +import io.izzel.taboolib.common.plugin.InternalPlugin; import io.izzel.taboolib.module.config.TConfigWatcher; import io.izzel.taboolib.module.locale.logger.TLogger; import io.izzel.taboolib.module.locale.type.*; @@ -154,7 +155,7 @@ public class TLocaleLoader { } private static YamlConfiguration getLocaleAsPlugin(Plugin plugin, File localeFile) { - try (InputStream canonicalResource = Files.getCanonicalResource(plugin, "lang/" + localeFile.getName())) { + try (InputStream canonicalResource = Files.getCanonicalResource(plugin, (plugin instanceof InternalPlugin ? "__resources__/" : "") + "lang/" + localeFile.getName())) { if (canonicalResource != null) { return YamlConfiguration.loadConfiguration(new InputStreamReader(canonicalResource, StandardCharsets.UTF_8)); }