Skip to content

Commit

Permalink
LambDynamicLights v1.2.1: Add some tweaks, TNT lighting and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Jul 9, 2020
1 parent 57abd1e commit 87c3ce4
Show file tree
Hide file tree
Showing 18 changed files with 438 additions and 49 deletions.
74 changes: 74 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# LambDynamicLights API

You added a new entity that could emit light? You want that it dynamically emits light?

Then try the API of this mod!

## Quick note

Every time entity is referenced it means either an entity or a block entity.

Block entity dynamic lighting is non-recommended if avoidable with block states.

If your entity re-implements tick without calling the super method the dynamic light handler will not work.

## Dynamic light handlers

### `DynamicLightHandler`

A dynamic light handler is an interface with one method: `int getLuminance(T lightSource)`.

The returned value is between 0 and 15 which are luminance values, `lightSource` is of the type of the entity and is the targeted entity.
The method is called for every entity matching the type at each tick.

### `DynamicLightHandlers`

That's where you register your handler!

Just call `DynamicLightHandlers#registerDynamicLightHandler(EntityType<T>, DynamicLightHandler<T>)`
or `DynamicLightHandlers#registerDynamicLightHandler(BlockEntityType<T>, DynamicLightHandler<T>)`
to register your handler!

If a handler is already registered for this entity, then it will merge the two handlers with a `Math#max` handler.

And that's all! The mod will light up your entities following your handler.

### Examples

#### Blaze

```java
registerDynamicLightHandler(EntityType.BLAZE, entity -> 10);
```

#### Enderman

```java
registerDynamicLightHandler(EntityType.ENDERMAN, entity -> {
int luminance = 0;
if (entity.getCarriedBlock() != null)
luminance = entity.getCarriedBlock().getLuminance();
return luminance;
});
```

#### Item frame

```java
registerDynamicLightHandler(EntityType.ITEM_FRAME, entity -> {
World world = entity.getEntityWorld();
return LambDynLights.getLuminanceFromItemStack(entity.getHeldItemStack(), !world.getFluidState(entity.getBlockPos()).isEmpty());
});
```

## Utility methods

- `DynamicLightHandler#makeLivingEntityHandler` will merge the given handler with a basic handler for living entity which detects item light sources.
- `DynamicLightHandler#makeCreeperEntityHandler` will optionally merge the given handler with a basic handler for creepers. May be useful for Creepers mod.
- `LambDynLights#getLuminanceFromItemStack` will return the luminance value of the given item stack.

## `#lambdynlights:water_sensitive` tag

This is an item tag which lists water-sensitive light-emitting items.

Every items listed in this tag will not emit light in water.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@
- Added "early/WIP" compatibility with [Canvas Renderer](https://www.curseforge.com/minecraft/mc-mods/canvas-renderer).
- Added a warning message about performance issues.
- Fixed a crash with Sodium rc7 with smooth lighting set to HIGH.

### v1.2.1

- Added TNT dynamic lighting.
- Added lighting options for TNT and Creepers.
- Updated SpruceUI to v1.5.8
- Fixed player dynamic lighting not getting tracked when changing dimensions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ yarn_mappings=1.16.1+build.19:v2
loader_version=0.8.9+build.203

# Mod Properties
mod_version = 1.2.0
mod_version = 1.2.1
maven_group = me.lambdaurora
archives_base_name = lambdynamiclights

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.14.0+build.371-1.16
spruceui_version=1.5.6
spruceui_version=1.5.8
modmenu_version=1.12.2+build.17
78 changes: 68 additions & 10 deletions src/main/java/me/lambdaurora/lambdynlights/DynamicLightsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@
* Represents the mod configuration.
*
* @author LambdAurora
* @version 1.2.0
* @version 1.2.1
* @since 1.0.0
*/
public class DynamicLightsConfig
{
private static final DynamicLightsMode DEFAULT_DYNAMIC_LIGHTS_MODE = DynamicLightsMode.OFF;
private static final boolean DEFAULT_ENTITIES_LIGHT_SOURCE = true;
private static final boolean DEFAULT_BLOCK_ENTITIES_LIGHT_SOURCE = true;
private static final boolean DEFAULT_WATER_SENSITIVE_CHECK = true;
private static final DynamicLightsMode DEFAULT_DYNAMIC_LIGHTS_MODE = DynamicLightsMode.OFF;
private static final boolean DEFAULT_ENTITIES_LIGHT_SOURCE = true;
private static final boolean DEFAULT_BLOCK_ENTITIES_LIGHT_SOURCE = true;
private static final boolean DEFAULT_WATER_SENSITIVE_CHECK = true;
private static final ExplosiveLightingMode DEFAULT_CREEPER_LIGHTING_MODE = ExplosiveLightingMode.SIMPLE;
private static final ExplosiveLightingMode DEFAULT_TNT_LIGHTING_MODE = ExplosiveLightingMode.OFF;

public static final Path CONFIG_FILE_PATH = Paths.get("config/lambdynlights.toml");
protected final FileConfig config;
private final LambDynLights mod;
private boolean firstTime;
private DynamicLightsMode dynamicLightsMode;
public static final Path CONFIG_FILE_PATH = Paths.get("config/lambdynlights.toml");
protected final FileConfig config;
private final LambDynLights mod;
private boolean firstTime;
private DynamicLightsMode dynamicLightsMode;
private ExplosiveLightingMode creeperLightingMode;
private ExplosiveLightingMode tntLightingMode;

public final Option dynamicLightsModeOption = new SpruceCyclingOption("lambdynlights.option.mode",
amount -> this.setDynamicLightsMode(this.dynamicLightsMode.next()),
Expand Down Expand Up @@ -68,6 +72,10 @@ public void load()
String dynamicLightsModeValue = this.config.getOrElse("mode", DEFAULT_DYNAMIC_LIGHTS_MODE.getName());
this.dynamicLightsMode = DynamicLightsMode.byId(dynamicLightsModeValue)
.orElse(DEFAULT_DYNAMIC_LIGHTS_MODE);
this.creeperLightingMode = ExplosiveLightingMode.byId(this.config.getOrElse("light_sources.creeper", DEFAULT_CREEPER_LIGHTING_MODE.getName()))
.orElse(DEFAULT_CREEPER_LIGHTING_MODE);
this.tntLightingMode = ExplosiveLightingMode.byId(this.config.getOrElse("light_sources.tnt", DEFAULT_TNT_LIGHTING_MODE.getName()))
.orElse(DEFAULT_TNT_LIGHTING_MODE);

if (dynamicLightsModeValue.equalsIgnoreCase("none")) {
this.firstTime = true;
Expand All @@ -93,6 +101,8 @@ public void reset()
this.setEntitiesLightSource(DEFAULT_ENTITIES_LIGHT_SOURCE);
this.setBlockEntitiesLightSource(DEFAULT_BLOCK_ENTITIES_LIGHT_SOURCE);
this.setWaterSensitiveCheck(DEFAULT_WATER_SENSITIVE_CHECK);
this.setCreeperLightingMode(DEFAULT_CREEPER_LIGHTING_MODE);
this.setTntLightingMode(DEFAULT_TNT_LIGHTING_MODE);
}

/**
Expand Down Expand Up @@ -195,4 +205,52 @@ public void setWaterSensitiveCheck(boolean waterSensitive)
{
this.config.set("light_sources.water_sensitive_check", waterSensitive);
}

/**
* Returns the Creeper dynamic lighting mode.
*
* @return The Creeper dynamic lighting mode.
*/
public ExplosiveLightingMode getCreeperLightingMode()
{
return this.creeperLightingMode;
}

/**
* Sets the Creeper dynamic lighting mode.
*
* @param lightingMode The Creeper dynamic lighting mode.
*/
public void setCreeperLightingMode(@NotNull ExplosiveLightingMode lightingMode)
{
this.creeperLightingMode = lightingMode;

if (!lightingMode.isEnabled())
this.mod.removeCreeperLightSources();
this.config.set("light_sources.creeper", lightingMode.getName());
}

/**
* Returns the TNT dynamic lighting mode.
*
* @return The TNT dynamic lighting mode.
*/
public ExplosiveLightingMode getTntLightingMode()
{
return this.tntLightingMode;
}

/**
* Sets the TNT dynamic lighting mode.
*
* @param lightingMode The TNT dynamic lighting mode.
*/
public void setTntLightingMode(@NotNull ExplosiveLightingMode lightingMode)
{
this.tntLightingMode = lightingMode;

if (!lightingMode.isEnabled())
this.mod.removeTntLightSources();
this.config.set("light_sources.tnt", lightingMode.getName());
}
}
27 changes: 11 additions & 16 deletions src/main/java/me/lambdaurora/lambdynlights/DynamicLightsMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

package me.lambdaurora.lambdynlights;

import me.lambdaurora.spruceui.SpruceTexts;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import org.aperlambda.lambdacommon.utils.Nameable;
import org.jetbrains.annotations.NotNull;
Expand All @@ -22,23 +22,23 @@
* Represents the dynamic lights mode.
*
* @author LambdAurora
* @version 1.0.0
* @version 1.2.1
* @since 1.0.0
*/
public enum DynamicLightsMode implements Nameable
{
OFF(0, Formatting.RED),
FASTEST(500, Formatting.GOLD),
FAST(250, Formatting.YELLOW),
FANCY(0, Formatting.GREEN);
OFF(0, Formatting.RED, SpruceTexts.OPTIONS_OFF),
FASTEST(500, Formatting.GOLD, SpruceTexts.OPTIONS_GENERIC_FASTEST),
FAST(250, Formatting.YELLOW, SpruceTexts.OPTIONS_GENERIC_FAST),
FANCY(0, Formatting.GREEN, SpruceTexts.OPTIONS_GENERIC_FANCY);

private final int delay;
private final Formatting formatting;
private final int delay;
private final Text translatedText;

DynamicLightsMode(int delay, @NotNull Formatting formatting)
DynamicLightsMode(int delay, @NotNull Formatting formatting, @NotNull Text translatedText)
{
this.delay = delay;
this.formatting = formatting;
this.translatedText = translatedText.copy().formatted(formatting);
}

/**
Expand Down Expand Up @@ -84,19 +84,14 @@ public DynamicLightsMode next()
return v[this.ordinal() + 1];
}

public @NotNull String getTranslationKey()
{
return this == OFF ? "options.off" : ("lambdynlights.mode." + this.getName());
}

/**
* Returns the translated text of the dynamic lights mode.
*
* @return The translated text of the dynamic lights mode.
*/
public @NotNull Text getTranslatedText()
{
return new TranslatableText(this.getTranslationKey()).formatted(this.formatting);
return this.translatedText;
}

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

package me.lambdaurora.lambdynlights;

import me.lambdaurora.spruceui.SpruceTexts;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.aperlambda.lambdacommon.utils.Nameable;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Optional;

/**
* Represents the explosives dynamic lighting mode.
*
* @author LambdAurora
* @version 1.2.1
* @since 1.2.1
*/
public enum ExplosiveLightingMode implements Nameable
{
OFF(Formatting.RED, SpruceTexts.OPTIONS_OFF),
SIMPLE(Formatting.YELLOW, SpruceTexts.OPTIONS_GENERIC_SIMPLE),
FANCY(Formatting.GREEN, SpruceTexts.OPTIONS_GENERIC_FANCY);

private final Text translatedText;

ExplosiveLightingMode(@NotNull Formatting formatting, @NotNull Text translatedText)
{
this.translatedText = translatedText.copy().formatted(formatting);
}

/**
* Returns whether this mode enables explosives dynamic lighting.
*
* @return True if the mode enables explosives dynamic lighting, else false.
*/
public boolean isEnabled()
{
return this != OFF;
}

/**
* Returns the next explosives dynamic lighting mode available.
*
* @return The next available explosives dynamic lighting mode.
*/
public ExplosiveLightingMode next()
{
ExplosiveLightingMode[] v = values();
if (v.length == this.ordinal() + 1)
return v[0];
return v[this.ordinal() + 1];
}

/**
* Returns the translated text of the explosives dynamic lighting mode.
*
* @return The translated text of the explosives dynamic lighting mode.
*/
public @NotNull Text getTranslatedText()
{
return this.translatedText;
}

@Override
public @NotNull String getName()
{
return this.name().toLowerCase();
}

/**
* Gets the explosives dynamic lighting mode from its identifier.
*
* @param id The identifier of the explosives dynamic lighting mode.
* @return The explosives dynamic lighting mode if found, else empty.
*/
public static @NotNull Optional<ExplosiveLightingMode> byId(@NotNull String id)
{
return Arrays.stream(values()).filter(mode -> mode.getName().equalsIgnoreCase(id)).findFirst();
}
}
Loading

0 comments on commit 87c3ce4

Please sign in to comment.