Skip to content

Commit 76fd265

Browse files
Fix wheel action in MouseTweaks and Crafting Station voiding items (#1485)
1 parent 2ed876a commit 76fd265

File tree

5 files changed

+144
-24
lines changed

5 files changed

+144
-24
lines changed

src/main/java/gregtech/api/gui/ModularUI.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import net.minecraft.entity.player.EntityPlayer;
1111
import net.minecraft.entity.player.InventoryPlayer;
1212
import net.minecraftforge.items.IItemHandlerModifiable;
13-
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;
1413

1514
import java.util.ArrayList;
1615
import java.util.List;
@@ -192,7 +191,7 @@ public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea
192191
public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea imageLocation, int x, int y) {
193192
for (int row = 0; row < 3; row++) {
194193
for (int col = 0; col < 9; col++) {
195-
this.widget(new SlotWidget(new PlayerMainInvWrapper(inventoryPlayer), col + (row + 1) * 9, x + col * 18, y + row * 18)
194+
this.widget(new SlotWidget(inventoryPlayer, col + (row + 1) * 9, x + col * 18, y + row * 18)
196195
.setBackgroundTexture(imageLocation)
197196
.setLocationInfo(true, false));
198197
}
@@ -202,7 +201,7 @@ public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea
202201

203202
public Builder bindPlayerHotbar(InventoryPlayer inventoryPlayer, TextureArea imageLocation, int x, int y) {
204203
for (int slot = 0; slot < 9; slot++) {
205-
this.widget(new SlotWidget(new PlayerMainInvWrapper(inventoryPlayer), slot, x + slot * 18, y)
204+
this.widget(new SlotWidget(inventoryPlayer, slot, x + slot * 18, y)
206205
.setBackgroundTexture(imageLocation)
207206
.setLocationInfo(true, true));
208207
}

src/main/java/gregtech/api/gui/widgets/SlotWidget.java

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import gregtech.api.util.Size;
1414
import net.minecraft.entity.player.EntityPlayer;
1515
import net.minecraft.inventory.ClickType;
16+
import net.minecraft.inventory.IInventory;
17+
import net.minecraft.inventory.Slot;
1618
import net.minecraft.item.ItemStack;
1719
import net.minecraftforge.fml.relauncher.Side;
1820
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -22,7 +24,7 @@
2224

2325
public 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
}

src/main/java/gregtech/common/gui/widget/CraftingSlotWidget.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import gregtech.common.metatileentities.storage.CraftingRecipeResolver;
88
import mezz.jei.api.gui.IGuiIngredient;
99
import net.minecraft.entity.player.EntityPlayer;
10+
import net.minecraft.inventory.IInventory;
11+
import net.minecraft.inventory.InventoryCraftResult;
1012
import net.minecraft.item.ItemStack;
1113
import net.minecraft.network.PacketBuffer;
12-
import net.minecraftforge.items.IItemHandler;
13-
import net.minecraftforge.items.ItemStackHandler;
1414

1515
import java.io.IOException;
1616
import java.util.HashMap;
@@ -23,13 +23,12 @@ public class CraftingSlotWidget extends SlotWidget implements IRecipeTransferHan
2323
private boolean canTakeStack = false;
2424

2525
public CraftingSlotWidget(CraftingRecipeResolver recipeResolver, int slotIndex, int xPosition, int yPosition) {
26-
//pass delegate here to avoid using IItemHandlerModifiable-related tricks by SlotItemHandler
27-
super(createItemHandler(recipeResolver), slotIndex, xPosition, yPosition, true, false);
26+
super(createInventory(recipeResolver), slotIndex, xPosition, yPosition, true, false);
2827
this.recipeResolver = recipeResolver;
2928
}
3029

31-
private static IItemHandler createItemHandler(CraftingRecipeResolver resolver) {
32-
return resolver == null ? new ItemStackHandler(1) : resolver.getCraftingResultInventory();
30+
private static IInventory createInventory(CraftingRecipeResolver resolver) {
31+
return resolver == null ? new InventoryCraftResult() : resolver.getCraftingResultInventory();
3332
}
3433

3534
@Override

src/main/java/gregtech/common/metatileentities/storage/CraftingRecipeResolver.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import gregtech.common.inventory.itemsource.ItemSourceList;
66
import gregtech.common.inventory.itemsource.sources.TileItemSource;
77
import net.minecraft.entity.player.EntityPlayer;
8+
import net.minecraft.inventory.IInventory;
9+
import net.minecraft.inventory.InventoryCraftResult;
810
import net.minecraft.inventory.InventoryCrafting;
911
import net.minecraft.item.ItemStack;
1012
import net.minecraft.item.crafting.CraftingManager;
@@ -28,7 +30,7 @@ public class CraftingRecipeResolver {
2830
private final ItemStackHandler craftingGrid;
2931
private final InventoryCrafting inventoryCrafting = new InventoryCrafting(new DummyContainer(), 3, 3);
3032
private IRecipe cachedRecipe = null;
31-
private final ItemStackHandler craftingResultInventory = new ItemStackHandler(1);
33+
private final IInventory craftingResultInventory = new InventoryCraftResult();
3234
private long timer = 0L;
3335
private CachedRecipeData cachedRecipeData = null;
3436
private int itemsCrafted = 0;
@@ -46,7 +48,7 @@ public ItemSourceList getItemSourceList() {
4648
return itemSourceList;
4749
}
4850

49-
public ItemStackHandler getCraftingResultInventory() {
51+
public IInventory getCraftingResultInventory() {
5052
return craftingResultInventory;
5153
}
5254

@@ -117,7 +119,7 @@ public void refreshOutputSlot() {
117119
if (cachedRecipe != null) {
118120
itemStack = cachedRecipe.getCraftingResult(inventoryCrafting).copy();
119121
}
120-
this.craftingResultInventory.setStackInSlot(0, itemStack);
122+
this.craftingResultInventory.setInventorySlotContents(0, itemStack);
121123
}
122124

123125
public boolean checkRecipeValid() {
@@ -137,12 +139,12 @@ private void updateCurrentRecipe() {
137139
this.cachedRecipe = newRecipe;
138140
if (newRecipe != null) {
139141
ItemStack resultStack = newRecipe.getCraftingResult(inventoryCrafting).copy();
140-
this.craftingResultInventory.setStackInSlot(0, resultStack.copy());
142+
this.craftingResultInventory.setInventorySlotContents(0, resultStack.copy());
141143
this.cachedRecipeData = new CachedRecipeData(itemSourceList, newRecipe, resultStack.copy());
142144
copyInventoryItems(craftingGrid, new InvWrapper(this.cachedRecipeData.inventory));
143145
this.cachedRecipeData.attemptMatchRecipe();
144146
} else {
145-
this.craftingResultInventory.setStackInSlot(0, ItemStack.EMPTY);
147+
this.craftingResultInventory.setInventorySlotContents(0, ItemStack.EMPTY);
146148
this.cachedRecipeData = null;
147149
}
148150
}

src/main/java/gregtech/integration/jei/recipe/RecipeMapCategory.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.minecraftforge.fluids.FluidStack;
2121
import net.minecraftforge.fluids.FluidTank;
2222
import net.minecraftforge.items.ItemStackHandler;
23+
import net.minecraftforge.items.SlotItemHandler;
2324

2425
import java.util.List;
2526

@@ -77,18 +78,21 @@ public void setRecipe(IRecipeLayout recipeLayout, GTRecipeWrapper recipeWrapper,
7778

7879
if (uiWidget instanceof SlotWidget) {
7980
SlotWidget slotWidget = (SlotWidget) uiWidget;
80-
if (slotWidget.getHandle().getItemHandler() == importItems) {
81+
if (!(slotWidget.getHandle() instanceof SlotItemHandler)) {
82+
continue;
83+
}
84+
SlotItemHandler handle = (SlotItemHandler) slotWidget.getHandle();
85+
if (handle.getItemHandler() == importItems) {
8186
//this is input item stack slot widget, so add it to item group
82-
itemStackGroup.init(slotWidget.getHandle().getSlotIndex(), true,
87+
itemStackGroup.init(handle.getSlotIndex(), true,
8388
slotWidget.getPosition().x,
8489
slotWidget.getPosition().y);
85-
} else if (slotWidget.getHandle().getItemHandler() == exportItems) {
90+
} else if (handle.getItemHandler() == exportItems) {
8691
//this is output item stack slot widget, so add it to item group
87-
itemStackGroup.init(importItems.getSlots() + slotWidget.getHandle().getSlotIndex(), false,
92+
itemStackGroup.init(importItems.getSlots() + handle.getSlotIndex(), false,
8893
slotWidget.getPosition().x,
8994
slotWidget.getPosition().y);
9095
}
91-
9296
} else if (uiWidget instanceof TankWidget) {
9397
TankWidget tankWidget = (TankWidget) uiWidget;
9498
if (importFluids.getFluidTanks().contains(tankWidget.fluidTank)) {

0 commit comments

Comments
 (0)