11package gregtech .api .mui .widget ;
22
3+ import com .cleanroommc .modularui .value .sync .SyncHandler ;
4+
35import gregtech .api .capability .impl .GhostCircuitItemStackHandler ;
46import gregtech .api .mui .GTGuiTextures ;
57import gregtech .api .mui .GTGuis ;
68import gregtech .api .recipes .ingredients .IntCircuitIngredient ;
79import gregtech .client .utils .TooltipHelper ;
810
11+ import net .minecraft .item .ItemStack ;
912import net .minecraft .network .PacketBuffer ;
1013import net .minecraftforge .items .IItemHandler ;
1114
2326import com .cleanroommc .modularui .widgets .layout .Grid ;
2427import com .cleanroommc .modularui .widgets .slot .ModularSlot ;
2528import org .jetbrains .annotations .NotNull ;
29+ import org .jetbrains .annotations .Nullable ;
2630
2731import java .io .IOException ;
2832import java .util .ArrayList ;
3135public class GhostCircuitSlotWidget extends ItemSlot {
3236
3337 private static final int SYNC_CIRCUIT_INDEX = 10 ;
38+ @ Nullable
39+ private IPanelHandler selectorPanel ;
40+ private GhostCircuitSyncHandler syncHandler ;
3441
3542 public GhostCircuitSlotWidget () {
3643 super ();
@@ -41,7 +48,7 @@ public GhostCircuitSlotWidget() {
4148 public @ NotNull Result onMousePressed (int mouseButton ) {
4249 if (!isSelectorPanelOpen ()) {
4350 if (mouseButton == 0 && TooltipHelper .isShiftDown ()) {
44- createSelectorPanel ();
51+ this . getSelectorPanel (). openPanel ();
4552 } else {
4653 MouseData mouseData = MouseData .create (mouseButton );
4754 getSyncHandler ().syncToServer (2 , mouseData ::writeToPacket );
@@ -58,17 +65,24 @@ public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount)
5865 return true ;
5966 }
6067
68+ @ Override
69+ public boolean isValidSyncHandler (SyncHandler syncHandler ) {
70+ this .syncHandler = castIfTypeElseNull (syncHandler , GhostCircuitSyncHandler .class );
71+ if (this .syncHandler == null ) return false ;
72+ return super .isValidSyncHandler (syncHandler );
73+ }
74+
6175 @ Override
6276 public ItemSlot slot (ModularSlot slot ) {
63- ItemSlotSH sh = new GhostCircuitSyncHandler (slot );
64- isValidSyncHandler (sh );
65- setSyncHandler (sh );
77+ this . syncHandler = new GhostCircuitSyncHandler (slot );
78+ isValidSyncHandler (this . syncHandler );
79+ setSyncHandler (this . syncHandler );
6680 return this ;
6781 }
6882
6983 protected void getCircuitSlotTooltip (@ NotNull RichTooltip tooltip ) {
7084 String configString ;
71- int value = getSyncHandler (). getGhostCircuitHandler () .getCircuitValue ();
85+ int value = this . syncHandler .getCircuitValue ();
7286 if (value == GhostCircuitItemStackHandler .NO_CONFIG ) {
7387 configString = IKey .lang ("gregtech.gui.configurator_slot.no_value" ).get ();
7488 } else {
@@ -78,11 +92,6 @@ protected void getCircuitSlotTooltip(@NotNull RichTooltip tooltip) {
7892 tooltip .addLine (IKey .lang ("gregtech.gui.configurator_slot.tooltip" , configString ));
7993 }
8094
81- @ Override
82- public @ NotNull GhostCircuitSyncHandler getSyncHandler () {
83- return (GhostCircuitSyncHandler ) super .getSyncHandler ();
84- }
85-
8695 @ Override
8796 public void onMouseDrag (int mouseButton , long timeSinceClick ) {}
8897
@@ -92,44 +101,39 @@ public boolean onMouseRelease(int mouseButton) {
92101 }
93102
94103 private boolean isSelectorPanelOpen () {
95- return getPanel (). getScreen ().isPanelOpen ("circuit_selector" );
104+ return this . getSelectorPanel ().isPanelOpen ();
96105 }
97106
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 ();
107+ @ NotNull
108+ private IPanelHandler getSelectorPanel () {
109+ if (this .selectorPanel == null ) {
110+ this .selectorPanel = IPanelHandler .simple (getPanel (), (mainPanel , player ) -> {
111+ ItemDrawable circuitPreview = new ItemDrawable (this .syncHandler .getCircuitStack ());
112+
113+ return GTGuis .createPopupPanel ("circuit_selector" , 176 , 120 )
114+ .child (IKey .lang ("metaitem.circuit.integrated.gui" ).asWidget ().pos (5 , 5 ))
115+ .child (circuitPreview .asIcon ().size (16 ).asWidget ()
116+ .size (18 )
117+ .top (19 ).alignX (0.5f )
118+ .background (GTGuiTextures .SLOT , GTGuiTextures .INT_CIRCUIT_OVERLAY ))
119+ .child (new Grid ()
120+ .left (7 ).right (7 ).top (41 ).height (4 * 18 )
121+ .mapTo (9 , 33 , value -> new ButtonWidget <>()
122+ .size (18 )
123+ .background (GTGuiTextures .SLOT , new ItemDrawable (
124+ IntCircuitIngredient .getIntegratedCircuit (value )).asIcon ())
125+ .disableHoverBackground ()
126+ .onMousePressed (mouseButton -> {
127+ getSyncHandler ().syncToServer (SYNC_CIRCUIT_INDEX , buf -> buf .writeShort (value ));
128+ circuitPreview .setItem (IntCircuitIngredient .getIntegratedCircuit (value ));
129+ if (Interactable .hasShiftDown ()) this .selectorPanel .closePanel ();
130+ return true ;
131+ }))
132+ .minColWidth (18 ).minRowHeight (18 )
133+ .minElementMargin (0 , 0 ));
134+ }, true );
135+ }
136+ return this .selectorPanel ;
133137 }
134138
135139 private static class GhostCircuitSyncHandler extends ItemSlotSH {
@@ -169,6 +173,14 @@ private void setCircuitValue(int value) {
169173 }
170174 }
171175
176+ public int getCircuitValue () {
177+ return getGhostCircuitHandler ().getCircuitValue ();
178+ }
179+
180+ public ItemStack getCircuitStack () {
181+ return getSlot ().getStack ();
182+ }
183+
172184 @ Override
173185 public void readOnServer (int id , PacketBuffer buf ) throws IOException {
174186 if (id == SYNC_CIRCUIT_INDEX ) {
0 commit comments