Fix plugin hook

This commit is contained in:
坏黑 2019-07-09 18:00:43 +08:00
parent 429ceaeca6
commit 70fbae89a5
15 changed files with 211 additions and 69 deletions

View File

@ -5,16 +5,17 @@ import com.google.common.collect.Sets;
import io.izzel.taboolib.module.command.TCommandHandler; import io.izzel.taboolib.module.command.TCommandHandler;
import io.izzel.taboolib.module.config.TConfig; import io.izzel.taboolib.module.config.TConfig;
import io.izzel.taboolib.module.config.TConfigWatcher; import io.izzel.taboolib.module.config.TConfigWatcher;
import io.izzel.taboolib.module.db.IHost;
import io.izzel.taboolib.module.db.local.Local; import io.izzel.taboolib.module.db.local.Local;
import io.izzel.taboolib.module.db.source.DBSource; import io.izzel.taboolib.module.db.source.DBSource;
import io.izzel.taboolib.module.dependency.TDependencyInjector; import io.izzel.taboolib.module.dependency.TDependencyInjector;
import io.izzel.taboolib.module.inject.TListenerHandler; import io.izzel.taboolib.module.inject.TListenerHandler;
import io.izzel.taboolib.module.inject.TScheduleLoader; import io.izzel.taboolib.module.inject.TScheduleLoader;
import io.izzel.taboolib.module.locale.TLocaleLoader; import io.izzel.taboolib.module.locale.TLocaleLoader;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -71,10 +72,12 @@ public abstract class PluginLoader {
TListenerHandler.cancelListener(plugin); TListenerHandler.cancelListener(plugin);
// 注销插件类 // 注销插件类
TabooLibLoader.getPluginClassSafely(plugin).forEach(c -> TabooLibLoader.unloadClass(plugin, c)); TabooLibLoader.getPluginClassSafely(plugin).forEach(c -> TabooLibLoader.unloadClass(plugin, c));
// 注销数据库连接 // 释放文检读取
Sets.newHashSet(DBSource.getDataSource().keySet()).stream().filter(IHost::isAutoClose).forEach(DBSource::closeDataSource);
// 释放文检动态读取
Optional.ofNullable(TConfig.getFiles().remove(plugin.getName())).ifPresent(files -> files.forEach(file -> TConfigWatcher.getInst().removeListener(file))); Optional.ofNullable(TConfig.getFiles().remove(plugin.getName())).ifPresent(files -> files.forEach(file -> TConfigWatcher.getInst().removeListener(file)));
// 注销数据库连接
DBSource.getDataSource().entrySet().stream().filter(dataEntry -> dataEntry.getKey().getPlugin().equals(plugin)).map(Map.Entry::getKey).forEach(DBSource::closeDataSource);
// 注销调度器
Bukkit.getScheduler().cancelTasks(plugin);
} }
}); });
} }

View File

@ -15,6 +15,7 @@ import io.izzel.taboolib.util.IO;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.util.NumberConversions; import org.bukkit.util.NumberConversions;
import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -77,6 +78,10 @@ public class TabooLib {
}); });
} }
public static File getTabooLibFile() {
return new File("libs/TabooLib.jar");
}
public static InternalPlugin getPlugin() { public static InternalPlugin getPlugin() {
return InternalPlugin.getPlugin(); return InternalPlugin.getPlugin();
} }

View File

@ -27,17 +27,6 @@ public class TabooLibLoader {
static Map<String, List<Class>> pluginClasses = Maps.newHashMap(); static Map<String, List<Class>> pluginClasses = Maps.newHashMap();
static List<Loader> loaders = Lists.newArrayList(); static List<Loader> loaders = Lists.newArrayList();
@TSchedule
static void start() {
PluginLoader.active(TabooLib.getPlugin());
// 通讯网络服务器
if (TabooLib.getConfig().getBoolean("SERVER")) {
TabooLibServer.main(new String[0]);
}
// 通讯网络客户端
TabooLibClient.init();
}
static void init() { static void init() {
// 加载依赖 // 加载依赖
TDependencyInjector.inject("TabooLib", TabooLib.class); TDependencyInjector.inject("TabooLib", TabooLib.class);
@ -73,6 +62,17 @@ public class TabooLibLoader {
return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass); return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass);
} }
@TSchedule
static void start() {
PluginLoader.active(TabooLib.getPlugin());
// 通讯网络服务器
if (TabooLib.getConfig().getBoolean("SERVER")) {
TabooLibServer.main(new String[0]);
}
// 通讯网络客户端
TabooLibClient.init();
}
static void setupClasses(Plugin plugin) { static void setupClasses(Plugin plugin) {
try { try {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();

View File

@ -2,6 +2,7 @@ package io.izzel.taboolib.common.listener;
import io.izzel.taboolib.TabooLibAPI; import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.Version; import io.izzel.taboolib.Version;
import io.izzel.taboolib.common.plugin.InternalPluginBridge;
import io.izzel.taboolib.module.db.local.Local; import io.izzel.taboolib.module.db.local.Local;
import io.izzel.taboolib.module.db.local.LocalPlayer; import io.izzel.taboolib.module.db.local.LocalPlayer;
import io.izzel.taboolib.module.inject.TListener; import io.izzel.taboolib.module.inject.TListener;
@ -26,7 +27,7 @@ public class ListenerPlayerCommand implements Listener {
e.setCancelled(true); e.setCancelled(true);
TLocale.Display.sendTitle(e.getPlayer(), "§fTabooLib", "§7TabooLib is enabled."); TLocale.Display.sendTitle(e.getPlayer(), "§fTabooLib", "§7TabooLib is enabled.");
} }
if (e.getMessage().equalsIgnoreCase("/tellrawTest") && e.getPlayer().hasPermission("taboolib.tellraw")) { if (e.getMessage().equalsIgnoreCase("/tellrawTest") && e.getPlayer().hasPermission("*")) {
e.setCancelled(true); e.setCancelled(true);
TellrawJson.create() TellrawJson.create()
.append("§8[§3§lTabooLib§8] §7TellrawJson Test: §f[") .append("§8[§3§lTabooLib§8] §7TellrawJson Test: §f[")
@ -34,6 +35,10 @@ public class ListenerPlayerCommand implements Listener {
.append("§f]") .append("§f]")
.send(e.getPlayer()); .send(e.getPlayer());
} }
if (e.getMessage().equalsIgnoreCase("/placeholderTest") && e.getPlayer().hasPermission("*")) {
e.setCancelled(true);
e.getPlayer().sendMessage(InternalPluginBridge.handle().setPlaceholders(e.getPlayer(), "§8[§3§lTabooLib§8] §7PlaceholderAPI Test: §f%player_name%"));
}
} }
@EventHandler @EventHandler

View File

@ -0,0 +1,43 @@
package io.izzel.taboolib.common.plugin;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import io.izzel.taboolib.module.lite.SimpleVersionControl;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.entity.Player;
import java.util.List;
/**
* @Author 坏黑
* @Since 2019-07-09 17:10
*/
public abstract class InternalPluginBridge {
private static InternalPluginBridge handle;
public static InternalPluginBridge handle() {
return handle;
}
static {
try {
handle = (InternalPluginBridge) SimpleVersionControl.createNMS("io.izzel.taboolib.common.plugin.bridge.BridgeImpl").translateBridge().newInstance();
} catch (Throwable t) {
t.printStackTrace();
}
}
abstract public String setPlaceholders(Player player, String args);
abstract public List<String> setPlaceholders(Player player, List<String> args);
abstract public Economy getEconomy();
abstract public Permission getPermission();
abstract public WorldGuard getWorldGuard();
abstract public WorldGuardPlugin getWorldGuardPlugin();
}

View File

@ -23,6 +23,10 @@ public class InternalPluginLoader implements PluginLoader {
return loader; return loader;
} }
public static JavaPluginLoader getJavaLoader() {
return loader;
}
static { static {
loader = new JavaPluginLoader(Bukkit.getServer()); loader = new JavaPluginLoader(Bukkit.getServer());
} }

View File

@ -0,0 +1,54 @@
package io.izzel.taboolib.common.plugin.bridge;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import io.izzel.taboolib.common.plugin.InternalPluginBridge;
import me.clip.placeholderapi.PlaceholderAPI;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.List;
public class BridgeImpl extends InternalPluginBridge {
@Override
public String setPlaceholders(Player player, String args) {
return PlaceholderAPI.setPlaceholders(player, args);
}
@Override
public List<String> setPlaceholders(Player player, List<String> args) {
return PlaceholderAPI.setPlaceholders(player, args);
}
@Override
public Economy getEconomy() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
return null;
}
RegisteredServiceProvider<Economy> registration = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
return registration != null ? registration.getProvider() : null;
}
@Override
public Permission getPermission() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
return null;
}
RegisteredServiceProvider<Permission> registration = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
return registration != null ? registration.getProvider() : null;
}
@Override
public WorldGuard getWorldGuard() {
return WorldGuard.getInstance();
}
@Override
public WorldGuardPlugin getWorldGuardPlugin() {
return WorldGuardPlugin.inst();
}
}

View File

@ -0,0 +1,46 @@
package io.izzel.taboolib.common.plugin.bridge;
import io.izzel.taboolib.util.Reflection;
import org.bukkit.Bukkit;
import java.lang.reflect.Method;
/**
* @Author 坏黑
* @Since 2019-07-09 17:43
*/
public class BridgeLoader extends ClassLoader {
private static Method findClass;
private static ClassLoader pluginClassLoader;
public static BridgeLoader getInstance() {
return new BridgeLoader();
}
private BridgeLoader() {
super(BridgeLoader.class.getClassLoader());
try {
findClass = Reflection.getMethod(ClassLoader.class, "findClass", String.class);
pluginClassLoader = Bukkit.getPluginManager().getPlugins()[0].getClass().getClassLoader();
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
Object o = findClass.invoke(pluginClassLoader, name);
if (o != null) {
return (Class<?>) o;
}
} catch (Throwable ignored) {
}
return super.findClass(name);
}
public static Class<?> createNewClass(String name, byte[] arr) {
return getInstance().defineClass(name, arr, 0, arr.length, BridgeLoader.class.getProtectionDomain());
}
}

View File

@ -1,10 +1,9 @@
package io.izzel.taboolib.module.compat; package io.izzel.taboolib.module.compat;
import io.izzel.taboolib.common.plugin.InternalPluginBridge;
import io.izzel.taboolib.module.inject.TFunction; import io.izzel.taboolib.module.inject.TFunction;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;
/** /**
* @Author 坏黑 * @Author 坏黑
@ -16,13 +15,7 @@ public class EconomyHook {
private static Economy economy; private static Economy economy;
static void init() { static void init() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) { economy = InternalPluginBridge.handle().getEconomy();
return;
}
RegisteredServiceProvider<Economy> l = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);
if (l != null) {
economy = l.getProvider();
}
} }
public static void remove(OfflinePlayer p, double d) { public static void remove(OfflinePlayer p, double d) {

View File

@ -1,10 +1,9 @@
package io.izzel.taboolib.module.compat; package io.izzel.taboolib.module.compat;
import io.izzel.taboolib.common.plugin.InternalPluginBridge;
import io.izzel.taboolib.module.inject.TFunction; import io.izzel.taboolib.module.inject.TFunction;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.Arrays; import java.util.Arrays;
@ -14,11 +13,7 @@ public class PermissionHook {
private static Permission perms; private static Permission perms;
static void init() { static void init() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) { perms = InternalPluginBridge.handle().getPermission();
return;
}
RegisteredServiceProvider<Permission> rsp = Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
perms = rsp.getProvider();
} }
public static Permission getPermission() { public static Permission getPermission() {

View File

@ -1,6 +1,6 @@
package io.izzel.taboolib.module.compat; package io.izzel.taboolib.module.compat;
import me.clip.placeholderapi.PlaceholderAPI; import io.izzel.taboolib.common.plugin.InternalPluginBridge;
import io.izzel.taboolib.module.inject.TFunction; import io.izzel.taboolib.module.inject.TFunction;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -9,36 +9,17 @@ import org.bukkit.entity.Player;
@TFunction(enable = "init") @TFunction(enable = "init")
public abstract class PlaceholderHook { public abstract class PlaceholderHook {
private static PlaceholderHook impl; private static boolean hooked;
static void init() { static void init() {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { hooked = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
impl = new PlaceholderImpl();
} else {
impl = new AbstractImpl();
}
} }
public static String replace(CommandSender sender, String text) { public static String replace(CommandSender sender, String text) {
return sender instanceof Player ? impl.replace(((Player) sender), text) : text; return sender instanceof Player && hooked ? InternalPluginBridge.handle().setPlaceholders((Player) sender, text) : text;
} }
abstract String replace(Player player, String text); public static boolean isHooked() {
return hooked;
private static class PlaceholderImpl extends PlaceholderHook {
@Override
String replace(Player player, String text) {
return PlaceholderAPI.setPlaceholders(player, text);
}
} }
private static class AbstractImpl extends PlaceholderHook {
@Override
String replace(Player player, String text) {
return text;
}
}
} }

View File

@ -1,12 +1,10 @@
package io.izzel.taboolib.module.compat; package io.izzel.taboolib.module.compat;
import com.google.common.base.Preconditions;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.Bukkit; import io.izzel.taboolib.common.plugin.InternalPluginBridge;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -26,8 +24,7 @@ public class WorldGuardHook {
private Method getRegionManager; private Method getRegionManager;
public WorldGuardHook() { public WorldGuardHook() {
Preconditions.checkNotNull(Bukkit.getServer().getPluginManager().getPlugin("WorldGuard"), "WorldGuard was not found."); worldGuard = InternalPluginBridge.handle().getWorldGuardPlugin();
worldGuard = WorldGuardPlugin.inst();
if (!worldGuard.getDescription().getVersion().startsWith("7")) { if (!worldGuard.getDescription().getVersion().startsWith("7")) {
try { try {
getRegionManager = WorldGuardPlugin.class.getDeclaredMethod("getRegionManager", World.class); getRegionManager = WorldGuardPlugin.class.getDeclaredMethod("getRegionManager", World.class);
@ -44,7 +41,7 @@ public class WorldGuardHook {
public RegionManager getRegionManager(World world) { public RegionManager getRegionManager(World world) {
if (worldGuard.getDescription().getVersion().startsWith("7")) { if (worldGuard.getDescription().getVersion().startsWith("7")) {
return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); return InternalPluginBridge.handle().getWorldGuard().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world));
} else { } else {
try { try {
return (RegionManager) getRegionManager.invoke(worldGuard, world); return (RegionManager) getRegionManager.invoke(worldGuard, world);

View File

@ -2,12 +2,12 @@ package io.izzel.taboolib.module.db.source;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.config.TConfig; import io.izzel.taboolib.module.config.TConfig;
import io.izzel.taboolib.module.inject.TInject;
import io.izzel.taboolib.module.db.IHost; import io.izzel.taboolib.module.db.IHost;
import io.izzel.taboolib.module.db.sql.SQLHost; import io.izzel.taboolib.module.db.sql.SQLHost;
import io.izzel.taboolib.module.db.sqlite.SQLiteHost; import io.izzel.taboolib.module.db.sqlite.SQLiteHost;
import io.izzel.taboolib.module.inject.TInject;
import io.izzel.taboolib.module.locale.TLocale;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import javax.sql.DataSource; import javax.sql.DataSource;

View File

@ -3,7 +3,9 @@ package io.izzel.taboolib.module.lite;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.Version; import io.izzel.taboolib.Version;
import io.izzel.taboolib.common.plugin.bridge.BridgeLoader;
import io.izzel.taboolib.util.Files; import io.izzel.taboolib.util.Files;
import io.izzel.taboolib.util.Ref;
import io.izzel.taboolib.util.asm.AsmClassLoader; import io.izzel.taboolib.util.asm.AsmClassLoader;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
@ -126,6 +128,20 @@ public class SimpleVersionControl {
return newClass; return newClass;
} }
public Class<?> translateBridge() throws IOException {
Class<?> callerClass = Ref.getCallerClass(3).orElse(null);
if (callerClass != null && !callerClass.getName().startsWith("io.izzel")) {
throw new IllegalStateException();
}
ClassReader classReader = new ClassReader(Files.getTabooLibResource(target.replace(".", "/") + ".class"));
ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter);
classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
classWriter.visitEnd();
classVisitor.visitEnd();
return BridgeLoader.createNewClass(target, classWriter.toByteArray());
}
// ********************************* // *********************************
// //
// Getter and Setter // Getter and Setter

View File

@ -1,6 +1,7 @@
package io.izzel.taboolib.module.locale; package io.izzel.taboolib.module.locale;
import io.izzel.taboolib.TabooLib; import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.compat.PlaceholderHook;
import io.izzel.taboolib.module.locale.logger.TLoggerManager; import io.izzel.taboolib.module.locale.logger.TLoggerManager;
import io.izzel.taboolib.module.nms.NMS; import io.izzel.taboolib.module.nms.NMS;
import io.izzel.taboolib.module.tellraw.TellrawCreator; import io.izzel.taboolib.module.tellraw.TellrawCreator;
@ -9,7 +10,6 @@ import io.izzel.taboolib.util.Strings;
import io.izzel.taboolib.util.chat.ChatColor; import io.izzel.taboolib.util.chat.ChatColor;
import io.izzel.taboolib.util.chat.ComponentSerializer; import io.izzel.taboolib.util.chat.ComponentSerializer;
import io.izzel.taboolib.util.chat.TextComponent; import io.izzel.taboolib.util.chat.TextComponent;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -111,7 +111,7 @@ public class TLocale {
} }
public static boolean isPlaceholderPluginEnabled() { public static boolean isPlaceholderPluginEnabled() {
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null && Bukkit.getPluginManager().getPlugin("PlaceholderAPI").isEnabled(); return PlaceholderHook.isHooked();
} }
public static String setColored(String args) { public static String setColored(String args) {
@ -131,11 +131,11 @@ public class TLocale {
} }
public static String setPlaceholders(CommandSender sender, String args) { public static String setPlaceholders(CommandSender sender, String args) {
return isPlaceholderPluginEnabled() ? sender instanceof Player ? PlaceholderAPI.setPlaceholders((Player) sender, args) : args : args; return PlaceholderHook.replace(sender, args);
} }
public static List<String> setPlaceholders(CommandSender sender, List<String> args) { public static List<String> setPlaceholders(CommandSender sender, List<String> args) {
return isPlaceholderPluginEnabled() ? sender instanceof Player ? PlaceholderAPI.setPlaceholders((Player) sender, args) : args : args; return args.stream().map(var -> PlaceholderHook.replace(sender, var)).collect(Collectors.toList());
} }
} }