1616import net .minecraft .network .FriendlyByteBuf ;
1717import net .minecraft .network .chat .Component ;
1818
19- import javax .annotation .Nullable ;
2019import java .util .HashMap ;
2120import java .util .List ;
2221import java .util .Map ;
@@ -32,107 +31,148 @@ public class OverclockFancyConfigurator implements IFancyConfigurator {
3231 protected IOverclockMachine overclockMachine ;
3332 // runtime
3433 protected int currentTier ;
35- @ Nullable
36- protected WidgetGroup group ;
37- protected Map <Integer , WidgetGroup > lightGroups ;
3834
3935 public OverclockFancyConfigurator (IOverclockMachine overclockMachine ) {
4036 this .overclockMachine = overclockMachine ;
41- this .lightGroups = new HashMap <>();
37+ }
38+
39+ @ Override
40+ public String getTitle () {
41+ return "gtceu.gui.overclock.title" ;
42+ }
43+
44+ @ Override
45+ public IGuiTexture getIcon () {
46+ return new TextTexture (GTValues .VNF [this .currentTier ]).setDropShadow (false );
47+ }
48+
49+ @ Override
50+ public void writeInitialData (FriendlyByteBuf buffer ) {
51+ this .currentTier = overclockMachine .getOverclockTier ();
52+ buffer .writeVarInt (currentTier );
53+ }
54+
55+ @ Override
56+ public void readInitialData (FriendlyByteBuf buffer ) {
57+ this .currentTier = buffer .readVarInt ();
4258 }
4359
4460 @ Override
4561 public void detectAndSendChange (BiConsumer <Integer , Consumer <FriendlyByteBuf >> sender ) {
4662 var newTier = overclockMachine .getOverclockTier ();
47- if (newTier != this . currentTier ) {
63+ if (newTier != currentTier ) {
4864 this .currentTier = newTier ;
49- sender .accept (0 , buf -> buf .writeVarInt (this .currentTier ));
50- }
51- int min = overclockMachine .getMinOverclockTier ();
52- int max = overclockMachine .getMaxOverclockTier ();
53- if (lightGroups .size () != max - min + 1 ) {
54- updateLightButton (min , max );
55- sender .accept (1 , buf -> {
56- buf .writeVarInt (min );
57- buf .writeVarInt (max );
58- });
59- } else {
60- for (int i = min ; i <= max ; i ++) {
61- if (!lightGroups .containsKey (i )) {
62- updateLightButton (min , max );
63- sender .accept (1 , buf -> {
64- buf .writeVarInt (min );
65- buf .writeVarInt (max );
66- });
67- return ;
68- }
69- }
70- }
71- }
72-
73- private void updateLightButton (int min , int max ) {
74- if (group != null ) {
75- for (WidgetGroup light : lightGroups .values ()) {
76- group .removeWidget (light );
77- }
78- lightGroups .clear ();
79- int x = 5 ;
80- for (int tier = min ; tier <= max ; tier ++) {
81- int finalTier = tier ;
82- var lightGroup = new WidgetGroup (x , 27 , 8 , 8 );
83- lightGroup .addWidget (new ButtonWidget (0 , 0 , 8 , 8 , null , cd -> {
84- if (!cd .isRemote ) {
85- overclockMachine .setOverclockTier (finalTier );
86- }
87- }));
88- lightGroup .addWidget (new ImageWidget (0 , 0 , 8 , 8 , () -> currentTier >= finalTier ? GuiTextures .LIGHT_ON : GuiTextures .LIGHT_OFF ));
89- lightGroups .put (tier , lightGroup );
90- group .addWidget (lightGroup );
91- x += 10 ;
92- }
65+ sender .accept (0 , buf -> buf .writeVarInt (newTier ));
9366 }
9467 }
9568
9669 @ Override
9770 public void readUpdateInfo (int id , FriendlyByteBuf buf ) {
9871 if (id == 0 ) {
9972 this .currentTier = buf .readVarInt ();
100- } else if (id == 1 ) {
101- int min = buf .readVarInt ();
102- int max = buf .readVarInt ();
103- updateLightButton (min , max );
10473 }
10574 }
10675
10776 @ Override
108- public String getTitle () {
109- return "gtceu.gui.overclock.title" ;
110- }
77+ public Widget createConfigurator () {
78+ return new WidgetGroup ( 0 , 0 , 120 , 40 ) {
79+ final Map < Integer , WidgetGroup > lightGroups = new HashMap <>();
11180
112- @ Override
113- public IGuiTexture getIcon () {
114- return new TextTexture (GTValues .VNF [this .currentTier ]).setDropShadow (false );
115- }
81+ @ Override
82+ public void initWidget () {
83+ super .initWidget ();
84+ setBackground (GuiTextures .BACKGROUND_INVERSE );
85+ addWidget (new PredicatedButtonWidget (5 , 5 , 10 , 20 , new GuiTextureGroup (GuiTextures .BUTTON , Icons .LEFT .copy ().scale (0.7f )), cd -> {
86+ if (!cd .isRemote ) {
87+ overclockMachine .setOverclockTier (currentTier - 1 );
88+ }
89+ }).setPredicate (() -> currentTier > overclockMachine .getMinOverclockTier ()));
90+ addWidget (new ImageWidget (20 , 5 , 120 - 5 - 10 - 5 - 20 , 20 , () -> new GuiTextureGroup (GuiTextures .DISPLAY_FRAME , new TextTexture (GTValues .VNF [currentTier ]))));
91+ addWidget (new PredicatedButtonWidget (120 -5 - 10 , 5 , 10 , 20 , new GuiTextureGroup (GuiTextures .BUTTON , Icons .RIGHT .copy ().scale (0.7f )), cd -> {
92+ if (!cd .isRemote ) {
93+ overclockMachine .setOverclockTier (currentTier + 1 );
94+ }
95+ }).setPredicate (() -> currentTier < overclockMachine .getMaxOverclockTier ()));
96+ }
11697
117- @ Override
118- public Widget createConfigurator () {
119- group = new WidgetGroup (0 , 0 , 120 , 40 );
120- group .setBackground (GuiTextures .BACKGROUND_INVERSE );
121- group .addWidget (new PredicatedButtonWidget (5 , 5 , 10 , 20 , new GuiTextureGroup (GuiTextures .BUTTON , Icons .LEFT .copy ().scale (0.7f )), cd -> {
122- if (!cd .isRemote ) {
123- overclockMachine .setOverclockTier (currentTier - 1 );
98+ @ Override
99+ public void writeInitialData (FriendlyByteBuf buffer ) {
100+ int min = overclockMachine .getMinOverclockTier ();
101+ int max = overclockMachine .getMaxOverclockTier ();
102+ buffer .writeVarInt (min );
103+ buffer .writeVarInt (max );
104+ buffer .writeVarInt (currentTier );
105+ updateLightButton (min , max );
106+ super .writeInitialData (buffer );
124107 }
125- }).setPredicate (() -> currentTier > overclockMachine .getMinOverclockTier ()));
126- group .addWidget (new ImageWidget (20 , 5 , 120 - 5 - 10 - 5 - 20 , 20 , () -> new GuiTextureGroup (GuiTextures .DISPLAY_FRAME , new TextTexture (GTValues .VNF [this .currentTier ]))));
127- group .addWidget (new PredicatedButtonWidget (120 -5 - 10 , 5 , 10 , 20 , new GuiTextureGroup (GuiTextures .BUTTON , Icons .RIGHT .copy ().scale (0.7f )), cd -> {
128- if (!cd .isRemote ) {
129- overclockMachine .setOverclockTier (currentTier + 1 );
108+
109+ @ Override
110+ public void readInitialData (FriendlyByteBuf buffer ) {
111+ int min = buffer .readVarInt ();
112+ int max = buffer .readVarInt ();
113+ currentTier = buffer .readVarInt ();
114+ updateLightButton (min , max );
115+ super .readInitialData (buffer );
130116 }
131- }).setPredicate (() -> currentTier < overclockMachine .getMaxOverclockTier ()));
132- return group ;
133- }
134117
118+ private void updateLightButton (int min , int max ) {
119+ for (WidgetGroup light : lightGroups .values ()) {
120+ removeWidget (light );
121+ }
122+ lightGroups .clear ();
123+ int x = 5 ;
124+ for (int tier = min ; tier <= max ; tier ++) {
125+ int finalTier = tier ;
126+ var lightGroup = new WidgetGroup (x , 27 , 8 , 8 );
127+ lightGroup .addWidget (new ButtonWidget (0 , 0 , 8 , 8 , null , cd -> {
128+ if (!cd .isRemote ) {
129+ overclockMachine .setOverclockTier (finalTier );
130+ }
131+ }));
132+ lightGroup .addWidget (new ImageWidget (0 , 0 , 8 , 8 , () -> currentTier >= finalTier ? GuiTextures .LIGHT_ON : GuiTextures .LIGHT_OFF ));
133+ lightGroups .put (tier , lightGroup );
134+ addWidget (lightGroup );
135+ x += 10 ;
136+ }
137+ }
135138
139+ @ Override
140+ public void detectAndSendChanges () {
141+ super .detectAndSendChanges ();
142+ int min = overclockMachine .getMinOverclockTier ();
143+ int max = overclockMachine .getMaxOverclockTier ();
144+ if (lightGroups .size () != max - min + 1 ) {
145+ updateLightButton (min , max );
146+ writeUpdateInfo (0 , buf -> {
147+ buf .writeVarInt (min );
148+ buf .writeVarInt (max );
149+ });
150+ } else {
151+ for (int i = min ; i <= max ; i ++) {
152+ if (!lightGroups .containsKey (i )) {
153+ updateLightButton (min , max );
154+ writeUpdateInfo (0 , buf -> {
155+ buf .writeVarInt (min );
156+ buf .writeVarInt (max );
157+ });
158+ return ;
159+ }
160+ }
161+ }
162+ }
163+
164+ @ Override
165+ public void readUpdateInfo (int id , FriendlyByteBuf buffer ) {
166+ if (id == 0 ) {
167+ int min = buffer .readVarInt ();
168+ int max = buffer .readVarInt ();
169+ updateLightButton (min , max );
170+ } else {
171+ super .readUpdateInfo (id , buffer );
172+ }
173+ }
174+ };
175+ }
136176
137177 @ Override
138178 public List <Component > getTooltips () {
0 commit comments