-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileMiningWell.java
168 lines (139 loc) · 4.4 KB
/
TileMiningWell.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* 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 io.netty.buffer.ByteBuf;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftFactory;
import buildcraft.api.blueprints.BuilderAPI;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.tiles.IControllable;
import buildcraft.api.tiles.IHasWork;
import buildcraft.api.transport.IPipeConnection;
import buildcraft.api.transport.IPipeTile;
import buildcraft.core.internal.ILEDProvider;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.utils.BlockMiner;
import buildcraft.core.lib.utils.BlockUtils;
public class TileMiningWell extends TileBuildCraft implements IHasWork, IPipeConnection, IControllable, ILEDProvider {
private boolean isDigging = true;
private BlockMiner miner;
private int ledState;
private int ticksSinceAction = 9001;
private SafeTimeTracker updateTracker = new SafeTimeTracker(BuildCraftCore.updateFactor);
public TileMiningWell() {
super();
this.setBattery(new RFBattery(2 * 64 * BuilderAPI.BREAK_ENERGY, BuilderAPI.BREAK_ENERGY * 4 + BuilderAPI.BUILD_ENERGY, 0));
}
/**
* Dig the next available piece of land if not done. As soon as it reaches
* bedrock, lava or goes below 0, it's considered done.
*/
@Override
public void updateEntity () {
super.updateEntity();
if (worldObj.isRemote) {
return;
}
if (updateTracker.markTimeIfDelay(worldObj)) {
sendNetworkUpdate();
}
ticksSinceAction++;
if (mode == Mode.Off) {
if (miner != null) {
miner.invalidate();
miner = null;
}
isDigging = false;
return;
}
if (getBattery().getEnergyStored() == 0) {
return;
}
if (miner == null) {
World world = worldObj;
int depth = yCoord - 1;
while (world.getBlock(xCoord, depth, zCoord) == BuildCraftFactory.plainPipeBlock) {
depth = depth - 1;
}
if (depth < 1 || depth < yCoord - BuildCraftFactory.miningDepth || !BlockUtils.canChangeBlock(world, xCoord, depth, zCoord)) {
isDigging = false;
// Drain energy, because at 0 energy this will stop doing calculations.
getBattery().useEnergy(0, 10, false);
return;
}
if (world.isAirBlock(xCoord, depth, zCoord) || world.getBlock(xCoord, depth, zCoord).isReplaceable(world, xCoord, depth, zCoord)) {
ticksSinceAction = 0;
world.setBlock(xCoord, depth, zCoord, BuildCraftFactory.plainPipeBlock);
} else {
miner = new BlockMiner(world, this, xCoord, depth, zCoord);
}
}
if (miner != null) {
isDigging = true;
ticksSinceAction = 0;
int usedEnergy = miner.acceptEnergy(getBattery().getEnergyStored());
getBattery().useEnergy(usedEnergy, usedEnergy, false);
if (miner.hasMined()) {
if (miner.hasFailed()) {
isDigging = false;
}
miner = null;
}
}
}
@Override
public void invalidate() {
super.invalidate();
if (miner != null) {
miner.invalidate();
}
if (worldObj != null && yCoord > 2) {
BuildCraftFactory.miningWellBlock.removePipes(worldObj, xCoord, yCoord, zCoord);
}
}
@Override
public void writeData(ByteBuf stream) {
super.writeData(stream);
ledState = (ticksSinceAction < 2 ? 16 : 0) | (getBattery().getEnergyStored() * 15 / getBattery().getMaxEnergyStored());
stream.writeByte(ledState);
}
@Override
public void readData(ByteBuf stream) {
super.readData(stream);
int newLedState = stream.readUnsignedByte();
if (newLedState != ledState) {
ledState = newLedState;
worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
}
}
@Override
public boolean hasWork() {
return isDigging;
}
@Override
public ConnectOverride overridePipeConnection(IPipeTile.PipeType type,
ForgeDirection with) {
return type == IPipeTile.PipeType.ITEM ? ConnectOverride.CONNECT : ConnectOverride.DEFAULT;
}
@Override
public boolean acceptsControlMode(Mode mode) {
return mode == Mode.Off || mode == Mode.On;
}
@Override
public int getLEDLevel(int led) {
if (led == 0) { // Red LED
return ledState & 15;
} else { // Green LED
return (ledState >> 4) > 0 ? 15 : 0;
}
}
}