+ optimize SimpleVersionControl

+ optimize TFunction
+ optimize TLocale
+ add TLocale.Display
This commit is contained in:
坏黑 2018-11-09 16:41:22 +08:00
parent 47faa031eb
commit a228ba5fe5
16 changed files with 164 additions and 51 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" /> <output url="file://$MODULE_DIR$/target/classes" />

View File

@ -9,6 +9,7 @@ import com.ilummc.tlib.util.Ref;
import com.ilummc.tlib.util.Strings; import com.ilummc.tlib.util.Strings;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.common.nms.NMSHandler;
import me.skymc.taboolib.json.tellraw.TellrawCreator; import me.skymc.taboolib.json.tellraw.TellrawCreator;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -74,6 +75,32 @@ public class TLocale {
Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(Ref.getCallerPlugin(clazz), false)); Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(Ref.getCallerPlugin(clazz), false));
} }
public static final class Tellraw extends TLocale {
public static void send(CommandSender sender, String rawMessage) {
if (sender instanceof Player) {
TellrawCreator.getAbstractTellraw().sendRawMessage((Player) sender, rawMessage);
} else {
sender.sendMessage(TextComponent.toLegacyText(ComponentSerializer.parse(rawMessage)));
}
}
}
public static final class Display extends TLocale {
public static void sendTitle(Player player, String title, String subTitle) {
sendTitle(player, title, subTitle, 10, 20, 10);
}
public static void sendTitle(Player player, String title, String subTitle, int fadein, int stay, int fadeout) {
NMSHandler.getHandler().sendTitle(player, title, fadein, stay, fadeout, subTitle, fadein, stay, fadeout);
}
public static void sendActionBar(Player player, String text) {
NMSHandler.getHandler().sendActionBar(player, text);
}
}
public static final class Translate extends TLocale { public static final class Translate extends TLocale {
public static boolean isPlaceholderUseDefault() { public static boolean isPlaceholderUseDefault() {
@ -131,15 +158,4 @@ public class TLocale {
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).verbose(locale))); Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).verbose(locale)));
} }
} }
public static class Tellraw extends TLocale {
public static void send(CommandSender sender, String rawMessage) {
if (sender instanceof Player) {
TellrawCreator.getAbstractTellraw().sendRawMessage((Player) sender, rawMessage);
} else {
sender.sendMessage(TextComponent.toLegacyText(ComponentSerializer.parse(rawMessage)));
}
}
}
} }

View File

@ -2,6 +2,7 @@ package com.ilummc.tlib.resources.type;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.ilummc.tlib.compat.PlaceholderHook; import com.ilummc.tlib.compat.PlaceholderHook;
import com.ilummc.tlib.resources.TLocale;
import com.ilummc.tlib.resources.TLocaleSerialize; import com.ilummc.tlib.resources.TLocaleSerialize;
import com.ilummc.tlib.util.Strings; import com.ilummc.tlib.util.Strings;
import me.skymc.taboolib.display.ActionUtils; import me.skymc.taboolib.display.ActionUtils;
@ -33,7 +34,7 @@ public class TLocaleActionBar extends TLocaleSerialize {
@Override @Override
public void sendTo(CommandSender sender, String... args) { public void sendTo(CommandSender sender, String... args) {
if (sender instanceof Player) { if (sender instanceof Player) {
ActionUtils.send(((Player) sender), replace(sender, text, args)); TLocale.Display.sendActionBar(((Player) sender), replace(sender, text, args));
} }
} }

View File

@ -52,7 +52,7 @@ public class TLocaleTitle extends TLocaleSerialize {
@Override @Override
public void sendTo(CommandSender sender, String... args) { public void sendTo(CommandSender sender, String... args) {
if (sender instanceof Player) { if (sender instanceof Player) {
TitleUtils.sendTitle((Player) sender, replaceText(sender, Strings.replaceWithOrder(title, args)), replaceText(sender, Strings.replaceWithOrder(subtitle, args)), fadein, stay, fadeout); TLocale.Display.sendTitle((Player) sender, replaceText(sender, Strings.replaceWithOrder(title, args)), replaceText(sender, Strings.replaceWithOrder(subtitle, args)), fadein, stay, fadeout);
} else { } else {
TLocale.Logger.error("LOCALE.TITLE-SEND-TO-NON-PLAYER", asString(args)); TLocale.Logger.error("LOCALE.TITLE-SEND-TO-NON-PLAYER", asString(args));
} }

View File

@ -20,7 +20,7 @@ import java.util.Map;
* @Author sky * @Author sky
* @Since 2018-09-30 17:30 * @Since 2018-09-30 17:30
*/ */
@TFunction @TFunction(enable = "init")
public class TCloudLoader { public class TCloudLoader {
private static String url = "https://gitee.com/bkm016/TabooLibCloud/raw/master/cloud.json"; private static String url = "https://gitee.com/bkm016/TabooLibCloud/raw/master/cloud.json";
@ -30,7 +30,7 @@ public class TCloudLoader {
private static Map<String, Expansion> expansionInternal = Maps.newHashMap(); private static Map<String, Expansion> expansionInternal = Maps.newHashMap();
private static File expansionInternalFolder; private static File expansionInternalFolder;
public void onEnable() { static void init() {
createFolder(); createFolder();
refresh(); refresh();
} }

View File

@ -1,7 +1,9 @@
package me.skymc.taboolib.common.function; package me.skymc.taboolib.common.function;
import com.ilummc.tlib.logger.TLogger;
import me.skymc.taboolib.TabooLibLoader; import me.skymc.taboolib.TabooLibLoader;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -17,9 +19,13 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
TFunction function = pluginClass.getAnnotation(TFunction.class); TFunction function = pluginClass.getAnnotation(TFunction.class);
try { try {
Method method = pluginClass.getDeclaredMethod(function.enable()); Method method = pluginClass.getDeclaredMethod(function.enable());
method.setAccessible(true); if (method != null) {
method.invoke(pluginClass.newInstance()); method.setAccessible(true);
} catch (Exception ignored) { method.invoke(null);
}
} catch (Exception e) {
TLogger.getGlobalLogger().warn("TFunction load Failed: " + pluginClass.getName());
e.printStackTrace();
} }
} }
} }
@ -30,9 +36,13 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
TFunction function = pluginClass.getAnnotation(TFunction.class); TFunction function = pluginClass.getAnnotation(TFunction.class);
try { try {
Method method = pluginClass.getDeclaredMethod(function.disable()); Method method = pluginClass.getDeclaredMethod(function.disable());
method.setAccessible(true); if (method != null) {
method.invoke(pluginClass.newInstance()); method.setAccessible(true);
} catch (Exception ignored) { method.invoke(null);
}
} catch (Exception e) {
TLogger.getGlobalLogger().warn("TFunction unload Failed: " + pluginClass.getName());
e.printStackTrace();
} }
} }
} }

View File

@ -1,5 +1,6 @@
package me.skymc.taboolib.common.loader; package me.skymc.taboolib.common.loader;
import com.ilummc.tlib.logger.TLogger;
import com.ilummc.tlib.util.Ref; import com.ilummc.tlib.util.Ref;
import me.skymc.taboolib.TabooLibLoader; import me.skymc.taboolib.TabooLibLoader;
import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.listener.TListener;
@ -59,6 +60,7 @@ public class InstantiableLoader implements Listener {
try { try {
instance.put(instantiable.value(), ReflectionUtils.instantiateObject(pluginClass)); instance.put(instantiable.value(), ReflectionUtils.instantiateObject(pluginClass));
} catch (Exception e) { } catch (Exception e) {
TLogger.getGlobalLogger().warn("Instance Failed: " + pluginClass.getName());
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -0,0 +1,31 @@
package me.skymc.taboolib.common.nms;
import me.skymc.taboolib.common.function.TFunction;
import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl;
import org.bukkit.entity.Player;
/**
* @Author 坏黑
* @Since 2018-11-09 14:38
*/
@TFunction(enable = "init")
public abstract class NMSHandler {
private static NMSHandler handler;
static void init() {
try {
handler = (NMSHandler) SimpleVersionControl.createNMS("me.skymc.taboolib.common.nms.NMSHandlerImpl").translate().newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
abstract public void sendTitle(Player player, String title, int titleFadein, int titleStay, int titleFadeout, String subtitle, int subtitleFadein, int subtitleStay, int subtitleFadeout);
abstract public void sendActionBar(Player player, String text);
public static NMSHandler getHandler() {
return handler;
}
}

View File

@ -0,0 +1,33 @@
package me.skymc.taboolib.common.nms;
import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.common.packet.TPacketHandler;
import net.minecraft.server.v1_12_R1.ChatMessageType;
import net.minecraft.server.v1_8_R3.ChatComponentText;
import net.minecraft.server.v1_8_R3.PacketPlayOutChat;
import net.minecraft.server.v1_8_R3.PacketPlayOutTitle;
import org.bukkit.entity.Player;
/**
* @Author 坏黑
* @Since 2018-11-09 14:42
*/
public class NMSHandlerImpl extends NMSHandler {
@Override
public void sendTitle(Player player, String title, int titleFadein, int titleStay, int titleFadeout, String subtitle, int subtitleFadein, int subtitleStay, int subtitleFadeout) {
TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, new ChatComponentText(String.valueOf(title)), titleFadein, titleStay, titleFadeout));
TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, new ChatComponentText(String.valueOf(title))));
TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, new ChatComponentText(String.valueOf(subtitle)), subtitleFadein, subtitleStay, subtitleFadeout));
TPacketHandler.sendPacket(player, new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, new ChatComponentText(String.valueOf(subtitle))));
}
@Override
public void sendActionBar(Player player, String text) {
if (TabooLib.getVersionNumber() > 11100) {
TPacketHandler.sendPacket(player, new net.minecraft.server.v1_12_R1.PacketPlayOutChat(new net.minecraft.server.v1_12_R1.ChatComponentText(String.valueOf(text)), ChatMessageType.GAME_INFO));
} else {
TPacketHandler.sendPacket(player, new PacketPlayOutChat(new ChatComponentText(String.valueOf(text)), (byte) 2));
}
}
}

View File

@ -30,11 +30,7 @@ public class TPacketHandler implements Listener {
public TPacketHandler() { public TPacketHandler() {
try { try {
channelExecutor = (ChannelExecutor) SimpleVersionControl.create() channelExecutor = (ChannelExecutor) SimpleVersionControl.createNMS("me.skymc.taboolib.common.packet.channel.InternalChannelExecutor").translate().newInstance();
.from("v1_8_R3")
.target("me.skymc.taboolib.common.packet.channel.InternalChannelExecutor")
.translate()
.newInstance();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -1,7 +1,7 @@
package me.skymc.taboolib.common.pathfinder; package me.skymc.taboolib.common.pathfinder;
import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl;
import me.skymc.taboolib.common.loader.Instantiable; import me.skymc.taboolib.common.loader.Instantiable;
import me.skymc.taboolib.common.versioncontrol.SimpleVersionControl;
/** /**
* @Author sky * @Author sky
@ -15,16 +15,8 @@ public class SimpleAiSelector {
public SimpleAiSelector() { public SimpleAiSelector() {
try { try {
internalPathfinderCreator = (PathfinderCreator) SimpleVersionControl.create() internalPathfinderCreator = (PathfinderCreator) SimpleVersionControl.createNMS("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderCreator").translate().newInstance();
.from("1_8_R3") internalPathfinderExecutor = (PathfinderExecutor) SimpleVersionControl.createNMS("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderExecutor").translate().newInstance();
.target("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderCreator")
.translate()
.newInstance();
internalPathfinderExecutor = (PathfinderExecutor) SimpleVersionControl.create()
.from("1_8_R3")
.target("me.skymc.taboolib.common.pathfinder.internal.InternalPathfinderExecutor")
.translate()
.newInstance();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -11,6 +11,7 @@ import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -27,6 +28,7 @@ public class SimpleVersionControl {
private List<String> from = Lists.newArrayList(); private List<String> from = Lists.newArrayList();
private Plugin plugin; private Plugin plugin;
private boolean useCache; private boolean useCache;
private boolean useNMS;
SimpleVersionControl() { SimpleVersionControl() {
useCache = false; useCache = false;
@ -40,36 +42,69 @@ public class SimpleVersionControl {
return new SimpleVersionControl().to(toVersion).plugin(Main.getInst()); return new SimpleVersionControl().to(toVersion).plugin(Main.getInst());
} }
public SimpleVersionControl target(Class<?> target) { public static SimpleVersionControl createSimple(String target, String... from) {
this.target = target.getName(); return create().target(target).from(from);
return this;
} }
public static SimpleVersionControl createNMS(String target) {
return create().target(target).useNMS();
}
/**
* 设置转换类地址写法如me.skymc.taboolib.packet.InternalPacket
*/
public SimpleVersionControl target(String target) { public SimpleVersionControl target(String target) {
this.target = target; this.target = target;
return this; return this;
} }
/**
* 设置原版本写法如v1_8_R3
*/
public SimpleVersionControl from(String from) { public SimpleVersionControl from(String from) {
this.from.add(from.startsWith("v") ? from : "v" + from); this.from.add(from.startsWith("v") ? from : "v" + from);
return this; return this;
} }
/**
* 设置原版本写法如v1_8_R3, v1_12_R1
*/
public SimpleVersionControl from(String... from) {
Arrays.stream(from).forEach(v -> this.from.add(v.startsWith("v") ? v : "v" + v));
return this;
}
/**
* 设置目标版本
*/
public SimpleVersionControl to(String to) { public SimpleVersionControl to(String to) {
this.to = to.startsWith("v") ? to : "v" + to; this.to = to.startsWith("v") ? to : "v" + to;
return this; return this;
} }
/**
* 设置插件不填默认指向 TabooLib
*/
public SimpleVersionControl plugin(Plugin plugin) { public SimpleVersionControl plugin(Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
return this; return this;
} }
/**
* 转换类将会保存在 TabooLib 防止出现 NoClassDefFoundError 异常
*/
public SimpleVersionControl useCache() { public SimpleVersionControl useCache() {
this.useCache = true; this.useCache = true;
return this; return this;
} }
/**
* 自动转换所有使用到的 NMS OBC 方法
*/
public SimpleVersionControl useNMS() {
this.useNMS = true;
return this;
}
public Class<?> translate() throws IOException { public Class<?> translate() throws IOException {
return translate(plugin); return translate(plugin);
} }
@ -110,10 +145,12 @@ public class SimpleVersionControl {
} }
public String replace(String origin) { public String replace(String origin) {
if (useNMS) {
origin = origin.replaceAll("net/minecraft/server/.*?/", "net/minecraft/server/" + to + "/").replaceAll("org/bukkit/craftbukkit/.*?/", "org/bukkit/craftbukkit/" + to + "/");
}
for (String from : from) { for (String from : from) {
origin = origin.replace("/" + from + "/", "/" + to + "/"); origin = origin.replace("/" + from + "/", "/" + to + "/");
} }
return origin; return origin;
} }
} }

View File

@ -13,7 +13,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
@ -31,7 +30,7 @@ public class TagDataHandler implements Listener {
private static TagDataHandler handler; private static TagDataHandler handler;
private HashMap<UUID, TagPlayerData> playersData = new HashMap<>(); private HashMap<UUID, TagPlayerData> playersData = new HashMap<>();
public static void init() { static void init() {
Preconditions.checkArgument(handler == null, "TagDataHandler is already instanced!"); Preconditions.checkArgument(handler == null, "TagDataHandler is already instanced!");
handler = new TagDataHandler(); handler = new TagDataHandler();
// 注册监听 // 注册监听

View File

@ -21,7 +21,7 @@ public class ScriptHandler {
private static ScriptEngine scriptEngine; private static ScriptEngine scriptEngine;
private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); private static ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
public static void init() { static void init() {
try { try {
NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null); NashornScriptEngineFactory factory = (NashornScriptEngineFactory) scriptEngineManager.getEngineFactories().stream().filter(factories -> "Oracle Nashorn".equalsIgnoreCase(factories.getEngineName())).findFirst().orElse(null);
scriptEngine = Objects.requireNonNull(factory).getScriptEngine("-doe", "--global-per-engine"); scriptEngine = Objects.requireNonNull(factory).getScriptEngine("-doe", "--global-per-engine");

View File

@ -20,11 +20,7 @@ public class TellrawCreator {
viaVersionLoaded = Bukkit.getPluginManager().getPlugin("ViaVersion") != null; viaVersionLoaded = Bukkit.getPluginManager().getPlugin("ViaVersion") != null;
protocolSupportLoaded = Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null; protocolSupportLoaded = Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null;
try { try {
abstractTellraw = (AbstractTellraw) SimpleVersionControl.create() abstractTellraw = (AbstractTellraw) SimpleVersionControl.createNMS("me.skymc.taboolib.json.tellraw.internal.InternalTellraw").translate().newInstance();
.from("v1_8_R3")
.target("me.skymc.taboolib.json.tellraw.internal.InternalTellraw")
.translate()
.newInstance();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -16,7 +16,7 @@ public class SoundUtils {
} }
} }
static String getModifiedSound(String str) { public static String getModifiedSound(String str) {
if (TabooLib.getVerint() < 10900) { if (TabooLib.getVerint() < 10900) {
str = str.replace("BLOCK_FIRE_EXTINGUISH", "FIZZ"); str = str.replace("BLOCK_FIRE_EXTINGUISH", "FIZZ");
str = str.replace("BLOCK_NOTE_HAT", "NOTE_STICKS"); str = str.replace("BLOCK_NOTE_HAT", "NOTE_STICKS");