Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ladestitute.spelunkcraft.blocks.traps;
- import java.util.List;
- import java.util.Random;
- import javax.annotation.Nonnull;
- import ladestitute.spelunkcraft.Main;
- import ladestitute.spelunkcraft.util.ModReference;
- import ladestitute.spelunkcraft.util.ModUtil;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockDirectional;
- import net.minecraft.block.BlockSourceImpl;
- import net.minecraft.block.SoundType;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.properties.IProperty;
- import net.minecraft.block.properties.PropertyBool;
- import net.minecraft.block.properties.PropertyDirection;
- import net.minecraft.block.state.BlockStateContainer;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.client.util.ITooltipFlag;
- import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
- import net.minecraft.dispenser.IBehaviorDispenseItem;
- import net.minecraft.dispenser.IBlockSource;
- import net.minecraft.dispenser.IPosition;
- import net.minecraft.dispenser.PositionImpl;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.InventoryHelper;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.TileEntityDispenser;
- import net.minecraft.util.EnumBlockRenderType;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.Mirror;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.Rotation;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.registry.RegistryDefaulted;
- import net.minecraft.world.World;
- public class ArrowTrap extends Block implements ITickable
- {
- public static final PropertyDirection FACING = BlockDirectional.FACING;
- public static final PropertyBool TRIGGERED = PropertyBool.create("triggered");
- /** Registry for all dispense behaviors. */
- public static final RegistryDefaulted<Item, IBehaviorDispenseItem> DISPENSE_BEHAVIOR_REGISTRY = new RegistryDefaulted<Item, IBehaviorDispenseItem>(new BehaviorDefaultDispenseItem());
- public static Object dispenseBehaviorRegistry;
- protected Random rand = new Random();
- public int firingrange = 5;
- public int cooldown = 0;
- public ArrowTrap(@Nonnull final String name)
- {
- super(Material.ROCK);
- ModUtil.setRegistryNames(this, name);
- this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(TRIGGERED, Boolean.valueOf(false)));
- setSoundType(SoundType.STONE);
- setHardness(50F);
- setResistance(0.0F);
- setHarvestLevel("pickaxe", 3);
- setCreativeTab(Main.TRAPS_TAB);
- }
- //BASIC FUNCTIONALITY STARTS HERE
- @Override
- public void addInformation(ItemStack stack, World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
- tooltip.add("A dangerous mechanical trap loaded with arrows");
- super.addInformation(stack, worldIn, tooltip, flagIn);
- }
- public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
- if (!world.isRemote) {
- if (player.isSneaking()) {
- } else
- {
- //Open the GUI if the player is not sneaking
- //We are passing our mod_id, the x/y/z pos and the int that GuiHandler needs (so it knows which gui to open)
- player.openGui(ModReference.MOD_ID, 0, world, pos.getX(), pos.getY(), pos.getZ());
- }
- }
- return true;
- }
- @Override
- public boolean hasTileEntity(IBlockState state)
- {
- return true;
- }
- @Override
- public TileEntity createTileEntity(World world, IBlockState state)
- {
- return new TileEntityDispenser();
- }
- //BASIC FUNCTIONALITY ENDS HERE
- //Something to go here...soon? Don't mind commented code, just screwing about
- @Override
- public void update()
- {}
- //public void paintSheeps(World worldIn, BlockPos pos)
- //{
- //double x = this.pos.getX();
- //double y = this.pos.getY();
- //double z = this.pos.getZ();
- //BlockPos newPos = pos.offset(EnumFacing.SOUTH, 1); // Gets the block position below the totem
- //IBlockState state = this.World.get; // Gets the block state of the newPos
- //Block block = state.getBlock(); // Gets the block of the newPos
- //EnumDyeColor color = null;
- //AxisAlignedBB axisalignedbb = new AxisAlignedBB(pos.getX(), pos.getY(), pos.getZ(), pos.getX(), pos.getY(), pos.getZ());
- //List<EntitySheep> list = worldIn.<EntitySheep>getEntitiesWithinAABB(EntitySheep.class, axisalignedbb.grow(4.0D, 4.0D, 7.0D));
- //for (EntitySheep entitysheep : list)
- //{
- // worldIn.createExplosion(null, pos.getX(), pos.getY(), pos.getZ(), 1F, false);
- // }
- // }
- //DISPENSING FUNCTIONALITY STARTS HERE
- public int tickRate(World worldIn)
- {
- return 1;
- }
- public void dispense(World worldIn, BlockPos pos)
- {
- BlockSourceImpl blocksourceimpl = new BlockSourceImpl(worldIn, pos);
- TileEntityDispenser tileentitydispenser = (TileEntityDispenser)blocksourceimpl.getBlockTileEntity();
- if (tileentitydispenser != null)
- {
- int i = tileentitydispenser.getDispenseSlot();
- if (i < 0)
- {
- worldIn.playEvent(1001, pos, 0);
- }
- else
- {
- ItemStack itemstack = tileentitydispenser.getStackInSlot(i);
- IBehaviorDispenseItem ibehaviordispenseitem = this.getBehavior(itemstack);
- if (ibehaviordispenseitem != IBehaviorDispenseItem.DEFAULT_BEHAVIOR)
- {
- tileentitydispenser.setInventorySlotContents(i, ibehaviordispenseitem.dispense(blocksourceimpl, itemstack));
- }
- }
- }
- }
- protected IBehaviorDispenseItem getBehavior(ItemStack stack)
- {
- return DISPENSE_BEHAVIOR_REGISTRY.getObject(stack.getItem());
- }
- public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos)
- {
- boolean flag = worldIn.isBlockPowered(pos) || worldIn.isBlockPowered(pos.up());
- boolean flag1 = ((Boolean)state.getValue(TRIGGERED)).booleanValue();
- if (flag && !flag1)
- {
- worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn));
- worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(true)), 4);
- }
- else if (!flag && flag1)
- {
- worldIn.setBlockState(pos, state.withProperty(TRIGGERED, Boolean.valueOf(false)), 4);
- }
- }
- public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
- {
- if (!worldIn.isRemote)
- {
- this.dispense(worldIn, pos);
- }
- }
- //Called serverside after this block is replaced with another in Chunk, but before the Tile Entity is updated
- //Changing this will cause drops from the block to break
- public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
- {
- TileEntity tileentity = worldIn.getTileEntity(pos);
- if (tileentity instanceof TileEntityDispenser)
- {
- InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityDispenser)tileentity);
- worldIn.updateComparatorOutputLevel(pos, this);
- }
- super.breakBlock(worldIn, pos, state);
- }
- public static IPosition getDispensePosition(IBlockSource coords)
- {
- EnumFacing enumfacing = (EnumFacing)coords.getBlockState().getValue(FACING);
- double d0 = coords.getX() + 0.7D * (double)enumfacing.getXOffset();
- double d1 = coords.getY() + 0.7D * (double)enumfacing.getYOffset();
- double d2 = coords.getZ() + 0.7D * (double)enumfacing.getZOffset();
- return new PositionImpl(d0, d1, d2);
- }
- //Only overriding for now, will replace with proper method for this later
- @Override
- public boolean hasComparatorInputOverride(IBlockState state)
- {
- return true;
- }
- //Only overriding for now, will replace with proper method for this later
- @Override
- public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
- {
- return Container.calcRedstone(worldIn.getTileEntity(pos));
- }
- //DISPENSING FUNCTIONALITY ENDS HERE
- //FACING FUNCTIONALITY STARTS HERE
- public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
- {
- super.onBlockAdded(worldIn, pos, state);
- this.setDefaultDirection(worldIn, pos, state);
- }
- private void setDefaultDirection(World worldIn, BlockPos pos, IBlockState state)
- {
- if (!worldIn.isRemote)
- {
- EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
- boolean flag = worldIn.getBlockState(pos.north()).isFullBlock();
- boolean flag1 = worldIn.getBlockState(pos.south()).isFullBlock();
- if (enumfacing == EnumFacing.NORTH && flag && !flag1)
- {
- enumfacing = EnumFacing.SOUTH;
- }
- else if (enumfacing == EnumFacing.SOUTH && flag1 && !flag)
- {
- enumfacing = EnumFacing.NORTH;
- }
- else
- {
- boolean flag2 = worldIn.getBlockState(pos.west()).isFullBlock();
- boolean flag3 = worldIn.getBlockState(pos.east()).isFullBlock();
- if (enumfacing == EnumFacing.WEST && flag2 && !flag3)
- {
- enumfacing = EnumFacing.EAST;
- }
- else if (enumfacing == EnumFacing.EAST && flag3 && !flag2)
- {
- enumfacing = EnumFacing.WEST;
- }
- }
- worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing).withProperty(TRIGGERED, Boolean.valueOf(false)), 2);
- }
- }
- public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
- {
- return this.getDefaultState().withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)).withProperty(TRIGGERED, Boolean.valueOf(false));
- }
- public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
- {
- worldIn.setBlockState(pos, state.withProperty(FACING, EnumFacing.getDirectionFromEntityLiving(pos, placer)), 2);
- }
- //Only overriding for now, will replace with proper method for this later
- @Override
- public EnumBlockRenderType getRenderType(IBlockState state)
- {
- return EnumBlockRenderType.MODEL;
- }
- /**
- * Convert the given metadata into a BlockState for this Block
- */
- public IBlockState getStateFromMeta(int meta)
- {
- return this.getDefaultState().withProperty(FACING, EnumFacing.byIndex(meta & 7)).withProperty(TRIGGERED, Boolean.valueOf((meta & 8) > 0));
- }
- /**
- * Convert the BlockState into the correct metadata value
- */
- public int getMetaFromState(IBlockState state)
- {
- int i = 0;
- i = i | ((EnumFacing)state.getValue(FACING)).getIndex();
- if (((Boolean)state.getValue(TRIGGERED)).booleanValue())
- {
- i |= 8;
- }
- return i;
- }
- //Only overriding for now, will replace with proper method for this later
- @Override
- public IBlockState withRotation(IBlockState state, Rotation rot)
- {
- return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
- }
- //Only overriding for now, will replace with proper method for this later
- @Override
- public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
- {
- return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
- }
- protected BlockStateContainer createBlockState()
- {
- return new BlockStateContainer(this, new IProperty[] {FACING, TRIGGERED});
- }
- }
Add Comment
Please, Sign In to add comment