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

I'm trying to make a block that changes light level based on it's metadata. I've got it to kind-sorta work, but not very well - it only works if I use /setblock. This is the code:

package com.henrich.epicwasteoftime.blocks;

import com.henrich.epicwasteoftime.EWOTCreativeTabs;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.util.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class PinkiteBlock extends Block {

public static final PropertyBool POWERED = PropertyBool.create("powered");

protected static final int onLight = (int)(15.0F * 1f);
protected static final int offLight = (int)(15.0F * 0f);

public PinkiteBlock(String unlocalizedName, Material material) {
	super(material);
	this.setUnlocalizedName(unlocalizedName);
	this.setDefaultState(this.blockState.getBaseState().withProperty(POWERED, false));
                this.setCreativeTab(EWOTCreativeTabs.ewotBlocks);
}

public PinkiteBlock(String unlocalizedName) {
	this(unlocalizedName, Material.rock);
}
    
    @Override
protected BlockState createBlockState() {
    return new BlockState(this, new IProperty[] { POWERED });
}
    
    @Override
public IBlockState getStateFromMeta(int meta) {
	switch(meta) {
	case 0:
		return getDefaultState().withProperty(POWERED, false);
	case 1:
		return getDefaultState().withProperty(POWERED, true);
	default:
		return getDefaultState().withProperty(POWERED, false);
	}
}
    
    @Override
public int getMetaFromState(IBlockState state) {
    return state == this.getDefaultState() ? 0 : 1;
}
    
    @Override
    /**
     * Get a light value for the block at the specified coordinates, normal ranges are between 0 and 15
     *
     * @param world The current world
     * @param pos Block position in world
     * @return The light value
     */
    public int getLightValue(IBlockAccess world, BlockPos pos)
    {
        Block block = world.getBlockState(pos).getBlock();
        if (block != this)
        {
            return block.getLightValue(world, pos);
        }
        return world.getBlockState(pos) == this.getDefaultState() ? offLight : onLight;
    }
    
    public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
    {
        super.onBlockAdded(worldIn, pos, state);

    	worldIn.setBlockState(pos, this.blockState.getBaseState().withProperty(POWERED, worldIn.isBlockPowered(pos)));
    	
    	worldIn.markBlockForUpdate(pos);
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
    {
    	worldIn.setBlockState(pos, this.blockState.getBaseState().withProperty(POWERED, worldIn.isBlockPowered(pos)));
    
    	worldIn.markBlockForUpdate(pos);
    }
}

I'll put something here when I have something of value I need to put at the end of every post. For now it's this mostly pointless text.

  • Author

Still haven't figured this out. Is there a method in World to update lighting? I can't find it if it exists.

I'll put something here when I have something of value I need to put at the end of every post. For now it's this mostly pointless text.

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.