Advertisement
Guest User

ArrowTrap.java

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