diff --git a/src/main/scala/io/izzel/taboolib/TabooLibAPI.java b/src/main/scala/io/izzel/taboolib/TabooLibAPI.java index c5fc38c..f17fe8c 100644 --- a/src/main/scala/io/izzel/taboolib/TabooLibAPI.java +++ b/src/main/scala/io/izzel/taboolib/TabooLibAPI.java @@ -15,14 +15,11 @@ import java.util.Arrays; public class TabooLibAPI { private static boolean bukkit; - private static boolean originLoaded; static { try { // 判断是否基于 Bukkit 运行 bukkit = Class.forName("org.bukkit.Bukkit") != null; - // 获取 TabooLib4.x 版本是否载入 - originLoaded = Bukkit.getPluginManager().getPlugin("TabooLib") != null; } catch (Exception ignored) { } } @@ -36,7 +33,7 @@ public class TabooLibAPI { } public static boolean isOriginLoaded() { - return originLoaded; + return Bukkit.getPluginManager().getPlugin("TabooLib") != null; } public static boolean isDependTabooLib(Plugin plugin) { diff --git a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java index ec3e956..ead8c4b 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java @@ -9,6 +9,7 @@ import org.bukkit.entity.Player; import java.util.Collection; import java.util.List; +import java.util.Map; /** * @Author 坏黑 @@ -63,4 +64,6 @@ public abstract class InternalPluginBridge { abstract public boolean placeholderHooked(); abstract public boolean worldguardHooked(); + + abstract public Map taboolibTLocaleSerialize(Object in); } diff --git a/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java b/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java index 68cfdac..36256e3 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/bridge/BridgeImpl.java @@ -1,5 +1,6 @@ package io.izzel.taboolib.common.plugin.bridge; +import com.google.common.collect.Maps; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; @@ -8,6 +9,7 @@ import com.sk89q.worldguard.protection.regions.ProtectedRegion; import io.izzel.taboolib.common.plugin.InternalPluginBridge; import io.izzel.taboolib.util.Reflection; import me.clip.placeholderapi.PlaceholderAPI; +import me.skymc.taboolib.sound.SoundPack; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; @@ -20,6 +22,7 @@ import org.bukkit.plugin.RegisteredServiceProvider; import java.lang.reflect.Method; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class BridgeImpl extends InternalPluginBridge { @@ -51,7 +54,7 @@ public class BridgeImpl extends InternalPluginBridge { @Override public T getRegisteredService(Class clazz) { RegisteredServiceProvider registeredServiceProvider = Bukkit.getServer().getServicesManager().getRegistration(clazz); - return (T) registeredServiceProvider.getProvider(); + return registeredServiceProvider == null ? null : (T) registeredServiceProvider.getProvider(); } @Override @@ -153,4 +156,45 @@ public class BridgeImpl extends InternalPluginBridge { return worldguard; } + @Override + public Map taboolibTLocaleSerialize(Object in) { + switch (in.getClass().getSimpleName()) { + case "TLocaleText": { + Map map = Maps.newHashMap(); + try { + map.put("text", Reflection.getValue(in, true, "text")); + } catch (Throwable t) { + t.printStackTrace(); + } + return map; + } + case "TLocaleSound": { + Map map = Maps.newHashMap(); + try { + List sounds = (List) Reflection.getValue(in, true, "soundPacks"); + map.put("sounds", sounds.stream().map(s -> s.getSound() + "-" + s.getA() + "-" + s.getB() + "-" + s.getDelay()).collect(Collectors.toList())); + } catch (Throwable t) { + t.printStackTrace(); + } + return map; + } + case "TLocaleBossBar": { + Map map = Maps.newHashMap(); + try { + map.put("text", Reflection.getValue(in, true, "text")); + map.put("color", Reflection.getValue(in, true, "color")); + map.put("style", Reflection.getValue(in, true, "style")); + map.put("progress", Reflection.getValue(in, true, "progress")); + map.put("timeout", Reflection.getValue(in, true, "timeout")); + map.put("timeoutInterval", Reflection.getValue(in, true, "timeoutInterval")); + map.put("papi", Reflection.getValue(in, true, "papi")); + } catch (Throwable t) { + t.printStackTrace(); + } + return map; + } + default: + return ((com.ilummc.tlib.resources.TLocaleSerialize) in).serialize(); + } + } } \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/locale/TLocaleInstance.java b/src/main/scala/io/izzel/taboolib/module/locale/TLocaleInstance.java index 8effd83..9d84a1d 100644 --- a/src/main/scala/io/izzel/taboolib/module/locale/TLocaleInstance.java +++ b/src/main/scala/io/izzel/taboolib/module/locale/TLocaleInstance.java @@ -2,7 +2,8 @@ package io.izzel.taboolib.module.locale; import com.google.common.collect.ImmutableList; import io.izzel.taboolib.TabooLib; -import io.izzel.taboolib.module.locale.type.TLocaleText; +import io.izzel.taboolib.TabooLibAPI; +import io.izzel.taboolib.module.locale.type.*; import io.izzel.taboolib.util.Strings; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -11,10 +12,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import javax.annotation.concurrent.ThreadSafe; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -54,7 +52,7 @@ class TLocaleInstance { public void sendTo(String path, CommandSender sender, String... args) { try { map.getOrDefault(path, ImmutableList.of(TLocaleSerialize.getEmpty(path))).forEach(tSender -> { - if (Bukkit.isPrimaryThread() || "true".equals(System.getProperty("tlib.forceAsync"))) { + if (Bukkit.isPrimaryThread() || Objects.equals(System.getProperty("tlib.forceAsync"), "true")) { tSender.sendTo(sender, args); } else { Bukkit.getScheduler().runTask(plugin, () -> tSender.sendTo(sender, args)); @@ -98,11 +96,24 @@ class TLocaleInstance { Object value = configuration.get(s); if (value instanceof TLocaleSerialize) { updated = map.put(s, Collections.singletonList((TLocaleSerialize) value)) != null; + } + // TabooLib 4.x 兼容 + else if (TabooLibAPI.isOriginLoaded() && value.getClass().getName().startsWith("com.ilummc.tlib.resources.type")) { + updated = map.put(s, Collections.singletonList(fromPluginVersion(value))) != null; } else if (value instanceof List && !((List) value).isEmpty()) { if (isListString((List) value)) { updated = map.put(s, Collections.singletonList(TLocaleText.of(value))) != null; } else { - updated = map.put(s, ((List) value).stream().map(o -> o instanceof TLocaleSerialize ? (TLocaleSerialize) o : TLocaleText.of(String.valueOf(o))).collect(Collectors.toList())) != null; + updated = map.put(s, ((List) value).stream().map(o -> { + if (o instanceof TLocaleSerialize) { + return (TLocaleSerialize) o; + } + // TabooLib 4.x 兼容 + else if (TabooLibAPI.isOriginLoaded() && o.getClass().getName().startsWith("com.ilummc.tlib.resources.type")) { + return fromPluginVersion(o); + } + return TLocaleText.of(String.valueOf(o)); + }).collect(Collectors.toList())) != null; } } else if (!(value instanceof ConfigurationSection)) { String str = String.valueOf(value); @@ -114,4 +125,23 @@ class TLocaleInstance { } latestUpdateNodes.set(originNodes - updateNodes); } + + private TLocaleSerialize fromPluginVersion(Object in) { + switch (in.getClass().getSimpleName()) { + case "TLocaleActionBar": + return TLocaleActionBar.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + case "TLocaleBook": + return TLocaleBook.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + case "TLocaleBossBar": + return TLocaleBossBar.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + case "TLocaleJson": + return TLocaleJson.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + case "TLocaleSound": + return TLocaleSound.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + case "TLocaleTitle": + return TLocaleTitle.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + default: + return TLocaleText.valueOf(TabooLibAPI.getPluginBridge().taboolibTLocaleSerialize(in)); + } + } } 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 ab30aa3..8661c4c 100644 --- a/src/main/scala/io/izzel/taboolib/module/locale/TLocaleLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/locale/TLocaleLoader.java @@ -24,6 +24,10 @@ public class TLocaleLoader { private static final Map map = new ConcurrentHashMap<>(); + /** + * 因插件版载入慢于非插件版,所以语言文件类型会被插件版本覆盖 + * 解决方案:主动兼容插件版本 + */ static { ConfigurationSerialization.registerClass(TLocaleText.class, "TEXT"); ConfigurationSerialization.registerClass(TLocaleJson.class, "JSON");