-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathTestContainer.java
91 lines (75 loc) · 3.06 KB
/
TestContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package mcjty.modtut.blocks.testcontainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import javax.annotation.Nullable;
public class TestContainer extends Container {
private TestContainerTileEntity te;
public TestContainer(IInventory playerInventory, TestContainerTileEntity te) {
this.te = te;
// This container references items out of our own inventory (the 9 slots we hold ourselves)
// as well as the slots from the player inventory so that the user can transfer items between
// both inventories. The two calls below make sure that slots are defined for both inventories.
addOwnSlots();
addPlayerSlots(playerInventory);
}
private void addPlayerSlots(IInventory playerInventory) {
// Slots for the main inventory
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 9; ++col) {
int x = 10 + col * 18;
int y = row * 18 + 70;
this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 9, x, y));
}
}
// Slots for the hotbar
for (int row = 0; row < 9; ++row) {
int x = 10 + row * 18;
int y = 58 + 70;
this.addSlotToContainer(new Slot(playerInventory, row, x, y));
}
}
private void addOwnSlots() {
IItemHandler itemHandler = this.te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
int x = 10;
int y = 6;
// Add our own slots
int slotIndex = 0;
for (int i = 0; i < itemHandler.getSlots(); i++) {
addSlotToContainer(new SlotItemHandler(itemHandler, slotIndex, x, y));
slotIndex++;
x += 18;
}
}
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.inventorySlots.get(index);
if (slot != null && slot.getHasStack()) {
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (index < TestContainerTileEntity.SIZE) {
if (!this.mergeItemStack(itemstack1, TestContainerTileEntity.SIZE, this.inventorySlots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!this.mergeItemStack(itemstack1, 0, TestContainerTileEntity.SIZE, false)) {
return ItemStack.EMPTY;
}
if (itemstack1.isEmpty()) {
slot.putStack(ItemStack.EMPTY);
} else {
slot.onSlotChanged();
}
}
return itemstack;
}
@Override
public boolean canInteractWith(EntityPlayer playerIn) {
return te.canInteractWith(playerIn);
}
}