/**
Copyright (C) <2017> <coolAlias>
This file is part of coolAlias' Zelda Sword Skills Minecraft Mod; as such,
you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package zeldaswordskills.block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.eventhandler.Event.Result;
import zeldaswordskills.util.BlockRotationData;
public class BlockTargetDirectional extends BlockTarget implements IVanillaRotation
{
public static final PropertyDirection FACING = PropertyDirection.create("facing");
public BlockTargetDirectional(Material material) {
super(material);
setDefaultState(blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}
@Override
public Result canGrabBlock(HookshotType type, World world, BlockPos pos, EnumFacing face) {
return (face.getOpposite() == world.getBlockState(pos).getValue(FACING) ? Result.ALLOW : Result.DENY);
}
@Override
public IBlockState onBlockPlaced(World world, BlockPos pos, EnumFacing face, float hitX, float hitY, float hitZ, int meta, EntityLivingBase entity) {
return getStateFromMeta(meta).withProperty(FACING, face.getOpposite());
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack) {
EnumFacing face = EnumFacing.fromAngle(entity.rotationYaw);
if (entity.rotationPitch < -45.0F) {
face = EnumFacing.UP;
} else if (entity.rotationPitch > 45.0F) {
face = EnumFacing.DOWN;
}
world.setBlockState(pos, state.withProperty(FACING, face), 3);
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, EnumFacing.getFront(meta));
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(FACING).getIndex();
}
@Override
protected BlockState createBlockState() {
return new BlockState(this, FACING);
}
@Override
public BlockRotationData.Rotation getRotationPattern() {
return BlockRotationData.Rotation.PISTON_CONTAINER;
}
}