generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create the only singleton in the code, MinecraftEmote
- Loading branch information
Showing
4 changed files
with
79 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package net.vinrobot.mcemote; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.vinrobot.mcemote.config.ConfigurationManager; | ||
import net.vinrobot.mcemote.config.ConfigurationService; | ||
import net.vinrobot.mcemote.config.impl.file.FileConfigurationService; | ||
|
||
import java.nio.file.Path; | ||
|
||
public class MinecraftEmote { | ||
public static MinecraftEmote getInstance() { | ||
return MinecraftEmoteMod.getMinecraftEmote(); | ||
} | ||
|
||
private final ConfigurationManager configManager; | ||
|
||
protected MinecraftEmote() { | ||
final Path configDir = FabricLoader.getInstance().getConfigDir(); | ||
final Path configFile = configDir.resolve("mcemote.json"); | ||
final ConfigurationService configService = new FileConfigurationService(configFile); | ||
this.configManager = new ConfigurationManager(configService); | ||
} | ||
|
||
public ConfigurationManager getConfigManager() { | ||
return this.configManager; | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/main/java/net/vinrobot/mcemote/config/ConfigurationManager.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 net.vinrobot.mcemote.config; | ||
|
||
import java.io.IOException; | ||
|
||
import static net.vinrobot.mcemote.MinecraftEmoteMod.LOGGER; | ||
|
||
public class ConfigurationManager { | ||
private final ConfigurationService configService; | ||
private Configuration configuration; | ||
|
||
public ConfigurationManager(final ConfigurationService configService) { | ||
this.configService = configService; | ||
} | ||
|
||
public Configuration getConfig() { | ||
final Configuration config = this.configuration; | ||
return config != null ? config : this.load(); | ||
} | ||
|
||
public Configuration load() { | ||
final ConfigurationService service = this.configService; | ||
try { | ||
LOGGER.info("Loading config"); | ||
return this.configuration = service.load(); | ||
} catch (final IOException e) { | ||
LOGGER.error("Failed to load config", e); | ||
return this.configuration = service.create(); | ||
} | ||
} | ||
|
||
public void save() { | ||
final Configuration config = this.configuration; | ||
if (config != null) { | ||
try { | ||
this.configService.save(config); | ||
} catch (final IOException e) { | ||
LOGGER.error("Failed to save config", e); | ||
} | ||
} | ||
} | ||
|
||
public void reset() { | ||
this.configuration = this.configService.create(); | ||
} | ||
} |