deathchanter Posted March 27, 2021 Share Posted March 27, 2021 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; } } Quote Link to comment Share on other sites More sharing options...
deathchanter Posted March 27, 2021 Author Share Posted March 27, 2021 Hmmm, I checked out redstone lamp's code and seems that tile entity is not needed, and I can use getStateForPlacement method to set the lightlevel. Can anyone corroborate this? Quote Link to comment Share on other sites More sharing options...
deathchanter Posted March 27, 2021 Author Share Posted March 27, 2021 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! Quote Link to comment Share on other sites More sharing options...
deathchanter Posted March 27, 2021 Author Share Posted March 27, 2021 (edited) It will be very nice if you can as well point me to similar codes if you want, so I can browse existing codes as well. Edited March 27, 2021 by deathchanter Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.