1313import gregtech .api .util .Size ;
1414import net .minecraft .entity .player .EntityPlayer ;
1515import net .minecraft .inventory .ClickType ;
16+ import net .minecraft .inventory .IInventory ;
17+ import net .minecraft .inventory .Slot ;
1618import net .minecraft .item .ItemStack ;
1719import net .minecraftforge .fml .relauncher .Side ;
1820import net .minecraftforge .fml .relauncher .SideOnly ;
2224
2325public class SlotWidget extends Widget implements INativeWidget {
2426
25- protected SlotItemHandler slotReference ;
27+ protected Slot slotReference ;
2628 protected boolean isEnabled = true ;
2729
2830 protected boolean canTakeItems ;
@@ -34,15 +36,26 @@ public class SlotWidget extends Widget implements INativeWidget {
3436
3537 protected Rectangle scissor ;
3638
39+ public SlotWidget (IInventory inventory , int slotIndex , int xPosition , int yPosition , boolean canTakeItems , boolean canPutItems ) {
40+ super (new Position (xPosition , yPosition ), new Size (18 , 18 ));
41+ this .canTakeItems = canTakeItems ;
42+ this .canPutItems = canPutItems ;
43+ this .slotReference = createSlot (inventory , slotIndex );
44+ }
45+
3746 public SlotWidget (IItemHandler itemHandler , int slotIndex , int xPosition , int yPosition , boolean canTakeItems , boolean canPutItems ) {
3847 super (new Position (xPosition , yPosition ), new Size (18 , 18 ));
3948 this .canTakeItems = canTakeItems ;
4049 this .canPutItems = canPutItems ;
4150 this .slotReference = createSlot (itemHandler , slotIndex );
4251 }
4352
44- protected SlotItemHandler createSlot (IItemHandler itemHandler , int index ) {
45- return new WidgetSlotDelegate (itemHandler , index , 0 , 0 );
53+ protected Slot createSlot (IInventory inventory , int index ) {
54+ return new WidgetSlot (inventory , index , 0 , 0 );
55+ }
56+
57+ protected Slot createSlot (IItemHandler itemHandler , int index ) {
58+ return new WidgetSlotItemHandler (itemHandler , index , 0 , 0 );
4659 }
4760
4861 @ Override
@@ -89,6 +102,10 @@ public SlotWidget(IItemHandlerModifiable itemHandler, int slotIndex, int xPositi
89102 this (itemHandler , slotIndex , xPosition , yPosition , true , true );
90103 }
91104
105+ public SlotWidget (IInventory inventory , int slotIndex , int xPosition , int yPosition ) {
106+ this (inventory , slotIndex , xPosition , yPosition , true , true );
107+ }
108+
92109 /**
93110 * Sets array of background textures used by slot
94111 * they are drawn on top of each other
@@ -141,10 +158,110 @@ public ItemStack slotClick(int dragType, ClickType clickTypeIn, EntityPlayer pla
141158 }
142159
143160 @ Override
144- public final SlotItemHandler getHandle () {
161+ public final Slot getHandle () {
145162 return slotReference ;
146163 }
147164
165+ protected class WidgetSlot extends Slot implements IScissored {
166+ public WidgetSlot (IInventory inventory , int index , int xPosition , int yPosition ) {
167+ super (inventory , index , xPosition , yPosition );
168+ }
169+
170+ @ Override
171+ public boolean isItemValid (@ Nonnull ItemStack stack ) {
172+ return SlotWidget .this .canPutStack (stack ) && super .isItemValid (stack );
173+ }
174+
175+ @ Override
176+ public boolean canTakeStack (EntityPlayer playerIn ) {
177+ return SlotWidget .this .canTakeStack (playerIn ) && super .canTakeStack (playerIn );
178+ }
179+
180+ @ Override
181+ public void putStack (@ Nonnull ItemStack stack ) {
182+ super .putStack (stack );
183+ if (changeListener != null ) {
184+ changeListener .run ();
185+ }
186+ }
187+
188+ @ Override
189+ public final ItemStack onTake (EntityPlayer thePlayer , ItemStack stack ) {
190+ return onItemTake (thePlayer , super .onTake (thePlayer , stack ), false );
191+ }
192+
193+ @ Override
194+ public void onSlotChanged () {
195+ SlotWidget .this .onSlotChanged ();
196+ }
197+
198+ @ Override
199+ public boolean isEnabled () {
200+ return SlotWidget .this .isEnabled ();
201+ }
202+
203+ @ Override
204+ public Rectangle getScissor () {
205+ return SlotWidget .this .scissor ;
206+ }
207+ }
208+
209+ protected class WidgetSlotItemHandler extends SlotItemHandler implements IScissored {
210+
211+ public WidgetSlotItemHandler (IItemHandler itemHandler , int index , int xPosition , int yPosition ) {
212+ super (itemHandler , index , xPosition , yPosition );
213+ }
214+
215+ @ Override
216+ public boolean isItemValid (@ Nonnull ItemStack stack ) {
217+ return SlotWidget .this .canPutStack (stack ) && super .isItemValid (stack );
218+ }
219+
220+ @ Override
221+ public boolean canTakeStack (EntityPlayer playerIn ) {
222+ return SlotWidget .this .canTakeStack (playerIn ) && super .canTakeStack (playerIn );
223+ }
224+
225+ @ Override
226+ public void putStack (@ Nonnull ItemStack stack ) {
227+ super .putStack (stack );
228+ if (changeListener != null ) {
229+ changeListener .run ();
230+ }
231+ }
232+
233+ @ Override
234+ public final ItemStack onTake (EntityPlayer thePlayer , ItemStack stack ) {
235+ return onItemTake (thePlayer , super .onTake (thePlayer , stack ), false );
236+ }
237+
238+ @ Override
239+ public void onSlotChanged () {
240+ SlotWidget .this .onSlotChanged ();
241+ }
242+
243+ @ Override
244+ public boolean isEnabled () {
245+ return SlotWidget .this .isEnabled ();
246+ }
247+
248+ @ Override
249+ public Rectangle getScissor () {
250+ return SlotWidget .this .scissor ;
251+ }
252+ }
253+
254+ /**
255+ * @deprecated
256+ * Use {@link WidgetSlotItemHandler} instead. <br>
257+ * {@link WidgetSlotDelegate} was renamed to {@link WidgetSlotItemHandler} since GregTech 1.15.0.<br>
258+ * Explanation of deprecation: In order to fix mouse wheel action a new class was introduced ({@link WidgetSlot}).
259+ * To have consistent names {@link WidgetSlotDelegate} was renamed to {@link WidgetSlotItemHandler}.
260+ *
261+ * @see <a href="https://github.com/GregTechCE/GregTech/pull/1485">GregTech#1495 (Pull request)</a>
262+ * @see <a href="https://github.com/GregTechCE/GregTech/issues/1291">GregTech#1291 (Issue)</a>
263+ */
264+ @ Deprecated
148265 protected class WidgetSlotDelegate extends SlotItemHandler implements IScissored {
149266
150267 public WidgetSlotDelegate (IItemHandler itemHandler , int index , int xPosition , int yPosition ) {
@@ -189,5 +306,4 @@ public Rectangle getScissor() {
189306 return SlotWidget .this .scissor ;
190307 }
191308 }
192-
193309}
0 commit comments