版本更新至:3.83
更新:Language2 工具类新增 [json2] 类型
This commit is contained in:
		@@ -14,6 +14,8 @@
 | 
			
		||||
#   50: BOOK 语言类型指定 option 不存在
 | 
			
		||||
#   51: BOOK 语言类型识别异常
 | 
			
		||||
#   52: BOOK 语言类型 url 地址错误(http:// or https://)
 | 
			
		||||
#   60: JSON2 语言类型指定 option 不存在
 | 
			
		||||
#   61: JSON2 语言类型识别异常
 | 
			
		||||
 | 
			
		||||
# 正常提示
 | 
			
		||||
TEXT: '&f正常的提示'
 | 
			
		||||
@@ -112,3 +114,12 @@ BOOK-TEXT:
 | 
			
		||||
- '    text: 测试'
 | 
			
		||||
- '    command: 执行命令'
 | 
			
		||||
- '    showtext: 显示文本'
 | 
			
		||||
 | 
			
		||||
# JSON2
 | 
			
		||||
JSON-NEW:
 | 
			
		||||
- '[json2]'
 | 
			
		||||
- '新 <@1> 内容 - 1'
 | 
			
		||||
- '新 <@1> 内容 - 2'
 | 
			
		||||
- '@option:1'
 | 
			
		||||
- '    text: JSON'
 | 
			
		||||
- '    showtext: 展示文本'
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@ website: http://www.15imc.com/index.html
 | 
			
		||||
 | 
			
		||||
main: me.skymc.taboolib.Main
 | 
			
		||||
 | 
			
		||||
version: 3.829
 | 
			
		||||
version: 3.83
 | 
			
		||||
 | 
			
		||||
commands:
 | 
			
		||||
  taboolib:
 | 
			
		||||
 
 | 
			
		||||
@@ -20,11 +20,6 @@ public class Language2Format implements Language2Line {
 | 
			
		||||
	@Getter
 | 
			
		||||
	private List<Language2Line> 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();
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,11 @@ public enum Language2Type {
 | 
			
		||||
	 */
 | 
			
		||||
	JSON,
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * JSON2 文本
 | 
			
		||||
	 */
 | 
			
		||||
	JSON2,
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
	 * 大标题
 | 
			
		||||
	 */
 | 
			
		||||
 
 | 
			
		||||
@@ -37,13 +37,13 @@ public class Language2Value {
 | 
			
		||||
	public Language2Value(Language2 language, String languageKey) {
 | 
			
		||||
		// 如果语言文件不存在
 | 
			
		||||
		if (language == null || languageKey == null) {
 | 
			
		||||
			languageValue = Collections.singletonList(ChatColor.DARK_RED + "[<ERROR-0>]");
 | 
			
		||||
			languageValue = Arrays.asList(ChatColor.DARK_RED + "[<ERROR-0>]", "[return]");
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		// 如果语言文本不存在
 | 
			
		||||
		if (!language.getConfiguration().contains(languageKey)) {
 | 
			
		||||
			languageValue = Collections.singletonList(ChatColor.DARK_RED + "[<ERROR-1: " + languageKey + ">]");
 | 
			
		||||
			languageValue = Arrays.asList(ChatColor.DARK_RED + "[<ERROR-1: " + languageKey + ">]", "[return]");
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
 
 | 
			
		||||
@@ -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<String, JSONFormatter> options = new HashMap<>();
 | 
			
		||||
	
 | 
			
		||||
	@Getter
 | 
			
		||||
	private JSONFormatter json = new JSONFormatter();
 | 
			
		||||
	
 | 
			
		||||
	public Language2Json2(Language2Format format, List<String> 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[<ERROR-60: " + format.getLanguage2Value().getLanguageKey() + ">]");
 | 
			
		||||
					} 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[<ERROR-61: " + format.getLanguage2Value().getLanguageKey() + ">]");
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				if (!find) {
 | 
			
		||||
					json.append(line);
 | 
			
		||||
				}
 | 
			
		||||
				if (lineNumber + 1 < list.size()) {
 | 
			
		||||
					json.newLine();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void formatOptions(List<String> list) {
 | 
			
		||||
		HashMap<String, List<String>> _options = getOptions(list);
 | 
			
		||||
		for (Entry<String, List<String>> entry : _options.entrySet()) {
 | 
			
		||||
			JSONFormatter jsonFormatter = new JSONFormatter();
 | 
			
		||||
			String current = ChatColor.DARK_RED + "[<ERROR-20: " + value.getLanguageKey() + ">]";
 | 
			
		||||
			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<String, List<String>> getOptions(List<String> list) {
 | 
			
		||||
		HashMap<String, List<String>> options_source = new HashMap<>();
 | 
			
		||||
		List<String> 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 + "[<ERROR-40: " + value.getLanguageKey() + ">]");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user