Jump to content

[Solved][1.11] Facing and meta


WildHeart

Recommended Posts

Hello, i have a problem. If you arrange the blocks to the North or to the East after rejoining the game, everything is fine, but if placed on a South or West some of the blocks shifted. What could be the problem?

Spoiler

public class BlockStand extends BlockSide
{
    public static final PropertyBool BARREL = PropertyBool.create("barrel"), UPPER = PropertyBool.create("upper"), SPIGOT = PropertyBool.create("spigot");
    protected static final AxisAlignedBB[] SIDE_AABB = new AxisAlignedBB[] {new AxisAlignedBB(0.0625D, 0.0D, 0.0625D, 0.9375D, 1.0D, 0.9375D), new AxisAlignedBB(0.0625D, 0.0D, 0.0625D, 0.9375D, 1.0D, 0.9375D), new AxisAlignedBB(0.0625D, 0.0D, 0.0625D, 0.9375D, 1.0D, 0.9375D), new AxisAlignedBB(0.0625D, 0.0D, 0.0625D, 0.9375D, 1.0D, 0.9375D)};

    public BlockStand(String name)
    {
        super(name, Material.WOOD, SIDE_AABB);
        this.setHardness(1.2F);
        this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(BARREL, false).withProperty(UPPER, false).withProperty(SPIGOT, false));
    }

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        if(!worldIn.isRemote)
        {
            if(playerIn.getHeldItem(hand).getItem() == Item.getItemFromBlock(BlocksRegister.BARREL) || playerIn.getHeldItem(hand).getItem() == ItemsRegister.SPIGOT)
            {
                if(state.getValue(BARREL) && !state.getValue(SPIGOT) && playerIn.getHeldItem(hand).getItem() != Item.getItemFromBlock(BlocksRegister.BARREL))
                {
                    worldIn.setBlockState(pos, state.withProperty(SPIGOT, true), 4);
                    if(!playerIn.isCreative()) playerIn.getHeldItem(hand).shrink(1);
                }
                else if(!state.getValue(BARREL) && playerIn.getHeldItem(hand).getItem() != ItemsRegister.SPIGOT)
                {
                    worldIn.setBlockState(pos, state.withProperty(BARREL, true), 4);
                    if(!playerIn.isCreative()) playerIn.getHeldItem(hand).shrink(1);
                }
            }
            else if(playerIn.getHeldItem(hand).getItem().getContainerItem() == null || playerIn.getHeldItem(hand).getItem() == null)
            {
                if(playerIn.isSneaking())
                {
                    if(state.getValue(SPIGOT))
                    {
                        worldIn.setBlockState(pos, state.withProperty(SPIGOT, false), 4);
                        if(!playerIn.isCreative()) playerIn.inventory.addItemStackToInventory(new ItemStack(ItemsRegister.SPIGOT));
                    }
                }
                else
                {
                    if(state.getValue(BARREL) && state.getValue(SPIGOT))
                    {
                        worldIn.setBlockState(pos, state.withProperty(BARREL, false).withProperty(SPIGOT, false), 4);
                        if(!playerIn.isCreative())
                        {
                            worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(ItemsRegister.SPIGOT)));
                            worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(BlocksRegister.BARREL)));
                        }
                    }

                    if(state.getValue(BARREL) && !state.getValue(SPIGOT))
                    {
                        worldIn.setBlockState(pos, state.withProperty(BARREL, false), 4);
                        if(!playerIn.isCreative()) worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(BlocksRegister.BARREL)));
                    }
                }
            }
        }
        return true;
    }

    @Override
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
    {
        super.dropBlockAsItemWithChance(worldIn, pos, state, chance, fortune);

        if(state.getValue(BARREL) && state.getValue(SPIGOT))
        {
            worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(ItemsRegister.SPIGOT)));
            worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(BlocksRegister.BARREL)));
        }
        else if(state.getValue(BARREL) && !state.getValue(SPIGOT))
        {
            worldIn.spawnEntity(new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(BlocksRegister.BARREL)));
        }
    }

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)
    {
        tooltip.add(ChatFormatting.GOLD + I18n.format("tooltip.stand.line_1"));
        tooltip.add(ChatFormatting.GOLD + I18n.format("tooltip.stand.line_2"));
    }

    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
        Block block = worldIn.getBlockState(pos.up()).getBlock();
        return state.withProperty(UPPER, Boolean.valueOf(block != Blocks.AIR));
    }

    @Override
    protected BlockStateContainer createBlockState()
    {
        return new BlockStateContainer(this, new IProperty[] {FACING, BARREL, UPPER, SPIGOT});
    }

    @Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta)).withProperty(BARREL, Boolean.valueOf((meta & 8) > 0)).withProperty(SPIGOT, Boolean.valueOf((meta & 4) > 0));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | state.getValue(FACING).getIndex();

        if(state.getValue(BARREL).booleanValue())
        {
            i |= 8;
        }

        if(state.getValue(SPIGOT).booleanValue())
        {
            i |= 4;
        }

        return i;
    }
}

 

BlockSide:

Spoiler

public class BlockSide extends BlockHorizontal
{
    private AxisAlignedBB[] SIDE_AABB;

    public BlockSide(String name, Material material, AxisAlignedBB[] aabb)
    {
        super(material);
        this.setRegistryName(name);
        this.setUnlocalizedName(name);
        this.setCreativeTab(Farmland.FMCT);
        this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
        this.SIDE_AABB = aabb;
    }

    @Override
    public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
    {
        switch (state.getValue(FACING))
        {
            case SOUTH:
                return this.SIDE_AABB[0];
            case NORTH:
            default:
                return this.SIDE_AABB[1];
            case WEST:
                return this.SIDE_AABB[2];
            case EAST:
                return this.SIDE_AABB[3];
        }
    }

    @Override
    public boolean isFullCube(IBlockState state)
    {
        return false;
    }

    @Override
    public boolean isOpaqueCube(IBlockState state)
    {
        return false;
    }

    @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 void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
    {
        EnumFacing enumfacing = EnumFacing.fromAngle((double)placer.rotationYaw);
        worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
    }

    @Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | state.getValue(FACING).getHorizontalIndex();
        return i;
    }

    @Override
    protected BlockStateContainer createBlockState()
    {
        return new BlockStateContainer(this, new IProperty[] {FACING});
    }
}

 

 

2017-05-08_13.04.39.png

2017-05-08_13.05.12.png

Edited by WildHeart
Link to comment
Share on other sites

 return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta))

This line won't work if meta > 4 (which BARREL and SPIGOT make it be)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

18 minutes ago, Draco18s said:

 return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta))

This line won't work if meta > 4 (which BARREL and SPIGOT make it be)

Then I need not to take EnumFacing.getHorizontal, to do it my way?

Link to comment
Share on other sites

No, what you need to do is extract the 2 bits of metadata.

EnumFacing.getHorizontal(meta&3)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

3 hours ago, Draco18s said:

Нет, то, что вам нужно сделать, это извлечь 2 бита метаданных.


EnumFacing.getHorizontal(Мета&3)

Where you can read more about metadata for blocks?

Edited by WildHeart
Link to comment
Share on other sites

Solved

@Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta & 3)).withProperty(BARREL, Boolean.valueOf((meta & 8) > 0)).withProperty(SPIGOT, Boolean.valueOf((meta & 4) > 0));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        int i = 0;
        i = i | state.getValue(FACING).getHorizontalIndex();

        if(state.getValue(BARREL).booleanValue())
        {
            i |= 8;
        }

        if(state.getValue(SPIGOT).booleanValue())
        {
            i |= 4;
        }

        return i;
    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.