mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-26 07:06:45 +01:00
Move healthbar stuff to it own class.
This commit is contained in:
parent
7a3921eace
commit
4fc6349824
@ -12,8 +12,6 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
@ -50,9 +48,9 @@ import com.gmail.nossr50.util.ChimaeraWing;
|
||||
import com.gmail.nossr50.util.HardcoreManager;
|
||||
import com.gmail.nossr50.util.ItemUtils;
|
||||
import com.gmail.nossr50.util.Misc;
|
||||
import com.gmail.nossr50.util.MobHealthbarUtils;
|
||||
import com.gmail.nossr50.util.Motd;
|
||||
import com.gmail.nossr50.util.Permissions;
|
||||
import com.gmail.nossr50.util.StringUtils;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
import com.gmail.nossr50.util.skills.SkillUtils;
|
||||
|
||||
@ -72,17 +70,7 @@ public class PlayerListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
EntityDamageEvent lastDamageCause = event.getEntity().getLastDamageCause();
|
||||
String replaceString;
|
||||
|
||||
if (lastDamageCause instanceof EntityDamageByEntityEvent) {
|
||||
replaceString = StringUtils.getPrettyEntityTypeString(((EntityDamageByEntityEvent) lastDamageCause).getDamager().getType());
|
||||
}
|
||||
else {
|
||||
replaceString = "a mob";
|
||||
}
|
||||
|
||||
event.setDeathMessage(deathMessage.replaceAll("(?:\u00A7(?:[0-9A-FK-ORa-fk-or]){1}(?:[\u2764\u25A0]{1,10})){1,2}", replaceString));
|
||||
event.setDeathMessage(MobHealthbarUtils.fixDeathMessage(deathMessage, event.getEntity()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +79,7 @@ public class PlayerListener implements Listener {
|
||||
* @param event The event to watch
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||||
public void onPlayerDeathMonitor(PlayerDeathEvent event) {
|
||||
if (!Config.getInstance().getHardcoreEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
144
src/main/java/com/gmail/nossr50/util/MobHealthbarUtils.java
Normal file
144
src/main/java/com/gmail/nossr50/util/MobHealthbarUtils.java
Normal file
@ -0,0 +1,144 @@
|
||||
package com.gmail.nossr50.util;
|
||||
|
||||
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;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.MobHealthbarType;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
import com.gmail.nossr50.runnables.MobHealthDisplayUpdaterTask;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
|
||||
public final class MobHealthbarUtils {
|
||||
private MobHealthbarUtils() {};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param profile The
|
||||
* @param target
|
||||
* @param damage
|
||||
*/
|
||||
public static void handleMobHealthbars(Player player, LivingEntity target, int damage) {
|
||||
if (!Permissions.mobHealthDisplay(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerProfile profile = UserManager.getPlayer(player).getProfile();
|
||||
|
||||
if (profile.getMobHealthbarType() == MobHealthbarType.DISABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
String oldName = target.getCustomName();
|
||||
boolean oldNameVisible = target.isCustomNameVisible();
|
||||
String newName = createHealthDisplay(profile, target, damage);
|
||||
|
||||
target.setCustomName(newName);
|
||||
target.setCustomNameVisible(true);
|
||||
|
||||
int displayTime = Config.getInstance().getMobHealthbarTime();
|
||||
|
||||
if (displayTime != -1) {
|
||||
if (oldName == null) {
|
||||
oldName = "";
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
new MobHealthDisplayUpdaterTask(target).runTaskLater(mcMMO.p, displayTime * 20); // Clear health display after 3 seconds
|
||||
}
|
||||
}
|
||||
|
||||
private static String createHealthDisplay(PlayerProfile profile, LivingEntity entity, int damage) {
|
||||
int maxHealth = entity.getMaxHealth();
|
||||
int currentHealth = Math.max(entity.getHealth() - damage, 0);
|
||||
double healthPercentage = (currentHealth / (double) maxHealth) * 100.0D;
|
||||
|
||||
int fullDisplay = 0;
|
||||
ChatColor color = ChatColor.BLACK;
|
||||
String symbol = "";
|
||||
|
||||
switch (profile.getMobHealthbarType()) {
|
||||
case HEARTS:
|
||||
fullDisplay = Math.min(maxHealth / 2, 10);
|
||||
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;
|
||||
}
|
||||
|
||||
int coloredDisplay = (int) (fullDisplay * (healthPercentage / 100.0D));
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.gmail.nossr50.util.skills;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.AnimalTamer;
|
||||
import org.bukkit.entity.Animals;
|
||||
@ -18,19 +17,15 @@ import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
|
||||
import org.bukkit.event.player.PlayerAnimationEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.metadata.FixedMetadataValue;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.MobHealthbarType;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
import com.gmail.nossr50.datatypes.skills.SkillType;
|
||||
import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
|
||||
import com.gmail.nossr50.events.fake.FakeEntityDamageEvent;
|
||||
import com.gmail.nossr50.locale.LocaleLoader;
|
||||
import com.gmail.nossr50.party.PartyManager;
|
||||
import com.gmail.nossr50.runnables.MobHealthDisplayUpdaterTask;
|
||||
import com.gmail.nossr50.runnables.skills.AwardCombatXpTask;
|
||||
import com.gmail.nossr50.runnables.skills.BleedTimerTask;
|
||||
import com.gmail.nossr50.skills.acrobatics.AcrobaticsManager;
|
||||
@ -43,6 +38,7 @@ import com.gmail.nossr50.skills.taming.TamingManager;
|
||||
import com.gmail.nossr50.skills.unarmed.UnarmedManager;
|
||||
import com.gmail.nossr50.util.ItemUtils;
|
||||
import com.gmail.nossr50.util.Misc;
|
||||
import com.gmail.nossr50.util.MobHealthbarUtils;
|
||||
import com.gmail.nossr50.util.ModUtils;
|
||||
import com.gmail.nossr50.util.Permissions;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
@ -296,37 +292,7 @@ public final class CombatUtils {
|
||||
return;
|
||||
}
|
||||
|
||||
PlayerProfile profile = UserManager.getPlayer(player).getProfile();
|
||||
|
||||
if (Permissions.mobHealthDisplay(player) && profile.getMobHealthbarType() != MobHealthbarType.DISABLED) {
|
||||
String oldName = target.getCustomName();
|
||||
boolean oldNameVisible = target.isCustomNameVisible();
|
||||
String newName = createHealthDisplay(profile, target, event.getDamage());
|
||||
|
||||
target.setCustomName(newName);
|
||||
target.setCustomNameVisible(true);
|
||||
|
||||
int displayTime = Config.getInstance().getMobHealthbarTime();
|
||||
|
||||
if (displayTime != -1) {
|
||||
if (oldName == null) {
|
||||
oldName = "";
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
new MobHealthDisplayUpdaterTask(target).runTaskLater(mcMMO.p, displayTime * 20); // Clear health display after 3 seconds
|
||||
}
|
||||
}
|
||||
MobHealthbarUtils.handleMobHealthbars(player, target, event.getDamage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -627,67 +593,4 @@ public final class CombatUtils {
|
||||
|
||||
return process;
|
||||
}
|
||||
|
||||
private static String createHealthDisplay(PlayerProfile profile, LivingEntity entity, int damage) {
|
||||
int maxHealth = entity.getMaxHealth();
|
||||
int currentHealth = Math.max(entity.getHealth() - damage, 0);
|
||||
double healthPercentage = (currentHealth / (double) maxHealth) * 100.0D;
|
||||
|
||||
int fullDisplay = 0;
|
||||
ChatColor color = ChatColor.BLACK;
|
||||
String symbol = "";
|
||||
|
||||
switch (profile.getMobHealthbarType()) {
|
||||
case HEARTS:
|
||||
fullDisplay = Math.min(maxHealth / 2, 10);
|
||||
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;
|
||||
}
|
||||
|
||||
int coloredDisplay = (int) (fullDisplay * (healthPercentage / 100.0D));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user