-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve configuration handling, readd ability to reload
- Loading branch information
1 parent
e95b11e
commit 91cc2dc
Showing
9 changed files
with
217 additions
and
146 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
8 changes: 4 additions & 4 deletions
8
src/main/java/com/triassic/geyserdebuginfo/command/commands/ToggleCommand.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
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
98 changes: 98 additions & 0 deletions
98
src/main/java/com/triassic/geyserdebuginfo/configuration/ConfigurationContainer.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,98 @@ | ||
package com.triassic.geyserdebuginfo.configuration; | ||
|
||
import org.geysermc.geyser.api.extension.ExtensionLogger; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.configurate.CommentedConfigurationNode; | ||
import org.spongepowered.configurate.yaml.NodeStyle; | ||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class ConfigurationContainer { | ||
|
||
private static final String HEADER = """ | ||
GeyserDebugInfo Configuration File | ||
A Geyser extension that strives to provide F3-like debug information for Bedrock Edition players. | ||
Report any issues on our GitHub repository: | ||
https://github.com/RealTriassic/GeyserDebugInfo"""; | ||
|
||
private final Path configFile; | ||
private final ExtensionLogger logger; | ||
private final YamlConfigurationLoader loader; | ||
private final Class<? extends Configuration> configClass; | ||
private final AtomicReference<Configuration> config = new AtomicReference<>(); | ||
|
||
public ConfigurationContainer( | ||
final Path dataFolder, | ||
final ExtensionLogger logger, | ||
final Class<? extends Configuration> configClass) { | ||
this.logger = logger; | ||
this.configClass = configClass; | ||
this.configFile = dataFolder.resolve("config.yml"); | ||
|
||
this.loader = YamlConfigurationLoader.builder() | ||
.indent(2) | ||
.path(configFile) | ||
.nodeStyle(NodeStyle.BLOCK) | ||
.defaultOptions(options -> options | ||
.shouldCopyDefaults(true) | ||
.header(HEADER)) | ||
.build(); | ||
|
||
this.load(); | ||
} | ||
|
||
/** | ||
* Loads the configuration from the file. | ||
* If loading fails, the previous configuration is retained. | ||
* | ||
* @return true if the configuration was loaded successfully, false otherwise. | ||
*/ | ||
private boolean load() { | ||
try { | ||
final Configuration loadedConfig = loadConfig(); | ||
config.set(loadedConfig); | ||
return true; | ||
} catch (Throwable e) { | ||
logger.error("Failed to load configuration", e); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Loads the configuration from the file and creates a new Configuration object. | ||
* | ||
* @return the loaded Configuration object. | ||
* @throws IOException if an error occurs while reading or parsing the file. | ||
*/ | ||
private Configuration loadConfig() throws IOException { | ||
CommentedConfigurationNode node = loader.load(); | ||
Configuration loadedConfig = node.get(configClass); | ||
|
||
if (Files.notExists(configFile)) { | ||
node.set(configClass, loadedConfig); | ||
loader.save(node); | ||
} | ||
|
||
return loadedConfig; | ||
} | ||
|
||
/** | ||
* Reloads the configuration from the file. | ||
* If reloading fails, the previous configuration is retained. | ||
* | ||
* @return true if the configuration was reloaded successfully, false otherwise. | ||
*/ | ||
public boolean reload() { | ||
return load(); | ||
} | ||
|
||
@Nullable | ||
public Configuration get() { | ||
return config.get(); | ||
} | ||
} |
Oops, something went wrong.