-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeItemsWood.java
173 lines (139 loc) · 5.33 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
/**
* 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 net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider;
import buildcraft.api.core.Position;
import buildcraft.api.inventory.ISpecialInventory;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.api.power.PowerProvider;
import buildcraft.api.transport.IPipedItem;
import buildcraft.api.transport.PipeManager;
import buildcraft.core.EntityPassiveItem;
import buildcraft.core.inventory.InventoryWrapper;
import buildcraft.core.utils.Utils;
import buildcraft.transport.Pipe;
import buildcraft.transport.PipeIconProvider;
import buildcraft.transport.PipeTransportItems;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class PipeItemsWood extends Pipe implements IPowerReceptor {
protected PowerProvider powerProvider;
protected int standardIconIndex = PipeIconProvider.PipeItemsWood_Standard;
protected int solidIconIndex = PipeIconProvider.PipeAllWood_Solid;
protected PipeItemsWood(PipeTransportItems transport, PipeLogic logic, int itemID) {
super(transport, logic, itemID);
powerProvider = new PowerProvider(this, false);
powerProvider.configure(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 IIconProvider getIconProvider() {
return BuildCraftTransport.instance.pipeIconProvider;
}
@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 PowerProvider getPowerProvider(ForgeDirection side) {
return powerProvider;
}
@Override
public void doWork(PowerProvider workProvider) {
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;
} else {
IInventory inv = Utils.getInventory(inventory);
ItemStack result = checkExtractGeneric(inv, doRemove, from);
if (result != null)
return new ItemStack[]{result};
}
return null;
}
public ItemStack checkExtractGeneric(IInventory inventory, boolean doRemove, ForgeDirection from) {
return checkExtractGeneric(InventoryWrapper.getWrappedInventory(inventory), doRemove, from);
}
public ItemStack checkExtractGeneric(ISidedInventory inventory, boolean doRemove, ForgeDirection from) {
for (int k : inventory.getAccessibleSlotsFromSide(from.ordinal())) {
ItemStack slot = inventory.getStackInSlot(k);
if (slot != null && slot.stackSize > 0 && inventory.canExtractItem(k, slot, from.ordinal())) {
if (doRemove) {
return inventory.decrStackSize(k, (int) powerProvider.useEnergy(1, slot.stackSize, true));
} else {
return slot;
}
}
}
return null;
}
}