Skip to content

Commit

Permalink
Fully add data-driven item lighting.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Aug 4, 2024
1 parent e39c01e commit a47cb73
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@
import net.minecraft.world.item.ItemStack;

public interface ItemLightSourceManager {
/**
* {@return the luminance value of the item stack}
*
* @param stack the item stack
*/
default int getLuminance(ItemStack stack) {
return this.getLuminance(stack, false);
}


int getLuminance(ItemStack stack);

/**
* {@return the luminance value of the item stack}
*
* @param stack the item stack
* @param submergedInWater {@code true} if the stack is submerged in water, else {@code false}
*/
int getLuminance(ItemStack stack, boolean submergedInWater);
}
9 changes: 6 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ if (!(System.getenv("CURSEFORGE_TOKEN") != null || System.getenv("MODRINTH_TOKEN
}
logger.lifecycle("Preparing version ${version}...")

configurations["api"].extendsFrom(configurations["shadow"])

repositories {
mavenLocal()
mavenCentral()
Expand Down Expand Up @@ -57,10 +55,13 @@ loom {
}

dependencies {
shadow(project(":api"))
implementation(project(":api", configuration = "namedElements"))
implementation(libs.yumi.commons.core)

modImplementation(libs.fabric.api)

implementation(libs.nightconfig.core)
implementation(libs.nightconfig.toml)
modImplementation(libs.spruceui)
include(libs.spruceui)
modImplementation(libs.pridelib)
Expand All @@ -72,6 +73,8 @@ dependencies {

modRuntimeOnly(libs.sodium)

shadow(project(":api", configuration = "namedElements"))
shadow(libs.yumi.commons.core)
shadow(libs.nightconfig.core)
shadow(libs.nightconfig.toml)
}
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mappings-yalmm = "8"
mappings-parchment = "2024.07.07"

# Dependencies
yumi-commons = "1.0.0-alpha.1"
spruceui = "5.1.0+1.21"
pridelib = "1.2.1+1.21"
modmenu = "11.0.1"
Expand All @@ -25,6 +26,7 @@ fabric-loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric-l
fabric-api = { module = "net.fabricmc.fabric-api:fabric-api", version.ref = "fabric-api" }

# Dependencies
yumi-commons-core = { module = "dev.yumi.commons:yumi-commons-core", version.ref = "yumi-commons" }
spruceui = { module = "dev.lambdaurora:spruceui", version.ref = "spruceui" }
pridelib = { module = "io.github.queerbric:pridelib", version.ref = "pridelib" }
modmenu = { module = "com.terraformersmc:modmenu", version.ref = "modmenu" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.Text;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

/**
* Represents the mod configuration.
*
* @author LambdAurora
* @version 2.3.1
* @version 3.0.0
* @since 1.0.0
*/
public class DynamicLightsConfig {
private static final Logger LOGGER = LoggerFactory.getLogger("LambDynamicLights|Config");
private static final DynamicLightsMode DEFAULT_DYNAMIC_LIGHTS_MODE = DynamicLightsMode.FANCY;
private static final boolean DEFAULT_ENTITIES_LIGHT_SOURCE = true;
private static final boolean DEFAULT_SELF_LIGHT_SOURCE = true;
Expand Down Expand Up @@ -106,7 +109,7 @@ public void load() {
this.tntLightingMode = ExplosiveLightingMode.byId(this.config.getOrElse("light_sources.tnt", DEFAULT_TNT_LIGHTING_MODE.getName()))
.orElse(DEFAULT_TNT_LIGHTING_MODE);

this.mod.log("Configuration loaded.");
LambDynLights.log(LOGGER, "Configuration loaded.");
}

/**
Expand Down
54 changes: 39 additions & 15 deletions src/main/java/dev/lambdaurora/lambdynlights/LambDynLights.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@
* Represents the LambDynamicLights mod.
*
* @author LambdAurora
* @version 2.3.2
* @version 3.0.0
* @since 1.0.0
*/
public class LambDynLights implements ClientModInitializer {
public static final String NAMESPACE = "lambdynlights";
private static final Logger LOGGER = LoggerFactory.getLogger("LambDynamicLights");
private static final double MAX_RADIUS = 7.75;
private static final double MAX_RADIUS_SQUARED = MAX_RADIUS * MAX_RADIUS;
private static LambDynLights INSTANCE;
public final Logger logger = LoggerFactory.getLogger(NAMESPACE);
public final DynamicLightsConfig config = new DynamicLightsConfig(this);
public final ItemLightSources itemLightSources = new ItemLightSources();
private final Set<DynamicLightSource> dynamicLightSources = new HashSet<>();
private final ReentrantReadWriteLock lightSourcesLock = new ReentrantReadWriteLock();
private long lastUpdate = System.currentTimeMillis();
Expand All @@ -67,7 +68,7 @@ public class LambDynLights implements ClientModInitializer {
@Override
public void onInitializeClient() {
INSTANCE = this;
this.log("Initializing LambDynamicLights...");
log(LOGGER, "Initializing LambDynamicLights...");

this.config.load();

Expand All @@ -83,7 +84,7 @@ public Identifier getFabricId() {

@Override
public void reload(ResourceManager manager) {
ItemLightSources.load(manager);
LambDynLights.this.itemLightSources.load(manager);
}
});

Expand Down Expand Up @@ -365,21 +366,45 @@ public void removeBlockEntitiesLightSource() {
}

/**
* Prints a message to the terminal.
* Logs an informational message.
*
* @param info the message to print
* @param logger the logger to use
* @param msg the message to log
*/
public void log(String info) {
this.logger.info("[LambDynLights] " + info);
public static void log(Logger logger, String msg) {
if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
msg = "[LambDynLights] " + msg;
}

logger.info(msg);
}

/**
* Prints a warning message to the terminal.
* Logs a warning message.
*
* @param info the message to print
* @param logger the logger to use
* @param msg the message to log
*/
public void warn(String info) {
this.logger.warn("[LambDynLights] " + info);
public static void warn(Logger logger, String msg) {
if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
msg = "[LambDynLights] " + msg;
}

logger.warn(msg);
}

/**
* Logs a warning message.
*
* @param logger the logger to use
* @param msg the message to log
*/
public static void warn(Logger logger, String msg, Object... args) {
if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
msg = "[LambDynLights] " + msg;
}

logger.warn(msg, args);
}

/**
Expand Down Expand Up @@ -462,14 +487,13 @@ public static int getLivingEntityLuminanceFromItems(LivingEntity entity) {
}

/**
* Returns the luminance from an item stack.
* {@return the luminance value of the item stack}
*
* @param stack the item stack
* @param submergedInWater {@code true} if the stack is submerged in water, else {@code false}
* @return the luminance of the item
*/
public static int getLuminanceFromItemStack(@NotNull ItemStack stack, boolean submergedInWater) {
return ItemLightSources.getLuminance(stack, submergedInWater);
return INSTANCE.itemLightSources.getLuminance(stack, submergedInWater);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright © 2024 LambdAurora <[email protected]>
*
* This file is part of LambDynamicLights.
*
* Licensed under the Lambda License. For more information,
* see the LICENSE file.
*/

package dev.lambdaurora.lambdynlights.mixin;

import dev.lambdaurora.lambdynlights.LambDynLights;
import dev.lambdaurora.lambdynlights.resource.item.ItemLightSources;
import net.minecraft.client.multiplayer.TagCollector;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.Predicate;

@Mixin(TagCollector.class)
public class TagCollectorMixin {
@Inject(
method = "applyTags(Lnet/minecraft/core/RegistryAccess;Ljava/util/function/Predicate;)V",
at = @At("RETURN")
)
private void onTagSynchronization(
RegistryAccess registryAccess, Predicate<ResourceKey<? extends Registry<?>>> predicate,
CallbackInfo ci
) {
LambDynLights.get().itemLightSources.apply(registryAccess);
}
}
Loading

0 comments on commit a47cb73

Please sign in to comment.