Added mcMMO health bar support

This commit is contained in:
Zeshan Aslam 2019-08-04 14:59:35 -04:00
parent a4c2d0aa29
commit 8c238ea3d6
18 changed files with 92 additions and 103 deletions

View File

@ -49,14 +49,14 @@ Empty Health Icon: "&7\u2764"
# Translate names. Case sensitive! # Translate names. Case sensitive!
Name Change: false Name Change: false
Name: Name:
- Snow Golem = New name - Snow Golem = New name
# ActionHealth will be disabled for any world names added below. Case sensitive! # ActionHealth will be disabled for any world names added below. Case sensitive!
Disabled worlds: Disabled worlds:
- randomworld - randomworld
Disabled regions: Disabled regions:
- testing_region - testing_region
# Disable ActionHealth in regions where PvP is denied. # Disable ActionHealth in regions where PvP is denied.
Region PvP: true Region PvP: true
@ -70,7 +70,7 @@ Remember Toggle: false
# Blacklist by entity name or entity display name. # Blacklist by entity name or entity display name.
Blacklist: Blacklist:
- 'CCPD Officer' - 'CCPD Officer'
# Show the health of the entity that the player is looking at. # Show the health of the entity that the player is looking at.
Show On Look: true Show On Look: true

View File

@ -1,7 +1,7 @@
name: ActionHealth name: ActionHealth
main: com.zeshanaslam.actionhealth.Main main: com.zeshanaslam.actionhealth.Main
version: 3.3.2 version: 3.3.3
softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard] softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard, mcMMO]
commands: commands:
Actionhealth: Actionhealth:
description: Actionhealth main commands. description: Actionhealth main commands.

View File

@ -44,12 +44,7 @@ public class LookThread extends BukkitRunnable {
if (livingEntity.getType().name().equals("ARMOR_STAND")) continue; if (livingEntity.getType().name().equals("ARMOR_STAND")) continue;
if (player.getWorld() != livingEntity.getWorld()) continue; if (player.getWorld() != livingEntity.getWorld()) continue;
String name; String name = plugin.healthUtil.getName(livingEntity);
if (livingEntity.getCustomName() == null) {
name = livingEntity.getName();
} else {
name = livingEntity.getCustomName();
}
if (TargetHelper.canSee(player, livingEntity.getLocation(), transparentTypeIds) && !plugin.configStore.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()); plugin.healthUtil.sendHealth(player, livingEntity, livingEntity.getHealth());

View File

@ -21,6 +21,7 @@ public class Main extends JavaPlugin {
public WorldGuardAPI worldGuardAPI; public WorldGuardAPI worldGuardAPI;
public HealthUtil healthUtil; public HealthUtil healthUtil;
public int taskID = -1; public int taskID = -1;
public boolean mcMMOEnabled;
public List<UUID> toggle = new ArrayList<>(); public List<UUID> toggle = new ArrayList<>();
@ -48,6 +49,10 @@ public class Main extends JavaPlugin {
this.worldGuardPlugin = ((WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard")); this.worldGuardPlugin = ((WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard"));
this.worldGuardAPI = new WorldGuardAPI(this); this.worldGuardAPI = new WorldGuardAPI(this);
} }
if (Bukkit.getServer().getPluginManager().isPluginEnabled("mcMMO")) {
mcMMOEnabled = true;
}
} }
@Override @Override

View File

@ -0,0 +1,17 @@
package com.zeshanaslam.actionhealth.support;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
public class McMMOSupport {
public String getName(MetadataValue metadataValue) {
/*if (metadataValue instanceof OldName) {
OldName oldName = (OldName) metadataValue;
return oldName.asString();
}*/
FixedMetadataValue fixedMetadataValue = (FixedMetadataValue) metadataValue;
return fixedMetadataValue.asString();
}
}

View File

@ -1,18 +1,12 @@
package com.zeshanaslam.actionhealth.support; package com.zeshanaslam.actionhealth.support;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.Association;
import com.sk89q.worldguard.protection.ApplicableRegionSet; 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.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.zeshanaslam.actionhealth.Main; import com.zeshanaslam.actionhealth.Main;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -28,16 +22,10 @@ public class WorldGuardAPI {
private WorldGuardPlugin worldGuardPlugin = null; private WorldGuardPlugin worldGuardPlugin = null;
private Object regionContainer = null; private Object regionContainer = null;
private Method regionContainerGetMethod = null; private Method regionContainerGetMethod = null;
private Method createQueryMethod = null;
private Method regionQueryTestStateMethod = null;
private Method locationAdaptMethod = null;
private Method worldAdaptMethod = null; private Method worldAdaptMethod = null;
private Method regionManagerGetMethod = null; private Method regionManagerGetMethod = null;
private Constructor<?> vectorConstructor = null; private Constructor<?> vectorConstructor = null;
private Method vectorConstructorAsAMethodBecauseWhyNot = null; private Method vectorConstructorAsAMethodBecauseWhyNot = null;
private StateFlag buildFlag;
private StateFlag pvpFlag;
private StateFlag exitFlag;
private boolean initialized = false; private boolean initialized = false;
public WorldGuardAPI(Main main) { public WorldGuardAPI(Main main) {
@ -52,18 +40,6 @@ public class WorldGuardAPI {
} }
} }
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() { private void initialize() {
if (!initialized) { if (!initialized) {
initialized = true; initialized = true;
@ -74,20 +50,10 @@ public class WorldGuardAPI {
Object platform = getPlatFormMethod.invoke(worldGuard); Object platform = getPlatFormMethod.invoke(worldGuard);
Method getRegionContainerMethod = platform.getClass().getMethod("getRegionContainer"); Method getRegionContainerMethod = platform.getClass().getMethod("getRegionContainer");
regionContainer = getRegionContainerMethod.invoke(platform); 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<?> worldEditWorldClass = Class.forName("com.sk89q.worldedit.world.World");
Class<?> worldEditAdapterClass = Class.forName("com.sk89q.worldedit.bukkit.BukkitAdapter"); Class<?> worldEditAdapterClass = Class.forName("com.sk89q.worldedit.bukkit.BukkitAdapter");
worldAdaptMethod = worldEditAdapterClass.getMethod("adapt", World.class); worldAdaptMethod = worldEditAdapterClass.getMethod("adapt", World.class);
locationAdaptMethod = worldEditAdapterClass.getMethod("adapt", Location.class);
regionContainerGetMethod = regionContainer.getClass().getMethod("get", worldEditWorldClass); 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) { } catch (Exception ex) {
regionContainer = null; regionContainer = null;
main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 1", ex); main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 1", ex);
@ -97,15 +63,7 @@ public class WorldGuardAPI {
} else { } else {
regionContainer = worldGuardPlugin.getRegionContainer(); regionContainer = worldGuardPlugin.getRegionContainer();
try { try {
createQueryMethod = regionContainer.getClass().getMethod("createQuery");
regionContainerGetMethod = regionContainer.getClass().getMethod("get", World.class); 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) { } catch (Exception ex) {
main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 2", ex); main.getLogger().log(Level.SEVERE, "Unable to hook into WG. SE: 2", ex);
main.worldGuardPlugin = null; main.worldGuardPlugin = null;
@ -175,48 +133,9 @@ public class WorldGuardAPI {
return null; 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<String> getRegionNames(Location location) { public List<String> getRegionNames(Location location) {
ApplicableRegionSet applicableRegionSet = getRegionSet(location); ApplicableRegionSet applicableRegionSet = getRegionSet(location);
return Objects.requireNonNull(applicableRegionSet).getRegions().stream() return Objects.requireNonNull(applicableRegionSet).getRegions().stream()
.map(ProtectedRegion::getId).collect(Collectors.toList()); .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;
}
} }

View File

@ -1,16 +1,21 @@
package com.zeshanaslam.actionhealth.utils; package com.zeshanaslam.actionhealth.utils;
import com.zeshanaslam.actionhealth.Main; import com.zeshanaslam.actionhealth.Main;
import com.zeshanaslam.actionhealth.support.McMMOSupport;
import com.zeshanaslam.actionhealth.support.PreAction; import com.zeshanaslam.actionhealth.support.PreAction;
import org.apache.commons.lang.WordUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List;
public class HealthUtil { public class HealthUtil {
@ -69,22 +74,13 @@ public class HealthUtil {
} }
private String getOutput(double health, Player receiver, LivingEntity entity) { private String getOutput(double health, Player receiver, LivingEntity entity) {
String name;
double maxHealth = entity.getMaxHealth(); double maxHealth = entity.getMaxHealth();
if (health < 0.0 || entity.isDead()) health = 0.0; if (health < 0.0 || entity.isDead()) health = 0.0;
if (entity.getCustomName() == null) { String name = getName(entity);
name = entity.getName();
} else {
name = entity.getCustomName();
}
if (plugin.configStore.blacklist.contains(name)) return null; if (plugin.configStore.blacklist.contains(name)) return null;
if (plugin.configStore.stripName) name = ChatColor.stripColor(name); 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.configStore.healthMessage; String output = plugin.configStore.healthMessage;
@ -165,6 +161,63 @@ public class HealthUtil {
return output; return output;
} }
public String getName(LivingEntity entity) {
String name;
// Supporting mcmmo health bar to get to display correct name.
List<MetadataValue> metadataValues = entity.getMetadata("mcMMO_oldName");
List<MetadataValue> metadataValuesOld = entity.getMetadata("mcMMO: Custom Name");
String mcMMOName = null;
if (plugin.mcMMOEnabled && entity.getCustomName() != null && (!metadataValues.isEmpty() || !metadataValuesOld.isEmpty())) {
mcMMOName = new McMMOSupport().getName(metadataValues.isEmpty() ? metadataValuesOld.get(0) : metadataValues.get(0));
}
if (mcMMOName == null) {
if (entity.getCustomName() == null) {
name = getNameReflection(entity);
} else {
name = entity.getCustomName();
}
} else if (mcMMOName.equals("")) {
name = getNameReflection(entity);
} else {
name = mcMMOName;
}
if (plugin.configStore.translate.containsKey(name))
name = plugin.configStore.translate.get(name);
return name;
}
private String getNameReflection(LivingEntity entity) {
String name;
Method getName = null;
try {
if (entity.getCustomName() == null)
getName = entity.getClass().getMethod("getName", (Class<?>[]) null);
} catch (NoSuchMethodException | SecurityException ignored) {
}
if (getName != null) {
try {
name = (String) getName.invoke(entity, (Object[]) null);
} catch (IllegalAccessException | InvocationTargetException e) {
name = capitalizeFully(entity.getType().name().replace("_", ""));
}
} else {
name = capitalizeFully(entity.getType().name().replace("_", ""));
}
return name;
}
private String capitalizeFully(String words) {
words = words.toLowerCase();
return WordUtils.capitalizeFully(words);
}
public void sendActionBar(Player player, String message) { public void sendActionBar(Player player, String message) {
message = ChatColor.translateAlternateColorCodes('&', message); message = ChatColor.translateAlternateColorCodes('&', message);