-
Notifications
You must be signed in to change notification settings - Fork 497
/
BuildCraftContainer.java
268 lines (239 loc) · 8.17 KB
/
BuildCraftContainer.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Copyright (c) SpaceToad, 2011 http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public License
* 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.gui;
import buildcraft.BuildCraftCore;
import buildcraft.core.gui.slots.IPhantomSlot;
import buildcraft.core.gui.slots.SlotBase;
import buildcraft.core.gui.widgets.Widget;
import buildcraft.core.inventory.StackHelper;
import buildcraft.core.network.PacketGuiWidget;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public abstract class BuildCraftContainer extends Container {
private List<Widget> widgets = new ArrayList<Widget>();
private int inventorySize;
public BuildCraftContainer(int inventorySize) {
this.inventorySize = inventorySize;
}
public List<Widget> getWidgets() {
return widgets;
}
public void addSlot(Slot slot) {
addSlotToContainer(slot);
}
public void addWidget(Widget widget) {
widget.addToContainer(this);
widgets.add(widget);
}
public void sendWidgetDataToClient(Widget widget, ICrafting player, byte[] data) {
PacketGuiWidget pkt = new PacketGuiWidget(windowId, widgets.indexOf(widget), data);
BuildCraftCore.instance.sendToPlayer((EntityPlayer) player, pkt);
}
public void handleWidgetClientData(int widgetId, ByteBuf data) {
InputStream input = new ByteBufInputStream (data);
DataInputStream stream = new DataInputStream(input);
try {
widgets.get(widgetId).handleClientPacketData(stream);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void addCraftingToCrafters(ICrafting player) {
super.addCraftingToCrafters(player);
for (Widget widget : widgets) {
widget.initWidget(player);
}
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
for (Widget widget : widgets) {
for (ICrafting player : (List<ICrafting>) crafters) {
widget.updateWidget(player);
}
}
}
@Override
public ItemStack slotClick(int slotNum, int mouseButton, int modifier, EntityPlayer player) {
Slot slot = slotNum < 0 ? null : (Slot) this.inventorySlots.get(slotNum);
if (slot instanceof IPhantomSlot) {
return slotClickPhantom(slot, mouseButton, modifier, player);
}
return super.slotClick(slotNum, mouseButton, modifier, player);
}
private ItemStack slotClickPhantom(Slot slot, int mouseButton, int modifier, EntityPlayer player) {
ItemStack stack = null;
if (mouseButton == 2) {
if (((IPhantomSlot) slot).canAdjust()) {
slot.putStack(null);
}
} else if (mouseButton == 0 || mouseButton == 1) {
InventoryPlayer playerInv = player.inventory;
slot.onSlotChanged();
ItemStack stackSlot = slot.getStack();
ItemStack stackHeld = playerInv.getItemStack();
if (stackSlot != null) {
stack = stackSlot.copy();
}
if (stackSlot == null) {
if (stackHeld != null && slot.isItemValid(stackHeld)) {
fillPhantomSlot(slot, stackHeld, mouseButton, modifier);
}
} else if (stackHeld == null) {
adjustPhantomSlot(slot, mouseButton, modifier);
slot.onPickupFromSlot(player, playerInv.getItemStack());
} else if (slot.isItemValid(stackHeld)) {
if (StackHelper.instance().canStacksMerge(stackSlot, stackHeld)) {
adjustPhantomSlot(slot, mouseButton, modifier);
} else {
fillPhantomSlot(slot, stackHeld, mouseButton, modifier);
}
}
}
return stack;
}
protected void adjustPhantomSlot(Slot slot, int mouseButton, int modifier) {
if (!((IPhantomSlot) slot).canAdjust()) {
return;
}
ItemStack stackSlot = slot.getStack();
int stackSize;
if (modifier == 1) {
stackSize = mouseButton == 0 ? (stackSlot.stackSize + 1) / 2 : stackSlot.stackSize * 2;
} else {
stackSize = mouseButton == 0 ? stackSlot.stackSize - 1 : stackSlot.stackSize + 1;
}
if (stackSize > slot.getSlotStackLimit()) {
stackSize = slot.getSlotStackLimit();
}
stackSlot.stackSize = stackSize;
if (stackSlot.stackSize <= 0) {
slot.putStack((ItemStack) null);
}
}
protected void fillPhantomSlot(Slot slot, ItemStack stackHeld, int mouseButton, int modifier) {
if (!((IPhantomSlot) slot).canAdjust()) {
return;
}
int stackSize = mouseButton == 0 ? stackHeld.stackSize : 1;
if (stackSize > slot.getSlotStackLimit()) {
stackSize = slot.getSlotStackLimit();
}
ItemStack phantomStack = stackHeld.copy();
phantomStack.stackSize = stackSize;
slot.putStack(phantomStack);
}
protected boolean shiftItemStack(ItemStack stackToShift, int start, int end) {
boolean changed = false;
if (stackToShift.isStackable()) {
for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) {
Slot slot = (Slot) inventorySlots.get(slotIndex);
ItemStack stackInSlot = slot.getStack();
if (stackInSlot != null && StackHelper.instance().canStacksMerge(stackInSlot, stackToShift)) {
int resultingStackSize = stackInSlot.stackSize + stackToShift.stackSize;
int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit());
if (resultingStackSize <= max) {
stackToShift.stackSize = 0;
stackInSlot.stackSize = resultingStackSize;
slot.onSlotChanged();
changed = true;
} else if (stackInSlot.stackSize < max) {
stackToShift.stackSize -= max - stackInSlot.stackSize;
stackInSlot.stackSize = max;
slot.onSlotChanged();
changed = true;
}
}
}
}
if (stackToShift.stackSize > 0) {
for (int slotIndex = start; stackToShift.stackSize > 0 && slotIndex < end; slotIndex++) {
Slot slot = (Slot) inventorySlots.get(slotIndex);
ItemStack stackInSlot = slot.getStack();
if (stackInSlot == null) {
int max = Math.min(stackToShift.getMaxStackSize(), slot.getSlotStackLimit());
stackInSlot = stackToShift.copy();
stackInSlot.stackSize = Math.min(stackToShift.stackSize, max);
stackToShift.stackSize -= stackInSlot.stackSize;
slot.putStack(stackInSlot);
slot.onSlotChanged();
changed = true;
}
}
}
return changed;
}
private boolean tryShiftItem(ItemStack stackToShift, int numSlots) {
for (int machineIndex = 0; machineIndex < numSlots - 9 * 4; machineIndex++) {
Slot slot = (Slot) inventorySlots.get(machineIndex);
if (slot instanceof SlotBase && !((SlotBase) slot).canShift()) {
continue;
}
if (slot instanceof IPhantomSlot) {
continue;
}
if (!slot.isItemValid(stackToShift)) {
continue;
}
if (shiftItemStack(stackToShift, machineIndex, machineIndex + 1)) {
return true;
}
}
return false;
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
ItemStack originalStack = null;
Slot slot = (Slot) inventorySlots.get(slotIndex);
int numSlots = inventorySlots.size();
if (slot != null && slot.getHasStack()) {
ItemStack stackInSlot = slot.getStack();
originalStack = stackInSlot.copy();
if (slotIndex >= numSlots - 9 * 4 && tryShiftItem(stackInSlot, numSlots)) {
// NOOP
} else if (slotIndex >= numSlots - 9 * 4 && slotIndex < numSlots - 9) {
if (!shiftItemStack(stackInSlot, numSlots - 9, numSlots)) {
return null;
}
} else if (slotIndex >= numSlots - 9 && slotIndex < numSlots) {
if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots - 9)) {
return null;
}
} else if (!shiftItemStack(stackInSlot, numSlots - 9 * 4, numSlots)) {
return null;
}
slot.onSlotChange(stackInSlot, originalStack);
if (stackInSlot.stackSize <= 0) {
slot.putStack(null);
} else {
slot.onSlotChanged();
}
if (stackInSlot.stackSize == originalStack.stackSize) {
return null;
}
slot.onPickupFromSlot(player, stackInSlot);
}
return originalStack;
}
public int getInventorySize() {
return inventorySize;
}
}