Jump to content

Dynamically change Block State


Sq3xd

Recommended Posts

I making a pipe that can change it state if other pipe is placed to it, but just setting state value doesn't work.

Here's my pipe code ->

public class MagicPipeBlockEntity extends BlockEntity {
    public MagicPipeBlockEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntities.MAGIC_PIPE.get(), pos, state);
    }

     // Tick
     public static void tick(Level level, BlockPos pos, BlockState state, MagicPipeBlockEntity entity) {
        if (level.isClientSide){
            if (level.getBlockState(pos.above()).is(ModBlocks.MAGIC_PIPE.get())){
                entity.getBlockState().setValue(BlockStateProperties.UP, true);
                state.setValue(BlockStateProperties.UP, true);
                System.out.println("YES");
            }
        }

        if (!level.isClientSide) {
            if (level.getBlockState(pos.above()).is(ModBlocks.MAGIC_PIPE.get())){
                entity.getBlockState().setValue(BlockStateProperties.UP, true);
            }
        }
     }
}

Also MagicPipeBlock code ->

public class MagicPipeBlock extends Block implements EntityBlock {
    public static final BooleanProperty UP = BlockStateProperties.UP;
    public static final BooleanProperty DOWN = BlockStateProperties.DOWN;
    public static final BooleanProperty NORTH = BlockStateProperties.NORTH;
    public static final BooleanProperty SOUTH = BlockStateProperties.SOUTH;
    public static final BooleanProperty EAST = BlockStateProperties.EAST;
    public static final BooleanProperty WEST = BlockStateProperties.WEST;

    public MagicPipeBlock(Properties properties){
        super(properties);
        this.registerDefaultState(this.stateDefinition.any().setValue(UP, false));
        this.registerDefaultState(this.stateDefinition.any().setValue(DOWN, false));
        this.registerDefaultState(this.stateDefinition.any().setValue(NORTH, false));
        this.registerDefaultState(this.stateDefinition.any().setValue(SOUTH, false));
        this.registerDefaultState(this.stateDefinition.any().setValue(EAST, false));
        this.registerDefaultState(this.stateDefinition.any().setValue(WEST, false));
    }

    public BlockState getStateForPlacement(BlockPlaceContext p_48689_) {
        return this.defaultBlockState().setValue(UP, false).setValue(DOWN, false).setValue(NORTH, false).setValue(SOUTH, false).setValue(EAST, false).setValue(WEST, false);
    }

    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> p_48725_) {
        p_48725_.add(UP, DOWN, NORTH, SOUTH, EAST, WEST);
    }

    @Override
    public RenderShape getRenderShape(BlockState p_60550_) {
        return RenderShape.MODEL;
    }

    // ENTITY

    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        return new MagicPipeBlockEntity(pos, state);
    }

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
        return createTickerHelper(type, ModBlockEntities.MAGIC_PIPE.get(),
                MagicPipeBlockEntity::tick);
    }

    @javax.annotation.Nullable
    protected static <E extends BlockEntity, A extends BlockEntity> BlockEntityTicker<A> createTickerHelper(BlockEntityType<A> p_152133_, BlockEntityType<E> p_152134_, BlockEntityTicker<? super E> p_152135_) {
        return p_152134_ == p_152133_ ? (BlockEntityTicker<A>)p_152135_ : null;
    }
}

 

Edited by Sq3xd
Link to comment
Share on other sites

BlockState.setValue() does not mutate the state.

It returns you a new BlockState with the property you modified.

 

BlockState newState = oldState.setValue(...)

 

Edited by warjort

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

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.