Skip to content

Commit

Permalink
Fix wheel action in MouseTweaks and Crafting Station voiding items (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CrafterKolyan committed May 18, 2021
1 parent 2ed876a commit 76fd265
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/main/java/gregtech/api/gui/ModularUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -192,7 +191,7 @@ public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea
public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea imageLocation, int x, int y) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
this.widget(new SlotWidget(new PlayerMainInvWrapper(inventoryPlayer), col + (row + 1) * 9, x + col * 18, y + row * 18)
this.widget(new SlotWidget(inventoryPlayer, col + (row + 1) * 9, x + col * 18, y + row * 18)
.setBackgroundTexture(imageLocation)
.setLocationInfo(true, false));
}
Expand All @@ -202,7 +201,7 @@ public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea

public Builder bindPlayerHotbar(InventoryPlayer inventoryPlayer, TextureArea imageLocation, int x, int y) {
for (int slot = 0; slot < 9; slot++) {
this.widget(new SlotWidget(new PlayerMainInvWrapper(inventoryPlayer), slot, x + slot * 18, y)
this.widget(new SlotWidget(inventoryPlayer, slot, x + slot * 18, y)
.setBackgroundTexture(imageLocation)
.setLocationInfo(true, true));
}
Expand Down
126 changes: 121 additions & 5 deletions src/main/java/gregtech/api/gui/widgets/SlotWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import gregtech.api.util.Size;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand All @@ -22,7 +24,7 @@

public class SlotWidget extends Widget implements INativeWidget {

protected SlotItemHandler slotReference;
protected Slot slotReference;
protected boolean isEnabled = true;

protected boolean canTakeItems;
Expand All @@ -34,15 +36,26 @@ public class SlotWidget extends Widget implements INativeWidget {

protected Rectangle scissor;

public SlotWidget(IInventory inventory, int slotIndex, int xPosition, int yPosition, boolean canTakeItems, boolean canPutItems) {
super(new Position(xPosition, yPosition), new Size(18, 18));
this.canTakeItems = canTakeItems;
this.canPutItems = canPutItems;
this.slotReference = createSlot(inventory, slotIndex);
}

public SlotWidget(IItemHandler itemHandler, int slotIndex, int xPosition, int yPosition, boolean canTakeItems, boolean canPutItems) {
super(new Position(xPosition, yPosition), new Size(18, 18));
this.canTakeItems = canTakeItems;
this.canPutItems = canPutItems;
this.slotReference = createSlot(itemHandler, slotIndex);
}

protected SlotItemHandler createSlot(IItemHandler itemHandler, int index) {
return new WidgetSlotDelegate(itemHandler, index, 0, 0);
protected Slot createSlot(IInventory inventory, int index) {
return new WidgetSlot(inventory, index, 0, 0);
}

protected Slot createSlot(IItemHandler itemHandler, int index) {
return new WidgetSlotItemHandler(itemHandler, index, 0, 0);
}

@Override
Expand Down Expand Up @@ -89,6 +102,10 @@ public SlotWidget(IItemHandlerModifiable itemHandler, int slotIndex, int xPositi
this(itemHandler, slotIndex, xPosition, yPosition, true, true);
}

public SlotWidget(IInventory inventory, int slotIndex, int xPosition, int yPosition) {
this(inventory, slotIndex, xPosition, yPosition, true, true);
}

/**
* Sets array of background textures used by slot
* they are drawn on top of each other
Expand Down Expand Up @@ -141,10 +158,110 @@ public ItemStack slotClick(int dragType, ClickType clickTypeIn, EntityPlayer pla
}

@Override
public final SlotItemHandler getHandle() {
public final Slot getHandle() {
return slotReference;
}

protected class WidgetSlot extends Slot implements IScissored {
public WidgetSlot(IInventory inventory, int index, int xPosition, int yPosition) {
super(inventory, index, xPosition, yPosition);
}

@Override
public boolean isItemValid(@Nonnull ItemStack stack) {
return SlotWidget.this.canPutStack(stack) && super.isItemValid(stack);
}

@Override
public boolean canTakeStack(EntityPlayer playerIn) {
return SlotWidget.this.canTakeStack(playerIn) && super.canTakeStack(playerIn);
}

@Override
public void putStack(@Nonnull ItemStack stack) {
super.putStack(stack);
if (changeListener != null) {
changeListener.run();
}
}

@Override
public final ItemStack onTake(EntityPlayer thePlayer, ItemStack stack) {
return onItemTake(thePlayer, super.onTake(thePlayer, stack), false);
}

@Override
public void onSlotChanged() {
SlotWidget.this.onSlotChanged();
}

@Override
public boolean isEnabled() {
return SlotWidget.this.isEnabled();
}

@Override
public Rectangle getScissor() {
return SlotWidget.this.scissor;
}
}

protected class WidgetSlotItemHandler extends SlotItemHandler implements IScissored {

public WidgetSlotItemHandler(IItemHandler itemHandler, int index, int xPosition, int yPosition) {
super(itemHandler, index, xPosition, yPosition);
}

@Override
public boolean isItemValid(@Nonnull ItemStack stack) {
return SlotWidget.this.canPutStack(stack) && super.isItemValid(stack);
}

@Override
public boolean canTakeStack(EntityPlayer playerIn) {
return SlotWidget.this.canTakeStack(playerIn) && super.canTakeStack(playerIn);
}

@Override
public void putStack(@Nonnull ItemStack stack) {
super.putStack(stack);
if (changeListener != null) {
changeListener.run();
}
}

@Override
public final ItemStack onTake(EntityPlayer thePlayer, ItemStack stack) {
return onItemTake(thePlayer, super.onTake(thePlayer, stack), false);
}

@Override
public void onSlotChanged() {
SlotWidget.this.onSlotChanged();
}

@Override
public boolean isEnabled() {
return SlotWidget.this.isEnabled();
}

@Override
public Rectangle getScissor() {
return SlotWidget.this.scissor;
}
}

/**
* @deprecated
* Use {@link WidgetSlotItemHandler} instead. <br>
* {@link WidgetSlotDelegate} was renamed to {@link WidgetSlotItemHandler} since GregTech 1.15.0.<br>
* Explanation of deprecation: In order to fix mouse wheel action a new class was introduced ({@link WidgetSlot}).
* To have consistent names {@link WidgetSlotDelegate} was renamed to {@link WidgetSlotItemHandler}.
*
* @see <a href="https://github.com/GregTechCE/GregTech/pull/1485">GregTech#1495 (Pull request)</a>
* @see <a href="https://github.com/GregTechCE/GregTech/issues/1291">GregTech#1291 (Issue)</a>
*/
@Deprecated
protected class WidgetSlotDelegate extends SlotItemHandler implements IScissored {

public WidgetSlotDelegate(IItemHandler itemHandler, int index, int xPosition, int yPosition) {
Expand Down Expand Up @@ -189,5 +306,4 @@ public Rectangle getScissor() {
return SlotWidget.this.scissor;
}
}

}
11 changes: 5 additions & 6 deletions src/main/java/gregtech/common/gui/widget/CraftingSlotWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import gregtech.common.metatileentities.storage.CraftingRecipeResolver;
import mezz.jei.api.gui.IGuiIngredient;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;

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

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

private static IItemHandler createItemHandler(CraftingRecipeResolver resolver) {
return resolver == null ? new ItemStackHandler(1) : resolver.getCraftingResultInventory();
private static IInventory createInventory(CraftingRecipeResolver resolver) {
return resolver == null ? new InventoryCraftResult() : resolver.getCraftingResultInventory();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import gregtech.common.inventory.itemsource.ItemSourceList;
import gregtech.common.inventory.itemsource.sources.TileItemSource;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
Expand All @@ -28,7 +30,7 @@ public class CraftingRecipeResolver {
private final ItemStackHandler craftingGrid;
private final InventoryCrafting inventoryCrafting = new InventoryCrafting(new DummyContainer(), 3, 3);
private IRecipe cachedRecipe = null;
private final ItemStackHandler craftingResultInventory = new ItemStackHandler(1);
private final IInventory craftingResultInventory = new InventoryCraftResult();
private long timer = 0L;
private CachedRecipeData cachedRecipeData = null;
private int itemsCrafted = 0;
Expand All @@ -46,7 +48,7 @@ public ItemSourceList getItemSourceList() {
return itemSourceList;
}

public ItemStackHandler getCraftingResultInventory() {
public IInventory getCraftingResultInventory() {
return craftingResultInventory;
}

Expand Down Expand Up @@ -117,7 +119,7 @@ public void refreshOutputSlot() {
if (cachedRecipe != null) {
itemStack = cachedRecipe.getCraftingResult(inventoryCrafting).copy();
}
this.craftingResultInventory.setStackInSlot(0, itemStack);
this.craftingResultInventory.setInventorySlotContents(0, itemStack);
}

public boolean checkRecipeValid() {
Expand All @@ -137,12 +139,12 @@ private void updateCurrentRecipe() {
this.cachedRecipe = newRecipe;
if (newRecipe != null) {
ItemStack resultStack = newRecipe.getCraftingResult(inventoryCrafting).copy();
this.craftingResultInventory.setStackInSlot(0, resultStack.copy());
this.craftingResultInventory.setInventorySlotContents(0, resultStack.copy());
this.cachedRecipeData = new CachedRecipeData(itemSourceList, newRecipe, resultStack.copy());
copyInventoryItems(craftingGrid, new InvWrapper(this.cachedRecipeData.inventory));
this.cachedRecipeData.attemptMatchRecipe();
} else {
this.craftingResultInventory.setStackInSlot(0, ItemStack.EMPTY);
this.craftingResultInventory.setInventorySlotContents(0, ItemStack.EMPTY);
this.cachedRecipeData = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;

import java.util.List;

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

if (uiWidget instanceof SlotWidget) {
SlotWidget slotWidget = (SlotWidget) uiWidget;
if (slotWidget.getHandle().getItemHandler() == importItems) {
if (!(slotWidget.getHandle() instanceof SlotItemHandler)) {
continue;
}
SlotItemHandler handle = (SlotItemHandler) slotWidget.getHandle();
if (handle.getItemHandler() == importItems) {
//this is input item stack slot widget, so add it to item group
itemStackGroup.init(slotWidget.getHandle().getSlotIndex(), true,
itemStackGroup.init(handle.getSlotIndex(), true,
slotWidget.getPosition().x,
slotWidget.getPosition().y);
} else if (slotWidget.getHandle().getItemHandler() == exportItems) {
} else if (handle.getItemHandler() == exportItems) {
//this is output item stack slot widget, so add it to item group
itemStackGroup.init(importItems.getSlots() + slotWidget.getHandle().getSlotIndex(), false,
itemStackGroup.init(importItems.getSlots() + handle.getSlotIndex(), false,
slotWidget.getPosition().x,
slotWidget.getPosition().y);
}

} else if (uiWidget instanceof TankWidget) {
TankWidget tankWidget = (TankWidget) uiWidget;
if (importFluids.getFluidTanks().contains(tankWidget.fluidTank)) {
Expand Down

0 comments on commit 76fd265

Please sign in to comment.