| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.block.material.MapColor;
|
| 6 | import net.minecraft.block.material.Material;
|
| 7 | import net.minecraft.block.properties.IProperty;
|
| 8 | import net.minecraft.block.properties.PropertyBool;
|
| 9 | import net.minecraft.block.properties.PropertyDirection;
|
| 10 | import net.minecraft.block.state.BlockState;
|
| 11 | import net.minecraft.block.state.IBlockState;
|
| 12 | import net.minecraft.creativetab.CreativeTabs;
|
| 13 | import net.minecraft.entity.Entity;
|
| 14 | import net.minecraft.entity.EntityLivingBase;
|
| 15 | import net.minecraft.entity.player.EntityPlayer;
|
| 16 | import net.minecraft.inventory.Container;
|
| 17 | import net.minecraft.inventory.InventoryHelper;
|
| 18 | import net.minecraft.item.ItemStack;
|
| 19 | import net.minecraft.stats.StatList;
|
| 20 | import net.minecraft.tileentity.TileEntity;
|
| 21 | import net.minecraft.tileentity.TileEntityHopper;
|
| 22 | import net.minecraft.util.AxisAlignedBB;
|
| 23 | import net.minecraft.util.BlockPos;
|
| 24 | import net.minecraft.util.EnumFacing;
|
| 25 | import net.minecraft.util.EnumWorldBlockLayer;
|
| 26 | import net.minecraft.world.IBlockAccess;
|
| 27 | import net.minecraft.world.World;
|
| 28 |
|
| 29 | public class BlockHopper extends BlockContainer
|
| 30 | {
|
| 31 | public static final PropertyDirection FACING = PropertyDirection.create("facing", new Predicate<EnumFacing>()
|
| 32 | {
|
| 33 | public boolean apply(EnumFacing p_apply_1_)
|
| 34 | {
|
| 35 | return p_apply_1_ != EnumFacing.UP;
|
| 36 | }
|
| 37 | });
|
| 38 | public static final PropertyBool ENABLED = PropertyBool.create("enabled");
|
| 39 |
|
| 40 | public BlockHopper()
|
| 41 | {
|
| 42 | super(Material.iron, MapColor.stoneColor);
|
| 43 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.DOWN).withProperty(ENABLED, Boolean.valueOf(true)));
|
| 44 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 45 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 46 | }
|
| 47 |
|
| 48 | public void setBlockBoundsBasedOnState(IBlockAccess worldIn, BlockPos pos)
|
| 49 | {
|
| 50 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Add all collision boxes of this Block to the list that intersect with the given mask.
|
| 55 | */
|
| 56 | public void addCollisionBoxesToList(World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB mask, List<AxisAlignedBB> list, Entity collidingEntity)
|
| 57 | {
|
| 58 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.625F, 1.0F);
|
| 59 | super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
|
| 60 | float f = 0.125F;
|
| 61 | this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
|
| 62 | super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
|
| 63 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
|
| 64 | super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
|
| 65 | this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 66 | super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
|
| 67 | this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
|
| 68 | super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
|
| 69 | this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 74 | * IBlockstate
|
| 75 | */
|
| 76 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 77 | {
|
| 78 | EnumFacing enumfacing = facing.getOpposite();
|
| 79 |
|
| 80 | if (enumfacing == EnumFacing.UP)
|
| 81 | {
|
| 82 | enumfacing = EnumFacing.DOWN;
|
| 83 | }
|
| 84 |
|
| 85 | return this.getDefaultState().withProperty(FACING, enumfacing).withProperty(ENABLED, Boolean.valueOf(true));
|
| 86 | }
|
| 87 |
|
| 88 | /**
|
| 89 | * Returns a new instance of a block's tile entity class. Called on placing the block.
|
| 90 | */
|
| 91 | public TileEntity createNewTileEntity(World worldIn, int meta)
|
| 92 | {
|
| 93 | return new TileEntityHopper();
|
| 94 | }
|
| 95 |
|
| 96 | /**
|
| 97 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 98 | */
|
| 99 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 100 | {
|
| 101 | super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
|
| 102 |
|
| 103 | if (stack.hasDisplayName())
|
| 104 | {
|
| 105 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 106 |
|
| 107 | if (tileentity instanceof TileEntityHopper)
|
| 108 | {
|
| 109 | ((TileEntityHopper)tileentity).setCustomName(stack.getDisplayName());
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 115 | {
|
| 116 | this.updateState(worldIn, pos, state);
|
| 117 | }
|
| 118 |
|
| 119 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 120 | {
|
| 121 | if (worldIn.isRemote)
|
| 122 | {
|
| 123 | return true;
|
| 124 | }
|
| 125 | else
|
| 126 | {
|
| 127 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 128 |
|
| 129 | if (tileentity instanceof TileEntityHopper)
|
| 130 | {
|
| 131 | playerIn.displayGUIChest((TileEntityHopper)tileentity);
|
| 132 | playerIn.triggerAchievement(StatList.field_181732_P);
|
| 133 | }
|
| 134 |
|
| 135 | return true;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * Called when a neighboring block changes.
|
| 141 | */
|
| 142 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 143 | {
|
| 144 | this.updateState(worldIn, pos, state);
|
| 145 | }
|
| 146 |
|
| 147 | private void updateState(World worldIn, BlockPos pos, IBlockState state)
|
| 148 | {
|
| 149 | boolean flag = !worldIn.isBlockPowered(pos);
|
| 150 |
|
| 151 | if (flag != ((Boolean)state.getValue(ENABLED)).booleanValue())
|
| 152 | {
|
| 153 | worldIn.setBlockState(pos, state.withProperty(ENABLED, Boolean.valueOf(flag)), 4);
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 158 | {
|
| 159 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 160 |
|
| 161 | if (tileentity instanceof TileEntityHopper)
|
| 162 | {
|
| 163 | InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityHopper)tileentity);
|
| 164 | worldIn.updateComparatorOutputLevel(pos, this);
|
| 165 | }
|
| 166 |
|
| 167 | super.breakBlock(worldIn, pos, state);
|
| 168 | }
|
| 169 |
|
| 170 | /**
|
| 171 | * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
|
| 172 | */
|
| 173 | public int getRenderType()
|
| 174 | {
|
| 175 | return 3;
|
| 176 | }
|
| 177 |
|
| 178 | public boolean isFullCube()
|
| 179 | {
|
| 180 | return false;
|
| 181 | }
|
| 182 |
|
| 183 | /**
|
| 184 | * Used to determine ambient occlusion and culling when rebuilding chunks for render
|
| 185 | */
|
| 186 | public boolean isOpaqueCube()
|
| 187 | {
|
| 188 | return false;
|
| 189 | }
|
| 190 |
|
| 191 | public boolean shouldSideBeRendered(IBlockAccess worldIn, BlockPos pos, EnumFacing side)
|
| 192 | {
|
| 193 | return true;
|
| 194 | }
|
| 195 |
|
| 196 | public static EnumFacing getFacing(int meta)
|
| 197 | {
|
| 198 | return EnumFacing.getFront(meta & 7);
|
| 199 | }
|
| 200 |
|
| 201 | /**
|
| 202 | * Get's the hopper's active status from the 8-bit of the metadata. Note that the metadata stores whether the block
|
| 203 | * is powered, so this returns true when that bit is 0.
|
| 204 | */
|
| 205 | public static boolean isEnabled(int meta)
|
| 206 | {
|
| 207 | return (meta & 8) != 8;
|
| 208 | }
|
| 209 |
|
| 210 | public boolean hasComparatorInputOverride()
|
| 211 | {
|
| 212 | return true;
|
| 213 | }
|
| 214 |
|
| 215 | public int getComparatorInputOverride(World worldIn, BlockPos pos)
|
| 216 | {
|
| 217 | return Container.calcRedstone(worldIn.getTileEntity(pos));
|
| 218 | }
|
| 219 |
|
| 220 | public EnumWorldBlockLayer getBlockLayer()
|
| 221 | {
|
| 222 | return EnumWorldBlockLayer.CUTOUT_MIPPED;
|
| 223 | }
|
| 224 |
|
| 225 | /**
|
| 226 | * Convert the given metadata into a BlockState for this Block
|
| 227 | */
|
| 228 | public IBlockState getStateFromMeta(int meta)
|
| 229 | {
|
| 230 | return this.getDefaultState().withProperty(FACING, getFacing(meta)).withProperty(ENABLED, Boolean.valueOf(isEnabled(meta)));
|
| 231 | }
|
| 232 |
|
| 233 | /**
|
| 234 | * Convert the BlockState into the correct metadata value
|
| 235 | */
|
| 236 | public int getMetaFromState(IBlockState state)
|
| 237 | {
|
| 238 | int i = 0;
|
| 239 | i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
|
| 240 |
|
| 241 | if (!((Boolean)state.getValue(ENABLED)).booleanValue())
|
| 242 | {
|
| 243 | i |= 8;
|
| 244 | }
|
| 245 |
|
| 246 | return i;
|
| 247 | }
|
| 248 |
|
| 249 | protected BlockState createBlockState()
|
| 250 | {
|
| 251 | return new BlockState(this, new IProperty[] {FACING, ENABLED});
|
| 252 | }
|
| 253 | }
|
| 254 |
|