001package net.minecraft.entity.item;
002
003import java.util.List;
004import net.minecraft.block.Block;
005import net.minecraft.command.IEntitySelector;
006import net.minecraft.entity.player.EntityPlayer;
007import net.minecraft.nbt.NBTTagCompound;
008import net.minecraft.tileentity.Hopper;
009import net.minecraft.tileentity.TileEntityHopper;
010import net.minecraft.util.DamageSource;
011import net.minecraft.world.World;
012import net.minecraftforge.common.MinecraftForge;
013import net.minecraftforge.event.entity.minecart.MinecartInteractEvent;
014
015public class EntityMinecartHopper extends EntityMinecartContainer implements Hopper
016{
017    /** Whether this hopper minecart is being blocked by an activator rail. */
018    private boolean isBlocked = true;
019    private int transferTicker = -1;
020
021    public EntityMinecartHopper(World par1World)
022    {
023        super(par1World);
024    }
025
026    public EntityMinecartHopper(World par1World, double par2, double par4, double par6)
027    {
028        super(par1World, par2, par4, par6);
029    }
030
031    public int getMinecartType()
032    {
033        return 5;
034    }
035
036    public Block getDefaultDisplayTile()
037    {
038        return Block.hopperBlock;
039    }
040
041    public int getDefaultDisplayTileOffset()
042    {
043        return 1;
044    }
045
046    /**
047     * Returns the number of slots in the inventory.
048     */
049    public int getSizeInventory()
050    {
051        return 5;
052    }
053
054    /**
055     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
056     */
057    public boolean interact(EntityPlayer par1EntityPlayer)
058    {
059        if(MinecraftForge.EVENT_BUS.post(new MinecartInteractEvent(this, par1EntityPlayer))) 
060        {
061            return true;
062        }
063        if (!this.worldObj.isRemote)
064        {
065            par1EntityPlayer.func_96125_a(this);
066        }
067
068        return true;
069    }
070
071    /**
072     * Called every tick the minecart is on an activator rail.
073     */
074    public void onActivatorRailPass(int par1, int par2, int par3, boolean par4)
075    {
076        boolean flag1 = !par4;
077
078        if (flag1 != this.getBlocked())
079        {
080            this.setBlocked(flag1);
081        }
082    }
083
084    /**
085     * Get whether this hopper minecart is being blocked by an activator rail.
086     */
087    public boolean getBlocked()
088    {
089        return this.isBlocked;
090    }
091
092    /**
093     * Set whether this hopper minecart is being blocked by an activator rail.
094     */
095    public void setBlocked(boolean par1)
096    {
097        this.isBlocked = par1;
098    }
099
100    /**
101     * Returns the worldObj for this tileEntity.
102     */
103    public World getWorldObj()
104    {
105        return this.worldObj;
106    }
107
108    /**
109     * Gets the world X position for this hopper entity.
110     */
111    public double getXPos()
112    {
113        return this.posX;
114    }
115
116    /**
117     * Gets the world Y position for this hopper entity.
118     */
119    public double getYPos()
120    {
121        return this.posY;
122    }
123
124    /**
125     * Gets the world Z position for this hopper entity.
126     */
127    public double getZPos()
128    {
129        return this.posZ;
130    }
131
132    /**
133     * Called to update the entity's position/logic.
134     */
135    public void onUpdate()
136    {
137        super.onUpdate();
138
139        if (!this.worldObj.isRemote && this.isEntityAlive() && this.getBlocked())
140        {
141            --this.transferTicker;
142
143            if (!this.canTransfer())
144            {
145                this.setTransferTicker(0);
146
147                if (this.func_96112_aD())
148                {
149                    this.setTransferTicker(4);
150                    this.onInventoryChanged();
151                }
152            }
153        }
154    }
155
156    public boolean func_96112_aD()
157    {
158        if (TileEntityHopper.suckItemsIntoHopper(this))
159        {
160            return true;
161        }
162        else
163        {
164            List list = this.worldObj.selectEntitiesWithinAABB(EntityItem.class, this.boundingBox.expand(0.25D, 0.0D, 0.25D), IEntitySelector.selectAnything);
165
166            if (list.size() > 0)
167            {
168                TileEntityHopper.func_96114_a(this, (EntityItem)list.get(0));
169            }
170
171            return false;
172        }
173    }
174
175    public void killMinecart(DamageSource par1DamageSource)
176    {
177        super.killMinecart(par1DamageSource);
178        this.dropItemWithOffset(Block.hopperBlock.blockID, 1, 0.0F);
179    }
180
181    /**
182     * (abstract) Protected helper method to write subclass entity data to NBT.
183     */
184    protected void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
185    {
186        super.writeEntityToNBT(par1NBTTagCompound);
187        par1NBTTagCompound.setInteger("TransferCooldown", this.transferTicker);
188    }
189
190    /**
191     * (abstract) Protected helper method to read subclass entity data from NBT.
192     */
193    protected void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
194    {
195        super.readEntityFromNBT(par1NBTTagCompound);
196        this.transferTicker = par1NBTTagCompound.getInteger("TransferCooldown");
197    }
198
199    /**
200     * Sets the transfer ticker, used to determine the delay between transfers.
201     */
202    public void setTransferTicker(int par1)
203    {
204        this.transferTicker = par1;
205    }
206
207    /**
208     * Returns whether the hopper cart can currently transfer an item.
209     */
210    public boolean canTransfer()
211    {
212        return this.transferTicker > 0;
213    }
214}