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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
package ihl.enviroment;
import java.util.List;
import java.util.Vector;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import ic2.api.energy.event.EnergyTileLoadEvent;
import ic2.api.energy.event.EnergyTileUnloadEvent;
import ic2.api.energy.tile.IEnergySink;
import ic2.api.network.INetworkDataProvider;
import ic2.api.tile.IWrenchable;
import ic2.core.IC2;
import ic2.core.ITickCallback;
import ihl.utils.IHLUtils;
public class LightBulbTileEntity extends TileEntity implements IEnergySink, IWrenchable, INetworkDataProvider
{
private boolean active = false;
private short facing = 0;
public boolean prevActive = false;
public short prevFacing = 0;
private double maxEnergy=1.1d;
private double energy;
public boolean addedToEnergyNet = false;
private boolean loaded = false;
private int ticker;
@Override
public void readFromNBT(NBTTagCompound nbttagcompound)
{
super.readFromNBT(nbttagcompound);
this.energy = nbttagcompound.getDouble("energy");
this.facing = nbttagcompound.getShort("facing");
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound)
{
super.writeToNBT(nbttagcompound);
nbttagcompound.setDouble("energy", this.energy);
nbttagcompound.setShort("facing", this.facing);
}
/**
* validates a tile entity
*/
@Override
public void validate()
{
super.validate();
IC2.tickHandler.addSingleTickCallback(this.worldObj, new ITickCallback()
{
@SuppressWarnings("unchecked")
@Override
public void tickCallback(World world)
{
if (!LightBulbTileEntity.this.isInvalid() && world.blockExists(LightBulbTileEntity.this.xCoord, LightBulbTileEntity.this.yCoord, LightBulbTileEntity.this.zCoord))
{
LightBulbTileEntity.this.onLoaded();
if (LightBulbTileEntity.this.enableUpdateEntity())
{
world.loadedTileEntityList.add(LightBulbTileEntity.this);
}
}
}
});
}
/**
* invalidates a tile entity
*/
@Override
public void invalidate()
{
super.invalidate();
if (this.loaded)
{
this.onUnloaded();
}
}
@Override
public void onChunkUnload()
{
super.onChunkUnload();
if (this.loaded)
{
this.onUnloaded();
}
}
public void onLoaded()
{
if (IC2.platform.isSimulating() && !this.addedToEnergyNet)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
this.addedToEnergyNet = true;
}
this.loaded = true;
}
public void onUnloaded()
{
if (IC2.platform.isSimulating())
{
if(this.addedToEnergyNet)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
this.addedToEnergyNet = false;
}
this.active=false;
this.updateLightState();
}
}
@Override
public final boolean canUpdate()
{
return false;
}
@Override
public void updateEntity()
{
if(++this.ticker % 4 == 0)
{
if(this.prevFacing != facing)
{
this.setFacing(facing);
}
if(this.energy>0)
{
this.energy--;
this.setActive(this.energy>0);
}
else
{
this.setActive(false);
}
}
}
protected void updateLightState()
{
int x,y,z;
int xyz[] = {0,0,1,0,0,-1,0,0};
Block block;
for(int i=0;i<=5;i++)
{
x=xCoord+xyz[i];
y=yCoord+xyz[i+1];
z=zCoord+xyz[i+2];
block = this.worldObj.getBlock(x,y,z);
if(block.isAir(this.worldObj, x,y,z))
{
if(this.getActive())
{
worldObj.setBlock(x, y, z, LightBulbBlock.glowningAir);
}
if(!this.getActive())
{
worldObj.setBlockToAir(x, y, z);
}
}
}
}
public boolean enableUpdateEntity()
{
return IC2.platform.isSimulating();
}
@Override
public boolean acceptsEnergyFrom(TileEntity emitter,
ForgeDirection direction) {
switch(direction)
{
case UP:
return this.getFacing()==0;
case DOWN:
return this.getFacing()==1;
case SOUTH:
return this.getFacing()==2;
case NORTH:
return this.getFacing()==3;
case EAST:
return this.getFacing()==4;
case WEST:
return this.getFacing()==5;
default:
return false;
}
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side)
{
return false;
}
@Override
public short getFacing() {
return this.facing;
}
@Override
public void setFacing(short facing1)
{
if (IC2.platform.isSimulating()&&this.addedToEnergyNet)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this));
this.addedToEnergyNet = false;
}
this.facing=facing1;
if(IC2.platform.isSimulating())
{
if (this.prevFacing != facing)
{
IC2.network.get().updateTileEntityField(this, "facing");
}
}
this.prevFacing = facing;
if (IC2.platform.isSimulating()&&!this.addedToEnergyNet)
{
MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this));
this.addedToEnergyNet = true;
}
}
@Override
public List<String> getNetworkedFields()
{
Vector<String> ret = new Vector<String>(2);
ret.add("active");
ret.add("facing");
return ret;
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
return true;
}
@Override
public float getWrenchDropRate() {
return 1;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return IHLUtils.getThisModItemStack("lightBulb");
}
@Override
public double getDemandedEnergy()
{
return this.maxEnergy-this.energy;
}
@Override
public int getSinkTier() {
return 1;
}
@Override
public double injectEnergy(ForgeDirection directionFrom, double amount, double voltage)
{
if (this.energy >= this.maxEnergy)
{
return amount;
}
else
{
this.energy += amount;
return 0.0D;
}
}
public boolean getActive()
{
return this.active;
}
public void setActive(boolean active1)
{
this.active = active1;
if (this.prevActive != active1)
{
IC2.network.get().updateTileEntityField(this, "active");
updateLightState();
}
this.prevActive = active1;
}
public void setActiveWithoutNotify(boolean active1)
{
this.active = active1;
this.prevActive = active1;
}
}
|