-
Notifications
You must be signed in to change notification settings - Fork 497
/
PipeItemsObsidian.java
293 lines (240 loc) · 9.79 KB
/
PipeItemsObsidian.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/** Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team http://www.mod-buildcraft.com
* <p/>
* 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.transport.pipes;
import java.util.List;
import java.util.WeakHashMap;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Vec3;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import cofh.api.energy.IEnergyReceiver;
import buildcraft.BuildCraftTransport;
import buildcraft.api.core.IIconProvider;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.inventory.ITransactor;
import buildcraft.core.lib.inventory.Transactor;
import buildcraft.core.lib.inventory.filters.StackFilter;
import buildcraft.core.lib.utils.Utils;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.transport.*;
import buildcraft.transport.pipes.events.PipeEventItem;
import buildcraft.transport.utils.TransportUtils;
public class PipeItemsObsidian extends Pipe<PipeTransportItems> implements IEnergyReceiver {
private final RFBattery battery = new RFBattery(2560, 640, 0);
private final WeakHashMap<Entity, Long> entityDropTime = new WeakHashMap<>();
public PipeItemsObsidian(Item item) {
super(new PipeTransportItems(), item);
}
@Override
@SideOnly(Side.CLIENT)
public IIconProvider getIconProvider() {
return BuildCraftTransport.instance.pipeIconProvider;
}
@Override
public int getIconIndex(EnumFacing direction) {
return PipeIconProvider.TYPE.PipeItemsObsidian.ordinal();
}
@Override
public void onEntityCollidedWithBlock(Entity entity) {
super.onEntityCollidedWithBlock(entity);
if (entity.isDead) {
return;
}
if (canSuck(entity, 0)) {
pullItemIntoPipe(entity, 0);
}
}
private AxisAlignedBB getSuckingBox(EnumFacing orientation, int distance) {
if (orientation == null) {
return null;
}
Vec3 p1 = Utils.convert(container.getPos()).add(Utils.VEC_HALF);
Vec3 p2 = p1;
switch (orientation) {
case EAST:
p1 = p1.addVector(distance, 0, 0);
p2 = p2.addVector(distance + 1, 0, 0);
break;
case WEST:
p1 = p1.addVector(-distance - 1, 0, 0);
p2 = p2.addVector(-distance, 0, 0);
break;
case UP:
case DOWN:
p1 = p1.addVector(distance + 1, 0, distance + 1);
p2 = p2.addVector(-distance, 0, -distance);
break;
case SOUTH:
p1 = p1.addVector(0, 0, distance);
p2 = p2.addVector(0, 0, distance + 1);
break;
case NORTH:
default:
p1 = p1.addVector(0, 0, -distance - 1);
p2 = p2.addVector(0, 0, -distance);
break;
}
switch (orientation) {
case EAST:
case WEST:
p1 = p1.addVector(0, distance + 1, distance + 1);
p2 = p2.addVector(0, -distance, -distance);
break;
case UP:
p1 = p1.addVector(0, distance + 1, 0);
p2 = p2.addVector(0, distance, 0);
break;
case DOWN:
p1 = p1.addVector(0, -distance - 1, 0);
p2 = p2.addVector(0, -distance, 0);
break;
case SOUTH:
case NORTH:
default:
p1 = p1.addVector(distance + 1, distance + 1, 0);
p2 = p2.addVector(-distance, -distance, 0);
break;
}
Vec3 min = Utils.min(p1, p2);
Vec3 max = Utils.max(p1, p2);
return new AxisAlignedBB(min.xCoord, min.yCoord, min.zCoord, max.xCoord, max.yCoord, max.zCoord);
}
@Override
public void updateEntity() {
super.updateEntity();
if (battery.getEnergyStored() > 0) {
for (int j = 1; j < 5; ++j) {
if (suckItem(j)) {
return;
}
}
battery.useEnergy(0, 5, false);
}
}
private boolean suckItem(int distance) {
AxisAlignedBB box = getSuckingBox(getOpenOrientation(), distance);
if (box == null) {
return false;
}
List<Entity> discoveredEntities = container.getWorld().getEntitiesWithinAABB(Entity.class, box);
for (Entity entity : discoveredEntities) {
if (canSuck(entity, distance)) {
pullItemIntoPipe(entity, distance);
return true;
}
if (distance == 1 && entity instanceof EntityMinecart && entity instanceof IInventory) {
EntityMinecart cart = (EntityMinecart) entity;
if (!cart.isDead) {
ITransactor trans = Transactor.getTransactorFor(cart);
EnumFacing openOrientation = getOpenOrientation();
ItemStack stack = trans.remove(StackFilter.ALL, openOrientation, false);
if (stack != null && battery.useEnergy(10, 10, false) > 0) {
stack = trans.remove(StackFilter.ALL, openOrientation, true);
if (stack != null) {
Vec3 pos = Utils.convertMiddle(container.getPos());
TravelingItem item = TravelingItem.make(pos, stack);
transport.injectItem(item, openOrientation.getOpposite());
}
return true;
}
}
}
}
return false;
}
public void pullItemIntoPipe(Entity entity, int distance) {
if (container.getWorld().isRemote) {
return;
}
EnumFacing orientation = getOpenOrientation().getOpposite();
if (orientation != null) {
container.getWorld().playSoundAtEntity(entity, "random.pop", 0.2F, ((container.getWorld().rand.nextFloat() - container.getWorld().rand
.nextFloat()) * 0.7F + 1.0F) * 2.0F);
ItemStack stack;
double speed = 0.01F;
if (entity instanceof EntityItem) {
EntityItem item = (EntityItem) entity;
ItemStack contained = item.getEntityItem();
if (contained == null) {
return;
}
TransportProxy.proxy.obsidianPipePickup(container.getWorld(), item, this.container);
int energyUsed = Math.min(10 * contained.stackSize * distance, battery.getEnergyStored());
if (distance == 0 || energyUsed / distance / 10 == contained.stackSize) {
stack = contained;
CoreProxy.proxy.removeEntity(entity);
} else {
stack = contained.splitStack(energyUsed / distance / 10);
}
battery.useEnergy(energyUsed, energyUsed, false);
speed = Math.sqrt(item.motionX * item.motionX + item.motionY * item.motionY + item.motionZ * item.motionZ);
speed = speed / 2F - 0.05;
if (speed < 0.01) {
speed = 0.01;
}
} else if (entity instanceof EntityArrow && battery.useEnergy(distance * 10, distance * 10, false) > 0) {
stack = new ItemStack(Items.arrow, 1);
CoreProxy.proxy.removeEntity(entity);
} else {
return;
}
if (stack == null) {
return;
}
TravelingItem item = TravelingItem.make(Utils.convert(container.getPos()).addVector(0.5, TransportUtils.getPipeFloorOf(stack), 0.5),
stack);
item.setSpeed((float) speed);
transport.injectItem(item, orientation);
}
}
public void eventHandler(PipeEventItem.DropItem event) {
entityDropTime.put(event.entity, event.entity.worldObj.getTotalWorldTime() + 200);
}
public boolean canSuck(Entity entity, int distance) {
if (!entity.isEntityAlive()) {
return false;
}
if (entity instanceof EntityItem) {
EntityItem item = (EntityItem) entity;
if (item.getEntityItem().stackSize <= 0) {
return false;
}
long wt = entity.worldObj.getTotalWorldTime();
if (entityDropTime.containsKey(entity) && entityDropTime.get(entity) >= wt) {
return false;
}
return battery.getEnergyStored() >= distance * 10;
} else if (entity instanceof EntityArrow) {
EntityArrow arrow = (EntityArrow) entity;
return arrow.canBePickedUp == 1 && battery.getEnergyStored() >= distance * 10;
}
return false;
}
@Override
public boolean canConnectEnergy(EnumFacing from) {
return true;
}
@Override
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate) {
return battery.receiveEnergy(maxReceive, simulate);
}
@Override
public int getEnergyStored(EnumFacing from) {
return battery.getEnergyStored();
}
@Override
public int getMaxEnergyStored(EnumFacing from) {
return battery.getMaxEnergyStored();
}
}