66import gregtech .api .recipes .ingredients .IntCircuitIngredient ;
77import gregtech .client .utils .TooltipHelper ;
88
9+ import net .minecraft .item .ItemStack ;
910import net .minecraft .network .PacketBuffer ;
1011import net .minecraftforge .items .IItemHandler ;
1112
1213import com .cleanroommc .modularui .api .IPanelHandler ;
1314import com .cleanroommc .modularui .api .drawable .IKey ;
14- import com .cleanroommc .modularui .api .widget .IWidget ;
1515import com .cleanroommc .modularui .api .widget .Interactable ;
1616import com .cleanroommc .modularui .drawable .ItemDrawable ;
1717import com .cleanroommc .modularui .screen .ModularScreen ;
1818import com .cleanroommc .modularui .screen .RichTooltip ;
1919import com .cleanroommc .modularui .utils .MouseData ;
2020import com .cleanroommc .modularui .value .sync .ItemSlotSH ;
21+ import com .cleanroommc .modularui .value .sync .SyncHandler ;
22+ import com .cleanroommc .modularui .widget .Widget ;
2123import com .cleanroommc .modularui .widgets .ButtonWidget ;
2224import com .cleanroommc .modularui .widgets .ItemSlot ;
2325import com .cleanroommc .modularui .widgets .layout .Grid ;
2426import com .cleanroommc .modularui .widgets .slot .ModularSlot ;
2527import org .jetbrains .annotations .NotNull ;
28+ import org .jetbrains .annotations .Nullable ;
2629
2730import java .io .IOException ;
28- import java .util .ArrayList ;
29- import java .util .List ;
3031
3132public class GhostCircuitSlotWidget extends ItemSlot {
3233
3334 private static final int SYNC_CIRCUIT_INDEX = 10 ;
35+ @ Nullable
36+ private IPanelHandler selectorPanel ;
37+ private GhostCircuitSyncHandler syncHandler ;
3438
3539 public GhostCircuitSlotWidget () {
3640 super ();
@@ -41,7 +45,7 @@ public GhostCircuitSlotWidget() {
4145 public @ NotNull Result onMousePressed (int mouseButton ) {
4246 if (!isSelectorPanelOpen ()) {
4347 if (mouseButton == 0 && TooltipHelper .isShiftDown ()) {
44- createSelectorPanel ();
48+ this . getSelectorPanel (). openPanel ();
4549 } else {
4650 MouseData mouseData = MouseData .create (mouseButton );
4751 getSyncHandler ().syncToServer (2 , mouseData ::writeToPacket );
@@ -58,17 +62,24 @@ public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount)
5862 return true ;
5963 }
6064
65+ @ Override
66+ public boolean isValidSyncHandler (SyncHandler syncHandler ) {
67+ this .syncHandler = castIfTypeElseNull (syncHandler , GhostCircuitSyncHandler .class );
68+ if (this .syncHandler == null ) return false ;
69+ return super .isValidSyncHandler (syncHandler );
70+ }
71+
6172 @ Override
6273 public ItemSlot slot (ModularSlot slot ) {
63- ItemSlotSH sh = new GhostCircuitSyncHandler (slot );
64- isValidSyncHandler (sh );
65- setSyncHandler (sh );
74+ this . syncHandler = new GhostCircuitSyncHandler (slot );
75+ isValidSyncHandler (this . syncHandler );
76+ setSyncHandler (this . syncHandler );
6677 return this ;
6778 }
6879
6980 protected void getCircuitSlotTooltip (@ NotNull RichTooltip tooltip ) {
7081 String configString ;
71- int value = getSyncHandler (). getGhostCircuitHandler () .getCircuitValue ();
82+ int value = this . syncHandler .getCircuitValue ();
7283 if (value == GhostCircuitItemStackHandler .NO_CONFIG ) {
7384 configString = IKey .lang ("gregtech.gui.configurator_slot.no_value" ).get ();
7485 } else {
@@ -78,11 +89,6 @@ protected void getCircuitSlotTooltip(@NotNull RichTooltip tooltip) {
7889 tooltip .addLine (IKey .lang ("gregtech.gui.configurator_slot.tooltip" , configString ));
7990 }
8091
81- @ Override
82- public @ NotNull GhostCircuitSyncHandler getSyncHandler () {
83- return (GhostCircuitSyncHandler ) super .getSyncHandler ();
84- }
85-
8692 @ Override
8793 public void onMouseDrag (int mouseButton , long timeSinceClick ) {}
8894
@@ -92,44 +98,41 @@ public boolean onMouseRelease(int mouseButton) {
9298 }
9399
94100 private boolean isSelectorPanelOpen () {
95- return getPanel (). getScreen ().isPanelOpen ("circuit_selector" );
101+ return this . getSelectorPanel ().isPanelOpen ();
96102 }
97103
98- private void createSelectorPanel () {
99- ItemDrawable circuitPreview = new ItemDrawable (getSyncHandler ().getSlot ().getStack ());
100-
101- IPanelHandler .simple (getPanel (), (mainPanel , player ) -> {
102- var panel = GTGuis .createPopupPanel ("circuit_selector" , 176 , 120 );
103- List <List <IWidget >> options = new ArrayList <>();
104- for (int i = 0 ; i < 4 ; i ++) {
105- options .add (new ArrayList <>());
106- for (int j = 0 ; j < 9 ; j ++) {
107- int index = i * 9 + j ;
108- if (index > 32 ) break ;
109- options .get (i ).add (new ButtonWidget <>()
110- .size (18 )
111- .background (GTGuiTextures .SLOT , new ItemDrawable (
112- IntCircuitIngredient .getIntegratedCircuit (index )).asIcon ())
113- .disableHoverBackground ()
114- .onMousePressed (mouseButton -> {
115- getSyncHandler ().syncToServer (SYNC_CIRCUIT_INDEX , buf -> buf .writeShort (index ));
116- circuitPreview .setItem (IntCircuitIngredient .getIntegratedCircuit (index ));
117- if (Interactable .hasShiftDown ()) panel .animateClose ();
118- return true ;
119- }));
120- }
121- }
122- return panel .child (IKey .lang ("metaitem.circuit.integrated.gui" ).asWidget ().pos (5 , 5 ))
123- .child (circuitPreview .asIcon ().size (16 ).asWidget ()
124- .size (18 )
125- .top (19 ).alignX (0.5f )
126- .background (GTGuiTextures .SLOT , GTGuiTextures .INT_CIRCUIT_OVERLAY ))
127- .child (new Grid ()
128- .left (7 ).right (7 ).top (41 ).height (4 * 18 )
129- .matrix (options )
130- .minColWidth (18 ).minRowHeight (18 )
131- .minElementMargin (0 , 0 ));
132- }, true ).openPanel ();
104+ @ NotNull
105+ private IPanelHandler getSelectorPanel () {
106+ if (this .selectorPanel == null ) {
107+ this .selectorPanel = IPanelHandler .simple (getPanel (), (mainPanel , player ) -> {
108+ ItemDrawable circuitPreview = new ItemDrawable (this .syncHandler .getCircuitStack ());
109+
110+ return GTGuis .createPopupPanel ("circuit_selector" , 176 , 120 )
111+ .child (IKey .lang ("metaitem.circuit.integrated.gui" ).asWidget ().pos (5 , 5 ))
112+ .child (new Widget <>()
113+ .size (18 )
114+ .top (19 ).alignX (0.5f )
115+ .overlay (circuitPreview .asIcon ().margin (1 ))
116+ .background (GTGuiTextures .SLOT , GTGuiTextures .INT_CIRCUIT_OVERLAY ))
117+ .child (new Grid ()
118+ .left (7 ).right (7 ).top (41 ).height (4 * 18 )
119+ .mapTo (9 , 33 , value -> new ButtonWidget <>()
120+ .size (18 )
121+ .background (GTGuiTextures .SLOT , new ItemDrawable (
122+ IntCircuitIngredient .getIntegratedCircuit (value )).asIcon ())
123+ .disableHoverBackground ()
124+ .onMousePressed (mouseButton -> {
125+ getSyncHandler ().syncToServer (SYNC_CIRCUIT_INDEX ,
126+ buf -> buf .writeShort (value ));
127+ circuitPreview .setItem (IntCircuitIngredient .getIntegratedCircuit (value ));
128+ if (Interactable .hasShiftDown ()) this .selectorPanel .closePanel ();
129+ return true ;
130+ }))
131+ .minColWidth (18 ).minRowHeight (18 )
132+ .minElementMargin (0 , 0 ));
133+ }, true );
134+ }
135+ return this .selectorPanel ;
133136 }
134137
135138 private static class GhostCircuitSyncHandler extends ItemSlotSH {
@@ -169,6 +172,14 @@ private void setCircuitValue(int value) {
169172 }
170173 }
171174
175+ public int getCircuitValue () {
176+ return getGhostCircuitHandler ().getCircuitValue ();
177+ }
178+
179+ public ItemStack getCircuitStack () {
180+ return getSlot ().getStack ();
181+ }
182+
172183 @ Override
173184 public void readOnServer (int id , PacketBuffer buf ) throws IOException {
174185 if (id == SYNC_CIRCUIT_INDEX ) {
0 commit comments