1111import org .bukkit .event .EventPriority ;
1212import org .bukkit .event .Listener ;
1313
14+ import java .util .ArrayList ;
15+ import java .util .List ;
16+
1417public class ServerPingListener implements Listener {
1518
1619 @ EventHandler (priority = EventPriority .HIGH )
1720 public void onServerPing (PaperServerListPingEvent event ) {
1821 String serverName = ServerSettingsAPI .getApi ().getServerName ();
19- int totalWidth = 25 ;
22+ String serverVersion = ServerSettingsAPI .getApi ().getServerVersion ();
23+
24+ int totalWidth = 45 ;
2025 int nameLength = serverName .length ();
2126
22- int spacesCount = Math .max (0 , totalWidth - nameLength );
27+ int spacesCount = Math .max (0 , ( totalWidth - ( nameLength + serverVersion . length ())) / 2 );
2328 String spaces2 = " " .repeat (spacesCount );
2429
2530 int length = serverName .length ();
2631
2732 Component serverNameComponent = Component .empty ();
2833 for (int i = 0 ; i < length ; i ++) {
2934 serverNameComponent = serverNameComponent .append (Gradient .gradient (
30- String .valueOf (serverName .charAt (i )),
31- "#50FB08" ,
32- "#006EFF" ,
33- i ,
34- length - 1
35+ String .valueOf (serverName .charAt (i )),
36+ "#50FB08" ,
37+ "#006EFF" ,
38+ i ,
39+ length - 1
3540 ));
3641 }
3742
38- Component pingMessage = Component .text (spaces2 )
43+ Component header = Component .text (spaces2 )
3944 .append (Component .text ("M " , NamedTextColor .GOLD , TextDecoration .OBFUSCATED ))
4045 .append (serverNameComponent .decorate (TextDecoration .BOLD ))
4146 .append (Component .text (" [" , NamedTextColor .WHITE ))
42- .append (Component .text (ServerSettingsAPI . getApi (). getServerVersion () , NamedTextColor .GOLD ))
47+ .append (Component .text (serverVersion , NamedTextColor .GOLD ))
4348 .append (Component .text ("] " , NamedTextColor .WHITE ))
4449 .append (Component .text (" M" , NamedTextColor .GOLD , TextDecoration .OBFUSCATED ))
4550 .append (Component .text ("\n " ));
51+
52+ int infoWidth = totalWidth ;
53+ int extraShift = 5 ;
54+
4655 if (Bukkit .hasWhitelist ()) {
47- event .motd (pingMessage .append (Component .text (" Info: " , NamedTextColor .GREEN )).append (Component .text ("Whitelist enabled!" , NamedTextColor .GOLD )));
56+ String info = "Info: Whitelist enabled!" ;
57+ Component centered = centerLines (wrapText (info , infoWidth ), infoWidth , NamedTextColor .GOLD , extraShift );
58+ event .motd (header .append (centered ));
4859 return ;
4960 }
50- String spaces = ServerSettingsAPI . getApi (). getMaintenanceReason (). length () > 80 ? " " : " " . repeat (( 60 - ServerSettingsAPI . getApi (). getMaintenanceReason (). length ()) / 2 );
61+
5162 if (ServerSettingsAPI .getApi ().isMaintenanceEnabled ()) {
52- event .motd (pingMessage .append (Component .text (" Info: " , NamedTextColor .GREEN )).append (Component .text ("Server is in maintenance!" , NamedTextColor .RED ))
53- .append (Component .text ("\n " + spaces + ServerSettingsAPI .getApi ().getMaintenanceReason (), NamedTextColor .RED )));
63+ List <String > lines = new ArrayList <>();
64+ lines .add ("Info: Server is in maintenance!" );
65+ String maintenanceReason = ServerSettingsAPI .getApi ().getMaintenanceReason () == null ? "" : ServerSettingsAPI .getApi ().getMaintenanceReason ().trim ();
66+ if (!maintenanceReason .isEmpty ()) {
67+ lines .addAll (wrapText (maintenanceReason , infoWidth ));
68+ }
69+ Component firstLine = centerLines (List .of (lines .get (0 )), infoWidth , NamedTextColor .GREEN , extraShift );
70+ List <String > detailLines = lines .size () > 1 ? lines .subList (1 , lines .size ()) : List .of ();
71+ Component details = centerLines (detailLines , infoWidth , NamedTextColor .RED , extraShift );
72+ event .motd (header .append (firstLine ).append (details ));
5473 return ;
5574 }
75+
5676 if (Bukkit .getOnlinePlayers ().size () >= Bukkit .getMaxPlayers ()) {
57- event .motd (pingMessage .append (Component .text (" Info: " , NamedTextColor .GREEN )).append (Component .text ( "Server is full!" , NamedTextColor .RED )));
77+ String info = "Info: Server is full!" ;
78+ Component centered = centerLines (wrapText (info , infoWidth ), infoWidth , NamedTextColor .RED , extraShift );
79+ event .motd (header .append (centered ));
5880 return ;
5981 }
60- event .motd (pingMessage .append (Component .text (" Info: " , NamedTextColor .GREEN )).append (Component .text ("Server is online!" , NamedTextColor .GREEN )));
82+
83+ String info = "Info: Server is online!" ;
84+ Component centered = centerLines (wrapText (info , infoWidth ), infoWidth , NamedTextColor .GREEN , extraShift );
85+ event .motd (header .append (centered ));
86+ }
87+
88+ private static List <String > wrapText (String text , int width ) {
89+ List <String > lines = new ArrayList <>();
90+ if (text == null || text .isEmpty ()) {
91+ return lines ;
92+ }
93+
94+ String [] words = text .split ("\\ s+" );
95+ StringBuilder current = new StringBuilder ();
96+
97+ for (String w : words ) {
98+ if (current .length () == 0 ) {
99+ current .append (w );
100+ } else if (current .length () + 1 + w .length () <= width ) {
101+ current .append (' ' ).append (w );
102+ } else {
103+ lines .add (current .toString ());
104+ current = new StringBuilder (w );
105+ }
106+ }
107+ if (current .length () > 0 ) lines .add (current .toString ());
108+ return lines ;
109+ }
110+
111+ private static Component centerLines (List <String > lines , int width , net .kyori .adventure .text .format .NamedTextColor color , int extraShift ) {
112+ if (lines == null || lines .isEmpty ()) {
113+ return Component .empty ();
114+ }
115+ Component result = Component .empty ();
116+ for (int i = 0 ; i < lines .size (); i ++) {
117+ String line = lines .get (i );
118+ int padding = Math .max (0 , (width - line .length ()) / 2 + extraShift );
119+ String pad = " " .repeat (padding );
120+ result = result .append (Component .text (pad )).append (Component .text (line , color ));
121+ if (i < lines .size () - 1 ) {
122+ result = result .append (Component .text ("\n " )); // removed the extra single space here
123+ }
124+ }
125+ return result ;
61126 }
62127}
0 commit comments