Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.7.10] Changing the Light Value of a Particular Block


Science!
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

?

 

Link to comment
Share on other sites

> 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

Link to comment
Share on other sites

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello There! In the 73rd episode of Bed Wars we get into a lobby with a guy who had a cool name and for some reason Ben is very impressed with the name and thus the title!  
    • I have been trying to install forge, but I can't even get the installer to run. I'm really not sure what the issue is, first let me make sure I am even using the installer right, I downloaded the forge installer matching my minecraft version (1.19.2), if I double click the .jar from the download then I see a flash of a black window that immediately disappears. If I use a command prompt, I receive the following output: C:\Users\austi> java -jar forge-1.19.2-43.0.19-installer.jar Error: Unable to access jarfile forge-1.19.2-43.0.19-installer.jar   I feel like this is more likely a java issue than a forge issue but I was hoping that people here might be able to help. I've tried a couple versions of java, at first I was using 8 I believe because all of the install guides for forge were a bit older so they linked to the java website rather than oracle. I tried version 18 from the oracle site and received the same result from command prompt. It is also entirely possible that I am doing something else wrong that is blatantly obvious to everyone else.
    • Here is my configuration: % java -version java version "18.0.2" 2022-07-19 Java(TM) SE Runtime Environment (build 18.0.2+9-61) Java HotSpot(TM) 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing) forge-1.19-41.1.0-mdk on macOS Monterey 12.5 I ran the command: ./gradlew genEclipseRuns and was hoping it will generate the Eclipse project files for me. The run log is at https://gist.github.com/arun-gupta/06b983a6976b4c0a0f510d0dc225ddd3 and the generated directory structure is at https://gist.github.com/arun-gupta/3f5bfc70ec5d3d903b0f97d42840abda. In previous versions (1.12-ish), it used to generate an eclipse directory but 1.19 does not seem to have it. I tried opening the top-level forge directory but Eclipse does not recognize it as a project as well. How do I open this project in Eclipse now?  
    • I didn't put in credentials, I used it only for testing installing forge. But still the problem mentioned above.
    • I am trying to use Forge 1.18.2, I have checked my firewall, redownloaded Forge, deleted the fml.toml file (which keeps reappearing) and looked at countless help pages. I am just trying to download and use forge so that I can play with my friends and it will not download. It tells me there may be an issue with the Java runtime config and that the exit code is one, and I have copied the exit code.   
  • Topics

×
×
  • Create New...

Important Information

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