-
Notifications
You must be signed in to change notification settings - Fork 10
/
ShulkerBoxContainer.java
185 lines (151 loc) · 7.82 KB
/
ShulkerBoxContainer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.progwml6.ironshulkerbox.common.inventory;
import com.progwml6.ironshulkerbox.common.blocks.ShulkerBoxType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public class ShulkerBoxContainer extends Container
{
private final IInventory inventory;
private final ShulkerBoxType shulkerBoxType;
private ShulkerBoxContainer(ContainerType<?> containerType, int windowId, PlayerInventory playerInventory)
{
this(containerType, windowId, playerInventory, new Inventory(ShulkerBoxType.VANILLA.size), ShulkerBoxType.VANILLA);
}
public static ShulkerBoxContainer createIronContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.IRON_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.IRON.size), ShulkerBoxType.IRON);
}
public static ShulkerBoxContainer createIronContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.IRON_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.IRON);
}
public static ShulkerBoxContainer createGoldContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.GOLD_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.GOLD.size), ShulkerBoxType.GOLD);
}
public static ShulkerBoxContainer createGoldContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.GOLD_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.GOLD);
}
public static ShulkerBoxContainer createDiamondContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.DIAMOND_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.DIAMOND.size), ShulkerBoxType.DIAMOND);
}
public static ShulkerBoxContainer createDiamondContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.DIAMOND_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.DIAMOND);
}
public static ShulkerBoxContainer createCrystalContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.CRYSTAL_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.CRYSTAL.size), ShulkerBoxType.CRYSTAL);
}
public static ShulkerBoxContainer createCrystalContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.CRYSTAL_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.CRYSTAL);
}
public static ShulkerBoxContainer createCopperContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.COPPER_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.COPPER.size), ShulkerBoxType.COPPER);
}
public static ShulkerBoxContainer createCopperContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.COPPER_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.COPPER);
}
public static ShulkerBoxContainer createSilverContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.SILVER_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.CRYSTAL.size), ShulkerBoxType.SILVER);
}
public static ShulkerBoxContainer createSilverContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.SILVER_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.SILVER);
}
public static ShulkerBoxContainer createObsidianContainer(int windowId, PlayerInventory playerInventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.OBSIDIAN_SHULKER_BOX, windowId, playerInventory, new Inventory(ShulkerBoxType.OBSIDIAN.size), ShulkerBoxType.OBSIDIAN);
}
public static ShulkerBoxContainer createObsidianContainer(int windowId, PlayerInventory playerInventory, IInventory inventory)
{
return new ShulkerBoxContainer(ShulkerBoxContainerType.OBSIDIAN_SHULKER_BOX, windowId, playerInventory, inventory, ShulkerBoxType.OBSIDIAN);
}
public ShulkerBoxContainer(ContainerType<?> containerType, int windowId, PlayerInventory playerInventory, IInventory inventory, ShulkerBoxType shulkerBoxType)
{
super(containerType, windowId);
assertInventorySize(inventory, shulkerBoxType.size);
this.inventory = inventory;
this.shulkerBoxType = shulkerBoxType;
inventory.openInventory(playerInventory.player);
for (int shulkerBoxRow = 0; shulkerBoxRow < shulkerBoxType.getRowCount(); shulkerBoxRow++)
{
for (int shulkerBoxCol = 0; shulkerBoxCol < shulkerBoxType.rowLength; shulkerBoxCol++)
{
this.addSlot(new ShulkerBoxSlot(inventory, shulkerBoxCol + shulkerBoxRow * shulkerBoxType.rowLength, 12 + shulkerBoxCol * 18, 18 + shulkerBoxRow * 18));
}
}
int leftCol = (shulkerBoxType.xSize - 162) / 2 + 1;
for (int playerInvRow = 0; playerInvRow < 3; playerInvRow++)
{
for (int playerInvCol = 0; playerInvCol < 9; playerInvCol++)
{
this.addSlot(new Slot(playerInventory, playerInvCol + playerInvRow * 9 + 9, leftCol + playerInvCol * 18, shulkerBoxType.ySize - (4 - playerInvRow) * 18 - 10));
}
}
for (int hotbarSlot = 0; hotbarSlot < 9; hotbarSlot++)
{
this.addSlot(new Slot(playerInventory, hotbarSlot, leftCol + hotbarSlot * 18, shulkerBoxType.ySize - 24));
}
}
@Override
public boolean canInteractWith(PlayerEntity playerIn)
{
return this.inventory.isUsableByPlayer(playerIn);
}
@Override
public ItemStack transferStackInSlot(PlayerEntity 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 < this.shulkerBoxType.size)
{
if (!this.mergeItemStack(itemstack1, this.shulkerBoxType.size, this.inventorySlots.size(), true))
{
return ItemStack.EMPTY;
}
}
else if (!this.mergeItemStack(itemstack1, 0, this.shulkerBoxType.size, false))
{
return ItemStack.EMPTY;
}
if (itemstack1.isEmpty())
{
slot.putStack(ItemStack.EMPTY);
}
else
{
slot.onSlotChanged();
}
}
return itemstack;
}
@Override
public void onContainerClosed(PlayerEntity playerIn)
{
super.onContainerClosed(playerIn);
this.inventory.closeInventory(playerIn);
}
@OnlyIn(Dist.CLIENT)
public ShulkerBoxType getShulkerBoxType()
{
return this.shulkerBoxType;
}
}