1+ package github .kasuminova .mmce .client .gui ;
2+
3+ import appeng .client .gui .AEBaseGui ;
4+ import appeng .client .gui .MathExpressionParser ;
5+ import appeng .client .gui .widgets .GuiTabButton ;
6+ import github .kasuminova .mmce .common .container .ContainerMEItemOutputBusStackSize ;
7+ import github .kasuminova .mmce .common .network .PktMEOutputBusStackSizeChange ;
8+ import github .kasuminova .mmce .common .network .PktSwitchGuiMEOutputBus ;
9+ import github .kasuminova .mmce .common .tile .MEItemOutputBus ;
10+ import hellfirepvp .modularmachinery .ModularMachinery ;
11+ import net .minecraft .client .gui .GuiButton ;
12+ import net .minecraft .client .gui .GuiScreen ;
13+ import net .minecraft .client .gui .GuiTextField ;
14+ import net .minecraft .entity .player .InventoryPlayer ;
15+ import net .minecraft .item .ItemStack ;
16+ import net .minecraft .util .ResourceLocation ;
17+ import org .lwjgl .input .Keyboard ;
18+ import org .lwjgl .input .Mouse ;
19+ import java .io .IOException ;
20+
21+ public class GuiMEItemOutputBusStackSize extends AEBaseGui {
22+
23+ private static final ResourceLocation TEXTURES = new ResourceLocation ("modularmachinery" , "textures/gui/stacksize.png" );
24+
25+ private GuiTextField stackSizeBox ;
26+ private GuiTabButton originalGuiBtn ;
27+ private final MEItemOutputBus outputBus ;
28+
29+ private GuiButton plus1 ;
30+ private GuiButton plus10 ;
31+ private GuiButton plus100 ;
32+ private GuiButton plus1000 ;
33+ private GuiButton minus1 ;
34+ private GuiButton minus10 ;
35+ private GuiButton minus100 ;
36+ private GuiButton minus1000 ;
37+
38+ public GuiMEItemOutputBusStackSize (final InventoryPlayer inventoryPlayer , final MEItemOutputBus outputBus ) {
39+ super (new ContainerMEItemOutputBusStackSize (inventoryPlayer , outputBus ));
40+ this .outputBus = outputBus ;
41+ this .ySize = 105 ;
42+ }
43+
44+ @ Override
45+ public void initGui () {
46+ Keyboard .enableRepeatEvents (true );
47+ super .initGui ();
48+
49+ this .buttonList .add (this .plus1 = new GuiButton (0 , this .guiLeft + 20 , this .guiTop + 32 , 22 , 20 , "+1" ));
50+ this .buttonList .add (this .plus10 = new GuiButton (0 , this .guiLeft + 48 , this .guiTop + 32 , 28 , 20 , "+10" ));
51+ this .buttonList .add (this .plus100 = new GuiButton (0 , this .guiLeft + 82 , this .guiTop + 32 , 32 , 20 , "+100" ));
52+ this .buttonList .add (this .plus1000 = new GuiButton (0 , this .guiLeft + 120 , this .guiTop + 32 , 38 , 20 , "+1000" ));
53+
54+ this .buttonList .add (this .minus1 = new GuiButton (0 , this .guiLeft + 20 , this .guiTop + 69 , 22 , 20 , "-1" ));
55+ this .buttonList .add (this .minus10 = new GuiButton (0 , this .guiLeft + 48 , this .guiTop + 69 , 28 , 20 , "-10" ));
56+ this .buttonList .add (this .minus100 = new GuiButton (0 , this .guiLeft + 82 , this .guiTop + 69 , 32 , 20 , "-100" ));
57+ this .buttonList .add (this .minus1000 = new GuiButton (0 , this .guiLeft + 120 , this .guiTop + 69 , 38 , 20 , "-1000" ));
58+
59+ final ContainerMEItemOutputBusStackSize container = (ContainerMEItemOutputBusStackSize ) this .inventorySlots ;
60+ final ItemStack busIcon = new ItemStack (this .outputBus .getBlockType ());
61+
62+ if (!busIcon .isEmpty ()) {
63+ this .buttonList .add (this .originalGuiBtn = new GuiTabButton (
64+ this .guiLeft + 154 ,
65+ this .guiTop ,
66+ busIcon ,
67+ "ME Machinery Item Output Bus" ,
68+ this .itemRender
69+ ));
70+ }
71+
72+ this .stackSizeBox = new GuiTextField (
73+ 0 ,
74+ this .fontRenderer ,
75+ this .guiLeft + 62 ,
76+ this .guiTop + 57 ,
77+ 75 ,
78+ this .fontRenderer .FONT_HEIGHT
79+ );
80+ this .stackSizeBox .setEnableBackgroundDrawing (false );
81+ this .stackSizeBox .setMaxStringLength (32 );
82+ this .stackSizeBox .setTextColor (0xFFFFFF );
83+ this .stackSizeBox .setVisible (true );
84+ this .stackSizeBox .setFocused (true );
85+
86+ this .stackSizeBox .setText (String .valueOf (container .getStackSize ()));
87+ }
88+
89+ @ Override
90+ protected void actionPerformed (final GuiButton btn ) throws IOException {
91+ super .actionPerformed (btn );
92+
93+ if (btn == this .originalGuiBtn ) {
94+ String text = this .stackSizeBox .getText ();
95+ if (!text .isEmpty ()) {
96+ try {
97+ long value = parseValue (text );
98+ if (value > Integer .MAX_VALUE ) {
99+ value = Integer .MAX_VALUE ;
100+ } else if (value < 1 ) {
101+ value = 1 ;
102+ }
103+ this .sendStackSizeToServer ((int ) value );
104+ } catch (NumberFormatException e ) {
105+ }
106+ }
107+
108+ ModularMachinery .NET_CHANNEL .sendToServer (
109+ new PktSwitchGuiMEOutputBus (this .outputBus .getPos (), 0 )
110+ );
111+ }
112+
113+ if (btn == this .plus1 ) this .addQty (1 );
114+ else if (btn == this .plus10 ) this .addQty (10 );
115+ else if (btn == this .plus100 ) this .addQty (100 );
116+ else if (btn == this .plus1000 ) this .addQty (1000 );
117+ else if (btn == this .minus1 ) this .addQty (-1 );
118+ else if (btn == this .minus10 ) this .addQty (-10 );
119+ else if (btn == this .minus100 ) this .addQty (-100 );
120+ else if (btn == this .minus1000 ) this .addQty (-1000 );
121+ }
122+
123+ private void addQty (final int amount ) {
124+ try {
125+ String text = this .stackSizeBox .getText ();
126+
127+ long currentValue = text .isEmpty () ? 0 : parseValue (text );
128+ long newValue = currentValue + amount ;
129+
130+ if (newValue > Integer .MAX_VALUE ) {
131+ newValue = Integer .MAX_VALUE ;
132+ } else if (newValue < 1 ) {
133+ newValue = 1 ;
134+ }
135+
136+ this .stackSizeBox .setText (String .valueOf (newValue ));
137+ this .sendStackSizeToServer ((int ) newValue );
138+ } catch (final NumberFormatException e ) {
139+ this .stackSizeBox .setText ("1" );
140+ this .sendStackSizeToServer (1 );
141+ }
142+ }
143+
144+ private void sendStackSizeToServer (int stackSize ) {
145+ ModularMachinery .NET_CHANNEL .sendToServer (
146+ new PktMEOutputBusStackSizeChange (this .outputBus .getPos (), stackSize )
147+ );
148+ }
149+
150+ private long parseValue (String text ) {
151+ try {
152+ return Long .parseLong (text );
153+ } catch (NumberFormatException e ) {
154+ try {
155+ text = preprocessExponents (text );
156+
157+ double result = MathExpressionParser .parse (text );
158+ if (Double .isNaN (result ) || Double .isInfinite (result )) {
159+ throw new NumberFormatException ("Invalid expression" );
160+ }
161+
162+ return (long ) Math .round (result );
163+ } catch (Exception ex ) {
164+ throw new NumberFormatException ("Invalid number or expression" );
165+ }
166+ }
167+ }
168+
169+ private String preprocessExponents (String expression ) {
170+ while (expression .contains ("^" )) {
171+ int caretIndex = expression .indexOf ('^' );
172+
173+ int baseStart = caretIndex - 1 ;
174+ while (baseStart > 0 && (Character .isDigit (expression .charAt (baseStart - 1 )) || expression .charAt (baseStart - 1 ) == '.' )) {
175+ baseStart --;
176+ }
177+
178+ int expEnd = caretIndex + 2 ;
179+ while (expEnd < expression .length () && (Character .isDigit (expression .charAt (expEnd )) || expression .charAt (expEnd ) == '.' )) {
180+ expEnd ++;
181+ }
182+
183+ String baseStr = expression .substring (baseStart , caretIndex );
184+ String expStr = expression .substring (caretIndex + 1 , expEnd );
185+
186+ double base = Double .parseDouble (baseStr );
187+ double exponent = Double .parseDouble (expStr );
188+ double result = Math .pow (base , exponent );
189+
190+ expression = expression .substring (0 , baseStart ) + result + expression .substring (expEnd );
191+ }
192+
193+ return expression ;
194+ }
195+
196+ @ Override
197+ protected void keyTyped (final char character , final int key ) throws IOException {
198+ if (!this .checkHotbarKeys (key )) {
199+ boolean isValidChar = Character .isDigit (character ) ||
200+ character == '+' || character == '-' ||
201+ character == '*' || character == '/' ||
202+ character == '^' ||
203+ character == '(' || character == ')' ||
204+ character == '.' || character == 'E' || character == 'e' ;
205+ boolean isControlKey = key == 14 || key == 211 || key == 203 || key == 205 ;
206+
207+ if ((isValidChar || isControlKey ) && this .stackSizeBox .textboxKeyTyped (character , key )) {
208+ } else {
209+ super .keyTyped (character , key );
210+ }
211+ }
212+ }
213+
214+ @ Override
215+ public void handleMouseInput () throws IOException {
216+ super .handleMouseInput ();
217+
218+ final int wheel = Mouse .getEventDWheel ();
219+ if (wheel != 0 ) {
220+ final int x = Mouse .getEventX () * this .width / this .mc .displayWidth ;
221+ final int y = this .height - Mouse .getEventY () * this .height / this .mc .displayHeight - 1 ;
222+
223+ int boxX = this .guiLeft + 62 ;
224+ int boxY = this .guiTop + 57 ;
225+ int boxWidth = 75 ;
226+ int boxHeight = this .fontRenderer .FONT_HEIGHT ;
227+
228+ if (x >= boxX && x <= boxX + boxWidth && y >= boxY && y <= boxY + boxHeight ) {
229+ this .onMouseWheelEvent (wheel );
230+ }
231+ }
232+ }
233+
234+ private void onMouseWheelEvent (int wheel ) {
235+ boolean isShiftHeld = GuiScreen .isShiftKeyDown ();
236+ boolean isCtrlHeld = GuiScreen .isCtrlKeyDown ();
237+
238+ String text = this .stackSizeBox .getText ();
239+ long currentValue ;
240+
241+ try {
242+ currentValue = text .isEmpty () ? 1 : parseValue (text );
243+ } catch (NumberFormatException e ) {
244+ currentValue = 1 ;
245+ }
246+
247+ long newValue = currentValue ;
248+
249+ if (isShiftHeld && isCtrlHeld ) {
250+ if (wheel > 0 ) {
251+ newValue = currentValue * 2 ;
252+ } else {
253+ newValue = currentValue / 2 ;
254+ }
255+ } else if (isShiftHeld ) {
256+ if (wheel > 0 ) {
257+ newValue = currentValue + 1 ;
258+ } else {
259+ newValue = currentValue - 1 ;
260+ }
261+ }
262+
263+ if (newValue > Integer .MAX_VALUE ) {
264+ newValue = Integer .MAX_VALUE ;
265+ } else if (newValue < 1 ) {
266+ newValue = 1 ;
267+ }
268+
269+ this .stackSizeBox .setText (String .valueOf (newValue ));
270+ this .sendStackSizeToServer ((int ) newValue );
271+ }
272+
273+ @ Override
274+ public void drawFG (final int offsetX , final int offsetY , final int mouseX , final int mouseY ) {
275+ this .fontRenderer .drawString ("Stack Size" , 8 , 6 , 0x404040 );
276+ }
277+
278+ @ Override
279+ public void drawBG (final int offsetX , final int offsetY , final int mouseX , final int mouseY ) {
280+ this .mc .getTextureManager ().bindTexture (TEXTURES );
281+ this .drawTexturedModalRect (offsetX , offsetY , 0 , 0 , this .xSize , this .ySize );
282+
283+ this .stackSizeBox .drawTextBox ();
284+ }
285+
286+ @ Override
287+ public void onGuiClosed () {
288+ super .onGuiClosed ();
289+ Keyboard .enableRepeatEvents (false );
290+
291+ String text = this .stackSizeBox .getText ();
292+ if (!text .isEmpty ()) {
293+ try {
294+ long value = parseValue (text );
295+ int finalValue ;
296+ if (value > Integer .MAX_VALUE ) {
297+ finalValue = Integer .MAX_VALUE ;
298+ } else if (value < 1 ) {
299+ finalValue = 1 ;
300+ } else {
301+ finalValue = (int ) value ;
302+ }
303+
304+ sendStackSizeToServer (finalValue );
305+
306+ } catch (NumberFormatException e ) {
307+ try {
308+ ContainerMEItemOutputBusStackSize container = (ContainerMEItemOutputBusStackSize ) this .inventorySlots ;
309+ sendStackSizeToServer (container .getStackSize ());
310+ } catch (Exception ex ) {
311+ }
312+ }
313+ }
314+ }
315+ }
0 commit comments