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.