Guest User

ArrowTrap.java (latest 4/29/2019)

a guest
Apr 30th, 2019
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ladestitute.spelunkcraft.blocks.traps;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5. import javax.annotation.Nonnull;
  6. import ladestitute.spelunkcraft.Main;
  7. import ladestitute.spelunkcraft.util.ModReference;
  8. import ladestitute.spelunkcraft.util.ModUtil;
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.BlockDirectional;
  11. import net.minecraft.block.BlockSourceImpl;
  12. import net.minecraft.block.SoundType;
  13. import net.minecraft.block.material.Material;
  14. import net.minecraft.block.properties.IProperty;
  15. import net.minecraft.block.properties.PropertyBool;
  16. import net.minecraft.block.properties.PropertyDirection;
  17. import net.minecraft.block.state.BlockStateContainer;
  18. import net.minecraft.block.state.IBlockState;
  19. import net.minecraft.client.util.ITooltipFlag;
  20. import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
  21. import net.minecraft.dispenser.IBehaviorDispenseItem;
  22. import net.minecraft.dispenser.IBlockSource;
  23. import net.minecraft.dispenser.IPosition;
  24. import net.minecraft.dispenser.PositionImpl;
  25. import net.minecraft.entity.EntityLivingBase;
  26. import net.minecraft.entity.player.EntityPlayer;
  27. import net.minecraft.inventory.Container;
  28. import net.minecraft.inventory.InventoryHelper;
  29. import net.minecraft.item.Item;
  30. import net.minecraft.item.ItemStack;
  31. import net.minecraft.tileentity.TileEntity;
  32. import net.minecraft.tileentity.TileEntityDispenser;
  33. import net.minecraft.util.EnumBlockRenderType;
  34. import net.minecraft.util.EnumFacing;
  35. import net.minecraft.util.EnumHand;
  36. import net.minecraft.util.Mirror;
  37. import net.minecraft.util.ITickable;
  38. import net.minecraft.util.Rotation;
  39. import net.minecraft.util.math.BlockPos;
  40. import net.minecraft.util.registry.RegistryDefaulted;
  41. import net.minecraft.world.World;
  42.  
  43. public class ArrowTrap extends Block implements ITickable
  44.  
  45. {
  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.     public int firingrange = 5;
  54.     public int cooldown = 0;
  55.  
  56.     public ArrowTrap(@Nonnull final String name)
  57.     {
  58.         super(Material.ROCK);
  59.         ModUtil.setRegistryNames(this, name);
  60.         this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(TRIGGERED, Boolean.valueOf(false)));
  61.         setSoundType(SoundType.STONE);
  62.         setHardness(50F);
  63.         setResistance(0.0F);
  64.         setHarvestLevel("pickaxe", 3);
  65.         setCreativeTab(Main.TRAPS_TAB);
  66.     }
  67.    
  68.     //BASIC FUNCTIONALITY STARTS HERE
  69.    
  70.     @Override
  71.     public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
  72.         tooltip.add("A dangerous mechanical trap loaded with arrows");
  73.         super.addInformation(stack, worldIn, tooltip, flagIn);
  74.     }
  75.    
  76.     public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
  77.         if (!world.isRemote) { 
  78.             if (player.isSneaking()) {     
  79.             } else
  80.             {
  81.                 //Open the GUI if the player is not sneaking
  82.                 //We are passing our mod_id, the x/y/z pos and the int that GuiHandler needs (so it knows which gui to open)
  83.                 player.openGui(ModReference.MOD_ID, 0, world, pos.getX(), pos.getY(), pos.getZ());
  84.             }
  85.         }
  86.         return true;
  87.     }
  88.    
  89.     @Override
  90.     public boolean hasTileEntity(IBlockState state)
  91.     {
  92.         return true;
  93.     }
  94.    
  95.     @Override
  96.     public TileEntity createTileEntity(World world, IBlockState state)
  97.     {
  98.         return new TileEntityDispenser();
  99.     }
  100.    
  101.     //BASIC FUNCTIONALITY ENDS HERE
  102.    
  103.     //Something to go here...soon? Don't mind commented code, just screwing about
  104.     @Override
  105.     public void update()
  106.     {}
  107.        
  108.     //public void paintSheeps(World worldIn, BlockPos pos)
  109.     //{
  110.         //double x = this.pos.getX();
  111.         //double y = this.pos.getY();
  112.         //double z = this.pos.getZ();
  113.        
  114.         //BlockPos newPos = pos.offset(EnumFacing.SOUTH, 1); // Gets the block position below the totem
  115.         //IBlockState state = this.World.get; // Gets the block state of the newPos
  116.         //Block block = state.getBlock(); // Gets the block of the newPos
  117.         //EnumDyeColor color = null;
  118.        
  119.         //AxisAlignedBB axisalignedbb = new AxisAlignedBB(pos.getX(), pos.getY(), pos.getZ(), pos.getX(), pos.getY(), pos.getZ());
  120.         //List<EntitySheep> list = worldIn.<EntitySheep>getEntitiesWithinAABB(EntitySheep.class, axisalignedbb.grow(4.0D, 4.0D, 7.0D));
  121.         //for (EntitySheep entitysheep : list)
  122.         //{
  123.         //  worldIn.createExplosion(null, pos.getX(), pos.getY(), pos.getZ(), 1F, false);
  124.        // }
  125.    // }
  126.    
  127.     //DISPENSING FUNCTIONALITY STARTS HERE
  128.    
  129.     public int tickRate(World worldIn)
  130.     {
  131.         return 1;
  132.     }
  133.    
  134.     public void dispense(World worldIn, BlockPos pos)
  135.     {
  136.         BlockSourceImpl blocksourceimpl = new BlockSourceImpl(worldIn, pos);
  137.         TileEntityDispenser tileentitydispenser = (TileEntityDispenser)blocksourceimpl.getBlockTileEntity();
  138.  
  139.         if (tileentitydispenser != null)
  140.         {
  141.             int i = tileentitydispenser.getDispenseSlot();
  142.  
  143.             if (i < 0)
  144.             {
  145.                 worldIn.playEvent(1001, pos, 0);
  146.             }
  147.             else
  148.             {
  149.                 ItemStack itemstack = tileentitydispenser.getStackInSlot(i);
  150.                 IBehaviorDispenseItem ibehaviordispenseitem = this.getBehavior(itemstack);
  151.  
  152.                 if (ibehaviordispenseitem != IBehaviorDispenseItem.DEFAULT_BEHAVIOR)
  153.                 {
  154.                     tileentitydispenser.setInventorySlotContents(i, ibehaviordispenseitem.dispense(blocksourceimpl, itemstack));
  155.                 }
  156.             }
  157.         }
  158.     }
  159.    
  160.     protected IBehaviorDispenseItem getBehavior(ItemStack stack)
  161.     {
  162.         return DISPENSE_BEHAVIOR_REGISTRY.getObject(stack.getItem());
  163.     }
  164.    
  165.     public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
  166.     {
  167.         boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(pos.up());
  168.         boolean flag1 = ((Boolean)state.getValue(TRIGGERED)).booleanValue();
  169.  
  170.         if (flag && !flag1)
  171.         {
  172.             worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
  173.             worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(true)), 4);
  174.         }
  175.         else if (!flag && flag1)
  176.         {
  177.             worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(false)), 4);
  178.         }
  179.     }
  180.  
  181.     public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
  182.     {
  183.         if (!worldIn.isRemote)
  184.         {
  185.             this.dispense(worldIn, pos);
  186.         }
  187.     }
  188.    
  189.     //Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated
  190.     //Changing this will cause drops from the block to break
  191.     public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  192.     {
  193.         TileEntity tileentity = worldIn.getTileEntity(pos);
  194.  
  195.         if (tileentity instanceof TileEntityDispenser)
  196.         {
  197.             InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityDispenser)tileentity);
  198.             worldIn.updateComparatorOutputLevel(pos, this);
  199.         }
  200.  
  201.         super.breakBlock(worldIn, pos, state);
  202.     }
  203.    
  204.     public static IPosition getDispensePosition(IBlockSource coords)
  205.     {
  206.         EnumFacing enumfacing = (EnumFacing)coords.getBlockState().getValue(FACING);
  207.         double d0 = coords.getX() + 0.7D * (double)enumfacing.getXOffset();
  208.         double d1 = coords.getY() + 0.7D * (double)enumfacing.getYOffset();
  209.         double d2 = coords.getZ() + 0.7D * (double)enumfacing.getZOffset();
  210.         return new PositionImpl(d0, d1, d2);
  211.     }
  212.    
  213.     //Only overriding for now, will replace with proper method for this later
  214.     @Override
  215.     public boolean hasComparatorInputOverride(IBlockState state)
  216.     {
  217.         return true;
  218.     }
  219.  
  220.     //Only overriding for now, will replace with proper method for this later
  221.     @Override
  222.     public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
  223.     {
  224.         return Container.calcRedstone(worldIn.getTileEntity(pos));
  225.     }
  226.    
  227.     //DISPENSING FUNCTIONALITY ENDS HERE
  228.     //FACING FUNCTIONALITY STARTS HERE
  229.  
  230.     public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
  231.     {
  232.         super.onBlockAdded(worldIn, pos, state);
  233.         this.setDefaultDirection(worldIn, pos, state);
  234.     }
  235.  
  236.     private void setDefaultDirection(World worldIn, BlockPos pos, IBlockState state)
  237.     {
  238.         if (!worldIn.isRemote)
  239.         {
  240.             EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
  241.             boolean flag = worldIn.getBlockState(pos.north()).isFullBlock();
  242.             boolean flag1 = worldIn.getBlockState(pos.south()).isFullBlock();
  243.  
  244.             if (enumfacing == EnumFacing.NORTH && flag && !flag1)
  245.             {
  246.                 enumfacing = EnumFacing.SOUTH;
  247.             }
  248.             else if (enumfacing == EnumFacing.SOUTH && flag1 && !flag)
  249.             {
  250.                 enumfacing = EnumFacing.NORTH;
  251.             }
  252.             else
  253.             {
  254.                 boolean flag2 = worldIn.getBlockState(pos.west()).isFullBlock();
  255.                 boolean flag3 = worldIn.getBlockState(pos.east()).isFullBlock();
  256.  
  257.                 if (enumfacing == EnumFacing.WEST && flag2 && !flag3)
  258.                 {
  259.                     enumfacing = EnumFacing.EAST;
  260.                 }
  261.                 else if (enumfacing == EnumFacing.EAST && flag3 && !flag2)
  262.                 {
  263.                     enumfacing = EnumFacing.WEST;
  264.                 }
  265.             }
  266.  
  267.             worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing).withProperty(TRIGGERED, Boolean.valueOf(false)), 2);
  268.         }
  269.     }
  270.  
  271.     public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
  272.     {
  273.         return this.getDefaultState().withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)).withProperty(TRIGGERED, Boolean.valueOf(false));
  274.     }
  275.  
  276.     public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
  277.     {
  278.         worldIn.setBlockState(pos, state.withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)), 2);
  279.     }
  280.  
  281.     //Only overriding for now, will replace with proper method for this later
  282.     @Override
  283.     public EnumBlockRenderType getRenderType(IBlockState state)
  284.     {
  285.         return EnumBlockRenderType.MODEL;
  286.     }
  287.  
  288.     /**
  289.      * Convert the given metadata into a BlockState for this Block
  290.      */
  291.     public IBlockState getStateFromMeta(int meta)
  292.     {
  293.         return this.getDefaultState().withProperty(FACING, EnumFacing.byIndex(meta & 7)).withProperty(TRIGGERED, Boolean.valueOf((meta & 8) > 0));
  294.     }
  295.  
  296.     /**
  297.      * Convert the BlockState into the correct metadata value
  298.      */
  299.     public int getMetaFromState(IBlockState state)
  300.     {
  301.         int i = 0;
  302.         i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
  303.  
  304.         if (((Boolean)state.getValue(TRIGGERED)).booleanValue())
  305.         {
  306.             i |= 8;
  307.         }
  308.  
  309.         return i;
  310.     }
  311.  
  312.     //Only overriding for now, will replace with proper method for this later
  313.     @Override
  314.     public IBlockState withRotation(IBlockState state, Rotation rot)
  315.     {
  316.         return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
  317.     }
  318.  
  319.     //Only overriding for now, will replace with proper method for this later
  320.     @Override
  321.     public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
  322.     {
  323.         return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
  324.     }
  325.  
  326.     protected BlockStateContainer createBlockState()
  327.     {
  328.         return new BlockStateContainer(this, new IProperty[] {FACING, TRIGGERED});
  329.     }
  330. }
RAW Paste Data Copied