-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlockMiner.java
130 lines (102 loc) · 3.77 KB
/
BlockMiner.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
package buildcraft.factory;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.world.BlockEvent;
import buildcraft.BuildCraftCore;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.BlockUtils;
import buildcraft.core.utils.MathUtils;
import buildcraft.core.utils.Utils;
public class BlockMiner {
protected final World world;
protected final TileEntity owner;
protected final int x, y, z, minerId;
private boolean hasMined, hasFailed;
private int energyRequired, energyAccepted;
public BlockMiner(World world, TileEntity owner, int x, int y, int z) {
this.world = world;
this.owner = owner;
this.x = x;
this.y = y;
this.z = z;
this.minerId = world.rand.nextInt();
}
public boolean hasMined() {
return hasMined;
}
public boolean hasFailed() {
return hasFailed;
}
public void mineStack(ItemStack stack) {
// First, try to add to a nearby chest
stack.stackSize -= Utils.addToRandomInventoryAround(owner.getWorldObj(), owner.xCoord, owner.yCoord, owner.zCoord, stack);
// Second, try to add to adjacent pipes
if (stack.stackSize > 0) {
stack.stackSize -= Utils.addToRandomInjectableAround(owner.getWorldObj(), owner.xCoord, owner.yCoord, owner.zCoord, ForgeDirection.UNKNOWN, stack);
}
// Lastly, throw the object away
if (stack.stackSize > 0) {
float f = world.rand.nextFloat() * 0.8F + 0.1F;
float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
float f2 = world.rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityitem = new EntityItem(owner.getWorldObj(), owner.xCoord + f, owner.yCoord + f1 + 0.5F, owner.zCoord + f2, stack);
entityitem.lifespan = BuildCraftCore.itemLifespan;
entityitem.delayBeforeCanPickup = 10;
float f3 = 0.05F;
entityitem.motionX = (float) world.rand.nextGaussian() * f3;
entityitem.motionY = (float) world.rand.nextGaussian() * f3 + 1.0F;
entityitem.motionZ = (float) world.rand.nextGaussian() * f3;
owner.getWorldObj().spawnEntityInWorld(entityitem);
}
}
public void invalidate() {
world.destroyBlockInWorldPartially(minerId, x, y, z, -1);
}
public int acceptEnergy(int offeredAmount) {
energyRequired = BlockUtils.computeBlockBreakEnergy(world, x, y, z);
int usedAmount = MathUtils.clamp(offeredAmount, 0, Math.max(0, energyRequired - energyAccepted));
energyAccepted += usedAmount;
if (energyAccepted >= energyRequired) {
world.destroyBlockInWorldPartially(minerId, x, y, z, -1);
hasMined = true;
Block block = world.getBlock(x, y, z);
int meta = world.getBlockMetadata(x, y, z);
BlockEvent.BreakEvent breakEvent = new BlockEvent.BreakEvent(x, y, z, world, block, meta,
CoreProxy.proxy.getBuildCraftPlayer((WorldServer) world).get());
MinecraftForge.EVENT_BUS.post(breakEvent);
if (!breakEvent.isCanceled()) {
List<ItemStack> stacks = BlockUtils.getItemStackFromBlock((WorldServer) world, x, y, z);
if (stacks != null) {
for (ItemStack s : stacks) {
if (s != null) {
mineStack(s);
}
}
}
world.playAuxSFXAtEntity(
null,
2001,
x, y, z,
Block.getIdFromBlock(block)
+ (meta << 12));
Utils.preDestroyBlock(world, x, y, z);
if (block.hasTileEntity(meta)) {
world.removeTileEntity(x, y, z);
}
world.setBlockToAir(x, y, z);
} else {
hasFailed = true;
}
} else {
world.destroyBlockInWorldPartially(minerId, x, y, z, MathUtils.clamp((int) Math.floor(energyAccepted * 10 / energyRequired), 0, 9));
}
return usedAmount;
}
}