Jump to content

[1.15.2][Solved] World#getLightValue always return 0


Recommended Posts

Posted (edited)

Hello !

I'm currently facing an issue for over a day now.

 

I am making an Item that indicate if the Block right under my position is "dark enough" to make hostile mobs to spawn.

For such a need, the function

World#getLightValue(BlockPos)

fits well.

It is supposed to return an integer between 0 and 15. As i'm referring to the way it works and after reading some vanilla code parts that calls this function (such as MonsterEntity), the less the value is, the more the hostile mobs can spawn (around 8 and below).

 

But the problem is, I've been trying to play around with this function, and it always returns 0. No matter if it's in the middle of the night or if i'm in a cave.

I have been trying some alternatives such as

World#getLight(BlockPos)

(without the 'Value' keyword in the name), but this time I got something working a way better than the first function. However, I have a weird behavior when i'm on a top surface block (so, not in a cave which it seems to works fine), the value keep being stuck at 0 when i'm just walking on the blocks or at 15 if i'm jumping/flying,  whatever if it's plain day or night...

 

So I've been trying many solutions that cames to my mind such as Client/Server tests, but nothing worked at all.

 

I'm still desperate now.

Here is the code snipped that i'm using to check the values of World#getLight / getLightValue :

 

My Item class:

public class SafeometerItem extends Item {

	public SafeometerItem(Properties properties) {
		super(properties);
		this.addPropertyOverride(new ResourceLocation("darken"), new IItemPropertyGetter() {
	         @OnlyIn(Dist.CLIENT)
	         private float[] TEX_COORDS = {
	        		 0.0f, 0.045f, 0.09f, 0.135f, 0.18f, 0.22500001f, 0.27f,
	        		 0.315f, 0.36f, 0.40500003f, 0.45000002f, 0.495f, 0.54f,
	        		 0.58500004f, 0.63f, 0.675f, 0.72f, 0.76500005f, 0.81000006f,
	        		 0.855f, 0.90000004f, 0.94500005f
	         };
			@OnlyIn(Dist.CLIENT)
			public float call(ItemStack arg0, World arg1, LivingEntity arg2) {
	            boolean flag = arg2 != null;
	            Entity entity = (Entity)(flag ? arg2 : arg0.getItemFrame());
	            if (arg1 == null && entity != null) {
	            	arg1 = entity.world;
	            }
	            if(arg1 != null && arg2 != null) {
	            	
	            	BlockPos pos = new BlockPos(MathHelper.floor(arg2.getPosition().getX()), MathHelper.floor(arg2.getPosition().getY())-1, MathHelper.floor(arg2.getPosition().getZ()));
	            	System.out.println(arg1.getLight(pos));
	            	System.out.println(arg1.getLightValue(pos));
	            	
	            	return arg1.getLight(pos) <= 8 ? TEX_COORDS[11] : TEX_COORDS[0];//i <= 8 ? 0.495F : 0.0F;
	            } else {
		            return 0.0F;
	            }
			}
		});
	}
}

 

Yes, it is an Item with a dynamic texture such as Compass / Clock.

 

 

Thanks for your help.

Edited by elias54
Problem solved
Posted

One of the easiest things to do when something is not working is to see has Minecraft done this anywhere. And the answer to this question can literally be pulled up by pressing F3 and looking at the debug menu. Minecraft can see the light from the client and the server. Here's a snippet from DebugOverlayGui, do with it what you need to solve this problem.

if (this.mc.world != null) {
  if (this.mc.world.isBlockLoaded(blockpos)) {
    Chunk chunk = this.func_212916_i();
    if (chunk.isEmpty()) {
      list.add("Waiting for chunk...");
    } else {
      int i = this.mc.world.getChunkProvider().getLightManager().func_227470_b_(blockpos, 0);
      int j = this.mc.world.func_226658_a_(LightType.SKY, blockpos);
      int k = this.mc.world.func_226658_a_(LightType.BLOCK, blockpos);
      list.add("Client Light: " + i + " (" + j + " sky, " + k + " block)");
      Chunk chunk1 = this.func_212919_h();
      if (chunk1 != null) {
        WorldLightManager worldlightmanager = world.getChunkProvider().getLightManager();
        list.add("Server Light: (" + worldlightmanager.getLightEngine(LightType.SKY).getLightFor(blockpos) + " sky, " + worldlightmanager.getLightEngine(LightType.BLOCK).getLightFor(blockpos) + " block)");
      } else {
        list.add("Server Light: (?? sky, ?? block)");
      }
      ...
    }
    ...
  }
  ...
}

 

Posted
9 hours ago, ChampionAsh5357 said:

One of the easiest things to do when something is not working is to see has Minecraft done this anywhere. And the answer to this question can literally be pulled up by pressing F3 and looking at the debug menu. Minecraft can see the light from the client and the server. Here's a snippet from DebugOverlayGui, do with it what you need to solve this problem.


if (this.mc.world != null) {
  if (this.mc.world.isBlockLoaded(blockpos)) {
    Chunk chunk = this.func_212916_i();
    if (chunk.isEmpty()) {
      list.add("Waiting for chunk...");
    } else {
      int i = this.mc.world.getChunkProvider().getLightManager().func_227470_b_(blockpos, 0);
      int j = this.mc.world.func_226658_a_(LightType.SKY, blockpos);
      int k = this.mc.world.func_226658_a_(LightType.BLOCK, blockpos);
      list.add("Client Light: " + i + " (" + j + " sky, " + k + " block)");
      Chunk chunk1 = this.func_212919_h();
      if (chunk1 != null) {
        WorldLightManager worldlightmanager = world.getChunkProvider().getLightManager();
        list.add("Server Light: (" + worldlightmanager.getLightEngine(LightType.SKY).getLightFor(blockpos) + " sky, " + worldlightmanager.getLightEngine(LightType.BLOCK).getLightFor(blockpos) + " block)");
      } else {
        list.add("Server Light: (?? sky, ?? block)");
      }
      ...
    }
    ...
  }
  ...
}

 

By the way, I forgot to put that detail in my post that I've already checked the debug menu and unfortunately the Server/Client Lights values shows the same values as what I've been tried to print in the console.

So in the end, it doesn't answer my question at all. Why the value still keep being stuck at 15 even in the middle of the night except in caves when it's supposed to be decremented to the correct light value ?

 

If that's not the way it's supposed to work (even if at many places in the code definitely show it), then what should I use other than the light system to check if a block is dark enough and whatever if it can see the sky or not so hostile mobs can spawn ? 

 

Thank you, in advance.

Posted

I've finally managed to find a way to do it, in a "2 times" function.

 

			@OnlyIn(Dist.CLIENT)
			public boolean isEnoughDark(World world, BlockPos pos) {
				if(world.getCelestialAngle(1.0F) >= 0.26F && world.getCelestialAngle(1.0F) <= 0.82) {
					return true;
				} else {
					return world.func_226658_a_(LightType.SKY, pos) <= 9 ? true : false;
				}
			}

 

1) When the night comes, hostile mobs spawn everywhere, technically. Because of that, when it's night, the function returns true.

2) Otherwise, if it's plain day, it'll check if the block where i'm standing on is dark enough to return true. (e.g : cave/deep shelter...)

 

I still have to catch up the case when i'm in another dimension than the overworld, and i'll be good. :) 

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.