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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
package ihl.flexible_cable;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import ihl.IHLMod;
import ihl.IHLModInfo;
import ihl.interfaces.ICableHolder;
import ihl.interfaces.IDataCableHolder;
import ihl.interfaces.IEnergyNetNode;
import ihl.interfaces.IMultiPowerCableHolder;
import ihl.interfaces.INetworkListener;
import ihl.items_blocks.FlexibleCableItem;
import ihl.utils.IHLUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.Unpooled;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
public class NodeEntity extends Entity implements INetworkListener{
public NodeEntity prevAnchorEntity;
public Entity nextAnchorEntity;
private int anchorX;
private int anchorY;
private int anchorZ;
private short anchorFacing;
private int anchorDimensionId;
public boolean shouldFollowPlayer=false;
protected int chainUniqueID=-2;
public int chainArrangeNumber=-2;
protected int checkTimer=201;
public int colorIndex=16777215;
public int renderEvery=1;
public int type=1;//0 - uninsulated wire; 1 - insulated cable; 2 - data cable
public float dx0=0;
public float dy0=0;
public float dz0=0;
public final int n=48;
public final float[] rotationPitchArray = new float[n+1];
public final float[] rotationYawArray = new float[n+1];
public final float[] translationX = new float[n+1];
public final float[] translationY = new float[n+1];
public final float[] translationZ = new float[n+1];
public double virtualNodePosX;
public double virtualNodePosY;
public double virtualNodePosZ;
public double renderPosX;
public double renderPosY;
public double renderPosZ;
public double lastTickRenderPosX;
public double lastTickRenderPosY;
public double lastTickRenderPosZ;
private boolean alreadyRegistered=false;
private boolean shouldUpdateRender=true;
public NodeEntity(World world)
{
super(world);
if(world.isRemote)
{
IHLMod.proxy.addEntityToList(this);
this.setSize(2f, 0.2f);
}
else
{
IHLMod.proxy.addEntityToServerList(this);
this.setSize(0.5F, 0.1F);
}
this.renderDistanceWeight = 5.0D;
this.yOffset+=0.15F;
this.virtualNodePosX=this.posX;
this.virtualNodePosY=this.posY;
this.virtualNodePosZ=this.posZ;
this.motionX=0D;
this.motionY=0D;
this.motionZ=0D;
}
@Override
public void setInPortal(){}
@Override
public void travelToDimension(int dimensionId){}
public void setSize(float width, float heigth)
{
super.setSize(width, heigth);
}
public void setVirtualNodePos(double d,double e, double f)
{
virtualNodePosX=d;
virtualNodePosY=e;
virtualNodePosZ=f;
this.registerAndSendData(null);
}
public void registerAndSendData(EntityPlayerMP player)
{
if(!worldObj.isRemote)
{
if(!alreadyRegistered)
{
Set<NodeEntity> nes;
if(IHLMod.proxy.nodeEntityRegistry.containsKey(this.getChainUniqueID()))
{
nes=IHLMod.proxy.nodeEntityRegistry.get(this.getChainUniqueID());
}
else
{
nes=new HashSet();
IHLMod.proxy.nodeEntityRegistry.put(this.getChainUniqueID(),nes);
}
nes.add(this);
alreadyRegistered=true;
}
ByteBuf bb = Unpooled.buffer(30);
ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb);
try
{
byteBufOutputStream.write(1);
byteBufOutputStream.writeInt(this.getEntityId());
byteBufOutputStream.writeInt(this.getChainUniqueID());
byteBufOutputStream.writeInt(this.chainArrangeNumber);
byteBufOutputStream.writeByte(this.type);
byteBufOutputStream.writeInt(this.colorIndex);
byteBufOutputStream.writeDouble(this.virtualNodePosX);
byteBufOutputStream.writeDouble(this.virtualNodePosY);
byteBufOutputStream.writeDouble(this.virtualNodePosZ);
byteBufOutputStream.writeBoolean(this.shouldFollowPlayer);
if(player==null)
{
IHLMod.proxy.sendFromServerToAll(new FMLProxyPacket(byteBufOutputStream.buffer(), IHLModInfo.MODID));
}
else
{
IHLMod.proxy.sendFromServerToPlayer(new FMLProxyPacket(byteBufOutputStream.buffer(), IHLModInfo.MODID),player);
}
byteBufOutputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Override
public void onUpdate()
{
super.onUpdate();
if(this.checkTimer==201)
{
if(worldObj.isRemote)
{
IHLMod.proxy.recieveDelayedDataPacket(this);
}
else
{
this.registerAndSendData(null);
}
}
if(prevAnchorEntity==null||(nextAnchorEntity==null || nextAnchorEntity instanceof EntityPlayer || nextAnchorEntity instanceof EntityItem))
{
double range = 16D;
AxisAlignedBB searchArea = AxisAlignedBB.getBoundingBox(this.posX-range,this.posY-range,this.posZ-range,this.posX+range,this.posY+range,this.posZ+range);
List<NodeEntity> eItemsList = this.worldObj.getEntitiesWithinAABB(NodeEntity.class, searchArea);
if(!eItemsList.isEmpty())
{
Iterator ei = eItemsList.iterator();
while(ei.hasNext())
{
NodeEntity node=(NodeEntity) ei.next();
if(node.getChainUniqueID()==this.getChainUniqueID())
{
if(node.chainArrangeNumber==this.chainArrangeNumber-1)
{
this.prevAnchorEntity=node;
node.nextAnchorEntity=this;
node.shouldFollowPlayer=false;
}
else if(node.chainArrangeNumber==this.chainArrangeNumber+1)
{
this.nextAnchorEntity=node;
this.shouldFollowPlayer=false;
node.prevAnchorEntity=this;
}
}
if(prevAnchorEntity!=null && nextAnchorEntity!=null)
{
break;
}
}
}
}
if(this.shouldFollowPlayer && (this.nextAnchorEntity == null || this.nextAnchorEntity.isDead))
{
double range = 16D;
AxisAlignedBB searchArea = AxisAlignedBB.getBoundingBox(this.posX-range,this.posY-range,this.posZ-range,this.posX+range,this.posY+range,this.posZ+range);
List<EntityPlayer> eItemsList = this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, searchArea);
if(!eItemsList.isEmpty())
{
Iterator ei = eItemsList.iterator();
while(ei.hasNext())
{
EntityPlayer player=(EntityPlayer) ei.next();
if(this.playerHasItemStack(player))
{
this.nextAnchorEntity=player;
}
}
}
}
if(this.shouldFollowPlayer && this.nextAnchorEntity instanceof EntityPlayer)
{
EntityPlayer player = (EntityPlayer) this.nextAnchorEntity;
if(!this.playerHasItemStack(player))
{
double range = 16D;
AxisAlignedBB searchArea = AxisAlignedBB.getBoundingBox(this.posX-range,this.posY-range,this.posZ-range,this.posX+range,this.posY+range,this.posZ+range);
List<EntityItem> eItemsList = this.worldObj.getEntitiesWithinAABB(EntityItem.class, searchArea);
if(!eItemsList.isEmpty())
{
Iterator<EntityItem> ei = eItemsList.iterator();
while(ei.hasNext())
{
EntityItem eitem = ei.next();
if(this.isItemHasSameChainId(eitem.getEntityItem()))
{
this.nextAnchorEntity=eitem;
break;
}
}
}
}
}
if(nextAnchorEntity!=null && this.getDistanceSqToEntity(nextAnchorEntity)>2D)
{
this.nextAnchorEntity.addVelocity((this.posX-this.nextAnchorEntity.posX)*0.02D, (this.posY-this.nextAnchorEntity.posY)*0.02D, (this.posZ-this.nextAnchorEntity.posZ)*0.02D);
}
if(!worldObj.isRemote)
{
double x0,x2,y0,y2,z0,z2;
x2=x0=this.virtualNodePosX;
y2=y0=this.virtualNodePosY;
z2=z0=this.virtualNodePosZ;
if(nextAnchorEntity!=null)
{
x2=nextAnchorEntity.posX;
y2=nextAnchorEntity.posY;
z2=nextAnchorEntity.posZ;
}
if(prevAnchorEntity!=null)
{
x0=prevAnchorEntity.posX;
y0=prevAnchorEntity.posY;
z0=prevAnchorEntity.posZ;
}
double d1 = (x0-x2)*(x0-x2)+(y0-y2)*(y0-y2)+(z0-z2)*(z0-z2);
if(d1>4D)
{
this.motionX+=(x0+x2)*0.05D-this.posX*0.1D;
this.motionY+=(y0+y2)*0.05D-this.posY*0.1D;
this.motionZ+=(z0+z2)*0.05D-this.posZ*0.1D;
}
else
{
double d2 = this.getDistanceSq(x2,y2,z2);
if(d2>1D)
{
this.motionX+=(x2-this.posX)*0.01D;
this.motionY+=(y2-this.posY)*0.01D;
this.motionZ+=(z2-this.posZ)*0.01D;
}
double d3 = this.getDistanceSq(x0,y0,z0);
if(d3>1D)
{
this.motionX+=(x0-this.posX)*0.01D;
this.motionY+=(y0-this.posY)*0.01D;
this.motionZ+=(z0-this.posZ)*0.01D;
}
}
this.motionY-=0.005D;
this.motionX*=0.8D;
this.motionY*=0.8D;
this.motionZ*=0.8D;
this.moveEntity(this.motionX, this.motionY, this.motionZ);
}
if(this.onGround)
{
this.motionY *= -0.5D;
}
if(worldObj.isRemote)
{
this.lastTickRenderPosX=this.renderPosX;
this.lastTickRenderPosY=this.renderPosY;
this.lastTickRenderPosZ=this.renderPosZ;
this.renderPosX=(float) this.prevPosX;
this.renderPosY=(float) this.prevPosY;
this.renderPosZ=(float) this.prevPosZ;
shouldUpdateRender=
Math.abs(this.lastTickRenderPosX-this.renderPosX)+
Math.abs(this.lastTickRenderPosY-this.renderPosY)+
Math.abs(this.lastTickRenderPosZ-this.renderPosZ)>0.01f;
float xi,yi,zi;
float x0=xi=(float)this.prevPosX;
float y0=yi=(float)this.prevPosY;
float z0=zi=(float)this.prevPosZ;
float dx0=this.dx0;
float dy0=this.dy0;
float dz0=this.dz0;
float dx1=(float) (this.virtualNodePosX-xi);
float dy1=(float) (this.virtualNodePosY-yi);
float dz1=(float) (this.virtualNodePosZ-zi);
if(this.nextAnchorEntity!=null)
{
dx1=(float) (this.nextAnchorEntity.prevPosX-xi);
dy1=(float) (this.nextAnchorEntity.prevPosY-yi);
dz1=(float) (this.nextAnchorEntity.prevPosZ-zi);
}
if(this.prevAnchorEntity==null)
{
this.renderPosX=this.virtualNodePosX;
this.renderPosY=this.virtualNodePosY;
this.renderPosZ=this.virtualNodePosZ;
x0=xi=(float) this.virtualNodePosX;
y0=yi=(float) this.virtualNodePosY;
z0=zi=(float) this.virtualNodePosZ;
}
if(this.nextAnchorEntity instanceof NodeEntity)
{
shouldUpdateRender=this.shouldUpdateRender || ((NodeEntity)this.nextAnchorEntity).shouldUpdateRender;
if(((NodeEntity)this.nextAnchorEntity).nextAnchorEntity!=null)
{
Entity nne = ((NodeEntity)this.nextAnchorEntity).nextAnchorEntity;
dx1=(float) (nne.prevPosX-xi);
dy1=(float) (nne.prevPosY-yi);
dz1=(float) (nne.prevPosZ-zi);
}
else
{
dx1=(float) (((NodeEntity) this.nextAnchorEntity).virtualNodePosX-xi);
dy1=(float) (((NodeEntity) this.nextAnchorEntity).virtualNodePosY-yi);
dz1=(float) (((NodeEntity) this.nextAnchorEntity).virtualNodePosZ-zi);
}
}
if(shouldUpdateRender)
{
float x1=(float) this.virtualNodePosX;
float y1=(float) this.virtualNodePosY;
float z1=(float) this.virtualNodePosZ;
if(this.nextAnchorEntity!=null)
{
if(nextAnchorEntity instanceof NodeEntity)
{
x1=(float) ((NodeEntity) nextAnchorEntity).renderPosX;
y1=(float) ((NodeEntity) nextAnchorEntity).renderPosY;
z1=(float) ((NodeEntity) nextAnchorEntity).renderPosZ;
}
else
{
x1=(float) nextAnchorEntity.posX;
y1=(float) nextAnchorEntity.posY;
z1=(float) nextAnchorEntity.posZ;
}
}
float d = (x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)+(z0-z1)*(z0-z1);
if(d>2f)
{
renderEvery=1;
}/*
else if(d>0.5f)
{
renderEvery=2;
}
else
{
renderEvery=4;
}*/
int i1=0;
for(float i=1f/n;i<=1f+1f/n;i+=((float)renderEvery)/n,i1+=renderEvery)
{
float dxi = xi;
float dyi = yi;
float dzi = zi;
xi=(dx1-2*x1+2*dx0+2*x0-dx0)*i*i*i+(3*x1-dx1-3*dx0-3*x0+dx0)*i*i+dx0*i+x0;
yi=(dy1-2*y1+2*dy0+2*y0-dy0)*i*i*i+(3*y1-dy1-3*dy0-3*y0+dy0)*i*i+dy0*i+y0;
zi=(dz1-2*z1+2*dz0+2*z0-dz0)*i*i*i+(3*z1-dz1-3*dz0-3*z0+dz0)*i*i+dz0*i+z0;
int blockX=(int)xi;
int blockY=(int)yi;
int blockZ=(int)zi;
dxi-=xi;
dyi-=yi;
dzi-=zi;
double var7 = MathHelper.sqrt_double(dxi * dxi + dzi * dzi);
float var9 = (float)(Math.atan2(dzi, dxi) * 180.0D / Math.PI) - 90.0F;
float var10 = (float)(-(Math.atan2(dyi, var7) * 180.0D / Math.PI));
float rotationPitch = (float) Math.atan2(dxi, dzi);
float rotationYaw = (float) (-Math.atan2(dyi, var7));
rotationPitchArray[i1]=rotationPitch;
rotationYawArray[i1]=rotationYaw;
translationX[i1]=dxi;
translationY[i1]=dyi;
translationZ[i1]=dzi;
}
if(this.nextAnchorEntity instanceof NodeEntity)
{
NodeEntity next = (NodeEntity) this.nextAnchorEntity;
next.dx0=dx1;
next.dy0=dy1;
next.dz0=dz1;
}
}
}
if(--this.checkTimer<=0)
{
if(!worldObj.isRemote)
{
WorldServer world = MinecraftServer.getServer().worldServerForDimension(this.anchorDimensionId);
TileEntity te = world.getTileEntity(this.anchorX, this.anchorY, this.anchorZ);
if(te==null || !(te instanceof ICableHolder || te instanceof IMultiPowerCableHolder))
{
this.setDead();
}
else
{
if(te instanceof ICableHolder)
{
if(((ICableHolder)te).isCableRemoved(this.chainUniqueID))
{
this.setDead();
}
}
if(te instanceof IMultiPowerCableHolder)
{
if(((IMultiPowerCableHolder)te).isCableRemoved(this.chainUniqueID))
{
this.setDead();
}
}
}
}
this.checkTimer=200;
}
}
@Override
protected void readEntityFromNBT(NBTTagCompound nbt)
{
this.setChainUniqueID(nbt.getInteger("chainUniqueID"));
this.chainArrangeNumber=nbt.getInteger("chainArrangeNumber");
this.anchorX=nbt.getInteger("anchorX");
this.anchorY=nbt.getInteger("anchorY");
this.anchorZ=nbt.getInteger("anchorZ");
this.anchorFacing=nbt.getShort("anchorFacing");
this.anchorDimensionId=nbt.getInteger("anchorDimensionId");
this.type=nbt.getInteger("type");
this.colorIndex=nbt.getInteger("colorIndex");
this.shouldFollowPlayer=nbt.getBoolean("shouldFollowPlayer");
if(nbt.hasKey("width"))
{
this.setSize(nbt.getFloat("width"), nbt.getFloat("height"));
}
this.virtualNodePosX=nbt.getDouble("virtualNodePosX");
this.virtualNodePosY=nbt.getDouble("virtualNodePosY");
this.virtualNodePosZ=nbt.getDouble("virtualNodePosZ");
}
@Override
protected void writeEntityToNBT(NBTTagCompound nbt)
{
nbt.setFloat("width", this.width);
nbt.setFloat("height", this.height);
nbt.setInteger("chainUniqueID", this.getChainUniqueID());
nbt.setInteger("chainArrangeNumber", this.chainArrangeNumber);
nbt.setInteger("anchorX", this.anchorX);
nbt.setInteger("anchorY", this.anchorY);
nbt.setInteger("anchorZ", this.anchorZ);
nbt.setShort("anchorFacing",this.anchorFacing);
nbt.setInteger("anchorDimensionId",this.anchorDimensionId);
nbt.setInteger("type", this.type);
nbt.setInteger("colorIndex", this.colorIndex);
nbt.setBoolean("shouldFollowPlayer",this.shouldFollowPlayer);
nbt.setDouble("virtualNodePosX",this.virtualNodePosX);
nbt.setDouble("virtualNodePosY",this.virtualNodePosY);
nbt.setDouble("virtualNodePosZ",this.virtualNodePosZ);
}
public void setAnchor(int x, int y, int z, short facing, int dimensionId)
{
this.anchorX=x;
this.anchorY=y;
this.anchorZ=z;
this.anchorFacing=facing;
this.anchorDimensionId=dimensionId;
}
public boolean playerHasItemStack(EntityPlayer player)
{
int var2;
for (var2 = 0; var2 < player.inventory.mainInventory.length; ++var2)
{
if(this.isItemHasSameChainId(player.inventory.mainInventory[var2]))
{
return true;
}
}
return false;
}
private boolean isItemHasSameChainId(ItemStack itemStack)
{
if(itemStack!=null)
{
if(itemStack.getItem() instanceof FlexibleCableItem)
{
return itemStack.stackTagCompound.getInteger("chainUID") == this.chainUniqueID;
}
}
return false;
}
public int getChainUniqueID() {
return chainUniqueID;
}
public void setChainUniqueID(int chainUniqueID) {
this.chainUniqueID = chainUniqueID;
}
@Override
public int getId()
{
return this.getEntityId();
}
@Override
public void recieveData(ByteBufInputStream byteBufInputStream)
{
try
{
this.setChainUniqueID(byteBufInputStream.readInt());
this.chainArrangeNumber=byteBufInputStream.readInt();
this.type=byteBufInputStream.readByte();
this.colorIndex=byteBufInputStream.readInt();
this.virtualNodePosX=byteBufInputStream.readDouble();
this.virtualNodePosY=byteBufInputStream.readDouble();
this.virtualNodePosZ=byteBufInputStream.readDouble();
this.shouldFollowPlayer=byteBufInputStream.readBoolean();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
protected void entityInit() {}
@Override
public boolean isInvalid()
{
return this.isDead;
}
public void setVirtualNodePosToNearestPortal()
{
int x0 = (int)this.posX;
int y0 = (int)this.posY;
int z0 = (int)this.posZ;
for(int xi=x0-2;xi<=x0+2;xi++)
{
for(int yi=y0-2;yi<=y0+2;yi++)
{
for(int zi=z0-2;zi<=z0+2;zi++)
{
Block block = worldObj.getBlock(xi, yi, zi);
if(block==Blocks.portal||block==Blocks.end_portal)
{
this.setVirtualNodePos(xi+0.5d, yi+0.5d, zi+0.5d);
return;
}
}
}
}
}
}
|