Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

}

 

  • Author

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?

  • Author

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!

  • Author

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 by deathchanter

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...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.