| 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.PropertyBool;
|
| 7 | import net.minecraft.block.properties.PropertyDirection;
|
| 8 | import net.minecraft.block.state.BlockState;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.creativetab.CreativeTabs;
|
| 11 | import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
|
| 12 | import net.minecraft.dispenser.IBehaviorDispenseItem;
|
| 13 | import net.minecraft.dispenser.IBlockSource;
|
| 14 | import net.minecraft.dispenser.IPosition;
|
| 15 | import net.minecraft.dispenser.PositionImpl;
|
| 16 | import net.minecraft.entity.EntityLivingBase;
|
| 17 | import net.minecraft.entity.player.EntityPlayer;
|
| 18 | import net.minecraft.inventory.Container;
|
| 19 | import net.minecraft.inventory.InventoryHelper;
|
| 20 | import net.minecraft.item.Item;
|
| 21 | import net.minecraft.item.ItemStack;
|
| 22 | import net.minecraft.stats.StatList;
|
| 23 | import net.minecraft.tileentity.TileEntity;
|
| 24 | import net.minecraft.tileentity.TileEntityDispenser;
|
| 25 | import net.minecraft.tileentity.TileEntityDropper;
|
| 26 | import net.minecraft.util.BlockPos;
|
| 27 | import net.minecraft.util.EnumFacing;
|
| 28 | import net.minecraft.util.RegistryDefaulted;
|
| 29 | import net.minecraft.world.World;
|
| 30 |
|
| 31 | public class BlockDispenser extends BlockContainer
|
| 32 | {
|
| 33 | public static final PropertyDirection FACING = PropertyDirection.create("facing");
|
| 34 | public static final PropertyBool TRIGGERED = PropertyBool.create("triggered");
|
| 35 | public static final RegistryDefaulted<Item, IBehaviorDispenseItem> dispenseBehaviorRegistry = new RegistryDefaulted(new BehaviorDefaultDispenseItem());
|
| 36 | protected Random rand = new Random();
|
| 37 |
|
| 38 | protected BlockDispenser()
|
| 39 | {
|
| 40 | super(Material.rock);
|
| 41 | this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(TRIGGERED, Boolean.valueOf(false)));
|
| 42 | this.setCreativeTab(CreativeTabs.tabRedstone);
|
| 43 | }
|
| 44 |
|
| 45 | /**
|
| 46 | * How many world ticks before ticking
|
| 47 | */
|
| 48 | public int tickRate(World worldIn)
|
| 49 | {
|
| 50 | return 4;
|
| 51 | }
|
| 52 |
|
| 53 | public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
|
| 54 | {
|
| 55 | super.onBlockAdded(worldIn, pos, state);
|
| 56 | this.setDefaultDirection(worldIn, pos, state);
|
| 57 | }
|
| 58 |
|
| 59 | private void setDefaultDirection(World worldIn, BlockPos pos, IBlockState state)
|
| 60 | {
|
| 61 | if (!worldIn.isRemote)
|
| 62 | {
|
| 63 | EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
|
| 64 | boolean flag = worldIn.getBlockState(pos.north()).getBlock().isFullBlock();
|
| 65 | boolean flag1 = worldIn.getBlockState(pos.south()).getBlock().isFullBlock();
|
| 66 |
|
| 67 | if (enumfacing == EnumFacing.NORTH && flag && !flag1)
|
| 68 | {
|
| 69 | enumfacing = EnumFacing.SOUTH;
|
| 70 | }
|
| 71 | else if (enumfacing == EnumFacing.SOUTH && flag1 && !flag)
|
| 72 | {
|
| 73 | enumfacing = EnumFacing.NORTH;
|
| 74 | }
|
| 75 | else
|
| 76 | {
|
| 77 | boolean flag2 = worldIn.getBlockState(pos.west()).getBlock().isFullBlock();
|
| 78 | boolean flag3 = worldIn.getBlockState(pos.east()).getBlock().isFullBlock();
|
| 79 |
|
| 80 | if (enumfacing == EnumFacing.WEST && flag2 && !flag3)
|
| 81 | {
|
| 82 | enumfacing = EnumFacing.EAST;
|
| 83 | }
|
| 84 | else if (enumfacing == EnumFacing.EAST && flag3 && !flag2)
|
| 85 | {
|
| 86 | enumfacing = EnumFacing.WEST;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing).withProperty(TRIGGERED, Boolean.valueOf(false)), 2);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 95 | {
|
| 96 | if (worldIn.isRemote)
|
| 97 | {
|
| 98 | return true;
|
| 99 | }
|
| 100 | else
|
| 101 | {
|
| 102 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 103 |
|
| 104 | if (tileentity instanceof TileEntityDispenser)
|
| 105 | {
|
| 106 | playerIn.displayGUIChest((TileEntityDispenser)tileentity);
|
| 107 |
|
| 108 | if (tileentity instanceof TileEntityDropper)
|
| 109 | {
|
| 110 | playerIn.triggerAchievement(StatList.field_181731_O);
|
| 111 | }
|
| 112 | else
|
| 113 | {
|
| 114 | playerIn.triggerAchievement(StatList.field_181733_Q);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | return true;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | protected void dispense(World worldIn, BlockPos pos)
|
| 123 | {
|
| 124 | BlockSourceImpl blocksourceimpl = new BlockSourceImpl(worldIn, pos);
|
| 125 | TileEntityDispenser tileentitydispenser = (TileEntityDispenser)blocksourceimpl.getBlockTileEntity();
|
| 126 |
|
| 127 | if (tileentitydispenser != null)
|
| 128 | {
|
| 129 | int i = tileentitydispenser.getDispenseSlot();
|
| 130 |
|
| 131 | if (i < 0)
|
| 132 | {
|
| 133 | worldIn.playAuxSFX(1001, pos, 0);
|
| 134 | }
|
| 135 | else
|
| 136 | {
|
| 137 | ItemStack itemstack = tileentitydispenser.getStackInSlot(i);
|
| 138 | IBehaviorDispenseItem ibehaviordispenseitem = this.getBehavior(itemstack);
|
| 139 |
|
| 140 | if (ibehaviordispenseitem != IBehaviorDispenseItem.itemDispenseBehaviorProvider)
|
| 141 | {
|
| 142 | ItemStack itemstack1 = ibehaviordispenseitem.dispense(blocksourceimpl, itemstack);
|
| 143 | tileentitydispenser.setInventorySlotContents(i, itemstack1.stackSize <= 0 ? null : itemstack1);
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | protected IBehaviorDispenseItem getBehavior(ItemStack stack)
|
| 150 | {
|
| 151 | return (IBehaviorDispenseItem)dispenseBehaviorRegistry.getObject(stack == null ? null : stack.getItem());
|
| 152 | }
|
| 153 |
|
| 154 | /**
|
| 155 | * Called when a neighboring block changes.
|
| 156 | */
|
| 157 | public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
|
| 158 | {
|
| 159 | boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(pos.up());
|
| 160 | boolean flag1 = ((Boolean)state.getValue(TRIGGERED)).booleanValue();
|
| 161 |
|
| 162 | if (flag && !flag1)
|
| 163 | {
|
| 164 | worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
|
| 165 | worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(true)), 4);
|
| 166 | }
|
| 167 | else if (!flag && flag1)
|
| 168 | {
|
| 169 | worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(false)), 4);
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 174 | {
|
| 175 | if (!worldIn.isRemote)
|
| 176 | {
|
| 177 | this.dispense(worldIn, pos);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Returns a new instance of a block's tile entity class. Called on placing the block.
|
| 183 | */
|
| 184 | public TileEntity createNewTileEntity(World worldIn, int meta)
|
| 185 | {
|
| 186 | return new TileEntityDispenser();
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
|
| 191 | * IBlockstate
|
| 192 | */
|
| 193 | public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
|
| 194 | {
|
| 195 | return this.getDefaultState().withProperty(FACING, BlockPistonBase.getFacingFromEntity(worldIn, pos, placer)).withProperty(TRIGGERED, Boolean.valueOf(false));
|
| 196 | }
|
| 197 |
|
| 198 | /**
|
| 199 | * Called by ItemBlocks after a block is set in the world, to allow post-place logic
|
| 200 | */
|
| 201 | public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
|
| 202 | {
|
| 203 | worldIn.setBlockState(pos, state.withProperty(FACING, BlockPistonBase.getFacingFromEntity(worldIn, pos, placer)), 2);
|
| 204 |
|
| 205 | if (stack.hasDisplayName())
|
| 206 | {
|
| 207 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 208 |
|
| 209 | if (tileentity instanceof TileEntityDispenser)
|
| 210 | {
|
| 211 | ((TileEntityDispenser)tileentity).setCustomName(stack.getDisplayName());
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
|
| 217 | {
|
| 218 | TileEntity tileentity = worldIn.getTileEntity(pos);
|
| 219 |
|
| 220 | if (tileentity instanceof TileEntityDispenser)
|
| 221 | {
|
| 222 | InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityDispenser)tileentity);
|
| 223 | worldIn.updateComparatorOutputLevel(pos, this);
|
| 224 | }
|
| 225 |
|
| 226 | super.breakBlock(worldIn, pos, state);
|
| 227 | }
|
| 228 |
|
| 229 | /**
|
| 230 | * Get the position where the dispenser at the given Coordinates should dispense to.
|
| 231 | */
|
| 232 | public static IPosition getDispensePosition(IBlockSource coords)
|
| 233 | {
|
| 234 | EnumFacing enumfacing = getFacing(coords.getBlockMetadata());
|
| 235 | double d0 = coords.getX() + 0.7D * (double)enumfacing.getFrontOffsetX();
|
| 236 | double d1 = coords.getY() + 0.7D * (double)enumfacing.getFrontOffsetY();
|
| 237 | double d2 = coords.getZ() + 0.7D * (double)enumfacing.getFrontOffsetZ();
|
| 238 | return new PositionImpl(d0, d1, d2);
|
| 239 | }
|
| 240 |
|
| 241 | /**
|
| 242 | * Get the facing of a dispenser with the given metadata
|
| 243 | */
|
| 244 | public static EnumFacing getFacing(int meta)
|
| 245 | {
|
| 246 | return EnumFacing.getFront(meta & 7);
|
| 247 | }
|
| 248 |
|
| 249 | public boolean hasComparatorInputOverride()
|
| 250 | {
|
| 251 | return true;
|
| 252 | }
|
| 253 |
|
| 254 | public int getComparatorInputOverride(World worldIn, BlockPos pos)
|
| 255 | {
|
| 256 | return Container.calcRedstone(worldIn.getTileEntity(pos));
|
| 257 | }
|
| 258 |
|
| 259 | /**
|
| 260 | * The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
|
| 261 | */
|
| 262 | public int getRenderType()
|
| 263 | {
|
| 264 | return 3;
|
| 265 | }
|
| 266 |
|
| 267 | /**
|
| 268 | * Possibly modify the given BlockState before rendering it on an Entity (Minecarts, Endermen, ...)
|
| 269 | */
|
| 270 | public IBlockState getStateForEntityRender(IBlockState state)
|
| 271 | {
|
| 272 | return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
|
| 273 | }
|
| 274 |
|
| 275 | /**
|
| 276 | * Convert the given metadata into a BlockState for this Block
|
| 277 | */
|
| 278 | public IBlockState getStateFromMeta(int meta)
|
| 279 | {
|
| 280 | return this.getDefaultState().withProperty(FACING, getFacing(meta)).withProperty(TRIGGERED, Boolean.valueOf((meta & 8) > 0));
|
| 281 | }
|
| 282 |
|
| 283 | /**
|
| 284 | * Convert the BlockState into the correct metadata value
|
| 285 | */
|
| 286 | public int getMetaFromState(IBlockState state)
|
| 287 | {
|
| 288 | int i = 0;
|
| 289 | i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
|
| 290 |
|
| 291 | if (((Boolean)state.getValue(TRIGGERED)).booleanValue())
|
| 292 | {
|
| 293 | i |= 8;
|
| 294 | }
|
| 295 |
|
| 296 | return i;
|
| 297 | }
|
| 298 |
|
| 299 | protected BlockState createBlockState()
|
| 300 | {
|
| 301 | return new BlockState(this, new IProperty[] {FACING, TRIGGERED});
|
| 302 | }
|
| 303 | }
|
| 304 |
|