2013-04-24 17:40:34 +02:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
2013-05-02 13:56:29 +02:00
|
|
|
import com.gmail.nossr50.config.AdvancedConfig;
|
2013-04-24 17:40:34 +02:00
|
|
|
import com.gmail.nossr50.config.Config;
|
|
|
|
import com.gmail.nossr50.datatypes.MobHealthbarType;
|
2019-01-15 16:19:00 +01:00
|
|
|
import com.gmail.nossr50.datatypes.meta.OldName;
|
2019-01-15 07:11:58 +01:00
|
|
|
import com.gmail.nossr50.mcMMO;
|
2013-04-24 17:40:34 +02:00
|
|
|
import com.gmail.nossr50.runnables.MobHealthDisplayUpdaterTask;
|
2019-01-15 07:11:58 +01:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
|
|
import org.bukkit.event.entity.EntityDamageEvent;
|
|
|
|
import org.bukkit.metadata.FixedMetadataValue;
|
2013-04-24 17:40:34 +02:00
|
|
|
|
|
|
|
public final class MobHealthbarUtils {
|
2017-10-04 05:32:54 +02:00
|
|
|
private MobHealthbarUtils() {}
|
2013-04-24 17:40:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix issues with death messages caused by the mob healthbars.
|
|
|
|
*
|
|
|
|
* @param deathMessage The original death message
|
|
|
|
* @param player The player who died
|
|
|
|
* @return the fixed death message
|
|
|
|
*/
|
|
|
|
public static String fixDeathMessage(String deathMessage, Player player) {
|
|
|
|
EntityDamageEvent lastDamageCause = player.getLastDamageCause();
|
|
|
|
String replaceString = lastDamageCause instanceof EntityDamageByEntityEvent ? StringUtils.getPrettyEntityTypeString(((EntityDamageByEntityEvent) lastDamageCause).getDamager().getType()) : "a mob";
|
|
|
|
|
|
|
|
return deathMessage.replaceAll("(?:\u00A7(?:[0-9A-FK-ORa-fk-or]){1}(?:[\u2764\u25A0]{1,10})){1,2}", replaceString);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the creation of mob healthbars.
|
2019-01-24 00:39:04 +01:00
|
|
|
* @param target the targetted entity
|
2013-05-17 05:28:32 +02:00
|
|
|
* @param damage damage done by the attack triggering this
|
2013-04-24 17:40:34 +02:00
|
|
|
*/
|
2019-01-24 00:39:04 +01:00
|
|
|
public static void handleMobHealthbars(LivingEntity target, double damage, mcMMO plugin) {
|
|
|
|
if (mcMMO.isHealthBarPluginEnabled() || !Config.getInstance().getMobHealthbarEnabled()) {
|
2013-04-24 17:40:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-28 13:36:48 +02:00
|
|
|
if (isBoss(target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-15 16:19:00 +01:00
|
|
|
String originalName = target.getName();
|
2013-04-24 17:40:34 +02:00
|
|
|
String oldName = target.getCustomName();
|
2013-05-02 03:17:59 +02:00
|
|
|
|
2019-01-15 16:19:00 +01:00
|
|
|
/*
|
|
|
|
* Store the name in metadata
|
|
|
|
*/
|
|
|
|
if(target.getMetadata("mcMMO_oldName").size() <= 0 && originalName != null)
|
|
|
|
target.setMetadata("mcMMO_oldName", new OldName(originalName, plugin));
|
|
|
|
|
2013-05-02 03:17:59 +02:00
|
|
|
if (oldName == null) {
|
|
|
|
oldName = "";
|
|
|
|
}
|
2013-05-02 13:56:29 +02:00
|
|
|
else if (oldName.equalsIgnoreCase(AdvancedConfig.getInstance().getKrakenName())) {
|
2013-05-02 03:17:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:16:12 +01:00
|
|
|
// Don't mangle invalid entities, they're not going to be rendered anyways
|
|
|
|
if (!target.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-24 17:40:34 +02:00
|
|
|
boolean oldNameVisible = target.isCustomNameVisible();
|
2019-01-24 00:39:04 +01:00
|
|
|
String newName = createHealthDisplay(Config.getInstance().getMobHealthbarDefault(), target, damage);
|
2013-04-24 17:40:34 +02:00
|
|
|
|
|
|
|
target.setCustomName(newName);
|
|
|
|
target.setCustomNameVisible(true);
|
|
|
|
|
|
|
|
int displayTime = Config.getInstance().getMobHealthbarTime();
|
|
|
|
|
|
|
|
if (displayTime != -1) {
|
|
|
|
boolean updateName = !ChatColor.stripColor(oldName).equalsIgnoreCase(ChatColor.stripColor(newName));
|
|
|
|
|
|
|
|
if (updateName) {
|
|
|
|
target.setMetadata(mcMMO.customNameKey, new FixedMetadataValue(mcMMO.p, oldName));
|
|
|
|
target.setMetadata(mcMMO.customVisibleKey, new FixedMetadataValue(mcMMO.p, oldNameVisible));
|
|
|
|
}
|
|
|
|
else if (!target.hasMetadata(mcMMO.customNameKey)) {
|
|
|
|
target.setMetadata(mcMMO.customNameKey, new FixedMetadataValue(mcMMO.p, ""));
|
|
|
|
target.setMetadata(mcMMO.customVisibleKey, new FixedMetadataValue(mcMMO.p, false));
|
|
|
|
}
|
|
|
|
|
2013-06-13 01:26:09 +02:00
|
|
|
new MobHealthDisplayUpdaterTask(target).runTaskLater(mcMMO.p, displayTime * Misc.TICK_CONVERSION_FACTOR); // Clear health display after 3 seconds
|
2013-04-24 17:40:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:39:04 +01:00
|
|
|
private static String createHealthDisplay(MobHealthbarType mobHealthbarType, LivingEntity entity, double damage) {
|
2013-07-11 18:43:36 +02:00
|
|
|
double maxHealth = entity.getMaxHealth();
|
|
|
|
double currentHealth = Math.max(entity.getHealth() - damage, 0);
|
|
|
|
double healthPercentage = (currentHealth / maxHealth) * 100.0D;
|
2013-04-24 17:40:34 +02:00
|
|
|
|
2014-01-20 22:58:40 +01:00
|
|
|
int fullDisplay;
|
2013-04-24 17:40:34 +02:00
|
|
|
ChatColor color = ChatColor.BLACK;
|
2014-01-20 22:58:40 +01:00
|
|
|
String symbol;
|
2013-04-24 17:40:34 +02:00
|
|
|
|
2019-01-24 00:39:04 +01:00
|
|
|
switch (mobHealthbarType) {
|
2013-04-24 17:40:34 +02:00
|
|
|
case HEARTS:
|
2013-07-11 18:43:36 +02:00
|
|
|
fullDisplay = Math.min((int) (maxHealth / 2), 10);
|
2013-04-24 17:40:34 +02:00
|
|
|
color = ChatColor.DARK_RED;
|
|
|
|
symbol = "❤";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BAR:
|
|
|
|
fullDisplay = 10;
|
|
|
|
|
|
|
|
if (healthPercentage >= 85) {
|
|
|
|
color = ChatColor.DARK_GREEN;
|
|
|
|
}
|
|
|
|
else if (healthPercentage >= 70) {
|
|
|
|
color = ChatColor.GREEN;
|
|
|
|
}
|
|
|
|
else if (healthPercentage >= 55) {
|
|
|
|
color = ChatColor.GOLD;
|
|
|
|
}
|
|
|
|
else if (healthPercentage >= 40) {
|
|
|
|
color = ChatColor.YELLOW;
|
|
|
|
}
|
|
|
|
else if (healthPercentage >= 25) {
|
|
|
|
color = ChatColor.RED;
|
|
|
|
}
|
|
|
|
else if (healthPercentage >= 0) {
|
|
|
|
color = ChatColor.DARK_RED;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol = "■";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-05-17 05:28:32 +02:00
|
|
|
int coloredDisplay = (int) Math.ceil(fullDisplay * (healthPercentage / 100.0D));
|
2013-04-24 17:40:34 +02:00
|
|
|
int grayDisplay = fullDisplay - coloredDisplay;
|
|
|
|
|
|
|
|
String healthbar = color + "";
|
|
|
|
|
|
|
|
for (int i = 0; i < coloredDisplay; i++) {
|
|
|
|
healthbar += symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
healthbar += ChatColor.GRAY;
|
|
|
|
|
|
|
|
for (int i = 0; i < grayDisplay; i++) {
|
|
|
|
healthbar += symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
return healthbar;
|
|
|
|
}
|
2013-06-28 13:36:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a given LivingEntity is a boss.
|
|
|
|
*
|
|
|
|
* @param livingEntity The {@link LivingEntity} of the livingEntity to check
|
|
|
|
* @return true if the livingEntity is a boss, false otherwise
|
|
|
|
*/
|
2013-11-01 13:22:04 +01:00
|
|
|
private static boolean isBoss(LivingEntity livingEntity) {
|
2013-06-28 13:36:48 +02:00
|
|
|
switch (livingEntity.getType()) {
|
|
|
|
case ENDER_DRAGON:
|
|
|
|
case WITHER:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
2014-02-03 20:48:43 +01:00
|
|
|
return false;
|
2013-06-28 13:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-24 17:40:34 +02:00
|
|
|
}
|