Jump to content

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


elias54

Recommended Posts

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
Link to comment
Share on other sites

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)");
      }
      ...
    }
    ...
  }
  ...
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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