diff --git a/pom.xml b/pom.xml
index 4aaeaa4..e1ec024 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
me.skymc
TabooLib
- 4.05
+ 4.06
UTF-8
@@ -176,6 +176,13 @@
system
${basedir}/libs/Vault.jar
+
+ bossbar
+ bossbar
+ 1
+ system
+ ${basedir}/libs/BossBarAPI.jar
+
\ No newline at end of file
diff --git a/src/main/java/com/ilummc/tlib/bungee/chat/ComponentSerializer.java b/src/main/java/com/ilummc/tlib/bungee/chat/ComponentSerializer.java
index b6165db..6d19961 100644
--- a/src/main/java/com/ilummc/tlib/bungee/chat/ComponentSerializer.java
+++ b/src/main/java/com/ilummc/tlib/bungee/chat/ComponentSerializer.java
@@ -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;
/**
diff --git a/src/main/java/com/ilummc/tlib/resources/TLocale.java b/src/main/java/com/ilummc/tlib/resources/TLocale.java
index b2dc858..9747a35 100644
--- a/src/main/java/com/ilummc/tlib/resources/TLocale.java
+++ b/src/main/java/com/ilummc/tlib/resources/TLocale.java
@@ -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,22 @@ 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)));
+ }
+ }
+ }
}
diff --git a/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java b/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java
index 2aef0c0..98f9a99 100644
--- a/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java
+++ b/src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java
@@ -30,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");
}
diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleBook.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleBook.java
new file mode 100644
index 0000000..978eb27
--- /dev/null
+++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleBook.java
@@ -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 pages;
+ private final Map map;
+ private final boolean papi;
+
+ public TLocaleBook(List pages, Map map, boolean papi) {
+ this.pages = pages;
+ this.map = map;
+ this.papi = papi;
+ }
+
+ @Override
+ public Map 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 map) {
+ Map pages = map.containsKey("pages") ? (Map) map.get("pages") : new HashMap<>();
+ Map section = map.containsKey("args") ? (Map) map.get("args") : new HashMap<>();
+ List pageJsonList = pages.values().stream().map(page -> TLocaleJson.formatJson(section, page, TellrawJson.create())).collect(Collectors.toList());
+ return new TLocaleBook(pageJsonList, map, isPlaceholderEnabled(map));
+ }
+}
diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleBossBar.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleBossBar.java
new file mode 100644
index 0000000..62261ca
--- /dev/null
+++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleBossBar.java
@@ -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 serialize() {
+ return null;
+ }
+
+ public static TLocaleBossBar valueOf(Map 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;
+ }
+ }
+}
diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleJson.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleJson.java
index 4cc689b..2a14974 100644
--- a/src/main/java/com/ilummc/tlib/resources/type/TLocaleJson.java
+++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleJson.java
@@ -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 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 replace(List 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 replace(List 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 section, Object textObject, TellrawJson pageJson) {
+ List textList = textObject instanceof List ? (List) 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 section, TellrawJson pageJson, String text, String node) {
+ if (section.containsKey(node)) {
+ try {
+ Map args = (Map) 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 *");
+ }
+ }
}
diff --git a/src/main/java/com/ilummc/tlib/util/Strings.java b/src/main/java/com/ilummc/tlib/util/Strings.java
index e454212..40207d0 100644
--- a/src/main/java/com/ilummc/tlib/util/Strings.java
+++ b/src/main/java/com/ilummc/tlib/util/Strings.java
@@ -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;
- }
}
diff --git a/src/main/java/me/skymc/taboolib/bookformatter/BookReflection.java b/src/main/java/me/skymc/taboolib/bookformatter/BookReflection.java
index 5cbf36c..b090544 100644
--- a/src/main/java/me/skymc/taboolib/bookformatter/BookReflection.java
+++ b/src/main/java/me/skymc/taboolib/bookformatter/BookReflection.java
@@ -1,8 +1,10 @@
package me.skymc.taboolib.bookformatter;
-import net.md_5.bungee.api.chat.BaseComponent;
-import net.md_5.bungee.api.chat.TextComponent;
-import net.md_5.bungee.chat.ComponentSerializer;
+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.logger.TLogger;
+import com.ilummc.tlib.resources.TLocale;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@@ -12,6 +14,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -31,11 +34,13 @@ public final class BookReflection {
private static final Method craftPlayerGetHandle;
- //This method takes an enum that represents the player's hand only in versions >= 1.9
- //In the other versions it only takes the nms item
+ /*
+ This method takes an enum that represents the player's hand only in versions >= 1.9
+ In the other versions it only takes the nms item
+ */
private static final Method entityPlayerOpenBook;
- //only version >= 1.9
+ // only version >= 1.9
private static final Object[] hands;
//Older versions
@@ -115,18 +120,22 @@ public final class BookReflection {
* @param meta the book meta to change
* @param components the pages of the book
*/
- @SuppressWarnings("unchecked")//reflections = unchecked warnings
- public static void setPages(BookMeta meta, BaseComponent[][] components) {
+ public static void setPages(BookMeta meta, BaseComponent[]... components) {
+ List