Support taboolib plugin version

master
坏黑 2019-07-12 01:48:15 +08:00
parent cd3ec3dbfa
commit 3d60301630
5 changed files with 90 additions and 12 deletions

View File

@ -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) {

View File

@ -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<String, Object> taboolibTLocaleSerialize(Object in);
}

View File

@ -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> T getRegisteredService(Class<? extends T> 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<String, Object> taboolibTLocaleSerialize(Object in) {
switch (in.getClass().getSimpleName()) {
case "TLocaleText": {
Map<String, Object> map = Maps.newHashMap();
try {
map.put("text", Reflection.getValue(in, true, "text"));
} catch (Throwable t) {
t.printStackTrace();
}
return map;
}
case "TLocaleSound": {
Map<String, Object> map = Maps.newHashMap();
try {
List<SoundPack> sounds = (List<SoundPack>) 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<String, Object> 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();
}
}
}

View File

@ -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));
}
}
}

View File

@ -24,6 +24,10 @@ public class TLocaleLoader {
private static final Map<String, TLocaleInstance> map = new ConcurrentHashMap<>();
/**
*
*
*/
static {
ConfigurationSerialization.registerClass(TLocaleText.class, "TEXT");
ConfigurationSerialization.registerClass(TLocaleJson.class, "JSON");