Advertisement
Guest User

Arrow Trap code

a guest
Apr 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.51 KB | None | 0 0
  1. public class ArrowTrap extends ContainerBase
  2. {
  3.    
  4.     //WIP code
  5.    
  6.     public static final PropertyDirection FACING = BlockDirectional.FACING;
  7.     public static final PropertyBool TRIGGERED = PropertyBool.create("triggered");
  8.     /** Registry for all dispense behaviors. */
  9.     public static final RegistryDefaulted<Item, IBehaviorDispenseItem> DISPENSE_BEHAVIOR_REGISTRY = new RegistryDefaulted<Item, IBehaviorDispenseItem>(new BehaviorDefaultDispenseItem());
  10.     protected Random rand = new Random();
  11.  
  12.     public ArrowTrap(String name, Material material)
  13.     {
  14.         super(name, Material.ROCK);
  15.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
  16.         setSoundType(SoundType.STONE);
  17.         setHardness(50F);
  18.         setResistance(0.0F);
  19.         setHarvestLevel("pickaxe", 3);
  20.         setCreativeTab(Main.TRAPS_TAB);
  21.     }
  22.  
  23.     protected static final AxisAlignedBB TORCH_NORTH_AABB = new AxisAlignedBB(1D, 1D, 1D, 1D, 1D, -7D);
  24.     protected static final AxisAlignedBB TORCH_SOUTH_AABB = new AxisAlignedBB(1D, 1D, 1D, 1D, 1D, 7D);
  25.     protected static final AxisAlignedBB TORCH_WEST_AABB = new AxisAlignedBB(1D, 1D, 1D, -7D, 1D, 1D);
  26.     protected static final AxisAlignedBB TORCH_EAST_AABB = new AxisAlignedBB(1D, 1D, 1D, 7D, 1D, 1D);
  27.    
  28.     public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  29.     {
  30.         switch ((EnumFacing)state.getValue(FACING))
  31.         {
  32.             case EAST:
  33.                 return TORCH_EAST_AABB;
  34.             case WEST:
  35.                 return TORCH_WEST_AABB;
  36.             case SOUTH:
  37.                 return TORCH_SOUTH_AABB;
  38.             case NORTH:
  39.                 return TORCH_NORTH_AABB;
  40.             default:
  41.                 return TORCH_NORTH_AABB;
  42.         }
  43.     }
  44.    
  45.     public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB selectedboundingbox,
  46.             Entity entityIn) {
  47.         worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(true)));
  48.     }
  49.    
  50.     //Below is dispenser code, haven't messed with much of it yet
  51.  
  52.     /**
  53.      * How many world ticks before ticking
  54.      */
  55.     public int tickRate(World worldIn)
  56.     {
  57.         return 4;
  58.     }
  59.  
  60.     /**
  61.      * Called after the block is set in the Chunk data, but before the Tile Entity is set
  62.      */
  63.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  64.     {
  65.         super.onBlockAdded(worldIn, pos, state);
  66.         this.setDefaultDirection(worldIn, pos, state);
  67.     }
  68.  
  69.     private void setDefaultDirection(World worldIn, BlockPos pos, IBlockState state)
  70.     {
  71.         if (!worldIn.isRemote)
  72.         {
  73.             EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  74.             boolean flag = worldIn.getBlockState(pos.north()).isFullBlock();
  75.             boolean flag1 = worldIn.getBlockState(pos.south()).isFullBlock();
  76.  
  77.             if (enumfacing == EnumFacing.NORTH && flag && !flag1)
  78.             {
  79.                 enumfacing = EnumFacing.SOUTH;
  80.             }
  81.             else if (enumfacing == EnumFacing.SOUTH && flag1 && !flag)
  82.             {
  83.                 enumfacing = EnumFacing.NORTH;
  84.             }
  85.             else
  86.             {
  87.                 boolean flag2 = worldIn.getBlockState(pos.west()).isFullBlock();
  88.                 boolean flag3 = worldIn.getBlockState(pos.east()).isFullBlock();
  89.  
  90.                 if (enumfacing == EnumFacing.WEST && flag2 && !flag3)
  91.                 {
  92.                     enumfacing = EnumFacing.EAST;
  93.                 }
  94.                 else if (enumfacing == EnumFacing.EAST && flag3 && !flag2)
  95.                 {
  96.                     enumfacing = EnumFacing.WEST;
  97.                 }
  98.             }
  99.  
  100.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing).withProperty(TRIGGERED, Boolean.valueOf(false)), 2);
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * Called when the block is right clicked by a player.
  106.      */
  107.     public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
  108.     {
  109.         if (worldIn.isRemote)
  110.         {
  111.             return true;
  112.         }
  113.         else
  114.         {
  115.             TileEntity tileentity = worldIn.getTileEntity(pos);
  116.  
  117.             if (tileentity instanceof TileEntityDispenser)
  118.             {
  119.                 playerIn.displayGUIChest((TileEntityDispenser)tileentity);
  120.  
  121.                 if (tileentity instanceof TileEntityDropper)
  122.                 {
  123.                     playerIn.addStat(StatList.DROPPER_INSPECTED);
  124.                 }
  125.                 else
  126.                 {
  127.                     playerIn.addStat(StatList.DISPENSER_INSPECTED);
  128.                 }
  129.             }
  130.  
  131.             return true;
  132.         }
  133.     }
  134.  
  135.     protected void dispense(World worldIn, BlockPos pos)
  136.     {
  137.         BlockSourceImpl blocksourceimpl = new BlockSourceImpl(worldIn, pos);
  138.         TileEntityDispenser tileentitydispenser = (TileEntityDispenser)blocksourceimpl.getBlockTileEntity();
  139.  
  140.         if (tileentitydispenser != null)
  141.         {
  142.             int i = tileentitydispenser.getDispenseSlot();
  143.  
  144.             if (i < 0)
  145.             {
  146.                 worldIn.playEvent(1001, pos, 0);
  147.             }
  148.             else
  149.             {
  150.                 ItemStack itemstack = tileentitydispenser.getStackInSlot(i);
  151.                 IBehaviorDispenseItem ibehaviordispenseitem = this.getBehavior(itemstack);
  152.  
  153.                 if (ibehaviordispenseitem != IBehaviorDispenseItem.DEFAULT_BEHAVIOR)
  154.                 {
  155.                     tileentitydispenser.setInventorySlotContents(i, ibehaviordispenseitem.dispense(blocksourceimpl, itemstack));
  156.                 }
  157.             }
  158.         }
  159.     }
  160.  
  161.     protected IBehaviorDispenseItem getBehavior(ItemStack stack)
  162.     {
  163.         return DISPENSE_BEHAVIOR_REGISTRY.getObject(stack.getItem());
  164.     }
  165.  
  166.     /**
  167.      * Called when a neighboring block was changed and marks that this state should perform any checks during a neighbor
  168.      * change. Cases may include when redstone power is updated, cactus blocks popping off due to a neighboring solid
  169.      * block, etc.
  170.      */
  171.     public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
  172.     {
  173.         boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(pos.up());
  174.         boolean flag1 = ((Boolean)state.getValue(TRIGGERED)).booleanValue();
  175.  
  176.         if (flag && !flag1)
  177.         {
  178.             worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
  179.             worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(true)), 4);
  180.         }
  181.         else if (!flag && flag1)
  182.         {
  183.             worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(false)), 4);
  184.         }
  185.     }
  186.  
  187.     public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
  188.     {
  189.         if (!worldIn.isRemote)
  190.         {
  191.             this.dispense(worldIn, pos);
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * Returns a new instance of a block's tile entity class. Called on placing the block.
  197.      */
  198.     public TileEntity createNewTileEntity(World worldIn, int meta)
  199.     {
  200.         return new TileEntityDispenser();
  201.     }
  202.  
  203.     /**
  204.      * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
  205.      * IBlockstate
  206.      */
  207.     public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  208.     {
  209.         return this.getDefaultState().withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)).withProperty(TRIGGERED, Boolean.valueOf(false));
  210.     }
  211.  
  212.     /**
  213.      * Called by ItemBlocks after a block is set in the world, to allow post-place logic
  214.      */
  215.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  216.     {
  217.         worldIn.setBlockState(pos, state.withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)), 2);
  218.  
  219.         if (stack.hasDisplayName())
  220.         {
  221.             TileEntity tileentity = worldIn.getTileEntity(pos);
  222.  
  223.             if (tileentity instanceof TileEntityDispenser)
  224.             {
  225.                 ((TileEntityDispenser)tileentity).setCustomName(stack.getDisplayName());
  226.             }
  227.         }
  228.     }
  229.  
  230.     /**
  231.      * Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated
  232.      */
  233.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  234.     {
  235.         TileEntity tileentity = worldIn.getTileEntity(pos);
  236.  
  237.         if (tileentity instanceof TileEntityDispenser)
  238.         {
  239.             InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityDispenser)tileentity);
  240.             worldIn.updateComparatorOutputLevel(pos, this);
  241.         }
  242.  
  243.         super.breakBlock(worldIn, pos, state);
  244.     }
  245.  
  246.     /**
  247.      * Get the position where the dispenser at the given Coordinates should dispense to.
  248.      */
  249.     public static IPosition getDispensePosition(IBlockSource coords)
  250.     {
  251.         EnumFacing enumfacing = (EnumFacing)coords.getBlockState().getValue(FACING);
  252.         double d0 = coords.getX() + 0.7D * (double)enumfacing.getFrontOffsetX();
  253.         double d1 = coords.getY() + 0.7D * (double)enumfacing.getFrontOffsetY();
  254.         double d2 = coords.getZ() + 0.7D * (double)enumfacing.getFrontOffsetZ();
  255.         return new PositionImpl(d0, d1, d2);
  256.     }
  257.  
  258.     public boolean hasComparatorInputOverride(IBlockState state)
  259.     {
  260.         return true;
  261.     }
  262.  
  263.     public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
  264.     {
  265.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  266.     }
  267.  
  268.     /**
  269.      * The type of render function called. MODEL for mixed tesr and static model, MODELBLOCK_ANIMATED for TESR-only,
  270.      * LIQUID for vanilla liquids, INVISIBLE to skip all rendering
  271.      */
  272.     public EnumBlockRenderType getRenderType(IBlockState state)
  273.     {
  274.         return EnumBlockRenderType.MODEL;
  275.     }
  276.  
  277.     /**
  278.      * Convert the given metadata into a BlockState for this Block
  279.      */
  280.     public IBlockState getStateFromMeta(int meta)
  281.     {
  282.         return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7)).withProperty(TRIGGERED, Boolean.valueOf((meta & 8) > 0));
  283.     }
  284.  
  285.     /**
  286.      * Convert the BlockState into the correct metadata value
  287.      */
  288.     public int getMetaFromState(IBlockState state)
  289.     {
  290.         int i = 0;
  291.         i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
  292.  
  293.         if (((Boolean)state.getValue(TRIGGERED)).booleanValue())
  294.         {
  295.             i |= 8;
  296.         }
  297.  
  298.         return i;
  299.     }
  300.  
  301.     /**
  302.      * Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
  303.      * blockstate.
  304.      */
  305.     public IBlockState withRotation(IBlockState state, Rotation rot)
  306.     {
  307.         return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  308.     }
  309.  
  310.     /**
  311.      * Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
  312.      * blockstate.
  313.      */
  314.     public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  315.     {
  316.         return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
  317.     }
  318.  
  319.     protected BlockStateContainer createBlockState()
  320.     {
  321.         return new BlockStateContainer(this, new IProperty[] {FACING, TRIGGERED});
  322.     }
  323.    
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement