-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to change brightness of Torchflower
- Loading branch information
1 parent
0bd12f2
commit 2fa2035
Showing
6 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
97 changes: 97 additions & 0 deletions
97
src/main/java/xyz/nikitacartes/glowingtorchflower/config/ConfigTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/xyz/nikitacartes/glowingtorchflower/config/MainConfigV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |