Merge branch 'master' of https://github.com/Bkm016/TabooLib
This commit is contained in:
@@ -13,6 +13,7 @@ import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleLoader;
|
||||
import com.ilummc.tlib.util.IO;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -49,7 +50,7 @@ public class TLib {
|
||||
libsFolder.mkdirs();
|
||||
}
|
||||
try {
|
||||
String yamlText = new String(IO.readFully(TLib.class.getResourceAsStream("/lang/internal.yml")), Charset.forName("utf-8"));
|
||||
String yamlText = new String(IO.readFully(FileUtils.getResource("lang/internal.yml")), Charset.forName("utf-8"));
|
||||
internalLanguage = new YamlConfiguration();
|
||||
internalLanguage.loadFromString(yamlText);
|
||||
} catch (IOException | InvalidConfigurationException ignored) {
|
||||
@@ -71,13 +72,14 @@ public class TLib {
|
||||
TLocaleLoader.init();
|
||||
PlaceholderHook.init();
|
||||
TLocaleLoader.load(Main.getInst(), false);
|
||||
TDependencyInjector.inject(Main.getInst(), tLib);
|
||||
}
|
||||
|
||||
public static void initPost() {
|
||||
TDependencyInjector.inject(Main.getInst(), TLib.getTLib());
|
||||
try {
|
||||
Pool.init();
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void unload() {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.ilummc.tlib.bungee.api.chat.TextComponent;
|
||||
import com.ilummc.tlib.bungee.api.chat.TranslatableComponent;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
|
||||
@@ -52,8 +52,8 @@ public class TDependency {
|
||||
}
|
||||
|
||||
private static boolean downloadMaven(String url, String groupId, String artifactId, String version, File target, String dl) {
|
||||
if (Main.getInst().getConfig().getBoolean("OFFLINE-MODE")) {
|
||||
TLocale.Logger.warn("DEPENDENCY.OFFLINE-DEPENDENCY-WARN");
|
||||
if (Main.isOfflineVersion()) {
|
||||
TLocale.Logger.warn("DEPENDENCY.DOWNLOAD-OFFLINE");
|
||||
return false;
|
||||
}
|
||||
AtomicBoolean failed = new AtomicBoolean(false);
|
||||
@@ -84,4 +84,4 @@ public class TDependency {
|
||||
private static int getDownloadPoolSize() {
|
||||
return Main.getInst().getConfig().getInt("DOWNLOAD-POOL-SIZE", 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,23 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TDependencyInjector {
|
||||
|
||||
public static Dependency[] getDependencies(Object o) {
|
||||
Dependency[] dependencies = new Dependency[0];
|
||||
Dependencies d = o.getClass().getAnnotation(Dependencies.class);
|
||||
if (d != null) {
|
||||
dependencies = d.value();
|
||||
}
|
||||
Dependency d2 = o.getClass().getAnnotation(Dependency.class);
|
||||
if (d2 != null) {
|
||||
dependencies = new Dependency[]{d2};
|
||||
}
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
public static void inject(Plugin plugin, Object o) {
|
||||
TLocaleLoader.load(plugin, true);
|
||||
injectDependencies(plugin, o);
|
||||
@@ -135,17 +149,7 @@ public class TDependencyInjector {
|
||||
}
|
||||
|
||||
private static void injectDependencies(Plugin plugin, Object o) {
|
||||
Dependency[] dependencies = new Dependency[0];
|
||||
{
|
||||
Dependencies d = o.getClass().getAnnotation(Dependencies.class);
|
||||
if (d != null) {
|
||||
dependencies = d.value();
|
||||
}
|
||||
Dependency d2 = o.getClass().getAnnotation(Dependency.class);
|
||||
if (d2 != null) {
|
||||
dependencies = new Dependency[]{d2};
|
||||
}
|
||||
}
|
||||
Dependency[] dependencies = getDependencies(o);
|
||||
if (dependencies.length != 0) {
|
||||
TLocale.Logger.info("DEPENDENCY.LOADING-START", plugin.getName());
|
||||
for (Dependency dependency : dependencies) {
|
||||
@@ -167,5 +171,4 @@ public class TDependencyInjector {
|
||||
TLocale.Logger.info("DEPENDENCY.LOAD-COMPLETE");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,20 +2,30 @@ package com.ilummc.tlib.resources;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.bungee.api.ChatColor;
|
||||
import com.ilummc.tlib.bungee.api.chat.BaseComponent;
|
||||
import com.ilummc.tlib.bungee.api.chat.TextComponent;
|
||||
import com.ilummc.tlib.bungee.chat.ComponentSerializer;
|
||||
import com.ilummc.tlib.inject.TLoggerManager;
|
||||
import com.ilummc.tlib.util.Ref;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.nms.NMSUtil19;
|
||||
import me.skymc.taboolib.nms.NMSUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author IzzelAliz
|
||||
*/
|
||||
public class TLocale {
|
||||
|
||||
private TLocale() {
|
||||
@@ -128,4 +138,23 @@ public class TLocale {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Tellraw extends TLocale {
|
||||
|
||||
private static final Field playerConnection = NMSUtils.getFieldSilent(NMSUtil19.class_EntityPlayer, "playerConnection");
|
||||
private static final Method sendPacket = NMSUtils.getMethodSilent(NMSUtil19.class_PlayerConnection, "sendPacket", NMSUtil19.class_Packet);
|
||||
private static final Constructor<?> PacketPlayOutChat = NMSUtils.getConstructorSilent(NMSUtil19.class_PacketPlayOutChat, NMSUtil19.class_IChatBaseComponent);
|
||||
|
||||
public static void send(CommandSender sender, String rawMessage) {
|
||||
if (sender instanceof Player) {
|
||||
try {
|
||||
sendPacket.invoke(playerConnection.get(NMSUtils.getHandle(sender)), PacketPlayOutChat.newInstance(rawMessage));
|
||||
} catch (Exception e) {
|
||||
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + rawMessage);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(TextComponent.toLegacyText(ComponentSerializer.parse(rawMessage)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import me.skymc.taboocode.TabooCodeLang;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@@ -29,8 +30,10 @@ public class TLocaleLoader {
|
||||
public static void init() {
|
||||
ConfigurationSerialization.registerClass(TLocaleText.class, "TEXT");
|
||||
ConfigurationSerialization.registerClass(TLocaleJson.class, "JSON");
|
||||
ConfigurationSerialization.registerClass(TLocaleBook.class, "BOOK");
|
||||
ConfigurationSerialization.registerClass(TLocaleSound.class, "SOUND");
|
||||
ConfigurationSerialization.registerClass(TLocaleTitle.class, "TITLE");
|
||||
ConfigurationSerialization.registerClass(TLocaleBossBar.class, "BAR");
|
||||
ConfigurationSerialization.registerClass(TLocaleActionBar.class, "ACTION");
|
||||
}
|
||||
|
||||
@@ -140,7 +143,7 @@ public class TLocaleLoader {
|
||||
}
|
||||
|
||||
private static YamlConfiguration getLocaleAtStream(Plugin plugin, File localeFile) {
|
||||
InputStream localeInputSteam = plugin.getClass().getResourceAsStream("/lang/" + localeFile.getName());
|
||||
InputStream localeInputSteam = FileUtils.getResource(plugin, "lang/" + localeFile.getName());
|
||||
try {
|
||||
String yamlText = new String(IO.readFully(localeInputSteam), Charset.forName("utf-8"));
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.ilummc.tlib.resources.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.bungee.chat.ComponentSerializer;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.bookformatter.BookFormatter;
|
||||
import me.skymc.taboolib.bookformatter.builder.BookBuilder;
|
||||
import me.skymc.taboolib.json.tellraw.TellrawJson;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-27 0:05
|
||||
*/
|
||||
@ThreadSafe
|
||||
@SerializableAs("BOOK")
|
||||
public class TLocaleBook extends TLocaleSerialize {
|
||||
|
||||
/*
|
||||
BookTest:
|
||||
- ==: BOOK
|
||||
pages:
|
||||
0:
|
||||
- '第一页内容'
|
||||
- '[ <变量1@page-1> ]'
|
||||
1:
|
||||
- '第二页内容'
|
||||
- '[ <变量2@page-2> ]'
|
||||
args:
|
||||
page-1:
|
||||
hover: '展示内容1'
|
||||
command: '/say %player_name% NB1'
|
||||
page-2:
|
||||
hover: '展示内容2'
|
||||
suggest: '/say %player_name% NB2'
|
||||
*/
|
||||
|
||||
private final List<TellrawJson> pages;
|
||||
private final Map<String, Object> map;
|
||||
private final boolean papi;
|
||||
|
||||
public TLocaleBook(List<TellrawJson> pages, Map<String, Object> map, boolean papi) {
|
||||
this.pages = pages;
|
||||
this.map = map;
|
||||
this.papi = papi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return Maps.newHashMap(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
BookBuilder bookBuilder = BookFormatter.writtenBook();
|
||||
pages.stream().map(jsonPage -> papi ? TLocale.Translate.setPlaceholders(sender, Strings.replaceWithOrder(jsonPage.toRawMessage(), args)) : TLocale.Translate.setColored(Strings.replaceWithOrder(jsonPage.toRawMessage(), args))).map(ComponentSerializer::parse).forEach(bookBuilder::addPages);
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
BookFormatter.forceOpen((Player) sender, bookBuilder.build());
|
||||
}
|
||||
}.runTask(Main.getInst());
|
||||
}
|
||||
}.runTaskAsynchronously(Main.getInst());
|
||||
}
|
||||
|
||||
public static TLocaleBook valueOf(Map<String, Object> map) {
|
||||
Map<String, Object> pages = map.containsKey("pages") ? (Map<String, Object>) map.get("pages") : new HashMap<>();
|
||||
Map<String, Object> section = map.containsKey("args") ? (Map<String, Object>) map.get("args") : new HashMap<>();
|
||||
List<TellrawJson> pageJsonList = pages.values().stream().map(page -> TLocaleJson.formatJson(section, page, TellrawJson.create())).collect(Collectors.toList());
|
||||
return new TLocaleBook(pageJsonList, map, isPlaceholderEnabled(map));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.ilummc.tlib.resources.type;
|
||||
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.inventivetalent.bossbar.BossBar;
|
||||
import org.inventivetalent.bossbar.BossBarAPI;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-27 18:52
|
||||
*/
|
||||
@ThreadSafe
|
||||
@SerializableAs("BAR")
|
||||
public class TLocaleBossBar extends TLocaleSerialize {
|
||||
|
||||
/*
|
||||
BossBar:
|
||||
- ==: BAR
|
||||
text: 'BossBar 血条公告'
|
||||
color: BLUE
|
||||
style: NOTCHED_20
|
||||
progress: 1.0
|
||||
timeout: 20
|
||||
timeout-interval: 2
|
||||
*/
|
||||
|
||||
private final String text;
|
||||
private final BossBarAPI.Color color;
|
||||
private final BossBarAPI.Style style;
|
||||
private final float progress;
|
||||
private final int timeout;
|
||||
private final int timeoutInterval;
|
||||
private final boolean papi;
|
||||
|
||||
public TLocaleBossBar(String text, BossBarAPI.Color color, BossBarAPI.Style style, float progress, int timeout, int timeoutInterval, boolean papi) {
|
||||
this.text = text;
|
||||
this.color = color;
|
||||
this.style = style;
|
||||
this.progress = progress;
|
||||
this.timeout = timeout;
|
||||
this.timeoutInterval = timeoutInterval;
|
||||
this.papi = papi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (Bukkit.getPluginManager().getPlugin("BossBarAPI") == null) {
|
||||
TLocale.Logger.error("LOCALE.BAR-PLUGIN-NOT-FOUND");
|
||||
return;
|
||||
}
|
||||
if (sender instanceof Player) {
|
||||
TextComponent textComponent = new TextComponent(papi ? TLocale.Translate.setPlaceholders(sender, Strings.replaceWithOrder(text, args)) : TLocale.Translate.setColored(Strings.replaceWithOrder(text, args)));
|
||||
BossBar bossBar = BossBarAPI.addBar((Player) sender, textComponent, color, style, progress, timeout, timeoutInterval);
|
||||
} else {
|
||||
sender.sendMessage(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static TLocaleBossBar valueOf(Map<String, Object> map) {
|
||||
return new TLocaleBossBar(map.getOrDefault("text", "§4* Invalid Text*").toString(), getColor(String.valueOf(map.get("color"))), getStyle(String.valueOf(map.get("style"))), (float) NumberUtils.getDouble(String.valueOf(map.getOrDefault("progress", 1))), NumberUtils.getInteger(String.valueOf(map.getOrDefault("timeout", 20))), NumberUtils.getInteger(String.valueOf(map.getOrDefault("timeout-interval", 2))), isPlaceholderEnabled(map));
|
||||
}
|
||||
|
||||
private static BossBarAPI.Color getColor(String color) {
|
||||
try {
|
||||
return BossBarAPI.Color.valueOf(color);
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("LOCALE.BAR-STYLE-IDENTIFICATION-FAILED", e.toString());
|
||||
return BossBarAPI.Color.WHITE;
|
||||
}
|
||||
}
|
||||
|
||||
private static BossBarAPI.Style getStyle(String style) {
|
||||
try {
|
||||
return BossBarAPI.Style.valueOf(style);
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("LOCALE.BAR-COLOR-IDENTIFICATION-FAILED", e.toString());
|
||||
return BossBarAPI.Style.NOTCHED_20;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,12 @@ import com.ilummc.tlib.compat.PlaceholderHook;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import me.skymc.taboolib.inventory.ItemUtils;
|
||||
import me.skymc.taboolib.json.tellraw.TellrawJson;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import me.skymc.taboolib.string.VariableFormatter;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.*;
|
||||
@@ -21,6 +23,7 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@ThreadSafe
|
||||
@SerializableAs("JSON")
|
||||
public class TLocaleJson extends TLocaleSerialize {
|
||||
@@ -72,11 +75,11 @@ public class TLocaleJson extends TLocaleSerialize {
|
||||
// 可能有很多个 BaseComponent,于是为每个 component 单独设置各种事件
|
||||
BaseComponent[] component = TextComponent.fromLegacyText(text);
|
||||
arg.forEach((key, value) -> {
|
||||
if ("suggest".equalsIgnoreCase(key)) {
|
||||
if (key.equalsIgnoreCase("suggest")) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, String.valueOf(value))));
|
||||
} else if ("command".equalsIgnoreCase(key) || "commands".equalsIgnoreCase(key)) {
|
||||
} else if (key.equalsIgnoreCase("command") || "commands".equalsIgnoreCase(key)) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.valueOf(value))));
|
||||
} else if ("hover".equalsIgnoreCase(key)) {
|
||||
} else if (key.equalsIgnoreCase("hover")) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(TLocale.Translate.setColored(String.valueOf(value))).create())));
|
||||
}
|
||||
});
|
||||
@@ -101,7 +104,7 @@ public class TLocaleJson extends TLocaleSerialize {
|
||||
|
||||
private static List<String> getTextList(Object textObj) {
|
||||
if (textObj instanceof List) {
|
||||
return ((List<?>) textObj).stream().map(Object::toString).map(s -> TLocale.Translate.setColored(s)).collect(Collectors.toList());
|
||||
return ((List<?>) textObj).stream().map(Object::toString).map(TLocale.Translate::setColored).collect(Collectors.toList());
|
||||
} else if (textObj instanceof String) {
|
||||
return Lists.newArrayList(TLocale.Translate.setColored((String) textObj));
|
||||
} else {
|
||||
@@ -125,11 +128,11 @@ public class TLocaleJson extends TLocaleSerialize {
|
||||
}
|
||||
|
||||
private void sendRawMessage(CommandSender sender, BaseComponent[] components) {
|
||||
if (sender instanceof Player) {
|
||||
JSONFormatter.sendRawMessage((Player) sender, ComponentSerializer.toString(components));
|
||||
} else {
|
||||
sender.sendMessage(TextComponent.toLegacyText(components));
|
||||
}
|
||||
TLocale.Tellraw.send(sender, ComponentSerializer.toString(components));
|
||||
}
|
||||
|
||||
private List<BaseComponent> replace(List<BaseComponent> component, CommandSender sender, String... args) {
|
||||
return component.stream().map(c -> replace(c, sender, args)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private BaseComponent[] replace(BaseComponent[] component, CommandSender sender, String... args) {
|
||||
@@ -140,10 +143,6 @@ public class TLocaleJson extends TLocaleSerialize {
|
||||
return components;
|
||||
}
|
||||
|
||||
private List<BaseComponent> replace(List<BaseComponent> component, CommandSender sender, String... args) {
|
||||
return component.stream().map(c -> replace(c, sender, args)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private BaseComponent replace(BaseComponent component, CommandSender sender, String... args) {
|
||||
if (component.getClickEvent() != null) {
|
||||
ClickEvent clickEvent = new ClickEvent(component.getClickEvent().getAction(), replace(sender, component.getClickEvent().getValue(), args));
|
||||
@@ -166,4 +165,63 @@ public class TLocaleJson extends TLocaleSerialize {
|
||||
String s = Strings.replaceWithOrder(text, args);
|
||||
return papi ? PlaceholderHook.replace(sender, s) : s;
|
||||
}
|
||||
|
||||
public static TellrawJson formatJson(Map<String, Object> section, Object textObject, TellrawJson pageJson) {
|
||||
List<String> textList = textObject instanceof List ? (List<String>) textObject : Collections.singletonList(String.valueOf(textObject));
|
||||
// 遍历本页文本
|
||||
for (int i = 0; i < textList.size(); i++) {
|
||||
// 捕捉变量
|
||||
for (VariableFormatter.Variable variable : new VariableFormatter(textList.get(i), pattern).find().getVariableList()) {
|
||||
// 如果是变量
|
||||
if (variable.isVariable()) {
|
||||
String[] split = variable.getText().split("@");
|
||||
// @ 前面的字符串
|
||||
String text = split.length > 1 ? split[0] : "§4* Invalid Text *";
|
||||
// @ 后面的节点名
|
||||
String node = split.length > 1 ? split[1] : null;
|
||||
// 处理变量
|
||||
formatNode(section, pageJson, text, node);
|
||||
} else {
|
||||
pageJson.append(variable.getText());
|
||||
}
|
||||
}
|
||||
if (i + 1 < textList.size()) {
|
||||
pageJson.newLine();
|
||||
}
|
||||
}
|
||||
return pageJson;
|
||||
}
|
||||
|
||||
private static void formatNode(Map<String, Object> section, TellrawJson pageJson, String text, String node) {
|
||||
if (section.containsKey(node)) {
|
||||
try {
|
||||
Map<String, Object> args = (Map<String, Object>) section.get(node);
|
||||
// 文本
|
||||
pageJson.append(args.getOrDefault("text", text).toString());
|
||||
// 功能
|
||||
if (args.containsKey("item")) {
|
||||
pageJson.hoverItem(ItemUtils.getCacheItem(args.get("item").toString()));
|
||||
}
|
||||
if (args.containsKey("hover")) {
|
||||
pageJson.hoverText(args.get("hover").toString());
|
||||
}
|
||||
if (args.containsKey("suggest")) {
|
||||
pageJson.clickSuggest(args.get("suggest").toString());
|
||||
}
|
||||
if (args.containsKey("command")) {
|
||||
pageJson.clickCommand(args.get("command").toString());
|
||||
}
|
||||
if (args.containsKey("page")) {
|
||||
pageJson.clickChangePage(NumberUtils.getInteger(args.get("page").toString()));
|
||||
}
|
||||
if (args.containsKey("url")) {
|
||||
pageJson.clickOpenURL(args.get("url").toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("LOCALE.BOOK-ARGUMENTS-IDENTIFICATION-FAILED", e.toString());
|
||||
}
|
||||
} else {
|
||||
pageJson.append("§4* Invalid Argument *");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,14 @@ package com.ilummc.tlib.util;
|
||||
|
||||
public class Strings {
|
||||
|
||||
public static boolean isBlank(String var) {
|
||||
return var == null || var.trim().isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isEmpty(CharSequence var) {
|
||||
return var == null || var.length() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 优化过的 String#replace,比默认快了大概 5 倍
|
||||
*
|
||||
@@ -28,15 +36,13 @@ public class Strings {
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Deprecated
|
||||
//
|
||||
// *********************************
|
||||
|
||||
public static String replaceWithOrder(String template, String... args) {
|
||||
return replaceWithOrder(template, (Object[]) args);
|
||||
}
|
||||
|
||||
public static boolean isBlank(String var) {
|
||||
return var == null || var.trim().isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isEmpty(CharSequence var) {
|
||||
return var == null || var.length() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user