-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeItemsWood.java
243 lines (193 loc) · 7.04 KB
/
PipeItemsWood.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
/**
* BuildCraft is open-source. It is distributed under the terms of the
* BuildCraft Open Source License. It grants rights to read, modify, compile
* or run the code. It does *NOT* grant the right to redistribute this software
* or its modifications in any form, binary or source, except if expressively
* granted by the copyright holder.
*/
package buildcraft.transport.pipes;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.ISidedInventory;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.Position;
import buildcraft.api.inventory.ISpecialInventory;
import buildcraft.api.power.IPowerProvider;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerFramework;
import buildcraft.api.transport.IPipedItem;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.DefaultProps;
import buildcraft.core.EntityPassiveItem;
import buildcraft.core.RedstonePowerFramework;
import buildcraft.core.utils.Utils;
import buildcraft.transport.IconTerrainConstants;
import buildcraft.transport.Pipe;
import buildcraft.transport.PipeTransportItems;
public class PipeItemsWood extends Pipe implements IPowerReceptor {
private IPowerProvider powerProvider;
protected int standardIconIndex = IconTerrainConstants.PipeItemsWood_Standard;
protected int solidIconIndex = IconTerrainConstants.PipeAllWood_Solid;
protected PipeItemsWood(PipeTransportItems transport, PipeLogic logic, int itemID) {
super(transport, logic, itemID);
powerProvider = PowerFramework.currentFramework.createPowerProvider();
powerProvider.configure(50, 1, 64, 1, 64);
powerProvider.configurePowerPerdition(64, 1);
}
protected PipeItemsWood(int itemID, PipeTransportItems transport) {
this(transport, new PipeLogicWood(), itemID);
}
public PipeItemsWood(int itemID) {
this(itemID, new PipeTransportItems());
}
@Override
@SideOnly(Side.CLIENT)
public Icon[] getTextureIcons() {
return BuildCraftTransport.instance.terrainIcons;
}
@Override
public int getIconIndex(ForgeDirection direction) {
if (direction == ForgeDirection.UNKNOWN)
return standardIconIndex;
else {
int metadata = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
if (metadata == direction.ordinal())
return solidIconIndex;
else
return standardIconIndex;
}
}
@Override
public void setPowerProvider(IPowerProvider provider) {
powerProvider = provider;
}
@Override
public IPowerProvider getPowerProvider() {
return powerProvider;
}
@Override
public void doWork() {
if (powerProvider.getEnergyStored() <= 0)
return;
World w = worldObj;
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
if (meta > 5)
return;
Position pos = new Position(xCoord, yCoord, zCoord, ForgeDirection.getOrientation(meta));
pos.moveForwards(1);
TileEntity tile = w.getBlockTileEntity((int) pos.x, (int) pos.y, (int) pos.z);
if (tile instanceof IInventory) {
if (!PipeManager.canExtractItems(this, w, (int) pos.x, (int) pos.y, (int) pos.z))
return;
IInventory inventory = (IInventory) tile;
ItemStack[] extracted = checkExtract(inventory, true, pos.orientation.getOpposite());
if (extracted == null)
return;
for (ItemStack stack : extracted) {
if (stack == null || stack.stackSize == 0) {
powerProvider.useEnergy(1, 1, false);
continue;
}
Position entityPos = new Position(pos.x + 0.5, pos.y + 0.5, pos.z + 0.5, pos.orientation.getOpposite());
entityPos.moveForwards(0.6);
IPipedItem entity = new EntityPassiveItem(w, entityPos.x, entityPos.y, entityPos.z, stack);
((PipeTransportItems) transport).entityEntering(entity, entityPos.orientation);
}
}
}
/**
* Return the itemstack that can be if something can be extracted from this inventory, null if none. On certain cases, the extractable slot depends on the
* position of the pipe.
*/
public ItemStack[] checkExtract(IInventory inventory, boolean doRemove, ForgeDirection from) {
// / ISPECIALINVENTORY
if (inventory instanceof ISpecialInventory) {
ItemStack[] stacks = ((ISpecialInventory) inventory).extractItem(doRemove, from, (int) powerProvider.getEnergyStored());
if (stacks != null && doRemove) {
for (ItemStack stack : stacks) {
if (stack != null) {
powerProvider.useEnergy(stack.stackSize, stack.stackSize, true);
}
}
}
return stacks;
}
if (inventory instanceof ISidedInventory) {
ISidedInventory sidedInv = (ISidedInventory) inventory;
int first = sidedInv.getStartInventorySide(from);
int last = first + sidedInv.getSizeInventorySide(from) - 1;
IInventory inv = Utils.getInventory(inventory);
ItemStack result = checkExtractGeneric(inv, doRemove, from, first, last);
if (result != null)
return new ItemStack[] { result };
} else if (inventory.getSizeInventory() == 2) {
// This is an input-output inventory
int slotIndex = 0;
if (from == ForgeDirection.DOWN || from == ForgeDirection.UP) {
slotIndex = 0;
} else {
slotIndex = 1;
}
ItemStack slot = inventory.getStackInSlot(slotIndex);
if (slot != null && slot.stackSize > 0) {
if (doRemove)
return new ItemStack[] { inventory.decrStackSize(slotIndex, (int) powerProvider.useEnergy(1, slot.stackSize, true)) };
else
return new ItemStack[] { slot };
}
} else if (inventory.getSizeInventory() == 3) {
// This is a furnace-like inventory
int slotIndex = 0;
if (from == ForgeDirection.UP) {
slotIndex = 0;
} else if (from == ForgeDirection.DOWN) {
slotIndex = 1;
} else {
slotIndex = 2;
}
ItemStack slot = inventory.getStackInSlot(slotIndex);
if (slot != null && slot.stackSize > 0) {
if (doRemove)
return new ItemStack[] { inventory.decrStackSize(slotIndex, (int) powerProvider.useEnergy(1, slot.stackSize, true)) };
else
return new ItemStack[] { slot };
}
} else {
// This is a generic inventory
IInventory inv = Utils.getInventory(inventory);
ItemStack result = checkExtractGeneric(inv, doRemove, from, 0, inv.getSizeInventory() - 1);
if (result != null)
return new ItemStack[] { result };
}
return null;
}
public ItemStack checkExtractGeneric(IInventory inventory, boolean doRemove, ForgeDirection from, int start, int stop) {
for (int k = start; k <= stop; ++k) {
ItemStack slot = inventory.getStackInSlot(k);
if (slot != null && slot.stackSize > 0) {
if (doRemove) {
return inventory.decrStackSize(k, (int) powerProvider.useEnergy(1, slot.stackSize, true));
} else {
return slot;
}
}
}
return null;
}
@Override
public int powerRequest() {
return getPowerProvider().getMaxEnergyReceived();
}
@Override
public boolean canConnectRedstone() {
if (PowerFramework.currentFramework instanceof RedstonePowerFramework)
return true;
return super.canConnectRedstone();
}
}