Skip to content

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
UsainSrht committed Mar 22, 2023
1 parent 648ea5e commit 7e185cb
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 120 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.purpurmc</groupId>
<artifactId>healthbar</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>Healthbar</name>
Expand Down
89 changes: 0 additions & 89 deletions src/main/java/com/purpurmc/healthbar/BossBar.java

This file was deleted.

88 changes: 88 additions & 0 deletions src/main/java/com/purpurmc/healthbar/BossBarUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.purpurmc.healthbar;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.bossbar.BossBar;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Damageable;
import org.bukkit.metadata.FixedMetadataValue;

import java.text.DecimalFormat;

public class BossBarUtils {

private static final DecimalFormat df = new DecimalFormat("0.00");
public static void giveBossBar(Player p, Entity entity) {
Damageable damageable = (Damageable) entity;
Attributable attributable = (Attributable) entity;
float progress = (float) (damageable.getHealth() / attributable.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
BossBar bar = getorCreateBossBar(entity).progress(progress);
p.showBossBar(bar);
p.setMetadata("healthbarcooldown" + entity.getUniqueId()
, new FixedMetadataValue(Healthbar.getInstance(), Bukkit.getCurrentTick()));
Bukkit.getScheduler().runTaskLater(Healthbar.getInstance(), () -> {
int current = Bukkit.getCurrentTick();
int cd = p.getMetadata("healthbarcooldown" + entity.getUniqueId()).get(0).asInt();
if ((current - cd) >= Healthbar.bartime) {
p.hideBossBar(bar);
}
}, Healthbar.bartime);

}

public static BossBar getorCreateBossBar(Entity entity) {

if (!entity.hasMetadata("healthbar")) {
ComponentLike componentLike = Component.empty();
float progress = 1f;
BossBar.Color color = Healthbar.getInstance().getBarcolor();
BossBar.Overlay overlay = Healthbar.getInstance().getBaroverlay();

BossBar bossbar = BossBar.bossBar(
componentLike,
progress,
color,
overlay);
entity.setMetadata("healthbar", new FixedMetadataValue(Healthbar.getInstance(), bossbar));
return bossbar;
}
else {
return (BossBar) entity.getMetadata("healthbar").get(0).value();
}
}

public static void updateHealthBar(Entity entity, double damage) {
BossBar bossbar = (BossBar) entity.getMetadata("healthbar").get(0).value();
Component parsed;
Component translate = Component.translatable(entity.getType().translationKey());
Damageable damageable = (Damageable) entity;
Attributable attributable = (Attributable) entity;
double max = attributable.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
double health = damageable.getHealth();
String title;
if (health <= 0) {
health = 0d;
title = Healthbar.getInstance().getDeadtitle();
}
else {
title = Healthbar.getInstance().getLivingtitle();
}
Component customName = entity.customName();
parsed = MiniMessage.miniMessage().deserialize(title,
Placeholder.component("entity", translate),
Placeholder.component("name", customName == null ? translate : customName),
Placeholder.component("max", Component.text(df.format(max))),
Placeholder.component("health", Component.text(df.format(health))),
Placeholder.component("damage", Component.text(df.format(damage)))
);
bossbar.name(parsed);
float progress = (float) (health / max);
bossbar.progress(progress);
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/purpurmc/healthbar/DamageEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.purpurmc.healthbar;

import org.bukkit.Bukkit;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
Expand All @@ -13,9 +14,9 @@

public class DamageEvent implements Listener {

private final BossBar bossbar = new BossBar();
@EventHandler
public void onDamage(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof Damageable)) return;
Entity attacker = event.getDamager();
if (event.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) event.getDamager();
Expand All @@ -24,7 +25,7 @@ public void onDamage(EntityDamageByEntityEvent event) {
if (attacker instanceof Player) {
if (!attacker.getPersistentDataContainer().has(visible)) {
if (!Healthbar.instance.getBlacklistEntities().contains(event.getEntityType())) {
bossbar.giveBossBar((Player) attacker, event.getEntity());
BossBarUtils.giveBossBar((Player) attacker, event.getEntity());
}
}

Expand All @@ -36,7 +37,7 @@ public void onDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
Bukkit.getScheduler().runTaskLater(Healthbar.instance, () -> {
if (entity.hasMetadata("healthbar")) {
bossbar.updateHealthBar(entity, event.getFinalDamage());
BossBarUtils.updateHealthBar(entity, event.getFinalDamage());
}
}, 1L);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/purpurmc/healthbar/HealthBarCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected HealthBarCommand(@NotNull String name, @NotNull String description, @N

@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String command, String[] args) {
if (args.length == 0) return true;
if (args[0].equals("toggle")) {
if (sender instanceof Player) {
Player p = (Player) sender;
Expand Down
51 changes: 24 additions & 27 deletions src/main/java/com/purpurmc/healthbar/Healthbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
public final class Healthbar extends JavaPlugin {

public static Healthbar instance;

public static NamespacedKey visible;
public String livingtitle;
public String deadtitle;
public BossBar.Overlay baroverlay;
public BossBar.Color barcolor;
public Collection<EntityType> blacklist;

public static String livingtitle;
public static String deadtitle;
public static BossBar.Overlay baroverlay;
public static BossBar.Color barcolor;
public static int bartime;
public static Collection<EntityType> blacklist;
@Override
public void onEnable() {
instance = this;
Expand All @@ -34,58 +33,56 @@ public void onEnable() {
"command to toggle/reload healthbar",
"/healthbar (toggle|reload)",
new ArrayList<>()));

try {
initializeYAML();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void onDisable() {

}

public static Healthbar getInstance() {
return instance;
}

public Collection<EntityType> getBlacklistEntities() {
return this.blacklist;
return blacklist;
}

public BossBar.Overlay getBaroverlay() {
return this.baroverlay;
return baroverlay;
}

public BossBar.Color getBarcolor() {
return this.barcolor;
return barcolor;
}
public String getLivingtitle() {
return livingtitle;
}
public String getDeadtitle() {
return deadtitle;
}

public String getLivingtitle() {return this.livingtitle;}
public String getDeadtitle() {return this.deadtitle;}

public void initializeYAML() throws IOException {
saveDefaultConfig();
getLogger().info("Loading config.yml");
FileConfiguration configyml = getConfig();

this.livingtitle = configyml.getString("bar.title.living");
livingtitle = configyml.getString("bar.title.living");

this.deadtitle = configyml.getString("bar.title.dead");
deadtitle = configyml.getString("bar.title.dead");

String overlay = configyml.getString("bar.overlay");
this.baroverlay = BossBar.Overlay.valueOf(overlay);
baroverlay = BossBar.Overlay.valueOf(overlay);

String color = configyml.getString("bar.color");
this.barcolor = BossBar.Color.valueOf(color);
barcolor = BossBar.Color.valueOf(color);

List<String> blacklist = configyml.getStringList("blacklist");
List<String> Lblacklist = configyml.getStringList("blacklist");
Collection<EntityType> blacklistentitytpye = new ArrayList<>();
blacklist.forEach(str -> blacklistentitytpye.add(EntityType.valueOf(str))
Lblacklist.forEach(str -> blacklistentitytpye.add(EntityType.valueOf(str))
);

this.blacklist = blacklistentitytpye;
blacklist = blacklistentitytpye;

bartime = configyml.getInt("bar.time");
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ bar:
overlay: PROGRESS
#https://jd.adventure.kyori.net/api/4.11.0/net/kyori/adventure/bossbar/BossBar.Color.html
color: RED
#in ticks, 20 = 1 second and 100 = 5 seconds.
time: 100

blacklist:
- ENDER_DRAGON
Expand Down

0 comments on commit 7e185cb

Please sign in to comment.