MobHealthbarUtils.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.gmail.nossr50.util;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.datatypes.meta.OldName;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.gmail.nossr50.runnables.MobHealthDisplayUpdaterTask;
  7. import com.gmail.nossr50.util.text.StringUtils;
  8. import com.neetgames.mcmmo.MobHealthBarType;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.entity.LivingEntity;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  13. import org.bukkit.event.entity.EntityDamageEvent;
  14. import org.bukkit.metadata.FixedMetadataValue;
  15. public final class MobHealthbarUtils {
  16. private MobHealthbarUtils() {}
  17. /**
  18. * Fix issues with death messages caused by the mob healthbars.
  19. *
  20. * @param deathMessage The original death message
  21. * @param player The player who died
  22. * @return the fixed death message
  23. */
  24. public static String fixDeathMessage(String deathMessage, Player player) {
  25. EntityDamageEvent lastDamageCause = player.getLastDamageCause();
  26. String replaceString = lastDamageCause instanceof EntityDamageByEntityEvent ? StringUtils.getPrettyEntityTypeString(((EntityDamageByEntityEvent) lastDamageCause).getDamager().getType()) : "a mob";
  27. return deathMessage.replaceAll("(?:(\u00A7(?:[0-9A-FK-ORa-fk-or]))*(?:[\u2764\u25A0]{1,10})){1,2}", replaceString);
  28. }
  29. /**
  30. * Handle the creation of mob healthbars.
  31. * @param target the targetted entity
  32. * @param damage damage done by the attack triggering this
  33. */
  34. public static void handleMobHealthbars(LivingEntity target, double damage, mcMMO plugin) {
  35. if (mcMMO.isHealthBarPluginEnabled() || !Config.getInstance().getMobHealthbarEnabled()) {
  36. return;
  37. }
  38. if (isBoss(target)) {
  39. return;
  40. }
  41. // Don't mangle invalid entities, they're not going to be rendered anyways
  42. if (!target.isValid()) {
  43. return;
  44. }
  45. String originalName = target.getName();
  46. String oldName = target.getCustomName();
  47. /*
  48. * Store the name in metadata
  49. */
  50. if(target.getMetadata(TransientMetadataTools.OLD_NAME_METAKEY).size() <= 0 && originalName != null)
  51. target.setMetadata(TransientMetadataTools.OLD_NAME_METAKEY, new OldName(originalName, plugin));
  52. if (oldName == null) {
  53. oldName = "";
  54. }
  55. else if (oldName.equalsIgnoreCase(AdvancedConfig.getInstance().getKrakenName())) {
  56. return;
  57. }
  58. boolean oldNameVisible = target.isCustomNameVisible();
  59. String newName = createHealthDisplay(Config.getInstance().getMobHealthbarDefault(), target, damage);
  60. target.setCustomName(newName);
  61. target.setCustomNameVisible(true);
  62. int displayTime = Config.getInstance().getMobHealthbarTime();
  63. if (displayTime != -1) {
  64. boolean updateName = !ChatColor.stripColor(oldName).equalsIgnoreCase(ChatColor.stripColor(newName));
  65. if (updateName) {
  66. target.setMetadata(mcMMO.customNameKey, new FixedMetadataValue(mcMMO.p, oldName));
  67. target.setMetadata(mcMMO.customVisibleKey, new FixedMetadataValue(mcMMO.p, oldNameVisible));
  68. }
  69. else if (!target.hasMetadata(mcMMO.customNameKey)) {
  70. target.setMetadata(mcMMO.customNameKey, new FixedMetadataValue(mcMMO.p, ""));
  71. target.setMetadata(mcMMO.customVisibleKey, new FixedMetadataValue(mcMMO.p, false));
  72. }
  73. new MobHealthDisplayUpdaterTask(target).runTaskLater(mcMMO.p, displayTime * Misc.TICK_CONVERSION_FACTOR); // Clear health display after 3 seconds
  74. }
  75. }
  76. private static String createHealthDisplay(MobHealthBarType mobHealthbarType, LivingEntity entity, double damage) {
  77. double maxHealth = entity.getMaxHealth();
  78. double currentHealth = Math.max(entity.getHealth() - damage, 0);
  79. double healthPercentage = (currentHealth / maxHealth) * 100.0D;
  80. int fullDisplay;
  81. ChatColor color = ChatColor.BLACK;
  82. String symbol;
  83. switch (mobHealthbarType) {
  84. case HEARTS:
  85. fullDisplay = Math.min((int) (maxHealth / 2), 10);
  86. color = ChatColor.DARK_RED;
  87. symbol = "❤";
  88. break;
  89. case BAR:
  90. fullDisplay = 10;
  91. if (healthPercentage >= 85) {
  92. color = ChatColor.DARK_GREEN;
  93. }
  94. else if (healthPercentage >= 70) {
  95. color = ChatColor.GREEN;
  96. }
  97. else if (healthPercentage >= 55) {
  98. color = ChatColor.GOLD;
  99. }
  100. else if (healthPercentage >= 40) {
  101. color = ChatColor.YELLOW;
  102. }
  103. else if (healthPercentage >= 25) {
  104. color = ChatColor.RED;
  105. }
  106. else if (healthPercentage >= 0) {
  107. color = ChatColor.DARK_RED;
  108. }
  109. symbol = "■";
  110. break;
  111. default:
  112. return null;
  113. }
  114. int coloredDisplay = (int) Math.ceil(fullDisplay * (healthPercentage / 100.0D));
  115. int grayDisplay = fullDisplay - coloredDisplay;
  116. StringBuilder healthbar = new StringBuilder(color + "");
  117. for (int i = 0; i < coloredDisplay; i++) {
  118. healthbar.append(symbol);
  119. }
  120. healthbar.append(ChatColor.GRAY);
  121. for (int i = 0; i < grayDisplay; i++) {
  122. healthbar.append(symbol);
  123. }
  124. return healthbar.toString();
  125. }
  126. /**
  127. * Check if a given LivingEntity is a boss.
  128. *
  129. * @param livingEntity The {@link LivingEntity} of the livingEntity to check
  130. * @return true if the livingEntity is a boss, false otherwise
  131. */
  132. private static boolean isBoss(LivingEntity livingEntity) {
  133. switch (livingEntity.getType()) {
  134. case ENDER_DRAGON:
  135. case WITHER:
  136. return true;
  137. default:
  138. return false;
  139. }
  140. }
  141. }