1111import gregtech .api .mui .GTGuis ;
1212import gregtech .api .util .GTLog ;
1313import gregtech .api .util .GTUtility ;
14+ import gregtech .api .util .JsonUtils ;
1415import gregtech .api .util .KeyUtil ;
1516import gregtech .api .util .TextFormattingUtil ;
1617import gregtech .common .ConfigHolder ;
2425import com .cleanroommc .modularui .api .widget .IWidget ;
2526import com .cleanroommc .modularui .drawable .DynamicDrawable ;
2627import com .cleanroommc .modularui .factory .PosGuiData ;
28+ import com .cleanroommc .modularui .network .NetworkUtils ;
2729import com .cleanroommc .modularui .screen .ModularPanel ;
28- import com .cleanroommc .modularui .screen .RichTooltip ;
2930import com .cleanroommc .modularui .utils .Alignment ;
3031import com .cleanroommc .modularui .value .sync .BooleanSyncValue ;
3132import com .cleanroommc .modularui .value .sync .IntSyncValue ;
3233import com .cleanroommc .modularui .value .sync .PanelSyncManager ;
34+ import com .cleanroommc .modularui .value .sync .SyncHandler ;
3335import com .cleanroommc .modularui .value .sync .ValueSyncHandler ;
3436import com .cleanroommc .modularui .widget .ParentWidget ;
3537import com .cleanroommc .modularui .widget .ScrollWidget ;
3638import com .cleanroommc .modularui .widget .Widget ;
3739import com .cleanroommc .modularui .widget .scroll .VerticalScrollData ;
38- import com .cleanroommc .modularui .widget .sizer .Area ;
3940import com .cleanroommc .modularui .widgets .CycleButtonWidget ;
4041import com .cleanroommc .modularui .widgets .ProgressWidget ;
4142import com .cleanroommc .modularui .widgets .RichTextWidget ;
@@ -136,8 +137,9 @@ public void readInitialSync(PacketBuffer buffer) {
136137 .child (createButtons (panel , panelSyncManager )));
137138 }
138139
139- private Widget <?> createIndicator () {
140+ private Widget <?> createIndicator (PanelSyncManager syncManager ) {
140141 var builder = builder ();
142+ builder .sync ("indicator" , syncManager );
141143 return new Widget <>()
142144 .size (18 )
143145 .pos (174 - 5 , screenHeight - 18 - 3 )
@@ -147,19 +149,16 @@ private Widget<?> createIndicator() {
147149 }
148150
149151 private IDrawable getIndicatorOverlay (Builder builder ) {
150- builder .clear ();
151- RichTooltip test = new RichTooltip (new Area ());
152-
153- this .errorText .accept (builder );
154- builder .build (test );
155- if (!test .isEmpty ()) {
152+ builder .setAction (this .errorText );
153+ builder .build ();
154+ if (!builder .isEmpty ()) {
156155 // error
157156 return GTGuiTextures .GREGTECH_LOGO_BLINKING_RED ;
158157 }
159158
160- this .warningText . accept ( builder );
161- builder .build (test );
162- if (!test .isEmpty ()) {
159+ builder . setAction ( this .warningText );
160+ builder .build ();
161+ if (!builder .isEmpty ()) {
163162 // warn
164163 return GTGuiTextures .GREGTECH_LOGO_BLINKING_YELLOW ;
165164 }
@@ -309,19 +308,25 @@ public MultiblockUIFactory customScreen(Supplier<ParentWidget<?>> customScreen)
309308 }
310309
311310 protected Widget <?> createScreen (PanelSyncManager syncManager ) {
312- final var builder = builder ();
313- this .displayText .accept (builder );
314-
315- return new ParentWidget <>()
316- .child (customScreen != null ? customScreen .get () : new ScrollWidget <>(new VerticalScrollData ())
317- .sizeRel (1f )
318- .child (new RichTextWidget ()
319- .sizeRel (1f )
320- .alignment (Alignment .TopLeft )
321- .margin (4 , 4 )
322- .autoUpdate (true )
323- .textBuilder (builder ::build )))
324- .child (createIndicator ())
311+ ParentWidget <?> root = new ParentWidget <>();
312+ if (customScreen != null && customScreen .get () != null ) {
313+ root .child (customScreen .get ());
314+ } else {
315+ Builder display = builder ();
316+ display .setAction (this .displayText );
317+ display .sync ("display" , syncManager );
318+
319+ root .child (new ScrollWidget <>(new VerticalScrollData ())
320+ .sizeRel (1f )
321+ .child (new RichTextWidget ()
322+ .sizeRel (1f )
323+ .alignment (Alignment .TopLeft )
324+ .margin (4 , 4 )
325+ .autoUpdate (true )
326+ .textBuilder (display ::build )));
327+ }
328+
329+ return root .child (createIndicator (syncManager ))
325330 .background (GTGuiTextures .DISPLAY )
326331 .size (190 , screenHeight )
327332 .pos (4 , 4 );
@@ -449,6 +454,8 @@ public static class Builder {
449454 * copying from GregTechDisplayScreen is easy, but extremely tedious to implement
450455 **/
451456 private final List <IDrawable > textList = new ArrayList <>();
457+ private Consumer <Builder > action ;
458+ private final SyncHandler syncHandler = makeSyncHandler ();
452459
453460 private BooleanSupplier isWorkingEnabled = () -> false ;
454461 private BooleanSupplier isActive = () -> false ;
@@ -870,10 +877,71 @@ protected void clear() {
870877 textList .clear ();
871878 }
872879
880+ protected boolean hasChanged () {
881+ if (this .action == null ) return false ;
882+ List <String > old = new ArrayList <>();
883+ for (var drawable : this .textList ) old .add (JsonUtils .toJsonString (drawable ));
884+ build ();
885+ if (textList .size () != old .size ()) return true ;
886+ for (int i = 0 ; i < textList .size (); i ++) {
887+ if (!JsonUtils .toJsonString (textList .get (i )).equals (old .get (i )))
888+ return true ;
889+ }
890+ return false ;
891+ }
892+
893+ protected void sync (String key , PanelSyncManager syncManager ) {
894+ syncManager .syncValue (key , this .syncHandler );
895+ }
896+
897+ private SyncHandler makeSyncHandler () {
898+ return new SyncHandler () {
899+
900+ @ Override
901+ public void detectAndSendChanges (boolean init ) {
902+ if (init || hasChanged ()) {
903+ sync (0 , this ::syncText );
904+ }
905+ }
906+
907+ private void syncText (PacketBuffer buffer ) {
908+ buffer .writeVarInt (textList .size ());
909+ for (IDrawable drawable : textList ) {
910+ var jsonString = JsonUtils .toJsonString (drawable );
911+ NetworkUtils .writeStringSafe (buffer , jsonString );
912+ }
913+ }
914+
915+ @ Override
916+ public void readOnClient (int id , PacketBuffer buf ) {
917+ if (id == 0 ) {
918+ clear ();
919+ for (int i = buf .readVarInt (); i > 0 ; i --) {
920+ String jsonString = NetworkUtils .readStringSafe (buf );
921+ addKey (JsonUtils .fromJsonString (jsonString ));
922+ }
923+ }
924+ }
925+
926+ @ Override
927+ public void readOnServer (int id , PacketBuffer buf ) {}
928+ };
929+ }
930+
873931 protected void build (IRichTextBuilder <?> richText ) {
932+ if (dirty ) build ();
874933 richText .addDrawableLines (this .textList );
875934 }
876935
936+ protected void build () {
937+ this .textList .clear ();
938+ if (this .action != null ) this .action .accept (this );
939+ }
940+
941+ protected void setAction (Consumer <Builder > action ) {
942+ this .action = action ;
943+ }
944+
877945 private void addKey (IDrawable key ) {
878946 this .textList .add (key );
879947 }
0 commit comments