Jump to content

[SOLVED] 1.19.2 Block reverts to previous state after block update


Guest

Recommended Posts

public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult blockHitResult) {
        ItemStack itemstack = player.getItemInHand(hand);


        boolean successfullyDyed = false;

        if (level.isClientSide) {
            if(player.getAbilities().mayBuild == true){
                if(itemstack.getItem() == Items.RED_DYE){
                    level.setBlock(blockPos,blockState.setValue(TYPE,0),0);
                    successfullyDyed = true;
                }
                else if(itemstack.getItem() == Items.ORANGE_DYE){
                    level.setBlock(blockPos,blockState.setValue(TYPE,1),0);
                    successfullyDyed = true;
                }
                else if(itemstack.getItem() == Items.YELLOW_DYE){
                    level.setBlock(blockPos,blockState.setValue(TYPE,2),0);
                    successfullyDyed = true;
                }
                else if(itemstack.getItem() == Items.GREEN_DYE){
                    level.setBlock(blockPos,blockState.setValue(TYPE,3),0);
                    successfullyDyed = true;
                }
                else if(itemstack.getItem() == Items.BLUE_DYE){
                    level.setBlock(blockPos,blockState.setValue(TYPE,4),0);
                    successfullyDyed = true;
                }
                else if(itemstack.getItem() == Items.PURPLE_DYE){
                    level.setBlock(blockPos,blockState.setValue(TYPE,5),0);
                    successfullyDyed = true;
                }
                else{
                    player.displayClientMessage(Component.translatable("block.thingamajigs.lava_lamp.wrong_dye"), true);
                    return InteractionResult.CONSUME;
                }
                // update THIS block, no matter what happens
                if(successfullyDyed == true){
                    level.updateNeighborsAt(blockPos,this);
                    level.playSound(null, blockPos, SoundEvents.DYE_USE, SoundSource.BLOCKS, 1.0F, 1.0F);
                    return InteractionResult.SUCCESS;
                }
                if(!player.isCreative()) {
                    if (itemstack.getCount() > 1) {
                        itemstack.setCount(itemstack.getCount() - 1);
                    } else {
                        itemstack.setCount(0);
                    }
                }
            }
        }
        return InteractionResult.CONSUME;
    }

Hey there, I was working on a block's 'use' method, however the behavior is not quite what I wanted. In-game, the block will flash to the desired state, then revert back to the state it was in previously, and won't actually update. I was wondering if perhaps I was updating the block wrong? The only state I am trying to update is an Integer value called 'TYPE' from 0-5 by using dyes.

Edited by CreativeMasterBonin
solved the problem with simple fix
Link to comment
Share on other sites

You only ever set the block on the client.

See HoneycombItem.useOn() for the "correct" logic.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.comย  You should also read the support forum sticky post.

Link to comment
Share on other sites

This is in a block class however, useOn is not in a block class. I am assuming I need to put the updateNeighbor method outside of my condition If(level.isClientSide), because otherwise the level.setBlock() method is ran on the client side because I check for the client side first.

ย 

Or is perhaps the bug within the setBlock() method itself?

level.setBlock(blockPos,blockState.setValue(TYPE,0),0 // <- here?);

I had set that number to 3 before and the same behavior happened anyways.

Edited by CreativeMasterBonin
Link to comment
Share on other sites

  • Guest changed the title to [SOLVED] 1.19.2 Block reverts to previous state after block update

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.