Made slab placing a bit more proper

This commit is contained in:
Ellpeck 2016-05-02 16:56:27 +02:00
parent 2599715973
commit 5aa63f2822

View file

@ -12,7 +12,6 @@ package de.ellpeck.actuallyadditions.mod.blocks;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockBase; import de.ellpeck.actuallyadditions.mod.blocks.base.BlockBase;
import de.ellpeck.actuallyadditions.mod.blocks.base.ItemBlockBase; import de.ellpeck.actuallyadditions.mod.blocks.base.ItemBlockBase;
import de.ellpeck.actuallyadditions.mod.util.PosUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.SoundType; import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyInteger; import net.minecraft.block.properties.PropertyInteger;
@ -25,11 +24,18 @@ import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory; import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockSlabs extends BlockBase{ public class BlockSlabs extends BlockBase{
private static final AxisAlignedBB AABB_BOTTOM_HALF = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.5D, 1.0D);
private static final AxisAlignedBB AABB_TOP_HALF = new AxisAlignedBB(0.0D, 0.5D, 0.0D, 1.0D, 1.0D, 1.0D);
private static final PropertyInteger META = PropertyInteger.create("meta", 0, 1); private static final PropertyInteger META = PropertyInteger.create("meta", 0, 1);
private Block fullBlock; private Block fullBlock;
private int meta; private int meta;
@ -81,6 +87,10 @@ public class BlockSlabs extends BlockBase{
return this.getStateFromMeta(meta); return this.getStateFromMeta(meta);
} }
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos){
return state.getValue(META) == 1 ? AABB_TOP_HALF : AABB_BOTTOM_HALF;
}
@Override @Override
protected ItemBlockBase getItemBlock(){ protected ItemBlockBase getItemBlock(){
return new TheItemBlock(this); return new TheItemBlock(this);
@ -104,17 +114,64 @@ public class BlockSlabs extends BlockBase{
this.setMaxDamage(0); this.setMaxDamage(0);
} }
@Override public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ){ if(stack.stackSize != 0 && playerIn.canPlayerEdit(pos.offset(facing), facing, stack)){
if(PosUtil.getBlock(pos, world) == this.block && ((side.ordinal() == 1 && PosUtil.getMetadata(pos, world) == 0) || (side.ordinal() == 0 && PosUtil.getMetadata(pos, world) == 1))){ IBlockState state = worldIn.getBlockState(pos);
if(PosUtil.setBlock(pos, world, ((BlockSlabs)this.block).fullBlock, ((BlockSlabs)this.block).meta, 3)){
SoundType type = this.block.getSoundType(); if(state.getBlock() == this.block){
world.playSound(null, pos, type.getPlaceSound(), SoundCategory.BLOCKS, (type.getVolume()+1.0F)/2.0F, type.getPitch()*0.8F); BlockSlabs theBlock = (BlockSlabs)this.block;
stack.stackSize--; if((facing == EnumFacing.UP && state.getValue(META) == 0 || facing == EnumFacing.DOWN && state.getValue(META) == 1)){
return EnumActionResult.SUCCESS; IBlockState newState = theBlock.fullBlock.getStateFromMeta(theBlock.meta);
AxisAlignedBB bound = newState.getCollisionBoundingBox(worldIn, pos);
if(bound != Block.NULL_AABB && worldIn.checkNoEntityCollision(bound.offset(pos)) && worldIn.setBlockState(pos, newState, 11)){
SoundType soundtype = theBlock.fullBlock.getSoundType();
worldIn.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume()+1.0F)/2.0F, soundtype.getPitch()*0.8F);
--stack.stackSize;
}
return EnumActionResult.SUCCESS;
}
}
return this.tryPlace(playerIn, stack, worldIn, pos.offset(facing)) ? EnumActionResult.SUCCESS : super.onItemUse(stack, playerIn, worldIn, pos, hand, facing, hitX, hitY, hitZ);
}
else{
return EnumActionResult.FAIL;
}
}
@SideOnly(Side.CLIENT)
public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, EnumFacing side, EntityPlayer player, ItemStack stack){
IBlockState state = worldIn.getBlockState(pos);
if(state.getBlock() == this.block){
if((side == EnumFacing.UP && state.getValue(META) == 0 || side == EnumFacing.DOWN && state.getValue(META) == 1)){
return true;
} }
} }
return super.onItemUse(stack, player, world, pos, hand, side, hitX, hitY, hitZ);
return worldIn.getBlockState(pos.offset(side)).getBlock() == this.block || super.canPlaceBlockOnSide(worldIn, pos, side, player, stack);
}
private boolean tryPlace(EntityPlayer player, ItemStack stack, World worldIn, BlockPos pos){
IBlockState iblockstate = worldIn.getBlockState(pos);
if(iblockstate.getBlock() == this.block){
BlockSlabs theBlock = (BlockSlabs)this.block;
IBlockState newState = theBlock.fullBlock.getStateFromMeta(theBlock.meta);
AxisAlignedBB bound = newState.getCollisionBoundingBox(worldIn, pos);
if(bound != Block.NULL_AABB && worldIn.checkNoEntityCollision(bound.offset(pos)) && worldIn.setBlockState(pos, newState, 11)){
SoundType soundtype = theBlock.fullBlock.getSoundType();
worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume()+1.0F)/2.0F, soundtype.getPitch()*0.8F);
--stack.stackSize;
}
return true;
}
return false;
} }
@Override @Override