@@ -27,6 +27,11 @@ public class Player extends Mob {
2727 private boolean [] swimType ;
2828 private int [] swimColour ;
2929 private int fireRate = 0 ;
30+
31+ // Need to add a class for constants
32+ private final int fontCharSize = 8 ;
33+ // "Cache" the division for the username length, no need for 60 divisions per second here.
34+ private int nameOffset = 0 ;
3035
3136 public Player (LevelHandler level , int x , int y , InputHandler input ,
3237 String userName , int shirtCol , int faceCol ) {
@@ -192,12 +197,12 @@ public void render(Screen screen) {
192197 * Using player's own x value cast to int with an adjusted formula
193198 * -posmicanomaly
194199 */
195- int fontCharSize = 8 ;
200+
196201 Font .render (userName ,
197202 screen ,
198- (int )x - (( userName . length () / 2 ) * fontCharSize ) ,
203+ (int )x - nameOffset ,
199204 yOffset - 10 ,
200- Colours .get (-1 , -1 , -1 , 555 ), 1 );
205+ Colours .get (-1 , -1 , -1 , 111 ), 1 );
201206
202207 }
203208 }
@@ -216,8 +221,17 @@ public void setUsername(String name) {
216221 public String getSanitisedUsername () {
217222 if (this .getUsername () == null || this .userName .isEmpty ()) {
218223 setUsername (guestPlayerName );
224+
225+ // Perfectly matches the text over the head
226+ int offsetUnit = ((userName .length () & 1 ) == 0 ? fontCharSize / 2 : 0 );
227+ nameOffset = (userName .length () / 2 ) * fontCharSize - offsetUnit ;
228+
219229 return guestPlayerName ;
220230 } else
231+ if (nameOffset == 0 ){
232+ int offsetUnit = ((userName .length () & 1 ) == 0 ? fontCharSize / 2 : 0 );
233+ nameOffset = (userName .length () / 2 ) * fontCharSize - offsetUnit ;
234+ }
221235 return this .getUsername ();
222236 }
223237
0 commit comments