Jump to content

Should I have a TileEntity and Entity for a sittable block with a dynamic texture?


The_Biggest_Noob

Recommended Posts

I have a Wrench Item and when you shift-right click a block with it, it checks if it is a stair, then replaces it with a sittable stair. I haven't actually implemented the sitting functionality yet because I have read on the forums that to make a sittable block you create an entity and when the sittable stair is clicked make the player mount the entity, but I also would like to implement a dynamic texture (not sure if that is the right term) using tile entities so when the stair changes into a sittable stair its texture doesn't change. I am not sure if this is the most practical way to do this, and I would like to know if it would affect performance having a tile entity and entity for each sittable stair. Also would I be able to have just a tile entity or entity to accomplish this? Also I'm kind-of new to modding and my code isn't very good.

Wrench class:

public class WrenchItem extends Item
{
    public WrenchItem(Properties properties)
    {
        super(properties);
    }

    @Override
    public ActionResultType onItemUse(ItemUseContext context)
    {
        // Useful variables
        World world = context.getWorld();
        BlockPos blockpos = context.getPos();
        BlockState blockstate = world.getBlockState(blockpos);
        PlayerEntity player = context.getPlayer();

        boolean flag = false;

        // On use
        if (player.isSneaking())
        {
            // Shift right click
            // Tile entity for dynamic tex
            // Entity for sitting
            // Add custom use sound
            if (StairsBlock.isBlockStairs(blockstate) && !world.isRemote)
            {
                world.setBlockState(blockpos, BlockInit.SITTABLE_STAIRS.get().getDefaultState()
                        .with(SittableStairsBlock.FACING, blockstate.get((StairsBlock.FACING)))
                        .with(SittableStairsBlock.HALF, blockstate.get((StairsBlock.HALF)))
                        .with(SittableStairsBlock.WATERLOGGED, blockstate.get((StairsBlock.WATERLOGGED)))
                        .with(SittableStairsBlock.SHAPE, blockstate.get((StairsBlock.SHAPE)))
                        , 1);

                flag = true;
            }
        }
        else
        {
            // Right click
            if (blockstate.getBlock() == Blocks.GRASS_BLOCK && !world.isRemote)
            {
                world.setBlockState(blockpos, Blocks.DIRT.getDefaultState(), 1);

                flag = true;
            }
        }


        // Damages wrench when used, updates changed blocks, and plays use sound
        if (flag)
        {
            context.getItem().damageItem(1, player, (Consumer) -> {player.sendBreakAnimation(context.getHand());});
            this.playUseSound(world, blockpos);
            world.notifyNeighborsOfStateChange(blockpos, blockstate.getBlock());

            return ActionResultType.SUCCESS;
        }
        else
        {
            return ActionResultType.PASS;
        }
    }

    private void playUseSound(World worldIn, BlockPos pos)
    {
        worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ITEM_FIRECHARGE_USE, SoundCategory.BLOCKS, 1.0F, (random.nextFloat() - random.nextFloat()) * 0.2F + 1.0F);
    }
}

 

Edited by The_Biggest_Noob
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.