summaryrefslogtreecommitdiff
path: root/src/main/java/ihl/explosion/IHLEntityFallingPile.java
blob: 79973d8aa12361d13e09d1bf9ee8408f6f7405dd (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
package ihl.explosion;

import ihl.utils.IHLUtils;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.S23PacketBlockChange;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.item.ItemExpireEvent;

public class IHLEntityFallingPile extends EntityItem{

	public IHLEntityFallingPile(World world) {
		super(world);
	}
	
	@Override
    public void onUpdate()
    {
        ItemStack stack = this.getDataWatcher().getWatchableObjectItemStack(10);
        if (stack != null && stack.getItem() != null)
        {
            if (stack.getItem().onEntityItemUpdate(this))
            {
                return;
            }
        }

        if (this.getEntityItem() == null)
        {
            this.setDead();
        }
        else
        {
            this.onEntityUpdate();

            if (this.delayBeforeCanPickup > 0)
            {
                --this.delayBeforeCanPickup;
            }

            this.prevPosX = this.posX;
            this.prevPosY = this.posY;
            this.prevPosZ = this.posZ;
            this.motionY -= 0.03999999910593033D;
            this.noClip = this.func_145771_j(this.posX, (this.boundingBox.minY + this.boundingBox.maxY) / 2.0D, this.posZ);
            this.moveEntity(this.motionX, this.motionY, this.motionZ);
            boolean flag = (int)this.prevPosX != (int)this.posX || (int)this.prevPosY != (int)this.posY || (int)this.prevPosZ != (int)this.posZ;

            if (flag || this.ticksExisted % 25 == 0)
            {
                if (this.worldObj.getBlock(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ)).getMaterial() == Material.lava)
                {
                    this.motionY = 0.20000000298023224D;
                    this.motionX = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
                    this.motionZ = (double)((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F);
                    this.playSound("random.fizz", 0.4F, 2.0F + this.rand.nextFloat() * 0.4F);
                }

            }

            this.motionX *= 0.98D;
            this.motionY *= 0.98D;
            this.motionZ *= 0.98D;

            if (this.onGround)
            {
                this.motionY *= -0.5D;
            }

            ++this.age;

            ItemStack item = this.getEntityItem();
    
            if (!this.worldObj.isRemote && 
            		(this.age >= lifespan || this.onGround))
            {
                if (item != null)
                {
                    ItemExpireEvent event = new ItemExpireEvent(this, (item.getItem() == null ? 6000 : item.getItem().getEntityLifespan(item, worldObj)));
                    if (MinecraftForge.EVENT_BUS.post(event))
                    {
                        lifespan += event.extraLife;
                    }
                    else
                    {
                        int x = (int)(this.boundingBox.minX);
                        int y = (int)(this.boundingBox.minY);
                        int z = (int)(this.boundingBox.minZ);
                        int i=1;
                        do
                        {
                        	int[] xz = new int[] {0,0,1,0,-1,0};
                        	if(i<xz.length)
                        	{
                        		if(IHLUtils.isBlockCanBeReplaced(worldObj, x+xz[i-1], y, z+xz[i]))
                        		{
                        			x+=xz[i-1];
                        			z+=xz[i];
                            		if(IHLUtils.isBlockCanBeReplaced(worldObj, x, y-1, z))
                            		{
                            			this.setPosition(x+0.5d, y-0.5d, z+0.5d);
                            			return;
                            		}
                                    PileTileEntity pte = new PileTileEntity();
                                    pte.content=this.getEntityItem();
                                    IHLUtils.setBlockAndTileEntityRaw(worldObj, x, y, z, PileBlock.instance, pte);
                            		for(Object player:worldObj.playerEntities)
                            		{
                            			if(player instanceof EntityPlayerMP)
                            			{
                            				EntityPlayerMP playerMP = (EntityPlayerMP)player;
                            				playerMP.playerNetServerHandler.sendPacket(new S23PacketBlockChange(x,y,z,worldObj));
                            			}
                            		}

                                	break;
                        		}
                        		else
                        		{
                        			i++;
                        		}
                        	}
                        	else
                        	{
                        		i=1;
                        		y++;
                        	}
                        }
                        while(!IHLUtils.isBlockCanBeReplaced(worldObj, x, y, z) && y < 254);
                        this.setDead();
                    }
                }
                else
                {
                    this.setDead();
                }
            }
    
            if (item != null && item.stackSize <= 0)
            {
                this.setDead();
            }
        }
    }
	
    public void applyEntityCollision(Entity entity){}
}