| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.block.state.IBlockState;
|
| 5 | import net.minecraft.entity.player.EntityPlayer;
|
| 6 | import net.minecraft.entity.player.InventoryPlayer;
|
| 7 | import net.minecraft.init.Blocks;
|
| 8 | import net.minecraft.inventory.Container;
|
| 9 | import net.minecraft.inventory.ContainerHopper;
|
| 10 | import net.minecraft.item.Item;
|
| 11 | import net.minecraft.nbt.NBTTagCompound;
|
| 12 | import net.minecraft.tileentity.IHopper;
|
| 13 | import net.minecraft.tileentity.TileEntityHopper;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.util.DamageSource;
|
| 16 | import net.minecraft.util.EntitySelectors;
|
| 17 | import net.minecraft.world.World;
|
| 18 |
|
| 19 | public class EntityMinecartHopper extends EntityMinecartContainer implements IHopper
|
| 20 | {
|
| 21 | /** Whether this hopper minecart is being blocked by an activator rail. */
|
| 22 | private boolean isBlocked = true;
|
| 23 | private int transferTicker = -1;
|
| 24 | private BlockPos field_174900_c = BlockPos.ORIGIN;
|
| 25 |
|
| 26 | public EntityMinecartHopper(World worldIn)
|
| 27 | {
|
| 28 | super(worldIn);
|
| 29 | }
|
| 30 |
|
| 31 | public EntityMinecartHopper(World worldIn, double x, double y, double z)
|
| 32 | {
|
| 33 | super(worldIn, x, y, z);
|
| 34 | }
|
| 35 |
|
| 36 | public EntityMinecart.EnumMinecartType getMinecartType()
|
| 37 | {
|
| 38 | return EntityMinecart.EnumMinecartType.HOPPER;
|
| 39 | }
|
| 40 |
|
| 41 | public IBlockState getDefaultDisplayTile()
|
| 42 | {
|
| 43 | return Blocks.hopper.getDefaultState();
|
| 44 | }
|
| 45 |
|
| 46 | public int getDefaultDisplayTileOffset()
|
| 47 | {
|
| 48 | return 1;
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Returns the number of slots in the inventory.
|
| 53 | */
|
| 54 | public int getSizeInventory()
|
| 55 | {
|
| 56 | return 5;
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * First layer of player interaction
|
| 61 | */
|
| 62 | public boolean interactFirst(EntityPlayer playerIn)
|
| 63 | {
|
| 64 | if (!this.worldObj.isRemote)
|
| 65 | {
|
| 66 | playerIn.displayGUIChest(this);
|
| 67 | }
|
| 68 |
|
| 69 | return true;
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * Called every tick the minecart is on an activator rail. Args: x, y, z, is the rail receiving power
|
| 74 | */
|
| 75 | public void onActivatorRailPass(int x, int y, int z, boolean receivingPower)
|
| 76 | {
|
| 77 | boolean flag = !receivingPower;
|
| 78 |
|
| 79 | if (flag != this.getBlocked())
|
| 80 | {
|
| 81 | this.setBlocked(flag);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Get whether this hopper minecart is being blocked by an activator rail.
|
| 87 | */
|
| 88 | public boolean getBlocked()
|
| 89 | {
|
| 90 | return this.isBlocked;
|
| 91 | }
|
| 92 |
|
| 93 | /**
|
| 94 | * Set whether this hopper minecart is being blocked by an activator rail.
|
| 95 | */
|
| 96 | public void setBlocked(boolean p_96110_1_)
|
| 97 | {
|
| 98 | this.isBlocked = p_96110_1_;
|
| 99 | }
|
| 100 |
|
| 101 | /**
|
| 102 | * Returns the worldObj for this tileEntity.
|
| 103 | */
|
| 104 | public World getWorld()
|
| 105 | {
|
| 106 | return this.worldObj;
|
| 107 | }
|
| 108 |
|
| 109 | /**
|
| 110 | * Gets the world X position for this hopper entity.
|
| 111 | */
|
| 112 | public double getXPos()
|
| 113 | {
|
| 114 | return this.posX;
|
| 115 | }
|
| 116 |
|
| 117 | /**
|
| 118 | * Gets the world Y position for this hopper entity.
|
| 119 | */
|
| 120 | public double getYPos()
|
| 121 | {
|
| 122 | return this.posY + 0.5D;
|
| 123 | }
|
| 124 |
|
| 125 | /**
|
| 126 | * Gets the world Z position for this hopper entity.
|
| 127 | */
|
| 128 | public double getZPos()
|
| 129 | {
|
| 130 | return this.posZ;
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Called to update the entity's position/logic.
|
| 135 | */
|
| 136 | public void onUpdate()
|
| 137 | {
|
| 138 | super.onUpdate();
|
| 139 |
|
| 140 | if (!this.worldObj.isRemote && this.isEntityAlive() && this.getBlocked())
|
| 141 | {
|
| 142 | BlockPos blockpos = new BlockPos(this);
|
| 143 |
|
| 144 | if (blockpos.equals(this.field_174900_c))
|
| 145 | {
|
| 146 | --this.transferTicker;
|
| 147 | }
|
| 148 | else
|
| 149 | {
|
| 150 | this.setTransferTicker(0);
|
| 151 | }
|
| 152 |
|
| 153 | if (!this.canTransfer())
|
| 154 | {
|
| 155 | this.setTransferTicker(0);
|
| 156 |
|
| 157 | if (this.func_96112_aD())
|
| 158 | {
|
| 159 | this.setTransferTicker(4);
|
| 160 | this.markDirty();
|
| 161 | }
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | public boolean func_96112_aD()
|
| 167 | {
|
| 168 | if (TileEntityHopper.captureDroppedItems(this))
|
| 169 | {
|
| 170 | return true;
|
| 171 | }
|
| 172 | else
|
| 173 | {
|
| 174 | List<EntityItem> list = this.worldObj.<EntityItem>getEntitiesWithinAABB(EntityItem.class, this.getEntityBoundingBox().expand(0.25D, 0.0D, 0.25D), EntitySelectors.selectAnything);
|
| 175 |
|
| 176 | if (list.size() > 0)
|
| 177 | {
|
| 178 | TileEntityHopper.putDropInInventoryAllSlots(this, (EntityItem)list.get(0));
|
| 179 | }
|
| 180 |
|
| 181 | return false;
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | public void killMinecart(DamageSource source)
|
| 186 | {
|
| 187 | super.killMinecart(source);
|
| 188 |
|
| 189 | if (this.worldObj.getGameRules().getBoolean("doEntityDrops"))
|
| 190 | {
|
| 191 | this.dropItemWithOffset(Item.getItemFromBlock(Blocks.hopper), 1, 0.0F);
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | /**
|
| 196 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 197 | */
|
| 198 | protected void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 199 | {
|
| 200 | super.writeEntityToNBT(tagCompound);
|
| 201 | tagCompound.setInteger("TransferCooldown", this.transferTicker);
|
| 202 | }
|
| 203 |
|
| 204 | /**
|
| 205 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 206 | */
|
| 207 | protected void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 208 | {
|
| 209 | super.readEntityFromNBT(tagCompund);
|
| 210 | this.transferTicker = tagCompund.getInteger("TransferCooldown");
|
| 211 | }
|
| 212 |
|
| 213 | /**
|
| 214 | * Sets the transfer ticker, used to determine the delay between transfers.
|
| 215 | */
|
| 216 | public void setTransferTicker(int p_98042_1_)
|
| 217 | {
|
| 218 | this.transferTicker = p_98042_1_;
|
| 219 | }
|
| 220 |
|
| 221 | /**
|
| 222 | * Returns whether the hopper cart can currently transfer an item.
|
| 223 | */
|
| 224 | public boolean canTransfer()
|
| 225 | {
|
| 226 | return this.transferTicker > 0;
|
| 227 | }
|
| 228 |
|
| 229 | public String getGuiID()
|
| 230 | {
|
| 231 | return "minecraft:hopper";
|
| 232 | }
|
| 233 |
|
| 234 | public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
|
| 235 | {
|
| 236 | return new ContainerHopper(playerInventory, this, playerIn);
|
| 237 | }
|
| 238 | }
|
| 239 |
|