| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.Random;
|
| 4 | import net.minecraft.block.material.Material;
|
| 5 | import net.minecraft.block.properties.IProperty;
|
| 6 | import net.minecraft.block.properties.PropertyDirection;
|
| 7 | import net.minecraft.block.state.BlockState;
|
| 8 | import net.minecraft.block.state.IBlockState;
|
| 9 | import net.minecraft.entity.EntityLivingBase;
|
| 10 | import net.minecraft.entity.player.EntityPlayer;
|
| 11 | import net.minecraft.init.Blocks;
|
| 12 | import net.minecraft.inventory.Container;
|
| 13 | import net.minecraft.inventory.InventoryHelper;
|
| 14 | import net.minecraft.item.Item;
|
| 15 | import net.minecraft.item.ItemStack;
|
| 16 | import net.minecraft.stats.StatList;
|
| 17 | import net.minecraft.tileentity.TileEntity;
|
| 18 | import net.minecraft.tileentity.TileEntityFurnace;
|
| 19 | import net.minecraft.util.BlockPos;
|
| 20 | import net.minecraft.util.EnumFacing;
|
| 21 | import net.minecraft.util.EnumParticleTypes;
|
| 22 | import net.minecraft.world.World;
|
| 23 |
|
| 24 | public class BlockFurnace extends BlockContainer
|
| 25 | {
|
| 26 | public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
|
| 27 | private final boolean isBurning;
|
| 28 | private static boolean keepInventory;
|
| 29 |
|
| 30 | protected BlockFurnace(boolean isBurning)
|
| 31 | {
|
| 32 | super(Material.rock);
|
| 33 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
|
| 34 | this.isBurning = isBurning;
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * Get the Item that this Block should drop when harvested.
|
| 39 | */
|
| 40 | public Item getItemDropped(IBlockState state, Random rand, int fortune)
|
| 41 | {
|
| 42 | return Item.getItemFromBlock(Blocks.furnace);
|
| 43 | }
|
| 44 |
|
| 45 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 46 | {
|
| 47 | this.setDefaultFacing(worldIn, pos, state);
|
| 48 | }
|
| 49 |
|
| 50 | private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
|
| 51 | {
|
| 52 | if (!worldIn.isRemote)
|
| 53 | {
|
| 54 | Block block = worldIn.getBlockState(pos.north()).getBlock();
|
| 55 | Block block1 = worldIn.getBlockState(pos.south()).getBlock();
|
| 56 | Block block2 = worldIn.getBlockState(pos.west()).getBlock();
|
| 57 | Block block3 = worldIn.getBlockState(pos.east()).getBlock();
|
| 58 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 59 |
|
| 60 | if (enumfacing == EnumFacing.NORTH && block.isFullBlock() && !block1.isFullBlock())
|
| 61 | {
|
| 62 | enumfacing = EnumFacing.SOUTH;
|
| 63 | }
|
| 64 | else if (enumfacing == EnumFacing.SOUTH && block1.isFullBlock() && !block.isFullBlock())
|
| 65 | {
|
| 66 | enumfacing = EnumFacing.NORTH;
|
| 67 | }
|
| 68 | else if (enumfacing == EnumFacing.WEST && block2.isFullBlock() && !block3.isFullBlock())
|
| 69 | {
|
| 70 | enumfacing = EnumFacing.EAST;
|
| 71 | }
|
| 72 | else if (enumfacing == EnumFacing.EAST && block3.isFullBlock() && !block2.isFullBlock())
|
| 73 | {
|
| 74 | enumfacing = EnumFacing.WEST;
|
| 75 | }
|
| 76 |
|
| 77 | worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | @SuppressWarnings("incomplete-switch")
|
| 82 | public void randomDisplayTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 83 | {
|
| 84 | if (this.isBurning)
|
| 85 | {
|
| 86 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 87 | double d0 = (double)pos.getX() + 0.5D;
|
| 88 | double d1 = (double)pos.getY() + rand.nextDouble() * 6.0D / 16.0D;
|
| 89 | double d2 = (double)pos.getZ() + 0.5D;
|
| 90 | double d3 = 0.52D;
|
| 91 | double d4 = rand.nextDouble() * 0.6D - 0.3D;
|
| 92 |
|
| 93 | switch (enumfacing)
|
| 94 | {
|
| 95 | case WEST:
|
| 96 | worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 97 | worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 98 | break;
|
| 99 |
|
| 100 | case EAST:
|
| 101 | worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 102 | worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 103 | break;
|
| 104 |
|
| 105 | case NORTH:
|
| 106 | worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 107 | worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 108 | break;
|
| 109 |
|
| 110 | case SOUTH:
|
| 111 | worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 112 | worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 118 | {
|
| 119 | if (worldIn.isRemote)
|
| 120 | {
|
| 121 | return true;
|
| 122 | }
|
| 123 | else
|
| 124 | {
|
| 125 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 126 |
|
| 127 | if (tileentity instanceof TileEntityFurnace)
|
| 128 | {
|
| 129 | playerIn.displayGUIChest((TileEntityFurnace)tileentity);
|
| 130 | playerIn.triggerAchievement(StatList.field_181741_Y);
|
| 131 | }
|
| 132 |
|
| 133 | return true;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public static void setState(boolean active, World worldIn, BlockPos pos)
|
| 138 | {
|
| 139 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 140 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 141 | keepInventory = true;
|
| 142 |
|
| 143 | if (active)
|
| 144 | {
|
| 145 | worldIn.setBlockState(pos, Blocks.lit_furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
|
| 146 | worldIn.setBlockState(pos, Blocks.lit_furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
|
| 147 | }
|
| 148 | else
|
| 149 | {
|
| 150 | worldIn.setBlockState(pos, Blocks.furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
|
| 151 | worldIn.setBlockState(pos, Blocks.furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
|
| 152 | }
|
| 153 |
|
| 154 | keepInventory = false;
|
| 155 |
|
| 156 | if (tileentity != null)
|
| 157 | {
|
| 158 | tileentity.validate();
|
| 159 | worldIn.setTileEntity(pos, tileentity);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | /**
|
| 164 | * Returns a new instance of a block's tile entity class. Called on placing the block.
|
| 165 | */
|
| 166 | public TileEntity createNewTileEntity(World worldIn, int meta)
|
| 167 | {
|
| 168 | return new TileEntityFurnace();
|
| 169 | }
|
| 170 |
|
| 171 | /**
|
| 172 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 173 | * IBlockstate
|
| 174 | */
|
| 175 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 176 | {
|
| 177 | return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
|
| 178 | }
|
| 179 |
|
| 180 | /**
|
| 181 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 182 | */
|
| 183 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 184 | {
|
| 185 | worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
|
| 186 |
|
| 187 | if (stack.hasDisplayName())
|
| 188 | {
|
| 189 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 190 |
|
| 191 | if (tileentity instanceof TileEntityFurnace)
|
| 192 | {
|
| 193 | ((TileEntityFurnace)tileentity).setCustomInventoryName(stack.getDisplayName());
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 199 | {
|
| 200 | if (!keepInventory)
|
| 201 | {
|
| 202 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 203 |
|
| 204 | if (tileentity instanceof TileEntityFurnace)
|
| 205 | {
|
| 206 | InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityFurnace)tileentity);
|
| 207 | worldIn.updateComparatorOutputLevel(pos, this);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | super.breakBlock(worldIn, pos, state);
|
| 212 | }
|
| 213 |
|
| 214 | public boolean hasComparatorInputOverride()
|
| 215 | {
|
| 216 | return true;
|
| 217 | }
|
| 218 |
|
| 219 | public int getComparatorInputOverride(World worldIn, BlockPos pos)
|
| 220 | {
|
| 221 | return Container.calcRedstone(worldIn.getTileEntity(pos));
|
| 222 | }
|
| 223 |
|
| 224 | public Item getItem(World worldIn, BlockPos pos)
|
| 225 | {
|
| 226 | return Item.getItemFromBlock(Blocks.furnace);
|
| 227 | }
|
| 228 |
|
| 229 | /**
|
| 230 | * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
|
| 231 | */
|
| 232 | public int getRenderType()
|
| 233 | {
|
| 234 | return 3;
|
| 235 | }
|
| 236 |
|
| 237 | /**
|
| 238 | * Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, ...)
|
| 239 | */
|
| 240 | public IBlockState getStateForEntityRender(IBlockState state)
|
| 241 | {
|
| 242 | return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
|
| 243 | }
|
| 244 |
|
| 245 | /**
|
| 246 | * Convert the given metadata into a BlockState for this Block
|
| 247 | */
|
| 248 | public IBlockState getStateFromMeta(int meta)
|
| 249 | {
|
| 250 | EnumFacing enumfacing = EnumFacing.getFront(meta);
|
| 251 |
|
| 252 | if (enumfacing.getAxis() == EnumFacing.Axis.Y)
|
| 253 | {
|
| 254 | enumfacing = EnumFacing.NORTH;
|
| 255 | }
|
| 256 |
|
| 257 | return this.getDefaultState().withProperty(FACING, enumfacing);
|
| 258 | }
|
| 259 |
|
| 260 | /**
|
| 261 | * Convert the BlockState into the correct metadata value
|
| 262 | */
|
| 263 | public int getMetaFromState(IBlockState state)
|
| 264 | {
|
| 265 | return ((EnumFacing)state.getValue(FACING)).getIndex();
|
| 266 | }
|
| 267 |
|
| 268 | protected BlockState createBlockState()
|
| 269 | {
|
| 270 | return new BlockState(this, new IProperty[] {FACING});
|
| 271 | }
|
| 272 | }
|
| 273 |
|