| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import net.minecraft.block.BlockDispenser;
|
| 4 | import net.minecraft.block.BlockRailBase;
|
| 5 | import net.minecraft.block.material.Material;
|
| 6 | import net.minecraft.block.state.IBlockState;
|
| 7 | import net.minecraft.creativetab.CreativeTabs;
|
| 8 | import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
|
| 9 | import net.minecraft.dispenser.IBehaviorDispenseItem;
|
| 10 | import net.minecraft.dispenser.IBlockSource;
|
| 11 | import net.minecraft.entity.item.EntityMinecart;
|
| 12 | import net.minecraft.entity.player.EntityPlayer;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.util.EnumFacing;
|
| 15 | import net.minecraft.world.World;
|
| 16 |
|
| 17 | public class ItemMinecart extends Item
|
| 18 | {
|
| 19 | private static final IBehaviorDispenseItem dispenserMinecartBehavior = new BehaviorDefaultDispenseItem()
|
| 20 | {
|
| 21 | private final BehaviorDefaultDispenseItem behaviourDefaultDispenseItem = new BehaviorDefaultDispenseItem();
|
| 22 | public ItemStack dispenseStack(IBlockSource source, ItemStack stack)
|
| 23 | {
|
| 24 | EnumFacing enumfacing = BlockDispenser.getFacing(source.getBlockMetadata());
|
| 25 | World world = source.getWorld();
|
| 26 | double d0 = source.getX() + (double)enumfacing.getFrontOffsetX() * 1.125D;
|
| 27 | double d1 = Math.floor(source.getY()) + (double)enumfacing.getFrontOffsetY();
|
| 28 | double d2 = source.getZ() + (double)enumfacing.getFrontOffsetZ() * 1.125D;
|
| 29 | BlockPos blockpos = source.getBlockPos().offset(enumfacing);
|
| 30 | IBlockState iblockstate = world.getBlockState(blockpos);
|
| 31 | BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = iblockstate.getBlock() instanceof BlockRailBase ? (BlockRailBase.EnumRailDirection)iblockstate.getValue(((BlockRailBase)iblockstate.getBlock()).getShapeProperty()) : BlockRailBase.EnumRailDirection.NORTH_SOUTH;
|
| 32 | double d3;
|
| 33 |
|
| 34 | if (BlockRailBase.isRailBlock(iblockstate))
|
| 35 | {
|
| 36 | if (blockrailbase$enumraildirection.isAscending())
|
| 37 | {
|
| 38 | d3 = 0.6D;
|
| 39 | }
|
| 40 | else
|
| 41 | {
|
| 42 | d3 = 0.1D;
|
| 43 | }
|
| 44 | }
|
| 45 | else
|
| 46 | {
|
| 47 | if (iblockstate.getBlock().getMaterial() != Material.air || !BlockRailBase.isRailBlock(world.getBlockState(blockpos.down())))
|
| 48 | {
|
| 49 | return this.behaviourDefaultDispenseItem.dispense(source, stack);
|
| 50 | }
|
| 51 |
|
| 52 | IBlockState iblockstate1 = world.getBlockState(blockpos.down());
|
| 53 | BlockRailBase.EnumRailDirection blockrailbase$enumraildirection1 = iblockstate1.getBlock() instanceof BlockRailBase ? (BlockRailBase.EnumRailDirection)iblockstate1.getValue(((BlockRailBase)iblockstate1.getBlock()).getShapeProperty()) : BlockRailBase.EnumRailDirection.NORTH_SOUTH;
|
| 54 |
|
| 55 | if (enumfacing != EnumFacing.DOWN && blockrailbase$enumraildirection1.isAscending())
|
| 56 | {
|
| 57 | d3 = -0.4D;
|
| 58 | }
|
| 59 | else
|
| 60 | {
|
| 61 | d3 = -0.9D;
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | EntityMinecart entityminecart = EntityMinecart.getMinecart(world, d0, d1 + d3, d2, ((ItemMinecart)stack.getItem()).minecartType);
|
| 66 |
|
| 67 | if (stack.hasDisplayName())
|
| 68 | {
|
| 69 | entityminecart.setCustomNameTag(stack.getDisplayName());
|
| 70 | }
|
| 71 |
|
| 72 | world.spawnEntityInWorld(entityminecart);
|
| 73 | stack.splitStack(1);
|
| 74 | return stack;
|
| 75 | }
|
| 76 | protected void playDispenseSound(IBlockSource source)
|
| 77 | {
|
| 78 | source.getWorld().playAuxSFX(1000, source.getBlockPos(), 0);
|
| 79 | }
|
| 80 | };
|
| 81 | private final EntityMinecart.EnumMinecartType minecartType;
|
| 82 |
|
| 83 | public ItemMinecart(EntityMinecart.EnumMinecartType type)
|
| 84 | {
|
| 85 | this.maxStackSize = 1;
|
| 86 | this.minecartType = type;
|
| 87 | this.setCreativeTab(CreativeTabs.tabTransport);
|
| 88 | BlockDispenser.dispenseBehaviorRegistry.putObject(this, dispenserMinecartBehavior);
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * Called when a Block is right-clicked with this Item
|
| 93 | */
|
| 94 | public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
|
| 95 | {
|
| 96 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 97 |
|
| 98 | if (BlockRailBase.isRailBlock(iblockstate))
|
| 99 | {
|
| 100 | if (!worldIn.isRemote)
|
| 101 | {
|
| 102 | BlockRailBase.EnumRailDirection blockrailbase$enumraildirection = iblockstate.getBlock() instanceof BlockRailBase ? (BlockRailBase.EnumRailDirection)iblockstate.getValue(((BlockRailBase)iblockstate.getBlock()).getShapeProperty()) : BlockRailBase.EnumRailDirection.NORTH_SOUTH;
|
| 103 | double d0 = 0.0D;
|
| 104 |
|
| 105 | if (blockrailbase$enumraildirection.isAscending())
|
| 106 | {
|
| 107 | d0 = 0.5D;
|
| 108 | }
|
| 109 |
|
| 110 | EntityMinecart entityminecart = EntityMinecart.getMinecart(worldIn, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.0625D + d0, (double)pos.getZ() + 0.5D, this.minecartType);
|
| 111 |
|
| 112 | if (stack.hasDisplayName())
|
| 113 | {
|
| 114 | entityminecart.setCustomNameTag(stack.getDisplayName());
|
| 115 | }
|
| 116 |
|
| 117 | worldIn.spawnEntityInWorld(entityminecart);
|
| 118 | }
|
| 119 |
|
| 120 | --stack.stackSize;
|
| 121 | return true;
|
| 122 | }
|
| 123 | else
|
| 124 | {
|
| 125 | return false;
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|