Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/dev/redstudio/fbp/FBP.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.IRenderHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
Expand Down Expand Up @@ -71,6 +72,8 @@ public final class FBP {

public static final VertexFormat VERTEX_FORMAT = new VertexFormat();

public static final boolean IS_CELERITASDYNAMICLIGHTS_LOADED = Loader.isModLoaded("celeritasdynamiclights");

@Mod.EventHandler
public void preInit(FMLPreInitializationEvent preInitializationEvent) {
oldMainConfigFile = new File(preInitializationEvent.getModConfigurationDirectory() + "/fbp/Particle.properties");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.redstudio.fbp.compat;

import net.minecraft.util.math.BlockPos;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class CeleritasDynamicLightsHandler {
private static MethodHandle getInstanceHandle;
private static MethodHandle getDynamicLightLevelHandle;

static {
try {
Class<?> cls = Class.forName("toni.sodiumdynamiclights.SodiumDynamicLights");

getInstanceHandle = MethodHandles.lookup().findStatic(
cls, "get", MethodType.methodType(cls)
);

getDynamicLightLevelHandle = MethodHandles.lookup().findVirtual(
cls, "getDynamicLightLevel", MethodType.methodType(double.class, BlockPos.class)
);

} catch (Exception ignored) {

}
}

public static int getDynamicLight(int x, int y, int z) {
if (getInstanceHandle == null || getDynamicLightLevelHandle == null) return 0;

try {
Object instance = getInstanceHandle.invoke();
double level = (double) getDynamicLightLevelHandle.invoke(instance, new BlockPos(x, y, z));
return (int) level;
} catch (Throwable t) {
return 0;
}
}
}
16 changes: 13 additions & 3 deletions src/main/java/dev/redstudio/fbp/renderer/light/LightUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.redstudio.fbp.renderer.light;

import dev.redstudio.fbp.compat.CeleritasDynamicLightsHandler;
import dev.redstudio.redcore.math.MathUtil;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;

import static dev.redstudio.fbp.FBP.IS_CELERITASDYNAMICLIGHTS_LOADED;
import static dev.redstudio.fbp.FBP.MC;

public class LightUtil {
Expand Down Expand Up @@ -49,6 +51,7 @@ public static int getCombinedLight(int x, int y, int z) {
world = MC.player.world;
chunk = world.getChunk(x >> 4, z >> 4);
section = chunk.getBlockStorageArray()[y >> 4];
int result;
if (section != null) {
if (section.get(x & 15, y & 15, z & 15).useNeighborBrightness()) {
int light = 0;
Expand All @@ -59,15 +62,22 @@ public static int getCombinedLight(int x, int y, int z) {
light = getNeighborLight(world, chunk, section, x, y, z - 1, light);
light = getNeighborLight(world, chunk, section, x, y, z + 1, light);
light = getNeighborLight(world, chunk, section, x - 1, y, z, light);
return getNeighborLight(world, chunk, section, x + 1, y, z, light);
result = getNeighborLight(world, chunk, section, x + 1, y, z, light);
} else {
return pack(world.provider.hasSkyLight() ? section.getSkyLight(x & 15, y & 15, z & 15) : 0,
result = pack(world.provider.hasSkyLight() ? section.getSkyLight(x & 15, y & 15, z & 15) : 0,
section.getBlockLight(x & 15, y & 15, z & 15));
}
} else {
return pack(world.provider.hasSkyLight() && y >= chunk.getHeightMap()[(z & 15) << 4 | (x & 15)] ? 15 : 0,
result = pack(world.provider.hasSkyLight() && y >= chunk.getHeightMap()[(z & 15) << 4 | (x & 15)] ? 15 : 0,
0);
}

if(IS_CELERITASDYNAMICLIGHTS_LOADED) {
int dynamic = CeleritasDynamicLightsHandler.getDynamicLight(x,y,z);
result = pack(sky(result), Math.max(block(result), dynamic));
}

return result;
}

private static int getNeighborLight(World world, Chunk chunk, ExtendedBlockStorage section, int x, int y, int z,
Expand Down