-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileAutoWorkbench.java
278 lines (222 loc) · 7.61 KB
/
TileAutoWorkbench.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
269
270
271
272
273
274
275
276
277
278
/**
* 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.factory;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
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;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
import buildcraft.api.inventory.ISpecialInventory;
import buildcraft.core.inventory.TransactorRoundRobin;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
public class TileAutoWorkbench extends TileEntity implements ISpecialInventory {
private IInventory craftResult = new InventoryCraftResult();
private InventoryCrafting crafting = new InventoryCrafting(new Container() {
@Override
public boolean canInteractWith(EntityPlayer entityplayer) {
return false;
}
@Override
public void onCraftMatrixChanged(IInventory inventory) {}
}, 3, 3);
@Override
public void onInventoryChanged() {
craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(crafting, this.worldObj));
super.onInventoryChanged();
}
public IInventory getCraftResult() {
return this.craftResult;
}
@Override
public int getSizeInventory() {
return this.crafting.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return this.crafting.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slot, int amount) {
ItemStack item = this.crafting.decrStackSize(slot, amount);
this.onInventoryChanged();
return item;
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
this.crafting.setInventorySlotContents(slot, stack);
this.onInventoryChanged();
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return this.crafting.getStackInSlotOnClosing(slot);
}
@Override
public String getInvName() {
return "AutoCrafting";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) == this;
}
public void refilFromNeibour(){
//pre-compute to see if there are enough items
{
ItemStack[] required = new ItemStack[this.getSizeInventory()];
for (int slot = 0; slot < this.getSizeInventory(); slot++){
ItemStack item = this.getStackInSlot(slot);
if (item != null && item.stackSize == 1){
required[slot] = item.copy();
}
}
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS){
TileEntity tileEntity = this.worldObj.getBlockTileEntity(side.offsetX + this.xCoord, side.offsetY + this.yCoord, side.offsetZ + this.zCoord);
if (tileEntity instanceof IInventory){
IInventory inventory = (IInventory) tileEntity;
ItemStack remaining = null;
for (int slot = 0; slot < inventory.getSizeInventory(); slot++){
ItemStack item = inventory.getStackInSlot(slot);
if (item != null){
remaining = item.copy();
for (int index = 0; index < required.length; index++){
if (required[index] != null && item.isItemEqual(required[index])){
required[index] = null;
if (remaining.stackSize == 1){
remaining = null;
break;
}else{
remaining.stackSize--;
}
}
}
}
}
}
}
for (int index = 0; index < required.length; index++){
if (required[index] != null) return;
}
}
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS){
TileEntity tileEntity = this.worldObj.getBlockTileEntity(side.offsetX + this.xCoord, side.offsetY + this.yCoord, side.offsetZ + this.zCoord);
/*
* FIXME: make this support ISidedInvontory. That means forge's and vanilla's implementation
* perhaps have this work with ISpecialInvenotry?
*/
if (tileEntity instanceof IInventory){
IInventory inventory = (IInventory) tileEntity;
for (int slot = 0; slot < inventory.getSizeInventory(); slot++){
ItemStack item = inventory.getStackInSlot(slot);
if (item != null){
boolean refilAll = false;
for (int slot1 = 0; slot1 < this.getSizeInventory(); slot1++){
ItemStack item1 = this.getStackInSlot(slot1);
if (item1 != null && item1.stackSize == 1 && item.isItemEqual(item1)){
item1.stackSize++;
inventory.decrStackSize(slot, 1);
if (slot1 == this.getSizeInventory() - 1) refilAll = true;
}else{
refilAll = false;
}
}
if (refilAll) return;
}
}
}
}
}
private void craft(){
for (int slot = 0; slot < this.getSizeInventory(); slot++){
ItemStack item = this.getStackInSlot(slot);
if (item != null){
this.decrStackSize(slot, 1);
if (item.getItem().hasContainerItem()){
ItemStack item2 = item.getItem().getContainerItemStack(item);
if (item2.isItemStackDamageable() && item2.getItemDamage() > item2.getMaxDamage()){
this.worldObj.playSoundAtEntity(CoreProxy.proxy.getBuildCraftPlayer(worldObj, xCoord, yCoord, zCoord), "random.break", 0.8F,
0.8F + this.worldObj.rand.nextFloat() * 0.4F);
item2 = null;
}
this.crafting.setInventorySlotContents(slot, item2);
}
}
}
}
private boolean canCraft() {
if (this.craftResult.getStackInSlot(0) == null){
return false;
}
for (int slot = 0; slot < this.getSizeInventory(); slot++){
ItemStack stack = this.getStackInSlot(slot);
if (stack != null && stack.stackSize <= 1){
if (!stack.getItem().hasContainerItem()){
ItemStack item = stack.getItem().getContainerItemStack(stack);
if (item == null || item.stackSize <= 0){
return false;
}
}
}
}
return true;
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
Utils.readStacksFromNBT(nbt, "stackList", this);
this.onInventoryChanged();
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
Utils.writeStacksToNBT(nbt, "stackList", this);
}
public void openChest() {}
public void closeChest() {}
@Override
public int addItem(ItemStack stack, boolean doAdd, ForgeDirection from) {
return new TransactorRoundRobin(this).add(stack, from, doAdd).stackSize;
}
@Override
public ItemStack[] extractItem(boolean doRemove, ForgeDirection from, int maxItemCount) {
ItemStack item = null;
ItemStack result = this.craftResult.getStackInSlot(0);
if (result != null){
if (!this.canCraft()){
this.refilFromNeibour();
if (!this.canCraft()){
return new ItemStack[0];
}
}
this.craft();
item = result.copy();
}else{
return new ItemStack[0];
}
return new ItemStack[] { item };
}
@Override
public boolean isInvNameLocalized(){
return false;
}
@Override
public boolean isStackValidForSlot(int slot, ItemStack stack){
return false;
}
}