Updated to use WG wrapper and updated PAPI

This commit is contained in:
Zeshan Aslam 2020-08-06 18:20:31 -04:00
parent faab244c85
commit 722e5882f6
5 changed files with 26 additions and 154 deletions

View File

@ -6,7 +6,11 @@
# {displayname} will use player/mob custom name.
# {opponentlastdamage} the amount of damage the enemy last received.
# {percenthealth} displays the percentage of health left.
# Has support for PlaceholderAPI and MVdWPlaceholderAPI.
#
# For PlaceholderAPI or MVdWPlaceholderAPI:
# By default placeholders will be retrieved as the attacking player.
# Prefix with 'ATTACKEDPLAYER_' to get the placeholders as the attacked player
# i.e %player_exp% would be %ATTACKEDPLAYER_player_exp%.
Health Message: '&7&l{name}: {usestyle}'
# If set empty it will default to the one above.

View File

@ -1,6 +1,6 @@
name: ActionHealth
main: com.zeshanaslam.actionhealth.Main
version: 3.4.9
version: 3.5.0
softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard, mcMMO, MythicMobs, LangUtils]
commands:
Actionhealth:

View File

@ -1,13 +1,11 @@
package com.zeshanaslam.actionhealth;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.zeshanaslam.actionhealth.action.ActionHelper;
import com.zeshanaslam.actionhealth.action.ActionListener;
import com.zeshanaslam.actionhealth.action.ActionTask;
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;
@ -21,8 +19,7 @@ import java.util.UUID;
public class Main extends JavaPlugin {
public ConfigStore configStore;
public WorldGuardPlugin worldGuardPlugin;
public WorldGuardAPI worldGuardAPI;
public boolean worldGuardEnabled;
public HealthUtil healthUtil;
public int taskID = -1;
public boolean mcMMOEnabled;
@ -53,10 +50,7 @@ public class Main extends JavaPlugin {
// 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);
}
worldGuardEnabled = Bukkit.getServer().getPluginManager().isPluginEnabled("WorldGuard");
if (Bukkit.getServer().getPluginManager().isPluginEnabled("mcMMO")) {
mcMMOEnabled = true;

View File

@ -1,139 +0,0 @@
package com.zeshanaslam.actionhealth.support;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
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 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 worldAdaptMethod = null;
private Method regionManagerGetMethod = null;
private Constructor<?> vectorConstructor = null;
private Method vectorConstructorAsAMethodBecauseWhyNot = null;
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) {
}
}
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);
Class<?> worldEditWorldClass = Class.forName("com.sk89q.worldedit.world.World");
Class<?> worldEditAdapterClass = Class.forName("com.sk89q.worldedit.bukkit.BukkitAdapter");
worldAdaptMethod = worldEditAdapterClass.getMethod("adapt", World.class);
regionContainerGetMethod = regionContainer.getClass().getMethod("get", worldEditWorldClass);
} catch (Exception ex) {
regionContainer = null;
main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 1", ex);
main.worldGuardPlugin = null;
return;
}
} else {
regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
try {
regionContainerGetMethod = regionContainer.getClass().getMethod("get", World.class);
} catch (Exception ex) {
main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 2", ex);
main.worldGuardPlugin = null;
regionContainer = null;
return;
}
}
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;
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 List<String> getRegionNames(Location location) {
ApplicableRegionSet applicableRegionSet = getRegionSet(location);
return Objects.requireNonNull(applicableRegionSet).getRegions().stream()
.map(ProtectedRegion::getId).collect(Collectors.toList());
}
}

View File

@ -1,5 +1,6 @@
package com.zeshanaslam.actionhealth.utils;
import be.maximvdw.placeholderapi.PlaceholderAPI;
import com.zeshanaslam.actionhealth.Main;
import com.zeshanaslam.actionhealth.api.HealthSendEvent;
import com.zeshanaslam.actionhealth.support.*;
@ -13,6 +14,8 @@ import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.codemc.worldguardwrapper.WorldGuardWrapper;
import org.codemc.worldguardwrapper.region.IWrappedRegion;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
@ -102,14 +105,15 @@ public class HealthUtil {
output = output.replace("{displayname}", displayName);
// Placeholder apis
// Set placeholders as attacker
if (plugin.configStore.hasMVdWPlaceholderAPI) {
output = output.replace("ATTACKEDPLAYER_", "");
output = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, output);
}
if (plugin.configStore.hasPlaceholderAPI) {
output = output.replace("ATTACKEDPLAYER_", "");
output = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, output);
output = me.clip.placeholderapi.PlaceholderAPI.setRelationalPlaceholders(receiver, player, output);
}
} else {
if (!plugin.configStore.healthMessageOther.isEmpty()) {
@ -119,6 +123,15 @@ public class HealthUtil {
output = output.replace("{displayname}", name);
}
// Set placeholders as receiver
if (plugin.configStore.hasMVdWPlaceholderAPI) {
output = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(receiver, output);
}
if (plugin.configStore.hasPlaceholderAPI) {
output = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(receiver, output);
}
output = output.replace("{name}", name);
output = output.replace("{health}", String.valueOf((int) health));
output = output.replace("{maxhealth}", String.valueOf((int) maxHealth));
@ -304,12 +317,12 @@ public class HealthUtil {
}
public boolean isDisabled(Location location) {
if (plugin.worldGuardPlugin == null) {
if (!plugin.worldGuardEnabled) {
return false;
}
for (String regionName : plugin.worldGuardAPI.getRegionNames(location)) {
if (plugin.configStore.regions.contains(regionName)) {
for (IWrappedRegion region : WorldGuardWrapper.getInstance().getRegions(location)) {
if (plugin.configStore.regions.contains(region.getId())) {
return true;
}
}