mirror of
https://github.com/GTNewHorizons/TinkersConstruct.git
synced 2026-11-13 17:04:24 +00:00
Make crafting stations slots use ISidedInventory/IInventory validation logic (#48)
* Update buildscript * Update dependencies * Make crafting stations slots use ISidedInventory/IInventory validation logic * Fix handling of inventories with inaccessible slots
This commit is contained in:
1 parent
6ae7c13408
commit
e9cacb5e37
5 files changed
+62
-28
No files matched your search
@@ -2,31 +2,59 @@ package tconstruct.tools.gui;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class ChestSlot extends Slot {
|
||||
/*
|
||||
* A Slot, used for adjacent Chest Inventories, that can be disabled.
|
||||
*/
|
||||
public boolean enabled = true;
|
||||
|
||||
public ChestSlot(IInventory inventory, int index, int xPosition, int yPosition) {
|
||||
private int accessSide;
|
||||
private int visualIndex;
|
||||
|
||||
public ChestSlot(IInventory inventory, int index, int visualIndex, int xPosition, int yPosition, int accessSide) {
|
||||
super(inventory, index, xPosition, yPosition);
|
||||
this.accessSide = accessSide;
|
||||
this.visualIndex = visualIndex;
|
||||
}
|
||||
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public boolean func_111238_b/*isEnabled*/() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
|
||||
public int getVisualIndex() {
|
||||
return visualIndex;
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
|
||||
public void enable() {
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(ItemStack itemStack) {
|
||||
if (inventory instanceof ISidedInventory) {
|
||||
ISidedInventory sided = (ISidedInventory) inventory;
|
||||
return sided.canInsertItem(this.getSlotIndex(), itemStack, accessSide);
|
||||
}
|
||||
return inventory.isItemValidForSlot(this.getSlotIndex(), itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTakeStack(EntityPlayer player) {
|
||||
if (inventory instanceof ISidedInventory) {
|
||||
ISidedInventory sided = (ISidedInventory) inventory;
|
||||
return sided.canExtractItem(this.getSlotIndex(), this.getStack(), accessSide);
|
||||
}
|
||||
return super.canTakeStack(player);
|
||||
}
|
||||
}
|
||||
@@ -325,13 +325,12 @@ public class CraftingStationGui extends GuiContainer implements INEIGuiHandler {
|
||||
|
||||
public boolean shouldDrawSlot(Slot slot) {
|
||||
if(!(slot instanceof ChestSlot)) return true;
|
||||
|
||||
if(slot.getSlotIndex() >= chestSlotCount) return false;
|
||||
ChestSlot chestSlot = (ChestSlot) slot;
|
||||
|
||||
// all visible
|
||||
if(!slider.isEnabled()) return true;
|
||||
|
||||
return firstSlotId <= slot.getSlotIndex() && lastSlotId > slot.getSlotIndex();
|
||||
return firstSlotId <= chestSlot.getVisualIndex() && lastSlotId > chestSlot.getVisualIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -445,7 +444,7 @@ public class CraftingStationGui extends GuiContainer implements INEIGuiHandler {
|
||||
if(shouldDrawSlot(slot)) {
|
||||
slot.enable();
|
||||
// calc position of the slot
|
||||
final int offset = slot.getSlotIndex() + (slot.inventory == secondInventory ? 27 : 0) - firstSlotId;
|
||||
final int offset = slot.getVisualIndex();
|
||||
final int x = (offset % logic.invColumns) * CraftingStationGui.slotElement.w;
|
||||
final int y = (offset / logic.invColumns) * CraftingStationGui.slotElement.h;
|
||||
|
||||
|
||||
@@ -92,7 +92,8 @@ public class CraftingStationContainer extends Container {
|
||||
IInventory inv = logic.getFirstInventory();
|
||||
IInventory secondInv = logic.getSecondInventory();
|
||||
|
||||
final Set<Integer> accessibleSlots = inv instanceof ISidedInventory ? new HashSet<>(Ints.asList(((ISidedInventory) inv).getAccessibleSlotsFromSide(logic.chestDirection.getOpposite().ordinal()))) : null;
|
||||
final int accessSide = logic.chestDirection.getOpposite().ordinal();
|
||||
final int[] accessibleSlots = inv instanceof ISidedInventory ? ((ISidedInventory) inv).getAccessibleSlotsFromSide(accessSide) : null;
|
||||
|
||||
int index = 0, curIndex = 0;
|
||||
IInventory curInv;
|
||||
@@ -104,10 +105,16 @@ public class CraftingStationContainer extends Container {
|
||||
curInv = secondInv != null && index >= 27 ? secondInv : inv;
|
||||
// Adjust the index for the inventory
|
||||
curIndex = secondInv != null && index >= 27 ? index - 27 : index;
|
||||
|
||||
if(accessibleSlots == null || accessibleSlots.contains(curIndex)) {
|
||||
this.addSlotToContainer(new ChestSlot(curInv, curIndex, 8 + col * 18, 19 + row * 18));
|
||||
|
||||
if(accessibleSlots != null) {
|
||||
if (curIndex >= accessibleSlots.length) {
|
||||
break;
|
||||
} else {
|
||||
curIndex = accessibleSlots[curIndex];
|
||||
}
|
||||
}
|
||||
|
||||
this.addSlotToContainer(new ChestSlot(curInv, curIndex, index, 8 + col * 18, 19 + row * 18, accessSide));
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user