ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockSlabs.java

188 lines
7.4 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("BlockSlabs.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.blocks;
2015-06-28 03:12:32 +02:00
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockBase;
import de.ellpeck.actuallyadditions.mod.blocks.base.ItemBlockBase;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2015-06-28 03:12:32 +02:00
import net.minecraft.block.Block;
2016-11-21 16:23:48 +01:00
import net.minecraft.block.BlockSlab;
2016-05-01 22:26:26 +02:00
import net.minecraft.block.SoundType;
2016-11-21 16:23:48 +01:00
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.entity.EntityLivingBase;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2015-06-28 03:12:32 +02:00
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
2021-02-27 16:33:00 +01:00
import net.minecraft.util.EnumActionResult;
2021-02-26 22:15:48 +01:00
import net.minecraft.util.Hand;
2016-11-26 21:32:27 +01:00
import net.minecraft.util.SoundCategory;
2016-05-02 16:56:27 +02:00
import net.minecraft.util.math.AxisAlignedBB;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.BlockPos;
2016-05-02 16:56:27 +02:00
import net.minecraft.world.IBlockAccess;
2015-06-28 03:12:32 +02:00
import net.minecraft.world.World;
2021-02-26 22:15:48 +01:00
import net.minecraftforge.fml.relauncher.OnlyIn;
2015-06-28 03:12:32 +02:00
2019-05-02 09:10:29 +02:00
public class BlockSlabs extends BlockBase {
2015-06-28 03:12:32 +02:00
2016-08-03 14:34:08 +02:00
public static final AxisAlignedBB AABB_BOTTOM_HALF = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.5D, 1.0D);
2016-05-02 16:56:27 +02:00
private static final AxisAlignedBB AABB_TOP_HALF = new AxisAlignedBB(0.0D, 0.5D, 0.0D, 1.0D, 1.0D, 1.0D);
2019-02-27 19:53:05 +01:00
2021-02-26 22:15:48 +01:00
private final BlockState fullBlockState;
2015-06-28 03:12:32 +02:00
2019-05-02 09:10:29 +02:00
public BlockSlabs(String name, Block fullBlock) {
this(name, fullBlock.getDefaultState());
2015-12-19 10:30:39 +01:00
}
2021-02-26 22:15:48 +01:00
public BlockSlabs(String name, BlockState fullBlockState) {
super(fullBlockState.getMaterial(), name);
this.setHardness(1.5F);
this.setResistance(10.0F);
this.fullBlockState = fullBlockState;
2015-12-17 20:18:22 +01:00
}
2016-03-18 23:47:22 +01:00
@Override
2021-02-26 22:15:48 +01:00
public boolean isOpaqueCube(BlockState state) {
2015-10-03 10:19:40 +02:00
return false;
2015-06-28 03:12:32 +02:00
}
2017-04-01 19:40:39 +02:00
@Override
2021-02-26 22:15:48 +01:00
public boolean isFullCube(BlockState state) {
2017-04-01 19:40:39 +02:00
return false;
}
2017-04-01 19:40:39 +02:00
2015-06-28 03:12:32 +02:00
@Override
public BlockState getStateForPlacement(World world, BlockPos pos, Direction facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
2021-02-26 22:15:48 +01:00
if (facing.ordinal() == 1) {
return this.getStateFromMeta(meta);
}
if (facing.ordinal() == 0 || hitY >= 0.5F) {
return this.getStateFromMeta(meta + 1);
}
return this.getStateFromMeta(meta);
2015-06-28 03:12:32 +02:00
}
2016-05-02 17:09:23 +02:00
@Override
2021-02-26 22:15:48 +01:00
public AxisAlignedBB getBoundingBox(BlockState state, IBlockAccess source, BlockPos pos) {
return state.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.TOP
? AABB_TOP_HALF
: AABB_BOTTOM_HALF;
2016-05-02 16:56:27 +02:00
}
2015-06-28 03:12:32 +02:00
@Override
2019-05-02 09:10:29 +02:00
protected ItemBlockBase getItemBlock() {
2016-04-20 21:39:03 +02:00
return new TheItemBlock(this);
2015-06-28 03:12:32 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public EnumRarity getRarity(ItemStack stack) {
return EnumRarity.COMMON;
2015-06-28 03:12:32 +02:00
}
2015-10-29 22:27:47 +01:00
2016-02-01 17:49:55 +01:00
@Override
2021-02-26 22:15:48 +01:00
public BlockState getStateFromMeta(int meta) {
return this.getDefaultState().withProperty(BlockSlab.HALF, meta == 0
? BlockSlab.EnumBlockHalf.BOTTOM
: BlockSlab.EnumBlockHalf.TOP);
2016-11-21 16:23:48 +01:00
}
@Override
2021-02-26 22:15:48 +01:00
public int getMetaFromState(BlockState state) {
return state.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.BOTTOM
? 0
: 1;
2016-11-21 16:23:48 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
protected BlockStateContainer createBlockState() {
2016-11-21 16:23:48 +01:00
return new BlockStateContainer(this, BlockSlab.HALF);
2016-02-01 17:49:55 +01:00
}
2019-05-02 09:10:29 +02:00
public static class TheItemBlock extends ItemBlockBase {
2015-10-29 22:27:47 +01:00
2019-05-02 09:10:29 +02:00
public TheItemBlock(Block block) {
2015-10-29 22:27:47 +01:00
super(block);
this.setHasSubtypes(false);
this.setMaxDamage(0);
}
2016-05-02 17:09:23 +02:00
@Override
public EnumActionResult onItemUse(PlayerEntity player, World world, BlockPos pos, Hand hand, Direction facing, float hitX, float hitY, float hitZ) {
ItemStack stack = player.getHeldItem(hand);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(stack) && player.canPlayerEdit(pos.offset(facing), facing, stack)) {
2021-02-26 22:15:48 +01:00
BlockState state = world.getBlockState(pos);
2016-05-02 16:56:27 +02:00
2019-05-02 09:10:29 +02:00
if (state.getBlock() == this.block) {
BlockSlabs theBlock = (BlockSlabs) this.block;
if (facing == Direction.UP && state.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.BOTTOM || facing == Direction.DOWN && state.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.TOP) {
2021-02-26 22:15:48 +01:00
BlockState newState = theBlock.fullBlockState;
AxisAlignedBB bound = newState.getCollisionBoundingBox(world, pos);
2016-05-02 16:56:27 +02:00
2019-05-02 09:10:29 +02:00
if (bound != Block.NULL_AABB && world.checkNoEntityCollision(bound.offset(pos)) && world.setBlockState(pos, newState, 11)) {
SoundType soundtype = theBlock.fullBlockState.getBlock().getSoundType(theBlock.fullBlockState, world, pos, player);
2019-05-02 09:10:29 +02:00
world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
player.setHeldItem(hand, StackUtil.shrink(stack, 1));
2016-05-02 16:56:27 +02:00
}
return EnumActionResult.SUCCESS;
}
2015-10-29 22:27:47 +01:00
}
2016-05-02 16:56:27 +02:00
2021-02-26 22:15:48 +01:00
return this.tryPlace(player, stack, hand, world, pos.offset(facing))
? EnumActionResult.SUCCESS
: super.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
2019-05-02 09:10:29 +02:00
} else {
2016-05-02 16:56:27 +02:00
return EnumActionResult.FAIL;
2015-10-29 22:27:47 +01:00
}
2016-05-02 16:56:27 +02:00
}
2016-05-02 17:09:23 +02:00
@Override
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public boolean canPlaceBlockOnSide(World worldIn, BlockPos pos, Direction side, PlayerEntity player, ItemStack stack) {
2021-02-26 22:15:48 +01:00
BlockState state = worldIn.getBlockState(pos);
2016-05-02 16:56:27 +02:00
2019-05-02 09:10:29 +02:00
if (state.getBlock() == this.block) {
if (side == Direction.UP && state.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.BOTTOM || side == Direction.DOWN && state.getValue(BlockSlab.HALF) == BlockSlab.EnumBlockHalf.TOP) {
2021-02-26 22:15:48 +01:00
return true;
}
2016-05-02 16:56:27 +02:00
}
return worldIn.getBlockState(pos.offset(side)).getBlock() == this.block || super.canPlaceBlockOnSide(worldIn, pos, side, player, stack);
}
2021-02-26 22:15:48 +01:00
private boolean tryPlace(PlayerEntity player, ItemStack stack, Hand hand, World world, BlockPos pos) {
BlockState iblockstate = world.getBlockState(pos);
2016-05-02 16:56:27 +02:00
2019-05-02 09:10:29 +02:00
if (iblockstate.getBlock() == this.block) {
BlockSlabs theBlock = (BlockSlabs) this.block;
2021-02-26 22:15:48 +01:00
BlockState newState = theBlock.fullBlockState;
AxisAlignedBB bound = newState.getCollisionBoundingBox(world, pos);
2016-05-02 16:56:27 +02:00
2019-05-02 09:10:29 +02:00
if (bound != Block.NULL_AABB && world.checkNoEntityCollision(bound.offset(pos)) && world.setBlockState(pos, newState, 11)) {
2019-02-27 19:53:05 +01:00
SoundType soundtype = theBlock.fullBlockState.getBlock().getSoundType(theBlock.fullBlockState, world, pos, player);
2019-05-02 09:10:29 +02:00
world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
player.setHeldItem(hand, StackUtil.shrink(stack, 1));
2016-05-02 16:56:27 +02:00
}
return true;
}
return false;
2015-10-29 22:27:47 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public String getTranslationKey(ItemStack stack) {
2018-08-04 04:22:20 +02:00
return this.getTranslationKey();
2015-10-29 22:27:47 +01:00
}
}
2015-06-28 03:12:32 +02:00
}