package me.km.blocks; import java.util.Random; import javax.annotation.Nullable; import net.minecraft.block.SoundType; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; 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.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; public class BlockBed extends net.minecraft.block.BlockBed { protected String name; private SoundType type; private final Item drop; public BlockBed(String name, String local, Item drop) { 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 Item getItemDropped(IBlockState state, Random rand, int fortune) { return state.getValue(PART) == BlockBed.EnumPartType.HEAD ? Items.AIR : drop; } @Override public final ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) { return new ItemStack(drop); } @Override public final BlockBed setSoundType(SoundType sound) { this.type = sound; return this; } @Override public final SoundType getSoundType(IBlockState state, World w, BlockPos pos, Entity entity) { return type; } @Override public final boolean isBed(IBlockState state, IBlockAccess world, BlockPos pos, Entity player) { return true; } // copied from net.minecraft.block.BlockBed @Override public final 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) != EnumPartType.HEAD) { pos = pos.offset((EnumFacing)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 anotherPlayer = this.getPlayerInBed(w, pos); if (anotherPlayer != 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 result = p.trySleep(pos); if(result == 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 (result) { 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; } } } public float getLayingHigh() { return 0.6875f; } // copied from net.minecraft.block.BlockBed @Nullable private EntityPlayer getPlayerInBed(World w, BlockPos pos) { for(EntityPlayer entityplayer : w.playerEntities) { if(entityplayer.isPlayerSleeping() && entityplayer.bedLocation.equals(pos)) { return entityplayer; } } return null; } }