-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileBuildCraft.java
254 lines (211 loc) · 5.93 KB
/
TileBuildCraft.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
/**
* 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.core.lib.block;
import java.util.HashSet;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import cofh.api.energy.IEnergyHandler;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.ISerializable;
import buildcraft.api.tiles.IControllable;
import buildcraft.core.DefaultProps;
import buildcraft.core.lib.RFBattery;
import buildcraft.core.lib.TileBuffer;
import buildcraft.core.lib.network.Packet;
import buildcraft.core.lib.network.PacketTileUpdate;
import buildcraft.core.lib.utils.Utils;
/**
* For future maintainers: This class intentionally does not implement
* just every interface out there. For some of them (such as IControllable),
* we expect the tiles supporting it to implement it - but TileBuildCraft
* provides all the underlying functionality to stop code repetition.
*/
public abstract class TileBuildCraft extends TileEntity implements IEnergyHandler, ISerializable {
protected TileBuffer[] cache;
protected HashSet<EntityPlayer> guiWatchers = new HashSet<EntityPlayer>();
protected IControllable.Mode mode;
private boolean init = false;
private String owner = "[BuildCraft]";
private RFBattery battery;
private int receivedTick, extractedTick;
public String getOwner() {
return owner;
}
public void addGuiWatcher(EntityPlayer player) {
if (!guiWatchers.contains(player)) {
guiWatchers.add(player);
}
}
public void removeGuiWatcher(EntityPlayer player) {
if (guiWatchers.contains(player)) {
guiWatchers.remove(player);
}
}
@Override
public void updateEntity() {
if (!init && !isInvalid()) {
initialize();
init = true;
}
if (battery != null) {
receivedTick = 0;
extractedTick = 0;
}
}
public void initialize() {
}
@Override
public void validate() {
super.validate();
cache = null;
}
@Override
public void invalidate() {
init = false;
super.invalidate();
cache = null;
}
public void onBlockPlacedBy(EntityLivingBase entity, ItemStack stack) {
if (entity instanceof EntityPlayer) {
owner = ((EntityPlayer) entity).getDisplayName();
}
}
public void destroy() {
cache = null;
}
public void sendNetworkUpdate() {
if (worldObj != null && !worldObj.isRemote) {
BuildCraftCore.instance.sendToPlayers(getPacketUpdate(), worldObj,
xCoord, yCoord, zCoord, DefaultProps.NETWORK_UPDATE_RANGE);
}
}
public void writeData(ByteBuf stream) {
}
public void readData(ByteBuf stream) {
}
public Packet getPacketUpdate() {
return new PacketTileUpdate(this);
}
@Override
public net.minecraft.network.Packet getDescriptionPacket() {
return Utils.toPacket(getPacketUpdate(), 0);
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setString("owner", owner);
if (battery != null) {
NBTTagCompound batteryNBT = new NBTTagCompound();
battery.writeToNBT(batteryNBT);
nbt.setTag("battery", batteryNBT);
}
if (mode != null) {
nbt.setByte("lastMode", (byte) mode.ordinal());
}
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
if (nbt.hasKey("owner")) {
owner = nbt.getString("owner");
}
if (battery != null) {
battery.readFromNBT(nbt.getCompoundTag("battery"));
}
if (nbt.hasKey("lastMode")) {
mode = IControllable.Mode.values()[nbt.getByte("lastMode")];
}
}
@Override
public int hashCode() {
return (xCoord * 37 + yCoord) * 37 + zCoord;
}
@Override
public boolean equals(Object cmp) {
return this == cmp;
}
@Override
public boolean canConnectEnergy(ForgeDirection from) {
return battery != null;
}
@Override
public int receiveEnergy(ForgeDirection from, int maxReceive,
boolean simulate) {
if (battery != null && this.canConnectEnergy(from)) {
int received = battery.receiveEnergy(maxReceive - receivedTick, simulate);
if (!simulate) {
receivedTick += received;
}
return received;
} else {
return 0;
}
}
/**
* If you want to use this, implement IEnergyProvider.
*/
public int extractEnergy(ForgeDirection from, int maxExtract,
boolean simulate) {
if (battery != null && this.canConnectEnergy(from)) {
int extracted = battery.extractEnergy(maxExtract - extractedTick, simulate);
if (!simulate) {
extractedTick += extracted;
}
return extracted;
} else {
return 0;
}
}
@Override
public int getEnergyStored(ForgeDirection from) {
if (battery != null && this.canConnectEnergy(from)) {
return battery.getEnergyStored();
} else {
return 0;
}
}
@Override
public int getMaxEnergyStored(ForgeDirection from) {
if (battery != null && this.canConnectEnergy(from)) {
return battery.getMaxEnergyStored();
} else {
return 0;
}
}
public RFBattery getBattery() {
return battery;
}
protected void setBattery(RFBattery battery) {
this.battery = battery;
}
public Block getBlock(ForgeDirection side) {
if (cache == null) {
cache = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false);
}
return cache[side.ordinal()].getBlock();
}
public TileEntity getTile(ForgeDirection side) {
if (cache == null) {
cache = TileBuffer.makeBuffer(worldObj, xCoord, yCoord, zCoord, false);
}
return cache[side.ordinal()].getTile();
}
public IControllable.Mode getControlMode() {
return mode;
}
public void setControlMode(IControllable.Mode mode) {
this.mode = mode;
}
}