11package net .kalbskinder .mobHealth .service ;
22
3- import lombok .RequiredArgsConstructor ;
43import lombok .extern .slf4j .Slf4j ;
54import net .kalbskinder .mobHealth .MobHealth ;
65import net .kalbskinder .mobHealth .configuration .Config ;
1514import org .bukkit .persistence .PersistentDataType ;
1615
1716@ Slf4j
18- @ RequiredArgsConstructor
1917public class RenameMobService {
2018
2119 private final HealthColorUtil healthColorUtil ;
2220 private final TextUtil textUtil ;
21+ private final NamespacedKey healthDisplayKey ;
22+
23+ public RenameMobService (HealthColorUtil healthColorUtil , TextUtil textUtil , MobHealth instance ) {
24+ this .healthColorUtil = healthColorUtil ;
25+ this .textUtil = textUtil ;
26+ this .healthDisplayKey = new NamespacedKey (instance , "mob_health_display" );
27+ }
2328
2429 public double getDefaultMaxHealth (LivingEntity entity ) {
2530 return entity .getAttribute (Attribute .MAX_HEALTH ).getValue ();
@@ -77,6 +82,8 @@ public void renameMob(Entity anyEntity, DisplaySetting activeDisplaySetting) {
7782 }
7883
7984 private void renameMobSkyblock (LivingEntity entity , EntityProperties props ) {
85+ resetMobName (entity );
86+ entity .setCustomNameVisible (false );
8087
8188 String healthColor = healthColorUtil .getSkyblockHealthColor (props .health (), props .maxHealth ());
8289 String prefix = Config .HealthBars .Skyblock .PREFIX ();
@@ -105,7 +112,7 @@ private void renameMobSkyblock(LivingEntity entity, EntityProperties props) {
105112 }
106113
107114 private void renameMobSprite (Entity entity , EntityProperties props ) {
108- entity . setCustomNameVisible ( false );
115+ resetMobName ( entity );
109116
110117 int totalHalfHeartsHealth = (int ) Math .round (props .health ());
111118 int fullSprites = totalHalfHeartsHealth / 2 ;
@@ -148,18 +155,9 @@ private void renameMobSprite(Entity entity, EntityProperties props) {
148155 String displayName = "%s\n %s%s" .formatted (convertedPrefix , heartsDisplay , convertedSuffix );
149156
150157 World world = entity .getWorld ();
151- NamespacedKey healthDisplayKey = new NamespacedKey (MobHealth .getInstance (), "mob_health_display" );
152- NamespacedKey healthDisplayBgKey = new NamespacedKey (MobHealth .getInstance (), "mob_health_display_bg" );
153158 String entityId = entity .getUniqueId ().toString ();
154159
155160 // Remove any lingering background display from previous implementation
156- world .getEntities ().stream ()
157- .filter (e -> e instanceof TextDisplay )
158- .map (e -> (TextDisplay ) e )
159- .filter (e -> e .getPersistentDataContainer ().has (healthDisplayBgKey , PersistentDataType .STRING )
160- && entityId .equals (e .getPersistentDataContainer ().get (healthDisplayBgKey , PersistentDataType .STRING )))
161- .forEach (Entity ::remove );
162-
163161 TextDisplay textDisplay = world .getEntities ().stream ()
164162 .filter (e -> e instanceof TextDisplay )
165163 .map (e -> (TextDisplay ) e )
@@ -183,6 +181,7 @@ private void renameMobSprite(Entity entity, EntityProperties props) {
183181 }
184182
185183 public void renameMobSymbol (Entity entity , EntityProperties props ) {
184+ resetMobName (entity );
186185 String symbol = Config .HealthBars .Symbols .SYMBOL ();
187186 String healthColor = healthColorUtil .getSymbolHealthColor (props .health (), props .maxHealth ());
188187 String prefix = Config .HealthBars .Symbols .PREFIX ();
@@ -192,12 +191,10 @@ public void renameMobSymbol(Entity entity, EntityProperties props) {
192191 prefix = "%s %s" .formatted (props .levelPrefix (), prefix );
193192 }
194193
195- double roundedHealth = Math .max (0 , roundToHalf (props .health ()));
196- String symbols = "" ;
197-
198- for (int i = 0 ; i < (int ) roundedHealth ; i ++) {
199- symbols = healthColor + symbol ;
200- }
194+ // 1 symbol per 2 HP, max 20, minimum 1
195+ int symbolsToShow = (int ) Math .ceil (props .health () / 2.0 );
196+ symbolsToShow = Math .max (1 , Math .min (symbolsToShow , 20 ));
197+ String symbols = symbol .repeat (symbolsToShow );
201198
202199 String healthBarDisplay = String .format ("%s%s" , healthColor , symbols );
203200
@@ -219,4 +216,16 @@ private void formatMobDisplayName(Entity entity, EntityProperties props, String
219216 entity .setCustomName (textUtil .parseLegacy (mobDisplayName ));
220217 }
221218 }
219+
220+ private void resetMobName (Entity entity ) {
221+ String entityId = entity .getUniqueId ().toString ();
222+ entity .getPassengers ().stream ()
223+ .filter (e -> e instanceof TextDisplay )
224+ .map (e -> (TextDisplay ) e )
225+ .filter (e -> e .getPersistentDataContainer ().has (healthDisplayKey , PersistentDataType .STRING )
226+ && entityId .equals (e .getPersistentDataContainer ().get (healthDisplayKey , PersistentDataType .STRING )))
227+ .forEach (Entity ::remove );
228+
229+ entity .setCustomName (null );
230+ }
222231}
0 commit comments