Jump to content

[1.16.2] Set block property randomly upon placement


deathchanter

Recommended Posts

Hi, I want to create a type of block such that whenever you place an instance in the world, it gets a different lightlevel randomly in {0,....,15}. To do this, seems I can't use randomness in the constructor. So I tried to override the getlightlevel method of the custom block. But then how do I feed the private field lightlevel of the block tile entity to this getlightlevel method? When initializing tile entities attached to the blocks, I randomly set these lightlevels already. Here's part of my code:

public class TestBlock extends Block {

//    int randLightLevel = rand.nextInt(16);
    public TestBlock(){
        super(AbstractBlock.Properties.of(Material.METAL)
                .jumpFactor(6)
                .lightLevel((blockstate) -> 0));
    }

//    @Override
//    public int getLightValue(BlockState state, IBlockReader world, BlockPos pos) {
//        return randLightLevel;
//    }

    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new TestBlockTile().setLightlevel();
    }

    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }
package com.example.examplemod.blocks;

import net.minecraft.tileentity.TileEntity;

import java.util.Random;

import static com.example.examplemod.RegistryHandler.TESTBLOCK_TILE;


public class TestBlockTile extends TileEntity{
    static private Random rand = new Random();
    private int lightlevel;
    public TestBlockTile(){
        super(TESTBLOCK_TILE.get());
    }

    public TileEntity setLightlevel() {
        this.lightlevel = rand.nextInt(16);
        return this;
    }

}

 

Link to comment
Share on other sites

Hi Ben, but I did create that block entity class for storing the random light level. Is there anything I did wrong there that should be corrected? Also, I'm not very sure about how to access this property from tile entity using lambda as you suggested. Can you perhaps clarify? Thank you!

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.