Skip to content

Commit

Permalink
Add ability to change brightness of Torchflower
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCartes committed Nov 29, 2024
1 parent 0bd12f2 commit 2fa2035
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 6 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ dependencies {

modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Hocon config
include implementation("org.spongepowered:configurate-core:${hocon_version}")
include implementation("org.spongepowered:configurate-hocon:${hocon_version}")
include implementation("org.apache.commons:commons-text:${commons_text_version}")
include implementation("com.typesafe:config:${typesafe_config_version}")
include implementation("io.leangen.geantyref:geantyref:${geantyref_version}")

}

tasks.withType(JavaCompile).configureEach {
Expand Down
7 changes: 6 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
mod_name=Glowing Torchflower
mod_id=glowing-torchflower
mod_version=1.1.0
mod_version=1.2.0

minecraft_version=1.21.2
yarn_mappings=1.21.2+build.1
loader_version=0.16.7

hocon_version=4.1.2
commons_text_version=1.10.0
typesafe_config_version=1.4.3
geantyref_version=1.3.13

org.gradle.jvmargs=-Xmx1G
org.gradle.parallel=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package xyz.nikitacartes.glowingtorchflower.config;

import net.fabricmc.loader.api.FabricLoader;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


public abstract class ConfigTemplate {
private transient final Pattern pattern = Pattern.compile("^[^$\"{}\\[\\]:=,+#`^?!@*&\\\\\\s/]+");
transient final String configPath;
public static Path gameDirectory = FabricLoader.getInstance().getGameDir();
private static String modName = "GlowingTorchflower";

ConfigTemplate(String configPath) {
this.configPath = configPath;
}

public static <Config extends ConfigTemplate> Config loadConfig(Class<Config> configClass, String configPath) {
Path path = gameDirectory.resolve("config/" + modName).resolve(configPath);
if (Files.exists(path)) {
final HoconConfigurationLoader loader = HoconConfigurationLoader.builder().path(path).build();
try {
return loader.load().get(configClass);
} catch (ConfigurateException e) {
throw new RuntimeException("[" + modName + "] Failed to load config file", e);
}
} else {
return null;
}
}

public void save() {
Path configDirectory = gameDirectory.resolve("config/" + modName);
if (!Files.exists(configDirectory)) {
try {
Files.createDirectories(configDirectory);
} catch (IOException e) {
System.err.println("Failed to create config directory" + e);
}
}
Path path = gameDirectory.resolve("config/" + modName + "/" + configPath);
try {
Files.writeString(path, handleTemplate());
} catch (IOException e) {
System.err.println("Failed to save config file" + e);
}
}

private String escapeString(String string) {
return string
.replace("\\", "\\\\")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t")
.replace("\b", "\\b")
.replace("\f", "\\f")
.replace("\"", "\\\"")
.replace("'", "\\'");
}

protected <T> String wrapIfNecessary(T string) {
String escapeString = escapeString(String.valueOf(string));
if (!pattern.matcher(escapeString).matches()) {
return "\"" + escapeString + "\"";
} else {
return escapeString;
}
}

protected String wrapIfNecessary(double string) {
return String.format(Locale.US, "%.4f", string);
}

protected String wrapIfNecessary(long string) {
return String.valueOf(string);
}

protected <T extends List<String>> String wrapIfNecessary(T strings) {
return "[" + strings
.stream()
.map(this::wrapIfNecessary)
.collect(Collectors.joining(",\n ")) + "]";
}

protected abstract String handleTemplate() throws IOException;



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package xyz.nikitacartes.glowingtorchflower.config;

import com.google.common.io.Resources;
import org.apache.commons.text.StringSubstitutor;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.io.Resources.getResource;
import static java.nio.charset.StandardCharsets.UTF_8;

@ConfigSerializable
public class MainConfigV1 extends ConfigTemplate {
public int torchflowerBrightness = 12;
public int torchflowerPotBrightness = 14;
public int torchflowerStage1Brightness = 3;
public int torchflowerStage2Brightness = 7;
public String configVersion = "1";

public MainConfigV1() {
super("main.conf");
}

public static MainConfigV1 load() {
MainConfigV1 config = loadConfig(MainConfigV1.class, "main.conf");
if (config == null) {
config = new MainConfigV1();
config.save();
}
return config;
}

protected String handleTemplate() throws IOException {
Map<String, String> configValues = new HashMap<>();
configValues.put("torchflowerBrightness", wrapIfNecessary(torchflowerBrightness));
configValues.put("torchflowerPotBrightness", wrapIfNecessary(torchflowerPotBrightness));
configValues.put("torchflowerStage1Brightness", wrapIfNecessary(torchflowerStage1Brightness));
configValues.put("torchflowerStage2Brightness", wrapIfNecessary(torchflowerStage2Brightness));
configValues.put("configVersion", wrapIfNecessary(configVersion));
String configTemplate = Resources.toString(getResource("config/" + configPath), UTF_8);
return new StringSubstitutor(configValues).replace(configTemplate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@
import net.minecraft.block.Blocks;
import net.minecraft.block.TorchflowerBlock;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Slice;
import xyz.nikitacartes.glowingtorchflower.config.MainConfigV1;

@Mixin(Blocks.class)
public class BlocksMixin {

@Unique
private static MainConfigV1 config;

@ModifyExpressionValue(method = "<clinit>",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/block/AbstractBlock$Settings;create()Lnet/minecraft/block/AbstractBlock$Settings;",
ordinal = 0),
slice = @Slice(from = @At(value = "CONSTANT", args = "stringValue=torchflower")))
private static AbstractBlock.Settings modifyTorchflower(AbstractBlock.Settings properties) {
return properties.luminance(blockState -> 12);
if (config == null) {
config = MainConfigV1.load();
}
return properties.luminance(blockState -> config.torchflowerBrightness);
}

@ModifyExpressionValue(method = "<clinit>",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/block/Blocks;createFlowerPotSettings()Lnet/minecraft/block/AbstractBlock$Settings;",
ordinal = 1))
private static AbstractBlock.Settings modifyPottedTorchflower(AbstractBlock.Settings properties) {
return properties.luminance(blockState -> 14);
if (config == null) {
config = MainConfigV1.load();
}
return properties.luminance(blockState -> config.torchflowerPotBrightness);
}

@ModifyExpressionValue(method = "<clinit>",
Expand All @@ -34,11 +45,14 @@ private static AbstractBlock.Settings modifyPottedTorchflower(AbstractBlock.Sett
ordinal = 0),
slice = @Slice(from = @At(value = "CONSTANT", args = "stringValue=torchflower_crop")))
private static AbstractBlock.Settings modifyTorchflowerCrop(AbstractBlock.Settings properties) {
if (config == null) {
config = MainConfigV1.load();
}

return properties.luminance(blockState -> switch (blockState.get(TorchflowerBlock.AGE)) {
case 0 -> 3;
case 1 -> 7;
default -> 12;
case 0 -> config.torchflowerStage1Brightness;
case 1 -> config.torchflowerStage2Brightness;
default -> config.torchflowerBrightness;
});
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/resources/config/main.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## ##
## Glowing Torchflower ##
## Main Configuration ##
## ##

# Brightness of the fully grown torchflower
# Default: 12
torchflower-brightness: ${torchflowerBrightness}

# Brightness of the torchflower in flowerpot
# Default: 14
torchflower-pot-brightness: ${torchflowerPotBrightness}

# Torchflower has three stages of growth (third stage is fully grown)
# Brightness of the first stage
# Default: 3
torchflower-stage1-brightness: ${torchflowerStage1Brightness}

# Brightness of the second stage
# Default: 7
torchflower-stage2-brightness: ${torchflowerStage2Brightness}

# Config Version. Used for automatic migration of config files.
# Do not change this value manually.
config-version: ${configVersion}

0 comments on commit 2fa2035

Please sign in to comment.