summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/crop_harvestors/SackTileEntity.java
blob: 800b2c5a06cc7caf38bb710b923bb2e3758d3d03 (plain)
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
package ihl.crop_harvestors;

import java.util.List;
import java.util.Random;

import ic2.core.IC2;
import ic2.core.block.TileEntityInventory;
import ihl.IHLMod;
import ihl.utils.IHLFluidTank;
import ihl.utils.IHLUtils;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;

public class SackTileEntity extends TileEntityInventory implements IFluidHandler
{
    private final int maxLeavesHeight=12;
    private final int maxLeavesWidth=5;
    public float leavesCounter=0F;
    private int updateCounter=1100;
    private Random rand=new Random();
    private int blobTimer=20;
    public byte currentTree = RUBBERTREE;
    public final int blobCapacity = 1;
    private final static byte RUBBERTREE=0;
    private final static byte SPRUCE=1;
	public final IHLFluidTank fluidTank = new IHLFluidTank(8000);
    public int visibleFluidId = -1;
    public int visibleFluidAmount = 1;
   
	public SackTileEntity() {
		super();
	}

	@Override
    public List<String> getNetworkedFields()
    {
		List<String> fields = super.getNetworkedFields();
		fields.add("visibleFluidId");
		fields.add("visibleFluidAmount");
		fields.add("leavesCounter");
		fields.add("currentTree");
		return fields;
    }
	
    @Override
	public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);
        this.fluidTank.readFromNBT(nbttagcompound.getCompoundTag("fluidTank"));
    }

    @Override
	public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        NBTTagCompound fluidTankTag = new NBTTagCompound();
        this.fluidTank.writeToNBT(fluidTankTag);
        nbttagcompound.setTag("fluidTank", fluidTankTag);
    }
    
	@Override
	public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
		return false;
	}
	
	@Override
	public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
		return new ItemStack(IHLMod.sackBlock,1);
	}
	
    public boolean enableUpdateEntity()
    {
        return true;
    }

    
    @Override
	public void updateEntityClient()
    {
    	super.updateEntityClient();
    	if(IHLMod.ic2Leaves!=null && IHLMod.ic2Wood!=null)
    	{
			if(this.blobTimer<=0)
			{
				if(IC2.platform.isRendering())
				{
	   				switch(this.currentTree)
	   				{
	   					case RUBBERTREE:
	   						IHLMod.proxy.spawnParticle(1,worldObj, xCoord+0.5D+mX()*0.5D,yCoord+1.05D,zCoord+0.5D+mZ()*0.5D,-(double)mX()*0.1D,-0.03D,-(double)mZ()*0.1D,0.1F);
	   						break;
	   					case SPRUCE:
	   						IHLMod.proxy.spawnParticle(2,worldObj, xCoord+0.5D+mX()*0.5D,yCoord+1.05D,zCoord+0.5D+mZ()*0.5D,-(double)mX()*0.1D,-0.03D,-(double)mZ()*0.1D,0.1F);
	   						break;
	   				}
    	    	}
	   			if(this.fluidTank.getFluidAmount()>=this.fluidTank.getCapacity())
	    		{
	   				if(IC2.platform.isRendering())
	   				{
		   				switch(this.currentTree)
		   				{
		   					case RUBBERTREE:
		   						IHLMod.proxy.spawnParticle(1,worldObj, xCoord+0.5D-mX()*0.3D+(rand.nextDouble()-0.5D)*mZ()*0.6D,yCoord+0.8D,zCoord+0.5D-mZ()*0.3D+(rand.nextDouble()-0.5D)*mX()*0.6D,0D,-0.05D,0D,0.1F);
		   						break;
		   					case SPRUCE:
		   						IHLMod.proxy.spawnParticle(2,worldObj, xCoord+0.5D-mX()*0.3D+(rand.nextDouble()-0.5D)*mZ()*0.6D,yCoord+0.8D,zCoord+0.5D-mZ()*0.3D+(rand.nextDouble()-0.5D)*mX()*0.6D,0D,-0.05D,0D,0.1F);
		   						break;
		   				}
	   				}
	    		}
				if(this.leavesCounter>1F)
				{
					this.blobTimer=Math.round(4000F/this.leavesCounter);
				}
				else
				{
					this.blobTimer=200;
				}
			}
			else
			{
				if(this.leavesCounter>1F)
				{
					this.blobTimer--;
				}
			}
    		if(this.updateCounter<1200)
    		{
    			this.updateCounter++;
    		}
    	}
    	
    }
    
    
    @Override
	public void updateEntityServer()
    {
    	super.updateEntityServer();
    	if(IHLMod.ic2Leaves!=null && IHLMod.ic2Wood!=null)
    	{
			if(this.blobTimer<=0)
			{
	   			{
					if(IC2.platform.isSimulating())
					{
						switch(this.currentTree)
						{
	   					case RUBBERTREE:
		   						this.fluidTank.fill(new FluidStack(FluidRegistry.getFluid("fluidrubbertreesap"), blobCapacity), true);
		   						break;
	   					case SPRUCE:
	   							this.fluidTank.fill(new FluidStack(FluidRegistry.getFluid("spruceresin"), blobCapacity), true);
	   							break;
	   					}
	   				}
	   			}
				if(this.leavesCounter>1F)
				{
					this.blobTimer=Math.round(4000F/this.leavesCounter);
				}
				else
				{
					this.blobTimer=200;
				}
			}
			else
			{
				if(this.leavesCounter>1F)
				{
					this.blobTimer--;
				}
			}
    		if(this.updateCounter<1200)
    		{
    			this.updateCounter++;
    		}
    		else
    		{
    			if(IC2.platform.isSimulating())
    			{
    				this.updateCounter=rand.nextInt(600);
    				if(checkCorrectPlacing())
    				{
    					countRubberTreeLeaves();
    				}
    				else
    				{
    					leavesCounter=0F;
    				}
    				IC2.network.get().updateTileEntityField(this, "currentTree");	   							
    				IC2.network.get().updateTileEntityField(this, "leavesCounter");
    				if(this.fluidTank.getFluid()!=null)
    				{

    						TileEntity te = worldObj.getTileEntity(xCoord, yCoord-1, zCoord);
    						if(te!=null && te instanceof IFluidHandler)
    						{
    							IFluidHandler fte = (IFluidHandler) te;
    							FluidStack fStack = this.fluidTank.drain(Integer.MAX_VALUE, false);
    							if(fte.canFill(ForgeDirection.UP, fStack.getFluid()))
    							{
    								if(fte.fill(ForgeDirection.UP, fStack, false)>0)
    								{
    									int amount = fte.fill(ForgeDirection.UP, fStack, true);
    									this.fluidTank.drain(amount, true);
    								}
    							}
    						}
    				}
    			}
    		}
			if(this.fluidTank.getFluid()!=null && (visibleFluidId!=this.fluidTank.getFluid().getFluid().getID() || Math.abs(visibleFluidAmount-this.fluidTank.getFluidAmount())>20))
			{
				visibleFluidId = this.fluidTank.getFluid().getFluid().getID();
				visibleFluidAmount = this.fluidTank.getFluidAmount();
	       		IC2.network.get().updateTileEntityField(this, "visibleFluidId");
	       		IC2.network.get().updateTileEntityField(this, "visibleFluidAmount");
	       	}
			else if(this.fluidTank.getFluid()==null && visibleFluidId!=-1)
	       	{
				visibleFluidId=-1;
	       		IC2.network.get().updateTileEntityField(this, "visibleFluidId");
	       	}
    	}
    }
    
    private boolean checkCorrectPlacing() 
    {
    	int xz[]={0,1,0,-1,0};
    	Block block, block2;
    	int meta,meta2;
    	for(int i =0;i<=3;i++)
    	{
    		block=worldObj.getBlock(xCoord+xz[i], yCoord, zCoord+xz[i+1]);
    		meta=worldObj.getBlockMetadata(xCoord+xz[i], yCoord, zCoord+xz[i+1]);
    		block2=worldObj.getBlock(xCoord+xz[i], yCoord+1, zCoord+xz[i+1]);
    		meta2=worldObj.getBlockMetadata(xCoord+xz[i], yCoord+1, zCoord+xz[i+1]);
    		if(
    			meta>0 && 
    			meta2>0 && 
    			(block==IHLMod.ic2Wood||block==IHLMod.rubberTreeBlock) &&
    			(block2==IHLMod.ic2Wood||block2==IHLMod.rubberTreeBlock)
    		  )
    		{
    			short newFacing = this.getFacingFromXZ(xz[i], xz[i+1]);
    			this.setFacing(newFacing);
    			if(block2==IHLMod.ic2Wood)
    			{
    				worldObj.setBlock(xCoord+xz[i], yCoord+1, zCoord+xz[i+1],IHLMod.rubberTreeBlock,newFacing,3);
    			}
    			else
    			{
    				if(newFacing!=meta2)
    				{
    					return false;
    				}
    			}
    			this.currentTree=RUBBERTREE;
    			return checkGround();
    		}
    		else if(
    				((block==Blocks.log && meta==1) || 
    				(block==IHLMod.spruceTreeBlock && meta>0)) &&
    				((block2==Blocks.log && meta2==1) || 
    				(block2==IHLMod.spruceTreeBlock && meta2>0))
    				)
    		{
    			short newFacing = this.getFacingFromXZ(xz[i], xz[i+1]);
    			this.setFacing(newFacing);
    			if(block2==Blocks.log)
    			{
    				worldObj.setBlock(xCoord+xz[i], yCoord+1, zCoord+xz[i+1],IHLMod.spruceTreeBlock,newFacing,3);
    			}
    			else
    			{
    				if(newFacing!=meta2)
    				{
    					return false;
    				}
    			}
    			this.currentTree=SPRUCE;
    			return checkGround();
    		}
    	}
		return false;
	}

	private boolean checkGround()
    {
	   	Block block;
    	int meta;
    	for(int h=0; h<=this.maxLeavesHeight; h++)
    	{
    		block=worldObj.getBlock(xCoord+mX(), yCoord-h, zCoord+mZ());
    		meta=worldObj.getBlockMetadata(xCoord+mX(), yCoord-h, zCoord+mZ());
    		if(!isLogBlock(block, meta))
    		{
    			if(IHLUtils.isBlockRegisteredInOreDictionaryAs(block, "blockDirt"))
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
    		}
    		else if(meta<=0)
    		{
				return false;
    		}
    	}
		return false;
    }
	
	private boolean isLogBlock(Block block, int meta)
	{
		switch(this.currentTree)
		{
			case RUBBERTREE:
				return block==IHLMod.ic2Wood || block==IHLMod.rubberTreeBlock;
			case SPRUCE:
				return (block==Blocks.log && meta==1) || (block==IHLMod.spruceTreeBlock && meta>0);
		}
		return false;
	}
	
	private boolean isLeavesBlock(Block block)
	{
		switch(this.currentTree)
		{
			case RUBBERTREE:
				return block==IHLMod.ic2Leaves;
			case SPRUCE:
				return block==Blocks.leaves;
		}
		return false;
	}
	
	private boolean isIncisedLog(Block block)
	{
		switch(this.currentTree)
		{
			case RUBBERTREE:
				return block==IHLMod.rubberTreeBlock;
			case SPRUCE:
				return block==IHLMod.spruceTreeBlock;
		}
		return false;
	}
    
	private void countRubberTreeLeaves()
    {
    	this.leavesCounter=0;
    	int[][][] leavesMatrix=new int[this.maxLeavesWidth][this.maxLeavesWidth][this.maxLeavesHeight];
    	for(int iy=1;iy<this.maxLeavesHeight;iy++)
    	{
    		Block block=worldObj.getBlock(xCoord+mX(), yCoord+iy, zCoord+mZ());
    		int meta=worldObj.getBlockMetadata(xCoord+mX(), yCoord+iy, zCoord+mZ());
    		if(isLogBlock(block, meta)||isLeavesBlock(block))
    		{
    			if(isLeavesBlock(block))
    			{
    				this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX(), yCoord+iy, zCoord+mZ());
    			}
    			else if(isIncisedLog(block) && iy>=2)
    			{
    				break;
    			}
				for(int sign=1;sign>=-1;sign-=2)
				{
					for(int ix=1;ix<=2;ix++)
					{

    					block=worldObj.getBlock(xCoord+mX()+ix*sign, yCoord+iy, zCoord+mZ());
    					if(isLeavesBlock(block))
    					{
    						this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX()+ix*sign, yCoord+iy, zCoord+mZ());
    						leavesMatrix[2+ix*sign][2][iy-1]=1;
    					}
    					else
    					{
    						break;
    					}
    				}
					for(int iz=1;iz<=2;iz++)
    				{
    					block=worldObj.getBlock(xCoord+mX(), yCoord+iy, zCoord+mZ()+iz*sign);
        				if(isLeavesBlock(block))
        				{
        					this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX(), yCoord+iy, zCoord+mZ()+iz*sign);
        					leavesMatrix[2][2+iz*sign][iy-1]=1;
        				}
        				else
        				{
        					break;
        				}
    				}
				}
				for(int signx=1;signx>=-1;signx-=2)
				{
					for(int signz=1;signz>=-1;signz-=2)
					{
						if(leavesMatrix[2][2+signz][iy-1]==1||leavesMatrix[2+signx][2][iy-1]==1)
						{
							block=worldObj.getBlock(xCoord+mX()+signx, yCoord+iy, zCoord+mZ()+signz);
	        				if(isLeavesBlock(block))
	        				{
	        					this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX()+signx, yCoord+iy, zCoord+mZ()+signz);
	        					leavesMatrix[2+signx][2+signz][iy-1]=1;
	        				}
						}
						if(leavesMatrix[2+signx*2][2][iy-1]==1||leavesMatrix[2+signx][2+signz][iy-1]==1)
						{
							block=worldObj.getBlock(xCoord+mX()+signx*2, yCoord+iy, zCoord+mZ()+signz);
	        				if(block==IHLMod.ic2Leaves)
	        				{
	        					this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX()+signx*2, yCoord+iy, zCoord+mZ()+signz);
								leavesMatrix[2+signx*2][2+signz][iy-1]=1;
	        				}
						}
						if(leavesMatrix[2][2+signz*2][iy-1]==1||leavesMatrix[2+signx][2+signz][iy-1]==1)
						{
							block=worldObj.getBlock(xCoord+mX()+signx, yCoord+iy, zCoord+mZ()+signz*2);
	        				if(isLeavesBlock(block))
	        				{
	        					this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX()+signx, yCoord+iy, zCoord+mZ()+signz*2);
	        					leavesMatrix[2+signx][2+signz*2][iy-1]=1;
	        				}
						}
						if(leavesMatrix[2+signx*2][2+signz][iy-1]==1||leavesMatrix[2+signx][2+signz*2][iy-1]==1)
						{
							block=worldObj.getBlock(xCoord+mX()+signx*2, yCoord+iy, zCoord+mZ()+signz*2);
	        				if(isLeavesBlock(block))
	        				{
	        					this.leavesCounter+=worldObj.getLightBrightness(xCoord+mX()+signx*2, yCoord+iy, zCoord+mZ()+signz*2);
	        				}
						}
					}
				}
    		}
    		else
    		{
    			break;
    		}
    	}
    }
	
    //1.7.10 API
	@Override
	public boolean canDrain(ForgeDirection arg0, Fluid arg1) {
		return true;
	}

	@Override
	public boolean canFill(ForgeDirection arg0, Fluid arg1) {
		return false;
	}

	@Override
	public String getInventoryName() {
		return "sack";
	}
	
    private int mX()
	{
		switch(this.getFacing())
		{
		case 4:
		return -1;
		case 5:
		return 1;
		default:
		return 0;
		}
	}
	
	private int mZ()
	{
		switch(this.getFacing())
		{
		case 3:
		return 1;
		case 2:
		return -1;
		case 4:
		return 0;
		case 5:
		return 0;
		default:
		return -1;
		}
	}
	
	private short getFacingFromXZ(int x, int z)
	{
		switch(x)
		{
			case -1:
				return (short)4;
			case 1:
				return (short)5;
			default:
				switch(z)
				{
				case 1:
					return (short)3;
				case -1:
					return (short)2;
				default:
					return (short)2;
				}
		}
	}
	
	public float getRenderLiquidLevel()
	{
		return (float)this.visibleFluidAmount/(float)this.fluidTank.getCapacity();
	}
	
	@Override
    public boolean shouldRenderInPass(int pass)
    {
        return pass==0;
    }

	@Override
	public FluidStack drain(ForgeDirection arg0, FluidStack arg1, boolean arg2) 
	{
		if(this.canDrain(arg0, arg1.getFluid()))
		{
			FluidStack fStack = fluidTank.drain(arg1, arg2);
			return fStack;
		}
		return null;
	}

	@Override
	public FluidStack drain(ForgeDirection arg0, int arg1, boolean arg2) 
	{
		if(this.canDrain(arg0, null))
		{
			FluidStack fStack = fluidTank.drain(arg1, arg2);
			return fStack;
		}
		return null;
	}

	@Override
	public int fill(ForgeDirection arg0, FluidStack arg1, boolean arg2) {
		return 0;
	}

	@Override
	public FluidTankInfo[] getTankInfo(ForgeDirection arg0) {
		return new FluidTankInfo[]{this.fluidTank.getInfo()};
	}
}