123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- package me.km.blocks;
- import java.util.Random;
- import javax.annotation.Nullable;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockBed;
- import net.minecraft.block.BlockHorizontal;
- import net.minecraft.block.SoundType;
- import net.minecraft.block.material.EnumPushReaction;
- import net.minecraft.block.material.MapColor;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.properties.IProperty;
- import net.minecraft.block.properties.PropertyBool;
- import net.minecraft.block.properties.PropertyEnum;
- import net.minecraft.block.state.BlockFaceShape;
- import net.minecraft.block.state.BlockStateContainer;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.EntityLivingBase;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.EntityPlayerMP;
- import net.minecraft.init.Biomes;
- import net.minecraft.init.Items;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.BlockRenderLayer;
- import net.minecraft.util.EnumBlockRenderType;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.EnumHand;
- import net.minecraft.util.Mirror;
- import net.minecraft.util.Rotation;
- import net.minecraft.util.math.AxisAlignedBB;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.text.TextComponentTranslation;
- import net.minecraft.world.IBlockAccess;
- import net.minecraft.world.World;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- public abstract class BlockModBed extends BlockHorizontal
- {
- public static final PropertyEnum<BlockBed.EnumPartType> PART = PropertyEnum.<BlockBed.EnumPartType>create("part", BlockBed.EnumPartType.class);
- public static final PropertyBool OCCUPIED = PropertyBool.create("occupied");
-
- protected String name;
- private SoundType type;
- private final Item drop;
-
- public BlockModBed(String name, String local, Item drop)
- {
- super(Material.CLOTH);
- this.setDefaultState(this.blockState.getBaseState().withProperty(PART, BlockBed.EnumPartType.FOOT)
- .withProperty(OCCUPIED, false));
- this.isBlockContainer = true;
-
- this.name = name;
- this.setRegistryName(name);
- super.setUnlocalizedName(local);
- this.type = SoundType.CLOTH;
- this.drop = drop;
- super.setHardness(0.2F);
- super.disableStats();
- }
- @Override
- public final BlockModBed setSoundType(SoundType sound)
- {
- this.type = sound;
- return this;
- }
- @Override
- public final SoundType getSoundType(IBlockState state, World w, BlockPos pos, Entity ent)
- {
- return type;
- }
- @Override
- public final boolean isBed(IBlockState state, IBlockAccess w, BlockPos pos, Entity p)
- {
- return true;
- }
-
- public float getLayingHigh()
- {
- return 0.6875f;
- }
- @Override
- public MapColor getMapColor(IBlockState state, IBlockAccess w, BlockPos pos)
- {
- return MapColor.CLOTH;
- }
- @Override
- public boolean onBlockActivated(World w, BlockPos pos, IBlockState state, EntityPlayer p, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
- {
- if(w.isRemote)
- {
- return true;
- }
- else
- {
- if(state.getValue(PART) != BlockBed.EnumPartType.HEAD)
- {
- pos = pos.offset(state.getValue(FACING));
- state = w.getBlockState(pos);
- if(state.getBlock() != this)
- {
- return true;
- }
- }
- if(w.provider.canRespawnHere() && w.getBiome(pos) != Biomes.HELL)
- {
- if((state.getValue(OCCUPIED)))
- {
- EntityPlayer entityplayer = this.getPlayerInBed(w, pos);
- if(entityplayer != null)
- {
- p.sendStatusMessage(new TextComponentTranslation("tile.bed.occupied", new Object[0]), true);
- return true;
- }
- state = state.withProperty(OCCUPIED, false);
- w.setBlockState(pos, state, 4);
- }
- EntityPlayer.SleepResult sleep = p.trySleep(pos);
- if(sleep == EntityPlayer.SleepResult.OK)
- {
- state = state.withProperty(OCCUPIED, true);
- // set players real position
- // subtracting normal bedsize, adding custom
- p.setPosition(p.posX, p.posY - 0.4375f + getLayingHigh(), p.posZ);
- // this is for evil servers which render the position wrong
- if(p instanceof EntityPlayerMP)
- {
- ((EntityPlayerMP) p).connection.setPlayerLocation(p.posX, p.posY, p.posZ, p.rotationYaw, p.rotationPitch);
- }
- // end
- w.setBlockState(pos, state, 4);
- return true;
- }
- else
- {
- switch (sleep)
- {
- case NOT_POSSIBLE_NOW:
- p.sendStatusMessage(new TextComponentTranslation("tile.bed.noSleep", new Object[0]), true);
- break;
- case NOT_SAFE:
- p.sendStatusMessage(new TextComponentTranslation("tile.bed.notSafe", new Object[0]), true);
- break;
- case TOO_FAR_AWAY:
- p.sendStatusMessage(new TextComponentTranslation("tile.bed.tooFarAway", new Object[0]), true);
- break;
- }
- return true;
- }
- }
- else
- {
- w.setBlockToAir(pos);
- BlockPos blockpos = pos.offset(state.getValue(FACING).getOpposite());
- if(w.getBlockState(blockpos).getBlock() == this)
- {
- w.setBlockToAir(blockpos);
- }
- w.newExplosion(null, pos.getX() + 0.5d, pos.getY() + 0.5d, pos.getZ() + 0.5d, 5, true, true);
- return true;
- }
- }
- }
- @Nullable
- private EntityPlayer getPlayerInBed(World w, BlockPos pos)
- {
- for(EntityPlayer p : w.playerEntities)
- {
- if (p.isPlayerSleeping() && p.bedLocation.equals(pos))
- {
- return p;
- }
- }
- return null;
- }
- @Override
- public boolean isFullCube(IBlockState state)
- {
- return false;
- }
- @Override
- public boolean isOpaqueCube(IBlockState state)
- {
- return false;
- }
- @Override
- public void onFallenUpon(World worldIn, BlockPos pos, Entity entityIn, float fallDistance)
- {
- super.onFallenUpon(worldIn, pos, entityIn, fallDistance * 0.5F);
- }
- @Override
- public void onLanded(World w, Entity ent)
- {
- if(ent.isSneaking())
- {
- super.onLanded(w, ent);
- }
- else if(ent.motionY < 0)
- {
- ent.motionY = -ent.motionY * 0.66d;
- if(!(ent instanceof EntityLivingBase))
- {
- ent.motionY *= 0.8d;
- }
- }
- }
- @Override
- public void neighborChanged(IBlockState state, World w, BlockPos pos, Block blockIn, BlockPos fromPos)
- {
- EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
- if(state.getValue(PART) == BlockBed.EnumPartType.FOOT)
- {
- if(w.getBlockState(pos.offset(enumfacing)).getBlock() != this)
- {
- w.setBlockToAir(pos);
- }
- }
- else if(w.getBlockState(pos.offset(enumfacing.getOpposite())).getBlock() != this)
- {
- if(!w.isRemote)
- {
- this.dropBlockAsItem(w, pos, state, 0);
- }
- w.setBlockToAir(pos);
- }
- }
- @Override
- public Item getItemDropped(IBlockState state, Random rand, int fortune)
- {
- return state.getValue(PART) == BlockBed.EnumPartType.FOOT ? Items.AIR : drop;
- }
- @Override
- public abstract AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos);
- @SideOnly(Side.CLIENT)
- @Override
- public boolean hasCustomBreakingProgress(IBlockState state)
- {
- return true;
- }
- @Nullable
- public static BlockPos getSafeExitLocation(World w, BlockPos pos, int tries)
- {
- return net.minecraft.block.BlockBed.getSafeExitLocation(w, pos, tries);
- }
- @Override
- public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
- {
- if(state.getValue(PART) == BlockBed.EnumPartType.HEAD)
- {
- spawnAsEntity(worldIn, pos, new ItemStack(drop));
- }
- }
- @Override
- public EnumPushReaction getMobilityFlag(IBlockState state)
- {
- return EnumPushReaction.DESTROY;
- }
- @SideOnly(Side.CLIENT)
- @Override
- public BlockRenderLayer getBlockLayer()
- {
- return BlockRenderLayer.CUTOUT;
- }
- @Override
- public EnumBlockRenderType getRenderType(IBlockState state)
- {
- return EnumBlockRenderType.MODEL;
- }
- @Override
- public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
- {
- return new ItemStack(drop);
- }
- @Override
- public void onBlockHarvested(World w, BlockPos pos, IBlockState state, EntityPlayer player)
- {
- if(player.capabilities.isCreativeMode &&
- state.getValue(PART) == BlockBed.EnumPartType.FOOT)
- {
- BlockPos blockpos = pos.offset((EnumFacing)state.getValue(FACING));
- if(w.getBlockState(blockpos).getBlock() == this)
- {
- w.setBlockToAir(blockpos);
- }
- }
- }
- @Override
- public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity te, ItemStack stack)
- {
- super.harvestBlock(worldIn, player, pos, state, null, stack);
- }
- @Override
- public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
- {
- super.breakBlock(worldIn, pos, state);
- }
- @Override
- public IBlockState getStateFromMeta(int meta)
- {
- EnumFacing f = EnumFacing.getHorizontal(meta);
- return (meta & 8) > 0 ? this.getDefaultState()
- .withProperty(PART, BlockBed.EnumPartType.HEAD)
- .withProperty(FACING, f)
- .withProperty(OCCUPIED, (meta & 4) > 0) :
- this.getDefaultState().withProperty(PART, BlockBed.EnumPartType.FOOT)
- .withProperty(FACING, f);
- }
- @Override
- public IBlockState getActualState(IBlockState state, IBlockAccess w, BlockPos pos)
- {
- if(state.getValue(PART) == BlockBed.EnumPartType.FOOT)
- {
- IBlockState iblockstate = w.getBlockState(pos.offset(state.getValue(FACING)));
- if(iblockstate.getBlock() == this)
- {
- state = state.withProperty(OCCUPIED, iblockstate.getValue(OCCUPIED));
- }
- }
- return state;
- }
- @Override
- public IBlockState withRotation(IBlockState state, Rotation rot)
- {
- return state.withProperty(FACING, rot.rotate(state.getValue(FACING)));
- }
- @Override
- public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
- {
- return state.withRotation(mirrorIn.toRotation(state.getValue(FACING)));
- }
- @Override
- public int getMetaFromState(IBlockState state)
- {
- int i = 0;
- i = i | (state.getValue(FACING)).getHorizontalIndex();
- if(state.getValue(PART) == BlockBed.EnumPartType.HEAD)
- {
- i |= 8;
- if (state.getValue(OCCUPIED))
- {
- i |= 4;
- }
- }
- return i;
- }
- @Override
- public BlockFaceShape getBlockFaceShape(IBlockAccess p_193383_1_, IBlockState p_193383_2_, BlockPos p_193383_3_, EnumFacing p_193383_4_)
- {
- return BlockFaceShape.UNDEFINED;
- }
- @Override
- protected BlockStateContainer createBlockState()
- {
- return new BlockStateContainer(this, new IProperty[] {FACING, PART, OCCUPIED});
- }
- @SideOnly(Side.CLIENT)
- public static boolean isHeadPiece(int metadata)
- {
- return (metadata & 8) != 0;
- }
- }
|