diff --git a/config.yml b/config.yml index acaf4ee..74ba355 100644 --- a/config.yml +++ b/config.yml @@ -34,18 +34,19 @@ Use Permissions: false Show Mob: true # If set to false player will not see health messages from other players. - Show Player: true + # If set to true player will get the message in a delay to get 100% accurate health. Useful for plugins that add damage by reading item lore. -# This also uses more resources :( Delay Message: false +# Amount delayed by +Delay Tick: 1 # Style Health Full Health Icon: "&4\u2764" Half Health Icon: "&c\u2764" Empty Health Icon: "&7\u2764" -# Set names. Case sensitive! +# Translate names. Case sensitive! Name Change: false Name: - Snow Golem = New name @@ -64,6 +65,7 @@ Region PvP: true Limit Health: 10 # Saves players /actionhealth toggle state. +# Uses flat file. If interested in SQL support ask in discussion. Remember Toggle: false # Blacklist by entity name or entity display name. @@ -71,7 +73,7 @@ Blacklist: - 'CCPD Officer' # Show the health of the entity that the player is looking at. -Show On Look: false +Show On Look: true Look Distance: 10 # Check if player can see entity before sending health. diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthCommand.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthCommand.class deleted file mode 100644 index 01beaf3..0000000 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthCommand.class and /dev/null differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthListeners.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthListeners.class deleted file mode 100644 index 4c25000..0000000 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthListeners.class and /dev/null differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthUtil$1.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthUtil$1.class deleted file mode 100644 index 36c6c50..0000000 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthUtil$1.class and /dev/null differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthUtil.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthUtil.class deleted file mode 100644 index 82bbdc7..0000000 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/HealthUtil.class and /dev/null differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/LookThread.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/LookThread.class index a2a446b..e345a2e 100644 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/LookThread.class and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/LookThread.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/Main.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/Main.class index 155f9a2..884ce07 100644 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/Main.class and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/Main.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/SettingsManager.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/SettingsManager.class deleted file mode 100644 index 17b9948..0000000 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/SettingsManager.class and /dev/null differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/TargetHelper.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/TargetHelper.class deleted file mode 100644 index 05953ed..0000000 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/TargetHelper.class and /dev/null differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/commands/HealthCommand.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/commands/HealthCommand.class new file mode 100644 index 0000000..2ab686f Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/commands/HealthCommand.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/config/ConfigStore.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/config/ConfigStore.class new file mode 100644 index 0000000..878d21d Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/config/ConfigStore.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/events/HealthListeners.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/events/HealthListeners.class new file mode 100644 index 0000000..7aa97eb Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/events/HealthListeners.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/PreAction.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/support/PreAction.class similarity index 79% rename from out/production/ActionHealth/com/zeshanaslam/actionhealth/PreAction.class rename to out/production/ActionHealth/com/zeshanaslam/actionhealth/support/PreAction.class index 4d72b87..ece44a1 100644 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/PreAction.class and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/support/PreAction.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/support/WorldGuardAPI.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/support/WorldGuardAPI.class new file mode 100644 index 0000000..f94e1b4 Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/support/WorldGuardAPI.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/FileHandler.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/FileHandler.class similarity index 87% rename from out/production/ActionHealth/com/zeshanaslam/actionhealth/FileHandler.class rename to out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/FileHandler.class index bb1bd27..8c4fd67 100644 Binary files a/out/production/ActionHealth/com/zeshanaslam/actionhealth/FileHandler.class and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/FileHandler.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/HealthUtil$1.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/HealthUtil$1.class new file mode 100644 index 0000000..f550210 Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/HealthUtil$1.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/HealthUtil.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/HealthUtil.class new file mode 100644 index 0000000..3a6453c Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/HealthUtil.class differ diff --git a/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/TargetHelper.class b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/TargetHelper.class new file mode 100644 index 0000000..3774f57 Binary files /dev/null and b/out/production/ActionHealth/com/zeshanaslam/actionhealth/utils/TargetHelper.class differ diff --git a/plugin.yml b/plugin.yml index f6dff39..2005eb9 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,7 +1,7 @@ name: ActionHealth main: com.zeshanaslam.actionhealth.Main -version: 3.3.0 +version: 3.3.2 softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard] commands: Actionhealth: - description: Actionhealth main command. \ No newline at end of file + description: Actionhealth main commands. \ No newline at end of file diff --git a/src/com/zeshanaslam/actionhealth/LookThread.java b/src/com/zeshanaslam/actionhealth/LookThread.java index 63c0d81..80f34f0 100644 --- a/src/com/zeshanaslam/actionhealth/LookThread.java +++ b/src/com/zeshanaslam/actionhealth/LookThread.java @@ -1,5 +1,6 @@ package com.zeshanaslam.actionhealth; +import com.zeshanaslam.actionhealth.utils.TargetHelper; import org.bukkit.Bukkit; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -31,13 +32,13 @@ public class LookThread extends BukkitRunnable { public void run() { for (Player player : Bukkit.getOnlinePlayers()) { if (plugin.toggle.contains(player.getUniqueId())) { - if (plugin.settingsManager.toggleMessage != null && !plugin.settingsManager.toggleMessage.equals("")) { - plugin.healthUtil.sendActionBar(player, plugin.settingsManager.toggleMessage.replace("{name}", player.getName())); + if (plugin.configStore.toggleMessage != null && !plugin.configStore.toggleMessage.equals("")) { + plugin.healthUtil.sendActionBar(player, plugin.configStore.toggleMessage.replace("{name}", player.getName())); } continue; } - List entities = TargetHelper.getLivingTargets(player, plugin.settingsManager.lookDistance); + List entities = TargetHelper.getLivingTargets(player, plugin.configStore.lookDistance); if (!entities.isEmpty()) { for (LivingEntity livingEntity : entities) { if (livingEntity.getType().name().equals("ARMOR_STAND")) continue; @@ -50,7 +51,7 @@ public class LookThread extends BukkitRunnable { name = livingEntity.getCustomName(); } - if (TargetHelper.canSee(player, livingEntity.getLocation(), transparentTypeIds) && !plugin.settingsManager.blacklist.contains(name) && !livingEntity.hasMetadata("NPC")) { + if (TargetHelper.canSee(player, livingEntity.getLocation(), transparentTypeIds) && !plugin.configStore.blacklist.contains(name) && !livingEntity.hasMetadata("NPC")) { plugin.healthUtil.sendHealth(player, livingEntity, livingEntity.getHealth()); break; } diff --git a/src/com/zeshanaslam/actionhealth/Main.java b/src/com/zeshanaslam/actionhealth/Main.java index 7de17cd..7c595fe 100644 --- a/src/com/zeshanaslam/actionhealth/Main.java +++ b/src/com/zeshanaslam/actionhealth/Main.java @@ -1,6 +1,11 @@ package com.zeshanaslam.actionhealth; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.zeshanaslam.actionhealth.commands.HealthCommand; +import com.zeshanaslam.actionhealth.config.ConfigStore; +import com.zeshanaslam.actionhealth.events.HealthListeners; +import com.zeshanaslam.actionhealth.support.WorldGuardAPI; +import com.zeshanaslam.actionhealth.utils.HealthUtil; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -11,8 +16,9 @@ import java.util.UUID; public class Main extends JavaPlugin { - public SettingsManager settingsManager; + public ConfigStore configStore; public WorldGuardPlugin worldGuardPlugin; + public WorldGuardAPI worldGuardAPI; public HealthUtil healthUtil; public int taskID = -1; @@ -26,7 +32,7 @@ public class Main extends JavaPlugin { this.healthUtil = new HealthUtil(this); // Load config settings - settingsManager = new SettingsManager(this); + configStore = new ConfigStore(this); // Create player folder File file = new File("plugins/ActionHealth/players/"); @@ -35,11 +41,12 @@ public class Main extends JavaPlugin { // Register listeners getServer().getPluginManager().registerEvents(new HealthListeners(this), this); - // Register command + // Register commands getCommand("Actionhealth").setExecutor(new HealthCommand(this)); if (Bukkit.getServer().getPluginManager().isPluginEnabled("WorldGuard")) { this.worldGuardPlugin = ((WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard")); + this.worldGuardAPI = new WorldGuardAPI(this); } } diff --git a/src/com/zeshanaslam/actionhealth/HealthCommand.java b/src/com/zeshanaslam/actionhealth/commands/HealthCommand.java similarity index 82% rename from src/com/zeshanaslam/actionhealth/HealthCommand.java rename to src/com/zeshanaslam/actionhealth/commands/HealthCommand.java index cc38381..2618f81 100644 --- a/src/com/zeshanaslam/actionhealth/HealthCommand.java +++ b/src/com/zeshanaslam/actionhealth/commands/HealthCommand.java @@ -1,5 +1,8 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.commands; +import com.zeshanaslam.actionhealth.Main; +import com.zeshanaslam.actionhealth.config.ConfigStore; +import com.zeshanaslam.actionhealth.utils.FileHandler; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -27,7 +30,7 @@ public class HealthCommand implements CommandExecutor { } plugin.reloadConfig(); - plugin.settingsManager = new SettingsManager(plugin); + plugin.configStore = new ConfigStore(plugin); sender.sendMessage(ChatColor.RED + "ActionHealth " + ChatColor.GRAY + "has been reloaded!"); return true; } @@ -39,14 +42,14 @@ public class HealthCommand implements CommandExecutor { if (plugin.toggle.contains(player.getUniqueId())) { plugin.toggle.remove(player.getUniqueId()); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.settingsManager.enableMessage).replace("{name}", player.getName())); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.configStore.enableMessage).replace("{name}", player.getName())); } else { plugin.toggle.add(player.getUniqueId()); - player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.settingsManager.disableMessage).replace("{name}", player.getName())); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.configStore.disableMessage).replace("{name}", player.getName())); } - if (plugin.settingsManager.rememberToggle) { + if (plugin.configStore.rememberToggle) { FileHandler fileHandler = new FileHandler("plugins/ActionHealth/players/" + player.getUniqueId() + ".yml"); fileHandler.set("toggle", plugin.toggle.contains(player.getUniqueId())); diff --git a/src/com/zeshanaslam/actionhealth/SettingsManager.java b/src/com/zeshanaslam/actionhealth/config/ConfigStore.java similarity index 93% rename from src/com/zeshanaslam/actionhealth/SettingsManager.java rename to src/com/zeshanaslam/actionhealth/config/ConfigStore.java index c93836f..19c6e66 100644 --- a/src/com/zeshanaslam/actionhealth/SettingsManager.java +++ b/src/com/zeshanaslam/actionhealth/config/ConfigStore.java @@ -1,5 +1,7 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.config; +import com.zeshanaslam.actionhealth.LookThread; +import com.zeshanaslam.actionhealth.Main; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.scheduler.BukkitTask; @@ -9,7 +11,7 @@ import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; -public class SettingsManager { +public class ConfigStore { public String healthMessage; public String healthMessageOther; @@ -17,6 +19,7 @@ public class SettingsManager { public boolean showMobs; public boolean showPlayers; public boolean delay; + public long delayTick; public boolean checkPvP; public boolean stripName; public boolean rememberToggle; @@ -41,7 +44,7 @@ public class SettingsManager { public boolean hasPlaceholderAPI; public int limitHealth; - public SettingsManager(Main plugin) { + public ConfigStore(Main plugin) { // Clear settings for reloads worlds.clear(); regions.clear(); @@ -68,6 +71,12 @@ public class SettingsManager { showMobs = plugin.getConfig().getBoolean("Show Mob"); showPlayers = plugin.getConfig().getBoolean("Show Player"); delay = plugin.getConfig().getBoolean("Delay Message"); + if (plugin.getConfig().contains("Delay Tick")) { + delayTick = plugin.getConfig().getLong("Delay Tick"); + } else { + delayTick = 1L; + } + checkPvP = plugin.getConfig().getBoolean("Region PvP"); stripName = plugin.getConfig().getBoolean("Strip Name"); filledHeartIcon = plugin.getConfig().getString("Full Health Icon"); diff --git a/src/com/zeshanaslam/actionhealth/HealthListeners.java b/src/com/zeshanaslam/actionhealth/events/HealthListeners.java similarity index 69% rename from src/com/zeshanaslam/actionhealth/HealthListeners.java rename to src/com/zeshanaslam/actionhealth/events/HealthListeners.java index af9b3fa..659131e 100644 --- a/src/com/zeshanaslam/actionhealth/HealthListeners.java +++ b/src/com/zeshanaslam/actionhealth/events/HealthListeners.java @@ -1,5 +1,7 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.events; +import com.zeshanaslam.actionhealth.Main; +import com.zeshanaslam.actionhealth.utils.FileHandler; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -21,7 +23,7 @@ public class HealthListeners implements Listener { @EventHandler(priority = EventPriority.MONITOR) public void onDamage(EntityDamageByEntityEvent event) { - if (plugin.settingsManager.checkPvP && event.isCancelled()) { + if (plugin.configStore.checkPvP && event.isCancelled()) { return; } @@ -29,14 +31,20 @@ public class HealthListeners implements Listener { return; } - if (plugin.settingsManager.worlds.contains(event.getDamager().getWorld().getName())) { + if (plugin.configStore.worlds.contains(event.getDamager().getWorld().getName())) { return; } - if (plugin.settingsManager.usePerms && !event.getDamager().hasPermission("ActionHealth.Health")) { + if (plugin.configStore.usePerms && !event.getDamager().hasPermission("ActionHealth.Health")) { return; } + // Check if the setting 'Show Player' is enabled + if (event.getEntity() instanceof Player) { + if (!plugin.configStore.showPlayers) { + return; + } + } Entity damaged = event.getEntity(); if (damaged.getType().name().equals("ARMOR_STAND")) return; @@ -47,14 +55,7 @@ public class HealthListeners implements Listener { if (projectile.getShooter() instanceof Player) { Player player = (Player) projectile.getShooter(); - // Check if the setting 'Show Player' is enabled - if (event.getEntity() instanceof Player) { - if (!plugin.settingsManager.showPlayers) { - return; - } - } - - if (!plugin.settingsManager.showMobs) { + if (!plugin.configStore.showMobs) { return; } @@ -63,9 +64,7 @@ public class HealthListeners implements Listener { } if (plugin.toggle.contains(player.getUniqueId())) { - if (plugin.settingsManager.toggleMessage != null && !plugin.settingsManager.toggleMessage.equals("")) { - plugin.healthUtil.sendActionBar(player, plugin.settingsManager.toggleMessage.replace("{name}", player.getName())); - } + sendMessage(player); return; } @@ -86,7 +85,7 @@ public class HealthListeners implements Listener { // Check if the setting 'Show Player' is enabled if (event.getEntity() instanceof Player) { - if (!plugin.settingsManager.showPlayers) { + if (!plugin.configStore.showPlayers) { return; } @@ -95,14 +94,12 @@ public class HealthListeners implements Listener { } } - if (!plugin.settingsManager.showMobs) { + if (!plugin.configStore.showMobs) { return; } if (plugin.toggle.contains(player.getUniqueId())) { - if (plugin.settingsManager.toggleMessage != null && !plugin.settingsManager.toggleMessage.equals("")) { - plugin.healthUtil.sendActionBar(player, plugin.settingsManager.toggleMessage.replace("{name}", player.getName())); - } + sendMessage(player); return; } @@ -114,11 +111,17 @@ public class HealthListeners implements Listener { } } + private void sendMessage(Player player) { + if (plugin.configStore.toggleMessage != null && !plugin.configStore.toggleMessage.equals("")) { + plugin.healthUtil.sendActionBar(player, plugin.configStore.toggleMessage.replace("{name}", player.getName())); + } + } + @EventHandler(priority = EventPriority.MONITOR) public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - if (plugin.settingsManager.rememberToggle) { + if (plugin.configStore.rememberToggle) { FileHandler fileHandler = new FileHandler("plugins/ActionHealth/players/" + player.getUniqueId() + ".yml"); if (fileHandler.getBoolean("toggle")) { @@ -131,8 +134,6 @@ public class HealthListeners implements Listener { public void onLeave(PlayerQuitEvent event) { Player player = event.getPlayer(); - if (plugin.toggle.contains(player.getUniqueId())) { - plugin.toggle.remove(player.getUniqueId()); - } + plugin.toggle.remove(player.getUniqueId()); } } diff --git a/src/com/zeshanaslam/actionhealth/PreAction.java b/src/com/zeshanaslam/actionhealth/support/PreAction.java similarity index 97% rename from src/com/zeshanaslam/actionhealth/PreAction.java rename to src/com/zeshanaslam/actionhealth/support/PreAction.java index e662af0..214f765 100644 --- a/src/com/zeshanaslam/actionhealth/PreAction.java +++ b/src/com/zeshanaslam/actionhealth/support/PreAction.java @@ -1,4 +1,4 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.support; import org.bukkit.Bukkit; import org.bukkit.entity.Player; diff --git a/src/com/zeshanaslam/actionhealth/support/WorldGuardAPI.java b/src/com/zeshanaslam/actionhealth/support/WorldGuardAPI.java new file mode 100644 index 0000000..2a41ab4 --- /dev/null +++ b/src/com/zeshanaslam/actionhealth/support/WorldGuardAPI.java @@ -0,0 +1,222 @@ +package com.zeshanaslam.actionhealth.support; + +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.domains.Association; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.association.Associables; +import com.sk89q.worldguard.protection.association.RegionAssociable; +import com.sk89q.worldguard.protection.flags.StateFlag; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.zeshanaslam.actionhealth.Main; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +import javax.annotation.Nullable; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Objects; +import java.util.logging.Level; +import java.util.stream.Collectors; + +public class WorldGuardAPI { + private final Main main; + private Object worldGuard = null; + private WorldGuardPlugin worldGuardPlugin = null; + private Object regionContainer = null; + private Method regionContainerGetMethod = null; + private Method createQueryMethod = null; + private Method regionQueryTestStateMethod = null; + private Method locationAdaptMethod = null; + private Method worldAdaptMethod = null; + private Method regionManagerGetMethod = null; + private Constructor vectorConstructor = null; + private Method vectorConstructorAsAMethodBecauseWhyNot = null; + private StateFlag buildFlag; + private StateFlag pvpFlag; + private StateFlag exitFlag; + private boolean initialized = false; + + public WorldGuardAPI(Main main) { + this.main = main; + worldGuardPlugin = main.worldGuardPlugin; + + try { + Class worldGuardClass = Class.forName("com.sk89q.worldguard.WorldGuard"); + Method getInstanceMethod = worldGuardClass.getMethod("getInstance"); + worldGuard = getInstanceMethod.invoke(null); + } catch (Exception e) { + } + } + + + protected RegionAssociable getAssociable(Player player) { + RegionAssociable associable; + if (player == null) { + associable = Associables.constant(Association.NON_MEMBER); + } else { + associable = worldGuardPlugin.wrapPlayer(player); + } + + return associable; + } + + private void initialize() { + if (!initialized) { + initialized = true; + // Super hacky reflection to deal with differences in WorldGuard 6 and 7+ + if (worldGuard != null) { + try { + Method getPlatFormMethod = worldGuard.getClass().getMethod("getPlatform"); + Object platform = getPlatFormMethod.invoke(worldGuard); + Method getRegionContainerMethod = platform.getClass().getMethod("getRegionContainer"); + regionContainer = getRegionContainerMethod.invoke(platform); + createQueryMethod = regionContainer.getClass().getMethod("createQuery"); + Class worldEditLocationClass = Class.forName("com.sk89q.worldedit.util.Location"); + Class worldEditWorldClass = Class.forName("com.sk89q.worldedit.world.World"); + Class worldEditAdapterClass = Class.forName("com.sk89q.worldedit.bukkit.BukkitAdapter"); + worldAdaptMethod = worldEditAdapterClass.getMethod("adapt", World.class); + locationAdaptMethod = worldEditAdapterClass.getMethod("adapt", Location.class); + regionContainerGetMethod = regionContainer.getClass().getMethod("get", worldEditWorldClass); + Class regionQueryClass = Class.forName("com.sk89q.worldguard.protection.regions.RegionQuery"); + regionQueryTestStateMethod = regionQueryClass.getMethod("testState", worldEditLocationClass, RegionAssociable.class, StateFlag[].class); + + Class flagsClass = Class.forName("com.sk89q.worldguard.protection.flags.Flags"); + buildFlag = (StateFlag) flagsClass.getField("BUILD").get(null); + pvpFlag = (StateFlag) flagsClass.getField("PVP").get(null); + exitFlag = (StateFlag) flagsClass.getField("EXIT").get(null); + } catch (Exception ex) { + regionContainer = null; + main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 1", ex); + main.worldGuardPlugin = null; + return; + } + } else { + regionContainer = worldGuardPlugin.getRegionContainer(); + try { + createQueryMethod = regionContainer.getClass().getMethod("createQuery"); + regionContainerGetMethod = regionContainer.getClass().getMethod("get", World.class); + Class regionQueryClass = Class.forName("com.sk89q.worldguard.bukkit.RegionQuery"); + regionQueryTestStateMethod = regionQueryClass.getMethod("testState", Location.class, RegionAssociable.class, StateFlag[].class); + + Class flagsClass = Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag"); + buildFlag = (StateFlag) flagsClass.getField("BUILD").get(null); + pvpFlag = (StateFlag) flagsClass.getField("PVP").get(null); + exitFlag = (StateFlag) flagsClass.getField("EXIT").get(null); + } catch (Exception ex) { + main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 2", ex); + main.worldGuardPlugin = null; + regionContainer = null; + return; + } + } + + // Ugh guys, API much? + try { + Class vectorClass = Class.forName("com.sk89q.worldedit.Vector"); + vectorConstructor = vectorClass.getConstructor(Double.TYPE, Double.TYPE, Double.TYPE); + regionManagerGetMethod = RegionManager.class.getMethod("getApplicableRegions", vectorClass); + } catch (Exception ex) { + try { + Class vectorClass = Class.forName("com.sk89q.worldedit.math.BlockVector3"); + vectorConstructorAsAMethodBecauseWhyNot = vectorClass.getMethod("at", Double.TYPE, Double.TYPE, Double.TYPE); + regionManagerGetMethod = RegionManager.class.getMethod("getApplicableRegions", vectorClass); + } catch (Exception sodonewiththis) { + main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 3", ex); + main.worldGuardPlugin = null; + regionContainer = null; + return; + } + } + + if (regionContainer == null) { + main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 2"); + main.worldGuardPlugin = null; + } + } + } + + @Nullable + private RegionManager getRegionManager(World world) { + initialize(); + if (regionContainer == null || regionContainerGetMethod == null) return null; + RegionManager regionManager = null; + try { + if (worldAdaptMethod != null) { + Object worldEditWorld = worldAdaptMethod.invoke(null, world); + regionManager = (RegionManager) regionContainerGetMethod.invoke(regionContainer, worldEditWorld); + } else { + regionManager = (RegionManager) regionContainerGetMethod.invoke(regionContainer, world); + } + } catch (Exception ex) { + main.getLogger().log(Level.SEVERE, "Unable to run WG lookup. SE: 1"); + } + return regionManager; + } + + @Nullable + private ApplicableRegionSet getRegionSet(Location location) { + RegionManager regionManager = getRegionManager(location.getWorld()); + if (regionManager == null) return null; + // The Location version of this method is gone in 7.0 + // Oh and then they also randomly changed the Vector class at some point without even a version bump. + // So awesome! + try { + Object vector = vectorConstructorAsAMethodBecauseWhyNot == null + ? vectorConstructor.newInstance(location.getX(), location.getY(), location.getZ()) + : vectorConstructorAsAMethodBecauseWhyNot.invoke(null, location.getX(), location.getY(), location.getZ()); + return (ApplicableRegionSet) regionManagerGetMethod.invoke(regionManager, vector); + } catch (Exception ex) { + main.getLogger().log(Level.SEVERE, "Unable to run WG lookup. SE: 2"); + } + return null; + } + + public boolean isPVPAllowed(Player player, Location location) { + if (worldGuardPlugin == null || location == null) return true; + + ApplicableRegionSet checkSet = getRegionSet(location); + if (checkSet == null) return true; + + return checkSet.queryState(getAssociable(player), pvpFlag) != StateFlag.State.DENY; + } + + public boolean isExitAllowed(Player player, Location location) { + if (worldGuardPlugin == null || location == null) return true; + + ApplicableRegionSet checkSet = getRegionSet(location); + if (checkSet == null) return true; + + return checkSet.queryState(getAssociable(player), exitFlag) != StateFlag.State.DENY; + } + + public List getRegionNames(Location location) { + ApplicableRegionSet applicableRegionSet = getRegionSet(location); + return Objects.requireNonNull(applicableRegionSet).getRegions().stream() + .map(ProtectedRegion::getId).collect(Collectors.toList()); + } + + public boolean hasBuildPermission(Player player, Block block) { + initialize(); + if (block != null && createQueryMethod != null && regionContainer != null) { + try { + boolean result; + Object query = createQueryMethod.invoke(regionContainer); + if (locationAdaptMethod != null) { + Object location = locationAdaptMethod.invoke(null, block.getLocation()); + result = (boolean) regionQueryTestStateMethod.invoke(query, location, getAssociable(player), new StateFlag[]{buildFlag}); + } else { + result = (boolean) regionQueryTestStateMethod.invoke(query, block.getLocation(), getAssociable(player), new StateFlag[]{buildFlag}); + } + return result; + } catch (Exception ex) { + main.getLogger().log(Level.SEVERE, "Unable to run WG lookup. SE: 3"); + } + } + + return true; + } +} diff --git a/src/com/zeshanaslam/actionhealth/FileHandler.java b/src/com/zeshanaslam/actionhealth/utils/FileHandler.java similarity index 99% rename from src/com/zeshanaslam/actionhealth/FileHandler.java rename to src/com/zeshanaslam/actionhealth/utils/FileHandler.java index e4cf3f1..d027d2f 100644 --- a/src/com/zeshanaslam/actionhealth/FileHandler.java +++ b/src/com/zeshanaslam/actionhealth/utils/FileHandler.java @@ -1,4 +1,4 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.utils; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfigurationOptions; diff --git a/src/com/zeshanaslam/actionhealth/HealthUtil.java b/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java similarity index 68% rename from src/com/zeshanaslam/actionhealth/HealthUtil.java rename to src/com/zeshanaslam/actionhealth/utils/HealthUtil.java index 03817d6..436e58a 100644 --- a/src/com/zeshanaslam/actionhealth/HealthUtil.java +++ b/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java @@ -1,10 +1,7 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.utils; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldguard.protection.ApplicableRegionSet; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; -import com.sk89q.worldguard.protection.regions.RegionContainer; -import com.sk89q.worldguard.protection.regions.RegionQuery; +import com.zeshanaslam.actionhealth.Main; +import com.zeshanaslam.actionhealth.support.PreAction; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.LivingEntity; @@ -24,7 +21,7 @@ public class HealthUtil { } public void sendHealth(Player receiver, LivingEntity entity, double health) { - if (plugin.settingsManager.canSee) { + if (plugin.configStore.canSee) { if (entity instanceof Player) { Player player = (Player) entity; @@ -35,7 +32,7 @@ public class HealthUtil { } } - if (plugin.settingsManager.spectatorMode) { + if (plugin.configStore.spectatorMode) { if (entity instanceof Player) { Player player = (Player) entity; @@ -47,13 +44,13 @@ public class HealthUtil { } } - if (plugin.settingsManager.invisiblePotion) { + if (plugin.configStore.invisiblePotion) { if (entity.hasPotionEffect(PotionEffectType.INVISIBILITY)) { return; } } - if (plugin.settingsManager.delay) { + if (plugin.configStore.delay) { new BukkitRunnable() { public void run() { @@ -62,7 +59,7 @@ public class HealthUtil { if (output != null) sendActionBar(receiver, output); } - }.runTaskLater(plugin, 1L); + }.runTaskLater(plugin, plugin.configStore.delayTick); } else { String output = getOutput(health, receiver, entity); @@ -83,13 +80,13 @@ public class HealthUtil { name = entity.getCustomName(); } - if (plugin.settingsManager.blacklist.contains(name)) return null; + if (plugin.configStore.blacklist.contains(name)) return null; - if (plugin.settingsManager.stripName) name = ChatColor.stripColor(name); - if (plugin.settingsManager.translate.containsKey(entity.getName())) - name = plugin.settingsManager.translate.get(entity.getName()); + if (plugin.configStore.stripName) name = ChatColor.stripColor(name); + if (plugin.configStore.translate.containsKey(entity.getName())) + name = plugin.configStore.translate.get(entity.getName()); - String output = plugin.settingsManager.healthMessage; + String output = plugin.configStore.healthMessage; if (entity instanceof Player) { String displayName; @@ -104,17 +101,17 @@ public class HealthUtil { output = output.replace("{displayname}", displayName); // Placeholder apis - if (plugin.settingsManager.hasMVdWPlaceholderAPI) { + if (plugin.configStore.hasMVdWPlaceholderAPI) { output = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, output); } - if (plugin.settingsManager.hasPlaceholderAPI) { + if (plugin.configStore.hasPlaceholderAPI) { output = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, output); output = me.clip.placeholderapi.PlaceholderAPI.setRelationalPlaceholders(receiver, player, output); } } else { - if (!plugin.settingsManager.healthMessageOther.isEmpty()) { - output = plugin.settingsManager.healthMessageOther; + if (!plugin.configStore.healthMessageOther.isEmpty()) { + output = plugin.configStore.healthMessageOther; } output = output.replace("{displayname}", name); @@ -126,17 +123,17 @@ public class HealthUtil { if (output.contains("{usestyle}")) { StringBuilder style = new StringBuilder(); - int left = plugin.settingsManager.limitHealth; - double heart = maxHealth / plugin.settingsManager.limitHealth; + int left = plugin.configStore.limitHealth; + double heart = maxHealth / plugin.configStore.limitHealth; double halfHeart = heart / 2; double tempHealth = health; if (maxHealth != health && health >= 0 && !entity.isDead()) { - for (int i = 0; i < plugin.settingsManager.limitHealth; i++) { + for (int i = 0; i < plugin.configStore.limitHealth; i++) { if (tempHealth - heart > 0) { tempHealth = tempHealth - heart; - style.append(plugin.settingsManager.filledHeartIcon); + style.append(plugin.configStore.filledHeartIcon); left--; } else { break; @@ -144,21 +141,21 @@ public class HealthUtil { } if (tempHealth > halfHeart) { - style.append(plugin.settingsManager.filledHeartIcon); + style.append(plugin.configStore.filledHeartIcon); left--; } else if (tempHealth > 0 && tempHealth <= halfHeart) { - style.append(plugin.settingsManager.halfHeartIcon); + style.append(plugin.configStore.halfHeartIcon); left--; } } if (maxHealth != health) { for (int i = 0; i < left; i++) { - style.append(plugin.settingsManager.emptyHeartIcon); + style.append(plugin.configStore.emptyHeartIcon); } } else { for (int i = 0; i < left; i++) { - style.append(plugin.settingsManager.filledHeartIcon); + style.append(plugin.configStore.filledHeartIcon); } } @@ -172,17 +169,17 @@ public class HealthUtil { message = ChatColor.translateAlternateColorCodes('&', message); try { - if (plugin.settingsManager.mcVersion.equals("v1_12_R1") || plugin.settingsManager.mcVersion.startsWith("v1_13") || plugin.settingsManager.mcVersion.startsWith("v1_14_")) { + if (plugin.configStore.mcVersion.equals("v1_12_R1") || plugin.configStore.mcVersion.startsWith("v1_13") || plugin.configStore.mcVersion.startsWith("v1_14_")) { new PreAction(player, message); - } else if (!(plugin.settingsManager.mcVersion.equalsIgnoreCase("v1_8_R1") || (plugin.settingsManager.mcVersion.contains("v1_7_")))) { - Class c1 = Class.forName("org.bukkit.craftbukkit." + plugin.settingsManager.mcVersion + ".entity.CraftPlayer"); + } else if (!(plugin.configStore.mcVersion.equalsIgnoreCase("v1_8_R1") || (plugin.configStore.mcVersion.contains("v1_7_")))) { + Class c1 = Class.forName("org.bukkit.craftbukkit." + plugin.configStore.mcVersion + ".entity.CraftPlayer"); Object p = c1.cast(player); Object ppoc; - Class c4 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".PacketPlayOutChat"); - Class c5 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".Packet"); + Class c4 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".PacketPlayOutChat"); + Class c5 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".Packet"); - Class c2 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".ChatComponentText"); - Class c3 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".IChatBaseComponent"); + Class c2 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".ChatComponentText"); + Class c3 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".IChatBaseComponent"); Object o = c2.getConstructor(new Class[]{String.class}).newInstance(message); ppoc = c4.getConstructor(new Class[]{c3, byte.class}).newInstance(o, (byte) 2); @@ -195,14 +192,14 @@ public class HealthUtil { Method sendPacket = playerConnection.getClass().getDeclaredMethod("sendPacket", c5); sendPacket.invoke(playerConnection, ppoc); } else { - Class c1 = Class.forName("org.bukkit.craftbukkit." + plugin.settingsManager.mcVersion + ".entity.CraftPlayer"); + Class c1 = Class.forName("org.bukkit.craftbukkit." + plugin.configStore.mcVersion + ".entity.CraftPlayer"); Object p = c1.cast(player); Object ppoc; - Class c4 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".PacketPlayOutChat"); - Class c5 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".Packet"); + Class c4 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".PacketPlayOutChat"); + Class c5 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".Packet"); - Class c2 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".ChatSerializer"); - Class c3 = Class.forName("net.minecraft.server." + plugin.settingsManager.mcVersion + ".IChatBaseComponent"); + Class c2 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".ChatSerializer"); + Class c3 = Class.forName("net.minecraft.server." + plugin.configStore.mcVersion + ".IChatBaseComponent"); Method m3 = c2.getDeclaredMethod("a", String.class); Object cbc = c3.cast(m3.invoke(c2, "{\"text\": \"" + message + "\"}")); ppoc = c4.getConstructor(new Class[]{c3, byte.class}).newInstance(cbc, (byte) 2); @@ -226,11 +223,8 @@ public class HealthUtil { return false; } - RegionContainer regionContainer = com.sk89q.worldguard.WorldGuard.getInstance().getPlatform().getRegionContainer(); - RegionQuery regionQuery = regionContainer.createQuery(); - ApplicableRegionSet applicableRegions = regionQuery.getApplicableRegions(BukkitAdapter.adapt(location)); - for (ProtectedRegion region : applicableRegions) { - if (plugin.settingsManager.regions.contains(region.getId())) { + for (String regionName : plugin.worldGuardAPI.getRegionNames(location)) { + if (plugin.configStore.regions.contains(regionName)) { return true; } } diff --git a/src/com/zeshanaslam/actionhealth/TargetHelper.java b/src/com/zeshanaslam/actionhealth/utils/TargetHelper.java similarity index 98% rename from src/com/zeshanaslam/actionhealth/TargetHelper.java rename to src/com/zeshanaslam/actionhealth/utils/TargetHelper.java index 049c875..6fb63c2 100644 --- a/src/com/zeshanaslam/actionhealth/TargetHelper.java +++ b/src/com/zeshanaslam/actionhealth/utils/TargetHelper.java @@ -1,7 +1,6 @@ -package com.zeshanaslam.actionhealth; +package com.zeshanaslam.actionhealth.utils; import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; @@ -47,6 +46,10 @@ public class TargetHelper { * @return all entities in the player's vision line */ public static List getLivingTargets(LivingEntity source, double range, double tolerance) { + if (source == null) { + return new ArrayList<>(); + } + List list = source.getNearbyEntities(range, range, range); List targets = new ArrayList(); @@ -157,6 +160,8 @@ public class TargetHelper { * @return true if the target is in front of the entity */ public static boolean isInFront(Entity entity, Entity target) { + if (entity.getWorld() != target.getWorld()) + return false; // Get the necessary vectors Vector facing = entity.getLocation().getDirection();