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!
Name Change: false
Name:
- Snow Golem = New name
- Snow Golem = New name
# ActionHealth will be disabled for any world names added below. Case sensitive!
Disabled worlds:
- randomworld
- randomworld
Disabled regions:
- testing_region
- testing_region
# Disable ActionHealth in regions where PvP is denied.
Region PvP: true
@ -70,7 +70,7 @@ Remember Toggle: false
# Blacklist by entity name or entity display name.
Blacklist:
- 'CCPD Officer'
- 'CCPD Officer'
# Show the health of the entity that the player is looking at.
Show On Look: true

View File

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

View File

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

View File

@ -21,6 +21,7 @@ public class Main extends JavaPlugin {
public WorldGuardAPI worldGuardAPI;
public HealthUtil healthUtil;
public int taskID = -1;
public boolean mcMMOEnabled;
public List<UUID> toggle = new ArrayList<>();
@ -48,6 +49,10 @@ public class Main extends JavaPlugin {
this.worldGuardPlugin = ((WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard"));
this.worldGuardAPI = new WorldGuardAPI(this);
}
if (Bukkit.getServer().getPluginManager().isPluginEnabled("mcMMO")) {
mcMMOEnabled = true;
}
}
@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;
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;
@ -28,16 +22,10 @@ public class WorldGuardAPI {
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) {
@ -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() {
if (!initialized) {
initialized = true;
@ -74,20 +50,10 @@ public class WorldGuardAPI {
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);
@ -97,15 +63,7 @@ public class WorldGuardAPI {
} 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;
@ -175,48 +133,9 @@ public class WorldGuardAPI {
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) {
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;
}
}

View File

@ -1,16 +1,21 @@
package com.zeshanaslam.actionhealth.utils;
import com.zeshanaslam.actionhealth.Main;
import com.zeshanaslam.actionhealth.support.McMMOSupport;
import com.zeshanaslam.actionhealth.support.PreAction;
import org.apache.commons.lang.WordUtils;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class HealthUtil {
@ -69,22 +74,13 @@ public class HealthUtil {
}
private String getOutput(double health, Player receiver, LivingEntity entity) {
String name;
double maxHealth = entity.getMaxHealth();
if (health < 0.0 || entity.isDead()) health = 0.0;
if (entity.getCustomName() == null) {
name = entity.getName();
} else {
name = entity.getCustomName();
}
String name = getName(entity);
if (plugin.configStore.blacklist.contains(name)) return null;
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;
@ -165,6 +161,63 @@ public class HealthUtil {
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) {
message = ChatColor.translateAlternateColorCodes('&', message);