From 59a3b609cac5f47ef437512f2c490666f007ca77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Tue, 9 Jul 2019 20:00:16 +0800 Subject: [PATCH] Fix plugin hook --- .../scala/io/izzel/taboolib/TabooLibAPI.java | 44 ++---- .../listener/ListenerPlayerCommand.java | 4 +- .../common/plugin/InternalPluginBridge.java | 39 +++-- .../common/plugin/bridge/BridgeImpl.java | 134 +++++++++++++++--- .../taboolib/module/compat/EconomyHook.java | 25 +--- .../module/compat/PermissionHook.java | 27 ++-- .../module/compat/PlaceholderHook.java | 19 ++- .../module/compat/WorldGuardHook.java | 84 ----------- .../io/izzel/taboolib/util/Commands.java | 38 +++++ .../izzel/tlibscala/runtime/RichPlayer.scala | 4 +- 10 files changed, 223 insertions(+), 195 deletions(-) delete mode 100644 src/main/scala/io/izzel/taboolib/module/compat/WorldGuardHook.java create mode 100644 src/main/scala/io/izzel/taboolib/util/Commands.java diff --git a/src/main/scala/io/izzel/taboolib/TabooLibAPI.java b/src/main/scala/io/izzel/taboolib/TabooLibAPI.java index fb0bffa..c5fc38c 100644 --- a/src/main/scala/io/izzel/taboolib/TabooLibAPI.java +++ b/src/main/scala/io/izzel/taboolib/TabooLibAPI.java @@ -1,15 +1,13 @@ package io.izzel.taboolib; +import io.izzel.taboolib.common.plugin.InternalPluginBridge; import io.izzel.taboolib.module.db.local.Local; import io.izzel.taboolib.module.nms.NMS; -import io.izzel.taboolib.util.Strings; import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.plugin.Plugin; +import java.util.Arrays; + /** * @Author 坏黑 * @Since 2019-07-05 14:31 @@ -29,6 +27,10 @@ public class TabooLibAPI { } } + public static InternalPluginBridge getPluginBridge() { + return InternalPluginBridge.handle(); + } + public static boolean isBukkit() { return bukkit; } @@ -49,7 +51,7 @@ public class TabooLibAPI { return Local.get().get("data").getBoolean("debug"); } - public static void setDebug(boolean debug) { + public static void debug(boolean debug) { Local.get().get("data").set("debug", debug); } @@ -58,34 +60,8 @@ public class TabooLibAPI { } public static void debug(Plugin plugin, String... args) { - if (!isDebug()) { - return; + if (isDebug()) { + Arrays.stream(args).forEach(line -> Bukkit.getConsoleSender().sendMessage("§4[" + plugin.getName() + "][DEBUG] §c" + line)); } - for (String line : args) { - Bukkit.getConsoleSender().sendMessage("§4[" + plugin.getName() + "][DEBUG] §c" + line); - } - } - - public static boolean dispatchCommand(CommandSender sender, String command) { - try { - if ((sender instanceof Player)) { - PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent((Player) sender, "/" + command); - Bukkit.getPluginManager().callEvent(e); - if (e.isCancelled() || Strings.isBlank(e.getMessage()) || !e.getMessage().startsWith("/")) { - return false; - } - return Bukkit.dispatchCommand(e.getPlayer(), e.getMessage().substring(1)); - } else { - ServerCommandEvent e = new ServerCommandEvent(sender, command); - Bukkit.getPluginManager().callEvent(e); - if (e.isCancelled() || Strings.isBlank(e.getCommand())) { - return false; - } - return Bukkit.dispatchCommand(e.getSender(), e.getCommand()); - } - } catch (Exception e) { - e.printStackTrace(); - } - return false; } } diff --git a/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerCommand.java b/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerCommand.java index 92be5fb..c6c345a 100644 --- a/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerCommand.java +++ b/src/main/scala/io/izzel/taboolib/common/listener/ListenerPlayerCommand.java @@ -55,10 +55,10 @@ public class ListenerPlayerCommand implements Listener { e.setCancelled(true); } if (TabooLibAPI.isDebug()) { - TabooLibAPI.setDebug(false); + TabooLibAPI.debug(false); TLogger.getGlobalLogger().info("&cDisabled."); } else { - TabooLibAPI.setDebug(true); + TabooLibAPI.debug(true); TLogger.getGlobalLogger().info("&aEnabled."); } } 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 f33223f..ec3e956 100644 --- a/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java +++ b/src/main/scala/io/izzel/taboolib/common/plugin/InternalPluginBridge.java @@ -1,12 +1,13 @@ package io.izzel.taboolib.common.plugin; -import com.sk89q.worldguard.WorldGuard; -import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.managers.RegionManager; import io.izzel.taboolib.module.lite.SimpleVersionControl; -import net.milkbowl.vault.economy.Economy; -import net.milkbowl.vault.permission.Permission; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.entity.Player; +import java.util.Collection; import java.util.List; /** @@ -29,15 +30,37 @@ public abstract class InternalPluginBridge { } } + abstract public T getRegisteredService(Class clazz); + abstract public String setPlaceholders(Player player, String args); abstract public List setPlaceholders(Player player, List args); - abstract public Economy getEconomy(); + abstract public void economyCreate(OfflinePlayer p); - abstract public Permission getPermission(); + abstract public void economyTake(OfflinePlayer p, double d); - abstract public WorldGuard getWorldGuard(); + abstract public void economyGive(OfflinePlayer p, double d); - abstract public WorldGuardPlugin getWorldGuardPlugin(); + abstract public double economyLook(OfflinePlayer p); + + abstract public void permissionAdd(Player player, String perm); + + abstract public void permissionRemove(Player player, String perm); + + abstract public boolean permissionHas(Player player, String perm); + + abstract public RegionManager worldguardRegionManager(World world); + + abstract public Collection worldguardGetRegions(World world); + + abstract public List worldguardGetRegion(World world, Location location); + + abstract public boolean economyHooked(); + + abstract public boolean permissionHooked(); + + abstract public boolean placeholderHooked(); + + abstract public boolean worldguardHooked(); } 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 c6539ba..68cfdac 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,54 +1,156 @@ package io.izzel.taboolib.common.plugin.bridge; +import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.protection.managers.RegionManager; +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 net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; +import java.lang.reflect.Method; +import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; public class BridgeImpl extends InternalPluginBridge { + private Object economy; + private Object permission; + private Method getRegionManager; + private boolean placeholder; + private boolean worldguard; + + public BridgeImpl() { + if (Bukkit.getPluginManager().getPlugin("Vault") != null) { + economy = getRegisteredService(Economy.class); + permission = getRegisteredService(Permission.class); + } + if (Bukkit.getPluginManager().getPlugin("WorldGuard") != null) { + if (!WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) { + try { + getRegionManager = Reflection.getMethod(WorldGuardPlugin.class, "worldguardRegionManager", World.class); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + } + worldguard = true; + } + placeholder = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null; + } + + @Override + public T getRegisteredService(Class clazz) { + RegisteredServiceProvider registeredServiceProvider = Bukkit.getServer().getServicesManager().getRegistration(clazz); + return (T) registeredServiceProvider.getProvider(); + } + @Override public String setPlaceholders(Player player, String args) { - return PlaceholderAPI.setPlaceholders(player, args); + return placeholder ? PlaceholderAPI.setPlaceholders(player, args) : args; } @Override public List setPlaceholders(Player player, List args) { - return PlaceholderAPI.setPlaceholders(player, args); + return placeholder ? PlaceholderAPI.setPlaceholders(player, args) : args; } @Override - public Economy getEconomy() { - if (Bukkit.getPluginManager().getPlugin("Vault") == null) { + public void economyCreate(OfflinePlayer p) { + if (economy instanceof Economy) { + ((Economy) economy).createPlayerAccount(p); + } + } + + @Override + public void economyTake(OfflinePlayer p, double d) { + if (economy instanceof Economy) { + ((Economy) economy).withdrawPlayer(p, d); + } + } + + @Override + public void economyGive(OfflinePlayer p, double d) { + if (economy instanceof Economy) { + ((Economy) economy).depositPlayer(p, d); + } + } + + @Override + public double economyLook(OfflinePlayer p) { + return economy instanceof Economy ? ((Economy) economy).getBalance(p) : 0; + } + + @Override + public void permissionAdd(Player player, String perm) { + if (permission instanceof Permission) { + ((Permission) permission).playerAdd(player, perm); + } + } + + @Override + public void permissionRemove(Player player, String perm) { + if (permission instanceof Permission) { + ((Permission) permission).playerRemove(player, perm); + } + } + + @Override + public boolean permissionHas(Player player, String perm) { + return permission instanceof Permission && ((Permission) permission).playerHas(player, perm); + } + + @Override + public Collection worldguardGetRegions(World world) { + return worldguardRegionManager(world).getRegions().keySet(); + } + + @Override + public List worldguardGetRegion(World world, Location location) { + return worldguardRegionManager(world).getRegions().values().stream().filter(r -> r.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ())).map(ProtectedRegion::getId).collect(Collectors.toList()); + } + + @Override + public RegionManager worldguardRegionManager(World world) { + if (WorldGuardPlugin.inst().getDescription().getVersion().startsWith("7")) { + return WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); + } else { + try { + return (RegionManager) getRegionManager.invoke(WorldGuardPlugin.inst(), world); + } catch (Throwable t) { + t.printStackTrace(); + } return null; } - RegisteredServiceProvider 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 registration = Bukkit.getServer().getServicesManager().getRegistration(Permission.class); - return registration != null ? registration.getProvider() : null; + public boolean economyHooked() { + return economy != null; } @Override - public WorldGuard getWorldGuard() { - return WorldGuard.getInstance(); + public boolean permissionHooked() { + return permission != null; } @Override - public WorldGuardPlugin getWorldGuardPlugin() { - return WorldGuardPlugin.inst(); + public boolean placeholderHooked() { + return placeholder; } + + @Override + public boolean worldguardHooked() { + return worldguard; + } + } \ No newline at end of file diff --git a/src/main/scala/io/izzel/taboolib/module/compat/EconomyHook.java b/src/main/scala/io/izzel/taboolib/module/compat/EconomyHook.java index 07c992f..c9f32ee 100644 --- a/src/main/scala/io/izzel/taboolib/module/compat/EconomyHook.java +++ b/src/main/scala/io/izzel/taboolib/module/compat/EconomyHook.java @@ -1,29 +1,20 @@ package io.izzel.taboolib.module.compat; import io.izzel.taboolib.common.plugin.InternalPluginBridge; -import io.izzel.taboolib.module.inject.TFunction; -import net.milkbowl.vault.economy.Economy; import org.bukkit.OfflinePlayer; /** * @Author 坏黑 * @Since 2019-07-05 18:50 */ -@TFunction(enable = "init") public class EconomyHook { - private static Economy economy; - - static void init() { - economy = InternalPluginBridge.handle().getEconomy(); + public static void add(OfflinePlayer p, double d) { + InternalPluginBridge.handle().economyGive(p, d); } public static void remove(OfflinePlayer p, double d) { - economy.withdrawPlayer(p, d); - } - - public static void add(OfflinePlayer p, double d) { - economy.depositPlayer(p, d); + InternalPluginBridge.handle().economyTake(p, d); } public static void set(OfflinePlayer p, double d) { @@ -31,18 +22,14 @@ public class EconomyHook { } public static double get(OfflinePlayer p) { - return economy.getBalance(p); + return InternalPluginBridge.handle().economyLook(p); } public static void create(OfflinePlayer p) { - economy.createPlayerAccount(p); + InternalPluginBridge.handle().economyCreate(p); } public static boolean exists() { - return economy != null; - } - - public static net.milkbowl.vault.economy.Economy getEconomy() { - return economy; + return InternalPluginBridge.handle().economyHooked(); } } diff --git a/src/main/scala/io/izzel/taboolib/module/compat/PermissionHook.java b/src/main/scala/io/izzel/taboolib/module/compat/PermissionHook.java index 3ccc7ae..cc7b1f5 100644 --- a/src/main/scala/io/izzel/taboolib/module/compat/PermissionHook.java +++ b/src/main/scala/io/izzel/taboolib/module/compat/PermissionHook.java @@ -1,34 +1,23 @@ package io.izzel.taboolib.module.compat; import io.izzel.taboolib.common.plugin.InternalPluginBridge; -import io.izzel.taboolib.module.inject.TFunction; -import net.milkbowl.vault.permission.Permission; import org.bukkit.entity.Player; -import java.util.Arrays; - -@TFunction(enable = "init") public class PermissionHook { - private static Permission perms; - - static void init() { - perms = InternalPluginBridge.handle().getPermission(); + public static void add(Player player, String perm) { + InternalPluginBridge.handle().permissionAdd(player, perm); } - public static Permission getPermission() { - return perms; + public static void remove(Player player, String perm) { + InternalPluginBridge.handle().permissionAdd(player, perm); } - public static void addPermission(Player player, String perm) { - perms.playerAdd(player, perm); + public static boolean has(Player player, String perm) { + return InternalPluginBridge.handle().permissionHas(player, perm); } - public static void removePermission(Player player, String perm) { - perms.playerRemove(player, perm); - } - - public static boolean hasPermission(Player player, String perm) { - return perms.playerHas(player, perm) || Arrays.stream(perms.getPlayerGroups(player)).anyMatch(group -> perms.groupHas(player.getWorld(), group, perm)); + public static boolean exists() { + return InternalPluginBridge.handle().permissionHooked(); } } diff --git a/src/main/scala/io/izzel/taboolib/module/compat/PlaceholderHook.java b/src/main/scala/io/izzel/taboolib/module/compat/PlaceholderHook.java index a778ba9..7f790d7 100644 --- a/src/main/scala/io/izzel/taboolib/module/compat/PlaceholderHook.java +++ b/src/main/scala/io/izzel/taboolib/module/compat/PlaceholderHook.java @@ -1,25 +1,22 @@ package io.izzel.taboolib.module.compat; import io.izzel.taboolib.common.plugin.InternalPluginBridge; -import io.izzel.taboolib.module.inject.TFunction; -import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -@TFunction(enable = "init") -public abstract class PlaceholderHook { +import java.util.List; - private static boolean hooked; - - static void init() { - hooked = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null; - } +public class PlaceholderHook { public static String replace(CommandSender sender, String text) { - return sender instanceof Player && hooked ? InternalPluginBridge.handle().setPlaceholders((Player) sender, text) : text; + return sender instanceof Player ? InternalPluginBridge.handle().setPlaceholders((Player) sender, text) : text; + } + + public static List replace(CommandSender sender, List text) { + return sender instanceof Player ? InternalPluginBridge.handle().setPlaceholders((Player) sender, text) : text; } public static boolean isHooked() { - return hooked; + return InternalPluginBridge.handle().placeholderHooked(); } } diff --git a/src/main/scala/io/izzel/taboolib/module/compat/WorldGuardHook.java b/src/main/scala/io/izzel/taboolib/module/compat/WorldGuardHook.java deleted file mode 100644 index ffb07ae..0000000 --- a/src/main/scala/io/izzel/taboolib/module/compat/WorldGuardHook.java +++ /dev/null @@ -1,84 +0,0 @@ -package io.izzel.taboolib.module.compat; - -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.protection.managers.RegionManager; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; -import io.izzel.taboolib.common.plugin.InternalPluginBridge; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.entity.Player; - -import java.lang.reflect.Method; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; - -/** - * @Author AgarthaLib - */ -public class WorldGuardHook { - - public static final WorldGuardHook INSTANCE = new WorldGuardHook(); - private WorldGuardPlugin worldGuard; - private Method getRegionManager; - - public WorldGuardHook() { - worldGuard = InternalPluginBridge.handle().getWorldGuardPlugin(); - if (!worldGuard.getDescription().getVersion().startsWith("7")) { - try { - getRegionManager = WorldGuardPlugin.class.getDeclaredMethod("getRegionManager", World.class); - getRegionManager.setAccessible(true); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - } - } - - public WorldGuardPlugin getWorldGuard() { - return this.worldGuard; - } - - public RegionManager getRegionManager(World world) { - if (worldGuard.getDescription().getVersion().startsWith("7")) { - return InternalPluginBridge.handle().getWorldGuard().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); - } else { - try { - return (RegionManager) getRegionManager.invoke(worldGuard, world); - } catch (Throwable t) { - t.printStackTrace(); - } - return null; - } - } - - public boolean isRegionManagerExists(World world) { - return this.getRegionManager(world) != null; - } - - public Collection getRegionIDs(World world) { - return getRegionManager(world).getRegions().keySet(); - } - - public Collection getRegions(World world) { - return getRegionManager(world).getRegions().values(); - } - - public List getRegionsAtLocation(World world, Location location) { - return getRegions(world).stream().filter(protectedRegion -> protectedRegion.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ())).map(ProtectedRegion::getId).collect(Collectors.toList()); - } - - public ProtectedRegion getRegion(World world, String id) { - RegionManager regionManager = this.getRegionManager(world); - return regionManager != null ? regionManager.getRegions().keySet().stream().filter(key -> key.equalsIgnoreCase(id)).findFirst().map(regionManager::getRegion).orElse(null) : null; - } - - public boolean isRegionExists(World world, String id) { - return this.getRegion(world, id) != null; - } - - public boolean isPlayerInsideRegion(ProtectedRegion region, Player player) { - Location location = player.getLocation(); - return region.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ()); - } -} diff --git a/src/main/scala/io/izzel/taboolib/util/Commands.java b/src/main/scala/io/izzel/taboolib/util/Commands.java new file mode 100644 index 0000000..485be7a --- /dev/null +++ b/src/main/scala/io/izzel/taboolib/util/Commands.java @@ -0,0 +1,38 @@ +package io.izzel.taboolib.util; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.server.ServerCommandEvent; + +/** + * @Author 坏黑 + * @Since 2019-07-09 19:40 + */ +public class Commands { + + public static boolean dispatchCommand(CommandSender sender, String command) { + try { + if ((sender instanceof Player)) { + PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent((Player) sender, "/" + command); + Bukkit.getPluginManager().callEvent(e); + if (e.isCancelled() || Strings.isBlank(e.getMessage()) || !e.getMessage().startsWith("/")) { + return false; + } + return Bukkit.dispatchCommand(e.getPlayer(), e.getMessage().substring(1)); + } else { + ServerCommandEvent e = new ServerCommandEvent(sender, command); + Bukkit.getPluginManager().callEvent(e); + if (e.isCancelled() || Strings.isBlank(e.getCommand())) { + return false; + } + return Bukkit.dispatchCommand(e.getSender(), e.getCommand()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + +} diff --git a/src/main/scala/io/izzel/tlibscala/runtime/RichPlayer.scala b/src/main/scala/io/izzel/tlibscala/runtime/RichPlayer.scala index bc844f2..8e378f4 100644 --- a/src/main/scala/io/izzel/tlibscala/runtime/RichPlayer.scala +++ b/src/main/scala/io/izzel/tlibscala/runtime/RichPlayer.scala @@ -15,9 +15,9 @@ class RichPlayer(private val player: Player) extends RichOfflinePlayer(player) { def displaySidebarUnranked(title: String, elements: String*): Unit = Scoreboards.display(player, elements: _*) - def addPermission(perm: String): Unit = PermissionHook.addPermission(player, perm) + def addPermission(perm: String): Unit = PermissionHook.add(player, perm) - def removePermission(perm: String): Unit = PermissionHook.removePermission(player, perm) + def removePermission(perm: String): Unit = PermissionHook.remove(player, perm) def sendTitle(title: String, subtitle: String, fadein: Int, stay: Int, fadeout: Int): Unit = TLocale.Display.sendTitle(player, title, subtitle, fadein, stay, fadeout)