Fix plugin hook

This commit is contained in:
坏黑 2019-07-09 20:00:16 +08:00
parent 70fbae89a5
commit 59a3b609ca
10 changed files with 223 additions and 195 deletions

View File

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

View File

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

View File

@ -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> T getRegisteredService(Class<? extends T> clazz);
abstract public String setPlaceholders(Player player, String args);
abstract public List<String> setPlaceholders(Player player, List<String> 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<String> worldguardGetRegions(World world);
abstract public List<String> worldguardGetRegion(World world, Location location);
abstract public boolean economyHooked();
abstract public boolean permissionHooked();
abstract public boolean placeholderHooked();
abstract public boolean worldguardHooked();
}

View File

@ -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> T getRegisteredService(Class<? extends T> 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<String> setPlaceholders(Player player, List<String> 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<String> worldguardGetRegions(World world) {
return worldguardRegionManager(world).getRegions().keySet();
}
@Override
public List<String> 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<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;
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;
}
}

View File

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

View File

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

View File

@ -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<String> replace(CommandSender sender, List<String> text) {
return sender instanceof Player ? InternalPluginBridge.handle().setPlaceholders((Player) sender, text) : text;
}
public static boolean isHooked() {
return hooked;
return InternalPluginBridge.handle().placeholderHooked();
}
}

View File

@ -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<String> getRegionIDs(World world) {
return getRegionManager(world).getRegions().keySet();
}
public Collection<ProtectedRegion> getRegions(World world) {
return getRegionManager(world).getRegions().values();
}
public List<String> 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());
}
}

View File

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

View File

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