1313import lombok .Setter ;
1414import net .minecraft .client .Minecraft ;
1515import net .minecraft .client .gui .Gui ;
16+ import net .minecraft .client .gui .components .toasts .SystemToast ;
1617import net .minecraft .client .multiplayer .ClientLevel ;
1718import net .minecraft .client .multiplayer .ClientPacketListener ;
1819import net .minecraft .client .player .LocalPlayer ;
@@ -70,7 +71,7 @@ public class DevUtils {
7071 // If you change this, please change it in the string "commands.usage.sba.help.copyEntity" as well.
7172 public static final int DEFAULT_ENTITY_COPY_RADIUS = 3 ;
7273 private static final List <Class <? extends Entity >> DEFAULT_ENTITY_NAMES = Collections .singletonList (LivingEntity .class );
73- private static final boolean DEFAULT_SIDEBAR_FORMATTED = false ;
74+ public static final boolean DEFAULT_SIDEBAR_FORMATTED = false ;
7475
7576 @ Getter @ Setter private static boolean loggingActionBarMessages = false ;
7677 @ Getter @ Setter private static boolean loggingSlayerTracker = false ;
@@ -98,17 +99,8 @@ public class DevUtils {
9899
99100 /**
100101 * Copies the objective and scores that are being displayed on a scoreboard's sidebar.
101- * When copying the sidebar, the control codes (e.g. §a) are removed.
102102 */
103- public static void copyScoreboardSideBar () {
104- copyScoreboardSidebar (sidebarFormatted );
105- }
106-
107- /**
108- * Copies the objective and scores that are being displayed on a scoreboard's sidebar.
109- * @param stripControlCodes if {@code true}, the control codes will be removed, otherwise they will be copied
110- */
111- private static void copyScoreboardSidebar (boolean stripControlCodes ) {
103+ private static void copyScoreboardSidebar () {
112104 if (MC .level == null ) return ;
113105
114106 Scoreboard scoreboard = MC .level .getScoreboard ();
@@ -123,9 +115,9 @@ private static void copyScoreboardSidebar(boolean stripControlCodes) {
123115 return ;
124116 }
125117
126- String title = stripControlCodes
127- ? TextUtils .stripColor (sideBarObjective .getDisplayName (). getString () )
128- : TextUtils .getFormattedText (sideBarObjective .getDisplayName (), true );
118+ String title = sidebarFormatted
119+ ? TextUtils .getFormattedText (sideBarObjective .getDisplayName (), true )
120+ : TextUtils .stripColor (sideBarObjective .getDisplayName (). getString () );
129121 StringBuilder stringBuilder = new StringBuilder (title ).append ("\n " );
130122
131123 scoreboard .listPlayerScores (sideBarObjective ).stream ()
@@ -140,19 +132,18 @@ private static void copyScoreboardSidebar(boolean stripControlCodes) {
140132 Component decoratedName = PlayerTeam .formatNameForTeam (team , name );
141133
142134 // return fixed name
143- String text = stripControlCodes
144- ? TextUtils .stripColor (decoratedName .getString ())
145- : TextUtils .getFormattedText (decoratedName , true );
146- stringBuilder .append (text .replace (owner , "" ));
147-
148- if (!stripControlCodes ) {
149- stringBuilder .append (" [" ).append (scoreboardEntry .value ()).append ("]" );
150- }
135+ String text = sidebarFormatted
136+ ? TextUtils .getFormattedText (decoratedName , true )
137+ : TextUtils .stripColor (decoratedName .getString ());
151138
152- stringBuilder .append ("\n " );
139+ // replace control codes (e.g. §j)
140+ stringBuilder .append (sidebarFormatted ? text : text .replace (owner , "" ));
141+ stringBuilder .append (" [" ).append (scoreboardEntry .value ()).append ("]" ).append ("\n " );
153142 });
154143
155- copyStringToClipboard (stringBuilder .toString (), ColorCode .GREEN + "Sidebar copied to clipboard!" );
144+ copyStringToClipboard (
145+ stringBuilder .toString (), ColorCode .GREEN + "Sidebar copied to clipboard!" , true
146+ );
156147 }
157148
158149 /**
@@ -212,7 +203,9 @@ private static void copyEntityData(List<Class<? extends Entity>> includedEntityC
212203 }
213204
214205 if (!stringBuilder .isEmpty ()) {
215- copyStringToClipboard (stringBuilder .toString (), ColorCode .GREEN + "Entity data was copied to clipboard!" );
206+ copyStringToClipboard (
207+ stringBuilder .toString (), ColorCode .GREEN + "Entity data was copied to clipboard!" , true
208+ );
216209 } else {
217210 Utils .sendErrorMessage ("No entities matching the given parameters were found." );
218211 }
@@ -292,7 +285,7 @@ public static void copyData() {
292285 copyEntityData ();
293286 break ;
294287 case SIDEBAR :
295- copyScoreboardSideBar ();
288+ copyScoreboardSidebar ();
296289 break ;
297290 case TAB_LIST :
298291 copyTabListHeaderAndFooter ();
@@ -311,7 +304,7 @@ public static void copyNBTTagToClipboard(Tag nbtTag, String message) {
311304 Utils .sendErrorMessage ("This item has no NBT data!" );
312305 return ;
313306 }
314- writeToClipboard (prettyPrintNBT (nbtTag ), message );
307+ writeToClipboard (prettyPrintNBT (nbtTag ), message , true );
315308 }
316309
317310 /**
@@ -342,7 +335,8 @@ public static void copyTabListHeaderAndFooter() {
342335
343336 copyStringToClipboard (
344337 output .toString (),
345- ColorCode .GREEN + "Successfully copied the tab list header and footer to clipboard!"
338+ ColorCode .GREEN + "Successfully copied the tab list header and footer to clipboard!" ,
339+ true
346340 );
347341 }
348342
@@ -364,8 +358,7 @@ public static void copyOpenGLLogs() {
364358 ```
365359 """ .formatted (cpu , gpu , version , lwjgl );
366360 copyStringToClipboard (
367- output ,
368- ColorCode .GREEN + "Successfully copied the OpenGL logs to clipboard!"
361+ output , ColorCode .GREEN + "Successfully copied the OpenGL logs to clipboard!" , true
369362 );
370363 }
371364
@@ -375,9 +368,10 @@ public static void copyOpenGLLogs() {
375368 *
376369 * @param string the string to copy
377370 * @param successMessage the custom message to show after successful copy
371+ * @param showToast show {@link net.minecraft.client.gui.components.toasts.Toast} instead of chat message
378372 */
379- public static void copyStringToClipboard (String string , String successMessage ) {
380- writeToClipboard (string , successMessage );
373+ public static void copyStringToClipboard (String string , String successMessage , boolean showToast ) {
374+ writeToClipboard (string , successMessage , showToast );
381375 }
382376
383377 /**
@@ -431,7 +425,7 @@ public static void copyBlockData() {
431425 }
432426 }
433427
434- writeToClipboard (prettyPrintNBT (nbt ), ColorCode .GREEN + "Successfully copied the block data!" );
428+ writeToClipboard (prettyPrintNBT (nbt ), ColorCode .GREEN + "Successfully copied the block data!" , true );
435429 }
436430
437431 /**
@@ -601,11 +595,24 @@ public static void reloadResources() {
601595 }
602596
603597 // Internal methods
604- private static void writeToClipboard (String text , String successMessage ) {
598+ private static void writeToClipboard (String text , String successMessage , boolean showToast ) {
605599 try {
606600 MC .keyboardHandler .setClipboard (text );
607601 if (successMessage != null ) {
608- Utils .sendMessage (successMessage );
602+ if (showToast ) {
603+ try {
604+ MC .getToastManager ().addToast (new SystemToast ( // TODO custom Toast
605+ new SystemToast .SystemToastId (2000L ),
606+ Utils .COMPONENT_TITLE ,
607+ Component .literal (successMessage )
608+ ));
609+ } catch (Exception e ) {
610+ LOGGER .error ("Couldn't add Toast!" , e );
611+ Utils .sendMessage (successMessage );
612+ }
613+ } else {
614+ Utils .sendMessage (successMessage );
615+ }
609616 }
610617 } catch (IllegalStateException exception ) {
611618 Utils .sendErrorMessage ("Clipboard not available!" );
0 commit comments