1010import net .betterhorses .common .progress .ProgressableHorse ;
1111import net .betterhorses .common .progress .HorseAttributeProgression ;
1212import net .betterhorses .common .util .MathUtils ;
13+ import net .minecraft .client .MinecraftClient ;
1314import net .minecraft .client .font .TextRenderer ;
1415import net .minecraft .client .gui .DrawContext ;
1516import net .minecraft .entity .passive .HorseEntity ;
1617import net .minecraft .text .Text ;
1718
1819public class HorseStatsHud {
20+ public enum HudPosition {
21+ TOP_LEFT ,
22+ TOP_RIGHT ,
23+ CENTER_ABOVE_HUD
24+ }
25+
1926 public static void render (DrawContext context , TextRenderer textRenderer , HorseEntity horse ) {
2027 BetterHorsesConfig .HudConfig config = getConfig ().hud ;
2128
22- int baseX = (int ) (10 / config .fontScale );
23- int baseY = (int ) (10 / config .fontScale );
2429 int lineHeight = (int ) (10 / config .fontScale );
2530
2631 context .getMatrices ().push ();
2732 context .getMatrices ().scale (config .fontScale , config .fontScale , 1.0f );
2833
2934 HorseStats stats = collectHorseStats (horse );
3035 formatUnitsIfNeeded (stats , config );
36+
37+ int [] position = calculateHudPosition (context , textRenderer , stats , config );
38+ int baseX = (int ) (position [0 ] / config .fontScale );
39+ int baseY = (int ) (position [1 ] / config .fontScale );
40+
3141 drawHorseStats (context , textRenderer , stats , baseX , baseY , lineHeight , config );
3242
3343 context .getMatrices ().pop ();
@@ -37,6 +47,114 @@ private static BetterHorsesConfig getConfig() {
3747 return AutoConfig .getConfigHolder (BetterHorsesConfig .class ).getConfig ();
3848 }
3949
50+ private static int [] calculateHudPosition (DrawContext context , TextRenderer textRenderer , HorseStats stats , BetterHorsesConfig .HudConfig config ) {
51+ MinecraftClient client = MinecraftClient .getInstance ();
52+ int screenWidth = client .getWindow ().getScaledWidth ();
53+ int screenHeight = client .getWindow ().getScaledHeight ();
54+ int lineHeight = (int ) (10 / config .fontScale );
55+
56+ switch (config .hudPosition ) {
57+ case TOP_RIGHT :
58+ int textWidth = getMaxTextWidth (textRenderer , stats , config );
59+ return new int []{screenWidth - textWidth - 10 , 10 };
60+ case CENTER_ABOVE_HUD :
61+ int centerTextWidth = getMaxTextWidth (textRenderer , stats , config );
62+ int centerX = (screenWidth - centerTextWidth ) / 2 ;
63+ int totalLines = getTotalLines (stats , config );
64+ int totalBlockHeight = totalLines * lineHeight ;
65+ int centerY = screenHeight - 50 - totalBlockHeight ;
66+
67+ return new int []{centerX , Math .max (10 , centerY )};
68+ default :
69+ return new int []{10 , 10 };
70+ }
71+ }
72+
73+ private static int getTotalLines (HorseStats stats , BetterHorsesConfig .HudConfig config ) {
74+ int lines = 0 ;
75+
76+ if (config .showBreed || config .showAttributes ) lines += 1 ; // breed + attributes in one line
77+ if (config .showLevels ) lines += 2 ; // speed level + jump level
78+ if (config .showNextLevelInfo ) lines += 2 ; // next speed + next jump
79+ if (config .showMaxAttributes ) lines += 1 ; // max speed + max jump in one line
80+ if (config .showBaseAttributes ) lines += 1 ; // orig speed + orig jump in one line
81+ if (config .showTotalProgress ) lines += 1 ; // total distance + total jumps in one line
82+
83+ return lines ;
84+ }
85+
86+ private static int getMaxTextWidth (TextRenderer textRenderer , HorseStats stats , BetterHorsesConfig .HudConfig config ) {
87+ int maxWidth = 0 ;
88+
89+ if (config .showBreed || config .showAttributes ) {
90+ StringBuilder text = new StringBuilder ();
91+
92+ if (config .showBreed ) {
93+ text .append (Text .translatable ("hud.betterhorses.breed" ).getString ()).append (" " ).append (stats .breed .getDisplayName ());
94+ }
95+
96+ if (config .showAttributes ) {
97+ if (!text .isEmpty ()) {
98+ text .append (" | " );
99+ }
100+
101+ text
102+ .append (Text .translatable ("hud.betterhorses.speed" ).getString ())
103+ .append (" " )
104+ .append (MathUtils .round2digits (stats .speed ))
105+ .append (stats .speedUnit )
106+ .append (" | " )
107+ .append (Text .translatable ("hud.betterhorses.jump" ).getString ())
108+ .append (" " )
109+ .append (MathUtils .round2digits (stats .jumpStrength ))
110+ .append (stats .jumpUnit );
111+ }
112+
113+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (text .toString ()));
114+ }
115+
116+ if (config .showLevels ) {
117+ String speedLevelText = Text .translatable ("hud.betterhorses.speed_level" ).getString () + " " + stats .speedLevel + "/10 (" + MathUtils .round2digits (stats .speedProgress * 100 ) + "%)" ;
118+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (speedLevelText ));
119+
120+ String jumpLevelText = Text .translatable ("hud.betterhorses.jump_level" ).getString () + " " + stats .jumpLevel + "/10 (" + MathUtils .round2digits (stats .jumpProgress * 100 ) + "%)" ;
121+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (jumpLevelText ));
122+ }
123+
124+ if (config .showNextLevelInfo ) {
125+ if (stats .speedLevel < 10 ) {
126+ String speedText = Text .translatable ("hud.betterhorses.next_speed_level" ).getString () + " " + (stats .speedLevel + 1 ) + ": " + (int ) stats .remainingDistance + " " + Text .translatable ("hud.betterhorses.blocks" ).getString ();
127+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (speedText ));
128+ } else {
129+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (Text .translatable ("hud.betterhorses.speed_max_level" ).getString ()));
130+ }
131+
132+ if (stats .jumpLevel < 10 ) {
133+ String jumpText = Text .translatable ("hud.betterhorses.next_jump_level" ).getString () + " " + (stats .jumpLevel + 1 ) + ": " + (int ) stats .remainingJumps + " " + Text .translatable ("hud.betterhorses.jumps" ).getString ();
134+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (jumpText ));
135+ } else {
136+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (Text .translatable ("hud.betterhorses.jump_max_level" ).getString ()));
137+ }
138+ }
139+
140+ if (config .showMaxAttributes ) {
141+ String text = Text .translatable ("hud.betterhorses.max_speed" ).getString () + " " + MathUtils .round2digits (stats .maxSpeed ) + stats .speedUnit + " | " + Text .translatable ("hud.betterhorses.max_jump" ).getString () + " " + MathUtils .round2digits (stats .maxJump ) + stats .jumpUnit ;
142+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (text ));
143+ }
144+
145+ if (config .showBaseAttributes ) {
146+ String text = Text .translatable ("hud.betterhorses.original_speed" ).getString () + " " + MathUtils .round2digits (stats .originalSpeed ) + stats .speedUnit + " | " + Text .translatable ("hud.betterhorses.original_jump" ).getString () + " " + MathUtils .round2digits (stats .originalJump ) + stats .jumpUnit ;
147+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (text ));
148+ }
149+
150+ if (config .showTotalProgress ) {
151+ String text = Text .translatable ("hud.betterhorses.total_distance" ).getString () + " " + stats .progress .getRunningDistance () + " | " + Text .translatable ("hud.betterhorses.total_jumps" ).getString () + " " + stats .progress .getJumpCount ();
152+ maxWidth = Math .max (maxWidth , textRenderer .getWidth (text ));
153+ }
154+
155+ return (int ) (maxWidth * config .fontScale );
156+ }
157+
40158 private static HorseStats collectHorseStats (HorseEntity horse ) {
41159 HorseStats s = new HorseStats ();
42160
@@ -78,38 +196,46 @@ private static void formatUnitsIfNeeded(HorseStats s, BetterHorsesConfig.HudConf
78196 s .originalSpeed = MathUtils .moveAttributeToBlocksPerSecond (s .originalSpeed );
79197 s .originalJump = MathUtils .jumpAttributeToBlocks (s .originalJump );
80198
81- s .speedUnit = " б/с" ;
82- s .jumpUnit = " б" ;
199+ s .speedUnit = " " + Text . translatable ( "hud.betterhorses.speedUnit" ). getString () ;
200+ s .jumpUnit = " " + Text . translatable ( "hud.betterhorses.jumpUnit" ). getString () ;
83201 }
84202
85203 private static void drawHorseStats (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int lineHeight , BetterHorsesConfig .HudConfig config ) {
86204 int line = 0 ;
87205
88- line = drawGroupBreed (context , renderer , s , x , y , line , lineHeight , config );
89- line = drawGroupAttributes (context , renderer , s , x , y , line , lineHeight , config );
206+ line = drawGroupBreedAndAttributes (context , renderer , s , x , y , line , lineHeight , config );
90207 line = drawGroupLevels (context , renderer , s , x , y , line , lineHeight , config );
91208 line = drawGroupNextLevelInfo (context , renderer , s , x , y , line , lineHeight , config );
92209 line = drawGroupMaxAttributes (context , renderer , s , x , y , line , lineHeight , config );
93210 line = drawGroupBaseAttributes (context , renderer , s , x , y , line , lineHeight , config );
94211 drawGroupTotalProgress (context , renderer , s , x , y , line , lineHeight , config );
95212 }
96213
97- private static int drawGroupBreed (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
98- if (!config .showBreed ) return line ;
99-
100- context .drawText (renderer , Text .literal ("Порода: " + s .breed .getDisplayName ()), x , y + lineHeight * line , 0xFFFFFF , false );
101- line ++;
102-
103- return line ;
104- }
105-
106- private static int drawGroupAttributes (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
107- if (!config .showAttributes ) return line ;
108-
109- context .drawText (renderer , Text .literal ("Скорость: " + MathUtils .round2digits (s .speed ) + s .speedUnit ), x , y + lineHeight * line , 0xFFFFFF , false );
110- line ++;
214+ private static int drawGroupBreedAndAttributes (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
215+ if (!config .showBreed && !config .showAttributes ) return line ;
111216
112- context .drawText (renderer , Text .literal ("Прыжок: " + MathUtils .round2digits (s .jumpStrength ) + s .jumpUnit ), x , y + lineHeight * line , 0xFFFFFF , false );
217+ StringBuilder text = new StringBuilder ();
218+
219+ if (config .showBreed ) {
220+ text .append (Text .translatable ("hud.betterhorses.breed" ).getString ()).append (" " ).append (s .breed .getDisplayName ());
221+ }
222+
223+ if (config .showAttributes ) {
224+ if (!text .isEmpty ()) text .append (" | " );
225+
226+ text
227+ .append (Text .translatable ("hud.betterhorses.speed" ).getString ())
228+ .append (" " )
229+ .append (MathUtils .round2digits (s .speed ))
230+ .append (s .speedUnit )
231+ .append (" | " )
232+ .append (Text .translatable ("hud.betterhorses.jump" ).getString ())
233+ .append (" " )
234+ .append (MathUtils .round2digits (s .jumpStrength ))
235+ .append (s .jumpUnit );
236+ }
237+
238+ context .drawText (renderer , Text .literal (text .toString ()), x , y + lineHeight * line , 0xFFFFFF , false );
113239 line ++;
114240
115241 return line ;
@@ -118,10 +244,12 @@ private static int drawGroupAttributes(DrawContext context, TextRenderer rendere
118244 private static int drawGroupLevels (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
119245 if (!config .showLevels ) return line ;
120246
121- context .drawText (renderer , Text .literal ("Уровень скорости: " + s .speedLevel + "/10 (" + MathUtils .round2digits (s .speedProgress * 100 ) + "%)" ), x , y + lineHeight * line , 0x00FF00 , false );
247+ String speedLevelText = Text .translatable ("hud.betterhorses.speed_level" ).getString () + " " + s .speedLevel + "/10 (" + MathUtils .round2digits (s .speedProgress * 100 ) + "%)" ;
248+ context .drawText (renderer , Text .literal (speedLevelText ), x , y + lineHeight * line , 0x00FF00 , false );
122249 line ++;
123250
124- context .drawText (renderer , Text .literal ("Уровень прыжков: " + s .jumpLevel + "/10 (" + MathUtils .round2digits (s .jumpProgress * 100 ) + "%)" ), x , y + lineHeight * line , 0x00FF00 , false );
251+ String jumpLevelText = Text .translatable ("hud.betterhorses.jump_level" ).getString () + " " + s .jumpLevel + "/10 (" + MathUtils .round2digits (s .jumpProgress * 100 ) + "%)" ;
252+ context .drawText (renderer , Text .literal (jumpLevelText ), x , y + lineHeight * line , 0x00FF00 , false );
125253 line ++;
126254
127255 return line ;
@@ -131,16 +259,18 @@ private static int drawGroupNextLevelInfo(DrawContext context, TextRenderer rend
131259 if (!config .showNextLevelInfo ) return line ;
132260
133261 if (s .speedLevel < 10 ) {
134- context .drawText (renderer , Text .literal ("До след. ур. скорости " + (s .speedLevel + 1 ) + ": " + (int ) s .remainingDistance + " блоков" ), x , y + lineHeight * line , 0x88FF88 , false );
262+ String speedText = Text .translatable ("hud.betterhorses.next_speed_level" ).getString () + " " + (s .speedLevel + 1 ) + ": " + (int ) s .remainingDistance + " " + Text .translatable ("hud.betterhorses.blocks" ).getString ();
263+ context .drawText (renderer , Text .literal (speedText ), x , y + lineHeight * line , 0x88FF88 , false );
135264 } else {
136- context .drawText (renderer , Text .literal ( "Скорость: МАКС УРОВЕНЬ " ), x , y + lineHeight * line , 0xFFD700 , false );
265+ context .drawText (renderer , Text .translatable ( "hud.betterhorses.speed_max_level " ), x , y + lineHeight * line , 0xFFD700 , false );
137266 }
138267 line ++;
139268
140269 if (s .jumpLevel < 10 ) {
141- context .drawText (renderer , Text .literal ("До след. ур. прыжков " + (s .jumpLevel + 1 ) + ": " + (int ) s .remainingJumps + " прыжков" ), x , y + lineHeight * line , 0x88FF88 , false );
270+ String jumpText = Text .translatable ("hud.betterhorses.next_jump_level" ).getString () + " " + (s .jumpLevel + 1 ) + ": " + (int ) s .remainingJumps + " " + Text .translatable ("hud.betterhorses.jumps" ).getString ();
271+ context .drawText (renderer , Text .literal (jumpText ), x , y + lineHeight * line , 0x88FF88 , false );
142272 } else {
143- context .drawText (renderer , Text .literal ( "Прыжки: МАКС УРОВЕНЬ " ), x , y + lineHeight * line , 0xFFD700 , false );
273+ context .drawText (renderer , Text .translatable ( "hud.betterhorses.jump_max_level " ), x , y + lineHeight * line , 0xFFD700 , false );
144274 }
145275 line ++;
146276
@@ -150,10 +280,8 @@ private static int drawGroupNextLevelInfo(DrawContext context, TextRenderer rend
150280 private static int drawGroupMaxAttributes (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
151281 if (!config .showMaxAttributes ) return line ;
152282
153- context .drawText (renderer , Text .literal ("Макс. скорость: " + MathUtils .round2digits (s .maxSpeed ) + s .speedUnit ), x , y + lineHeight * line , 0xFFFF00 , false );
154- line ++;
155-
156- context .drawText (renderer , Text .literal ("Макс. прыжок: " + MathUtils .round2digits (s .maxJump ) + s .jumpUnit ), x , y + lineHeight * line , 0xFFFF00 , false );
283+ String text = Text .translatable ("hud.betterhorses.max_speed" ).getString () + " " + MathUtils .round2digits (s .maxSpeed ) + s .speedUnit + " | " + Text .translatable ("hud.betterhorses.max_jump" ).getString () + " " + MathUtils .round2digits (s .maxJump ) + s .jumpUnit ;
284+ context .drawText (renderer , Text .literal (text ), x , y + lineHeight * line , 0xFFFF00 , false );
157285 line ++;
158286
159287 return line ;
@@ -162,10 +290,8 @@ private static int drawGroupMaxAttributes(DrawContext context, TextRenderer rend
162290 private static int drawGroupBaseAttributes (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
163291 if (!config .showBaseAttributes ) return line ;
164292
165- context .drawText (renderer , Text .literal ("Ориг. скорость: " + MathUtils .round2digits (s .originalSpeed ) + s .speedUnit ), x , y + lineHeight * line , 0xCCCCCC , false );
166- line ++;
167-
168- context .drawText (renderer , Text .literal ("Ориг. прыжок: " + MathUtils .round2digits (s .originalJump ) + s .jumpUnit ), x , y + lineHeight * line , 0xCCCCCC , false );
293+ String text = Text .translatable ("hud.betterhorses.original_speed" ).getString () + " " + MathUtils .round2digits (s .originalSpeed ) + s .speedUnit + " | " + Text .translatable ("hud.betterhorses.original_jump" ).getString () + " " + MathUtils .round2digits (s .originalJump ) + s .jumpUnit ;
294+ context .drawText (renderer , Text .literal (text ), x , y + lineHeight * line , 0xCCCCCC , false );
169295 line ++;
170296
171297 return line ;
@@ -174,10 +300,8 @@ private static int drawGroupBaseAttributes(DrawContext context, TextRenderer ren
174300 private static void drawGroupTotalProgress (DrawContext context , TextRenderer renderer , HorseStats s , int x , int y , int line , int lineHeight , BetterHorsesConfig .HudConfig config ) {
175301 if (!config .showTotalProgress ) return ;
176302
177- context .drawText (renderer , Text .literal ("Общая дистанция: " + s .progress .getRunningDistance ()), x , y + lineHeight * line , 0x888888 , false );
178- line ++;
179-
180- context .drawText (renderer , Text .literal ("Общие прыжки: " + s .progress .getJumpCount ()), x , y + lineHeight * line , 0x888888 , false );
303+ String text = Text .translatable ("hud.betterhorses.total_distance" ).getString () + " " + s .progress .getRunningDistance () + " | " + Text .translatable ("hud.betterhorses.total_jumps" ).getString () + " " + s .progress .getJumpCount ();
304+ context .drawText (renderer , Text .literal (text ), x , y + lineHeight * line , 0x888888 , false );
181305 }
182306
183307 private static class HorseStats {
0 commit comments