From cd65b0848263d15aca4d4398ea17400cf745e6ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Thu, 29 Mar 2018 23:59:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0=E8=87=B3?= =?UTF-8?q?=EF=BC=9A3.83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新:Language2 工具类新增 [json2] 类型 --- resources/Language2/Language2.yml | 13 +- resources/plugin.yml | 2 +- .../string/language2/Language2Format.java | 18 +- .../string/language2/Language2Type.java | 5 + .../string/language2/Language2Value.java | 4 +- .../language2/value/Language2Json2.java | 206 ++++++++++++++++++ 6 files changed, 237 insertions(+), 11 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/string/language2/value/Language2Json2.java diff --git a/resources/Language2/Language2.yml b/resources/Language2/Language2.yml index b495e0d..06221ec 100644 --- a/resources/Language2/Language2.yml +++ b/resources/Language2/Language2.yml @@ -14,6 +14,8 @@ # 50: BOOK 语言类型指定 option 不存在 # 51: BOOK 语言类型识别异常 # 52: BOOK 语言类型 url 地址错误(http:// or https://) +# 60: JSON2 语言类型指定 option 不存在 +# 61: JSON2 语言类型识别异常 # 正常提示 TEXT: '&f正常的提示' @@ -111,4 +113,13 @@ BOOK-TEXT: - '@option:2' - ' text: 测试' - ' command: 执行命令' -- ' showtext: 显示文本' \ No newline at end of file +- ' showtext: 显示文本' + +# JSON2 +JSON-NEW: +- '[json2]' +- '新 <@1> 内容 - 1' +- '新 <@1> 内容 - 2' +- '@option:1' +- ' text: JSON' +- ' showtext: 展示文本' diff --git a/resources/plugin.yml b/resources/plugin.yml index c7c2d59..36af626 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -6,7 +6,7 @@ website: http://www.15imc.com/index.html main: me.skymc.taboolib.Main -version: 3.829 +version: 3.83 commands: taboolib: diff --git a/src/main/java/me/skymc/taboolib/string/language2/Language2Format.java b/src/main/java/me/skymc/taboolib/string/language2/Language2Format.java index a2ebcf0..b5bd00d 100644 --- a/src/main/java/me/skymc/taboolib/string/language2/Language2Format.java +++ b/src/main/java/me/skymc/taboolib/string/language2/Language2Format.java @@ -20,11 +20,6 @@ public class Language2Format implements Language2Line { @Getter private List language2Lines = new ArrayList<>(); - /** - * 构造方法 - * - * @param value 父类 - */ public Language2Format(Player player, Language2Value value) { language2Value = value; // 语言类型 @@ -62,6 +57,13 @@ public class Language2Format implements Language2Line { // 更改类型 type = Language2Type.JSON; } + // JSON2 + else if (line.contains("[json2]")) { + // 递交数据 + parseValue(player, values, type); + // 更改类型 + type = Language2Type.JSON2; + } // 音效 else if (line.contains("[sound]")) { // 递交数据 @@ -101,7 +103,6 @@ public class Language2Format implements Language2Line { } // 变量转换 list = language2Value.setPlaceholder(list, player); - // 大标题 switch (type) { case TITLE: @@ -115,6 +116,10 @@ public class Language2Format implements Language2Line { case JSON: language2Lines.add(new Language2Json(this, list, player)); break; + // JSON2 + case JSON2: + language2Lines.add(new Language2Json2(this, list, player)); + break; // 音效 case SOUND: language2Lines.add(new Language2Sound(this, list)); @@ -127,7 +132,6 @@ public class Language2Format implements Language2Line { language2Lines.add(new Language2Text(this, list)); break; } - // 清理数据 list.clear(); } diff --git a/src/main/java/me/skymc/taboolib/string/language2/Language2Type.java b/src/main/java/me/skymc/taboolib/string/language2/Language2Type.java index 173e6ce..c7fbda8 100644 --- a/src/main/java/me/skymc/taboolib/string/language2/Language2Type.java +++ b/src/main/java/me/skymc/taboolib/string/language2/Language2Type.java @@ -16,6 +16,11 @@ public enum Language2Type { */ JSON, + /** + * JSON2 文本 + */ + JSON2, + /** * 大标题 */ diff --git a/src/main/java/me/skymc/taboolib/string/language2/Language2Value.java b/src/main/java/me/skymc/taboolib/string/language2/Language2Value.java index d56ff03..fddf89e 100644 --- a/src/main/java/me/skymc/taboolib/string/language2/Language2Value.java +++ b/src/main/java/me/skymc/taboolib/string/language2/Language2Value.java @@ -37,13 +37,13 @@ public class Language2Value { public Language2Value(Language2 language, String languageKey) { // 如果语言文件不存在 if (language == null || languageKey == null) { - languageValue = Collections.singletonList(ChatColor.DARK_RED + "[]"); + languageValue = Arrays.asList(ChatColor.DARK_RED + "[]", "[return]"); return; } // 如果语言文本不存在 if (!language.getConfiguration().contains(languageKey)) { - languageValue = Collections.singletonList(ChatColor.DARK_RED + "[]"); + languageValue = Arrays.asList(ChatColor.DARK_RED + "[]", "[return]"); return; } diff --git a/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json2.java b/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json2.java new file mode 100644 index 0000000..436cc1d --- /dev/null +++ b/src/main/java/me/skymc/taboolib/string/language2/value/Language2Json2.java @@ -0,0 +1,206 @@ +package me.skymc.taboolib.string.language2.value; + +import lombok.Getter; +import me.skymc.taboolib.inventory.ItemUtils; +import me.skymc.taboolib.jsonformatter.JSONFormatter; +import me.skymc.taboolib.jsonformatter.click.ClickEvent; +import me.skymc.taboolib.jsonformatter.click.OpenUrlEvent; +import me.skymc.taboolib.jsonformatter.click.RunCommandEvent; +import me.skymc.taboolib.jsonformatter.click.SuggestCommandEvent; +import me.skymc.taboolib.jsonformatter.hover.HoverEvent; +import me.skymc.taboolib.jsonformatter.hover.ShowItemEvent; +import me.skymc.taboolib.jsonformatter.hover.ShowTextEvent; +import me.skymc.taboolib.string.language2.Language2Format; +import me.skymc.taboolib.string.language2.Language2Line; +import me.skymc.taboolib.string.language2.Language2Value; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author sky + * @since 2018-03-10 15:55:28 + */ +public class Language2Json2 implements Language2Line { + + private static final String KEY_TEXT = " text: "; + private static final String KEY_COMMAND = " command: "; + private static final String KEY_SUGGEST = " suggest: "; + private static final String KEY_URL = " url: "; + private static final String KEY_SHOWTEXT = " showtext: "; + private static final String KEY_SHOWITEM = " showitem: "; + private static final String KEY_OPTION = "@option:"; + private static final Pattern pattern = Pattern.compile("<@(\\S+)>"); + + @Getter + private Player player; + + @Getter + private Language2Value value; + + @Getter + private HashMap options = new HashMap<>(); + + @Getter + private JSONFormatter json = new JSONFormatter(); + + public Language2Json2(Language2Format format, List list, Player player) { + // 变量 + this.player = player; + this.value = format.getLanguage2Value(); + + // 获取书本设置 + formatOptions(list); + // 遍历内容 + int lineNumber = 0; + for (String line : list) { + if (line.startsWith("@option")) { + break; + } else { + Matcher matcher = pattern.matcher(line); + boolean find = false; + while (matcher.find()) { + find = true; + String optionName = matcher.group(1); + String optionFullName = "<@" + matcher.group(1) + ">"; + // 判断设置是否存在 + if (!options.containsKey(optionName)) { + json.append("§4[]"); + } else { + String[] line_split = line.split(optionFullName); + try { + // 单独一行 + if (line_split.length == 0) { + json.append(options.get(optionName)); + } else { + // 前段 + json.append(line_split[0]); + // 变量 + json.append(options.get(optionName)); + // 后段 + if (line_split.length >= 2) { + // 获取文本 + StringBuilder sb = new StringBuilder(); + for (int i = 1 ; i < line_split.length ; i++) { + sb.append(line_split[i]).append(optionFullName); + } + // 更改文本 + line = sb.substring(0, sb.length() - optionFullName.length()); + // 如果后段还有变量 + if (!pattern.matcher(line).find()) { + json.append(line_split[1]); + } + } + } + } catch (Exception e) { + json.append("§4[]"); + } + } + } + if (!find) { + json.append(line); + } + if (lineNumber + 1 < list.size()) { + json.newLine(); + } + } + } + } + + private void formatOptions(List list) { + HashMap> _options = getOptions(list); + for (Entry> entry : _options.entrySet()) { + JSONFormatter jsonFormatter = new JSONFormatter(); + String current = ChatColor.DARK_RED + "[]"; + ClickEvent clickEvent = null; + HoverEvent hoverEvent = null; + for (String _option : entry.getValue()) { + if (_option.startsWith(KEY_TEXT)) { + current = _option.substring(KEY_TEXT.length()); + } + else if (_option.startsWith(KEY_COMMAND)) { + clickEvent = new RunCommandEvent(_option.substring(KEY_COMMAND.length())); + } + else if (_option.startsWith(KEY_SUGGEST)) { + clickEvent = new SuggestCommandEvent(_option.substring(KEY_SUGGEST.length())); + } + else if (_option.startsWith(KEY_URL)) { + clickEvent = new OpenUrlEvent(_option.substring(KEY_URL.length())); + } + else if (_option.startsWith(KEY_SHOWTEXT)) { + hoverEvent = new ShowTextEvent(_option.replace("||", "\n").substring(KEY_SHOWTEXT.length())); + } + else if (_option.startsWith(KEY_SHOWITEM)) { + ItemStack item = ItemUtils.getCacheItem(_option.substring(KEY_SHOWITEM.length())); + if (item == null) { + item = new ItemStack(Material.STONE); + } + hoverEvent = new ShowItemEvent(item); + } + } + append(jsonFormatter, current, clickEvent, hoverEvent); + options.put(entry.getKey(), jsonFormatter); + } + } + + private void append(JSONFormatter json, String current, ClickEvent clickEvent, HoverEvent hoverEvent) { + if (clickEvent == null && hoverEvent == null) { + json.append(current); + } else if (clickEvent != null && hoverEvent == null) { + json.appendClick(current, clickEvent); + } else if (clickEvent == null && hoverEvent != null) { + json.appendHover(current, hoverEvent); + } else { + json.appendHoverClick(current, hoverEvent, clickEvent); + } + } + + private HashMap> getOptions(List list) { + HashMap> options_source = new HashMap<>(); + List option = new ArrayList<>(); + // 遍历 + String optionName = null; + boolean start = false; + // 遍历所有代码 + for (String line : list) { + if (line.startsWith(KEY_OPTION)) { + // 如果已经开始检测 + if (start) { + // 返回源码 + options_source.put(optionName, new ArrayList<>(option)); + // 清除源码 + option.clear(); + } + // 标签 + start = true; + // 当前设置名称 + optionName = line.substring(KEY_OPTION.length()); + } + else if (start) { + option.add(line); + } + } + // 返回最后设置 + options_source.put(optionName, option); + return options_source; + } + + @Override + public void send(Player player) { + json.send(player); + } + + @Override + public void console() { + Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + "[]"); + } +}