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

[1.16.5] Giving potion effect based on the light level of the block player is standing on


Sinu
 Share

Recommended Posts

Posted (edited)

I am trying to give a blinding effect to players who are in light level of 0 but I can't seem to get it to work.

@SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event)
    {
		PlayerEntity player = event.player;
		World world = event.player.level;
		BlockPos blockpos = player.blockPosition();
		BlockState block = world.getBlockState(blockpos);
		
		if (block.getLightValue(world, blockpos) == 0 && block != null)
		{
			((LivingEntity)event.player).addEffect(new EffectInstance(Effects.BLINDNESS, 20, 3));
		}
    }

Am I using getLightValue correctly? Also I am correctly feeding in the correct block position?

Edited by Sinu
Link to comment
Share on other sites

I see. What is the second int parameter in World#getRawBrightness for? I looked at the reference and it just seems to subtract it? Not sure why..
Also I believe I tried using World#getBrightness with LightType.BLOCK but I don't think it detected the light level properly. I am trying to give the Blindness effect to players who walk into caves with light levels of 0.

Link to comment
Share on other sites

6 minutes ago, Sinu said:

I see. What is the second int parameter in World#getRawBrightness for? I looked at the reference and it just seems to subtract it? Not sure why..

It's subtracted from the sky light only, not the block light. It is used for mob spawning rules (sky light gets 10 units subtracted during thundering, block light doesn't).

7 minutes ago, Sinu said:

Also I believe I tried using World#getBrightness with LightType.BLOCK but I don't think it detected the light level properly. I am trying to give the Blindness effect to players who walk into caves with light levels of 0.

Block light is 0 almost everywhere, except when you place light emitting blocks like torches. You probably want getRawBrightness instead (or actually getMaxLocalRawBrightness, which checks that the position is correct first).

Link to comment
Share on other sites

Sweet, that seems to have worked!
The one issue is that, I had to check if the instance was client or server because for some reason the server kept thinking the light level of the player is always 0 (is it client-side only?). So my code looks like this now:

PlayerEntity player = event.player;
		World world = event.player.level;
		BlockPos blockpos = player.blockPosition();
		BlockState block = world.getBlockState(blockpos);
		
		if (world.isClientSide)
		{
			System.out.print(world.getMaxLocalRawBrightness(blockpos) + System.lineSeparator());
			if (world.getMaxLocalRawBrightness(blockpos) == 0)
			{
				((LivingEntity)event.player).addEffect(new EffectInstance(Effects.BLINDNESS, 20, 3));	
			}
		}

But now once I walk out of the light level of 0, it still says that I have the Blindness effect even though I can see clearly. I am assuming that since I checked if the instance is client-side or not, it didn't actually give the potion effect according to the server (which makes sense) but I don't understand why Blindness goes away even after I walk into the light?

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



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.