Jump to content

[1.7.10] Changing the Light Value of a Particular Block


Recommended Posts

Posted

Title explains it pretty well. If possible, I'd like to make a particular block glow. Unfortunately, due to compatibility issues, I can't really just make a copy with a different light value. I initially tried setLightValue(), but it changed all blocks of the same type. (Sidenote- could someone confirm or deny for me that there is only one instance of each type of block? It definitely seems like when you change even non-static values like lightValue it affects all of them). So next I looked into world.setLightValue() method and I came up with the following (stuck in a PlayerInteractEvent Handler for the sake of quick testing):

@SubscribeEvent
public void plantHandler(PlayerInteractEvent event) {
	//You can ignore everything from here until the next comment. It's for testing other stuff
	if (event.entityPlayer.getHeldItem() == null) return;
	if (event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) return;
	if(event.entityPlayer.getHeldItem().getItem() == Items.arrow){
		SeedHandler.testChamberBuilder(new int[]{event.x, event.y, event.z, event.world.provider.dimensionId});
	}
	//Start of relevant code
	if(event.entityPlayer.getHeldItem().getItem() == Items.glowstone_dust){
		//Set the light value
		event.world.setLightValue(EnumSkyBlock.Block, event.x, event.y, event.z, 15);
		//Mark the nearby blocks for a render update
		event.world.markBlockRangeForRenderUpdate(event.x, event.y, event.z, 15, 15, 15);
		LogHelper.info("Get block light value: " + event.world.getBlockLightValue(event.x, event.y, event.z));
		LogHelper.info("Get light brightness: " + event.world.getLightBrightness(event.x, event.y, event.z));
	}
	//Everything after here is prettymuch irrelevant to the issue at hand
	if(event.entityPlayer.getHeldItem().getItem() == Items.bowl){
		event.world.getBlock(event.x, event.y, event.z).setTickRandomly(false);
	}
	if(event.entityPlayer.getHeldItem().getItem() == Items.saddle){
		event.world.getBlock(event.x, event.y, event.z).setTickRandomly(true);
	}
	ItemStack stack = event.entityPlayer.getHeldItem();
	if (stack.getTagCompound() == null || !stack.getTagCompound().hasKey("genTechId")) return;
	PlantData data = PlantData.get(event.world);
	GenTechPlant plant = data.getPlant(stack.getTagCompound().getInteger("genTechId"));
	if (plant.canPlant(event.x, event.y, event.z, event.world, ForgeDirection.getOrientation(event.face)))
		data.addBlock(new int[]{event.x, event.y, event.z, event.world.provider.dimensionId}, plant);

}

Unfortunately, this has some odd behavior. When I first use it, it seems to do nothing. Upon exiting and reopening the world, the block is actually lit up. However, if I place something on top of the block, the light goes out, and won't reappear until I exit and reopen the world again. Oh, and the block light value and light brightness return the correct values, both before and after exiting and reopening.

Posted

Hi

 

> Sidenote- could someone confirm or deny for me that there is only one instance of each type of block

The answer is "yes" and "no" :)

For example - There is only one instance of BlockDirt.  But there are seven instances of BlockOre, one for each "type" of ore, eg gold, iron, diamond, etc.

 

Regardless of how many diamond ores you place in the world, there is only ever one instance of the diamon BlockOre.

 

Look in Blocks for a list, this is where all the instances are.  The instances are created in Block.registerBlocks().

 

You might be interested in this link about blocks

http://greyminecraftcoder.blogspot.com.au/2013/07/blocks.html

and about Lighting

http://greyminecraftcoder.blogspot.com.au/2014/12/lighting-18.html

 

The short explanation about lighting is that you can't just set the light value using setLightValue, because it gets overwritten when the lighting is recalculated.

Instead, you need to override the Block method getLightValue() and return a different value depending on whether your block is glowing or not.  Your block will need to have some way of knowing whether it should glow.  You really only have a few choices:

1) use a different block which looks the same but has a different instance (the furnace does this, for example)

2) use a block with metadata

3) use a TileEntity attached to the block which stores the light value (probably unneccessary for

4) use some sort of logic eg - is the block within a certain distance? or - record a list of the recent blocks in a custom data structure of your own, stored separately, and refer to that

 

You will need to force the blocks to re-render when their lighting changes, otherwise you won't see any change.

 

    /**
     * Get a light value for the block at the specified coordinates, normal ranges are between 0 and 15
     *
     * @param world The current world
     * @param x X Position
     * @param y Y position
     * @param z Z position
     * @return The light value
     */
    public int getLightValue(IBlockAccess world, int x, int y, int z)
    {
        Block block = world.getBlock(x, y, z);
        if (block != this)
        {
            return block.getLightValue(world, x, y, z);
        }
        /**
         * Gets the light value of the specified block coords. Args: x, y, z
         */
        return getLightValue();
    }

 

-TGG

 

 

Posted

Hmm.... I might just end up not being able to do this, unfortunately, at least not without a bit of asm. My hands are kind of tied in terms of giving the blocks themselves special logic, as I'd like to be able to change the light value of blocks from mods, for example. Even more unfortunately, these blocks are plants, meaning I'd have to not only copy how they look but their logic as well. That lighting link is interesting though, and shows me where to look if I do end up going the asm route. There's one thing I find curious though- if the lighting should revert whenever its recalculated, why does the light value persist, albeit sporadically, even between doing things like placing blocks around it and reopening the world? Also, as far as forcing a re-render, isn't that what I was doing with

event.world.markBlockRangeForRenderUpdate(event.x, event.y, event.z, 15, 15, 15);

?

 

Posted

> why does the light value persist, albeit sporadically, even between doing things like placing blocks around it and reopening the world?

Because the recalculation is very expensive, one block can cause cascading updates to others around it, so vanilla tries to avoid updating lighting unless it absolutely has to

Light values are saved with the world and aren't automatically recalculated on load.  Generally it's caused by placing of a block next to it, or by a semi-random recalculation.  If you stand near your "dark" block for a while, you'll eventually see it pop back to the proper calculated value, just by itself

 

> Also, as far as forcing a re-render, isn't that what I was doing with         

event.world.markBlockRangeForRenderUpdate(event.x, event.y, event.z, 15, 15, 15);

 

Yes, I think.  I have personally used this instead, which worked fine for me:

	if (cachedNumberOfBurningSlots != numberBurning) {
		cachedNumberOfBurningSlots = numberBurning;
		if (worldObj.isRemote) {
			worldObj.markBlockForUpdate(pos);
		}
		worldObj.updateLightByType(EnumSkyBlock.BLOCK, x, y, z);
	}

 

-TGG

Posted

Alright, thank you. I think I'll probably end up either messing with World.computeLightValue() via asm or just decide that glowing plants, while neat, are kind of a superfluous visual thing which I can probably scrap.

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.