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;
}
}