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.
- Loading branch information
Showing
6 changed files
with
139 additions
and
2 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,21 +1,40 @@ | ||
package net.vinrobot.mcemote; | ||
|
||
import net.fabricmc.api.ModInitializer; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.vinrobot.mcemote.config.ConfigurationService; | ||
import net.vinrobot.mcemote.config.impl.file.FileConfigurationService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.file.Path; | ||
|
||
public class MinecraftEmoteMod implements ModInitializer { | ||
// This logger is used to write text to the console and the log file. | ||
// It is considered best practice to use your mod id as the logger's name. | ||
// That way, it's clear which mod wrote info, warnings, and errors. | ||
public static final Logger LOGGER = LoggerFactory.getLogger("MCEmote"); | ||
|
||
private static ConfigurationService configService; | ||
|
||
public static ConfigurationService getConfigService() { | ||
ConfigurationService ret = configService; | ||
if (ret == null) { | ||
throw new RuntimeException("Configuration service not yet available!"); | ||
} | ||
return ret; | ||
} | ||
|
||
@Override | ||
public void onInitialize() { | ||
// This code runs as soon as Minecraft is in a mod-load-ready state. | ||
// However, some things (like resources) may still be uninitialized. | ||
// Proceed with mild caution. | ||
|
||
LOGGER.info("Hello Fabric world!"); | ||
|
||
final Path configDir = FabricLoader.getInstance().getConfigDir(); | ||
final Path configFile = configDir.resolve("mcemote.json"); | ||
configService = new FileConfigurationService(configFile); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/net/vinrobot/mcemote/config/ConfigurationService.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,11 @@ | ||
package net.vinrobot.mcemote.config; | ||
|
||
import java.io.IOException; | ||
|
||
public interface ConfigurationService { | ||
Configuration create(); | ||
|
||
Configuration load() throws IOException; | ||
|
||
void save(Configuration config) throws IOException; | ||
} |
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,30 @@ | ||
package net.vinrobot.mcemote.config; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonIOException; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
import java.io.Reader; | ||
|
||
public class TypedGson<S> { | ||
private static final Gson DEFAULT_GSON = new GsonBuilder().setPrettyPrinting().create(); | ||
|
||
private final Gson gson; | ||
|
||
public TypedGson() { | ||
this(DEFAULT_GSON); | ||
} | ||
|
||
public TypedGson(final Gson gson) { | ||
this.gson = gson; | ||
} | ||
|
||
public void toJson(S serializable, Appendable writer) throws JsonIOException { | ||
this.gson.toJson(serializable, writer); | ||
} | ||
|
||
public <T extends S> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException { | ||
return this.gson.fromJson(json, classOfT); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/net/vinrobot/mcemote/config/impl/file/FileConfiguration.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,17 @@ | ||
package net.vinrobot.mcemote.config.impl.file; | ||
|
||
import net.vinrobot.mcemote.config.Configuration; | ||
|
||
import java.util.Optional; | ||
|
||
class FileConfiguration { | ||
public String twitchId; | ||
|
||
public void copyFrom(final Configuration domain) { | ||
this.twitchId = domain.twitchId().get(); | ||
} | ||
|
||
public void copyTo(final Configuration configuration) { | ||
configuration.twitchId().set(Optional.ofNullable(this.twitchId)); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/net/vinrobot/mcemote/config/impl/file/FileConfigurationService.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,50 @@ | ||
package net.vinrobot.mcemote.config.impl.file; | ||
|
||
import net.vinrobot.mcemote.config.Configuration; | ||
import net.vinrobot.mcemote.config.ConfigurationService; | ||
import net.vinrobot.mcemote.config.TypedGson; | ||
import net.vinrobot.mcemote.config.impl.ConfigurationImpl; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class FileConfigurationService implements ConfigurationService { | ||
private final TypedGson<FileConfiguration> gson = new TypedGson<>(); | ||
private final Path configFile; | ||
|
||
public FileConfigurationService(final Path configFile) { | ||
this.configFile = configFile; | ||
} | ||
|
||
@Override | ||
public Configuration create() { | ||
return new ConfigurationImpl(); | ||
} | ||
|
||
@Override | ||
public Configuration load() throws IOException { | ||
FileConfiguration fileConfiguration; | ||
try (final Reader reader = Files.newBufferedReader(this.configFile)) { | ||
fileConfiguration = this.gson.fromJson(reader, FileConfiguration.class); | ||
} | ||
|
||
final Configuration config = this.create(); | ||
if (fileConfiguration != null) { | ||
fileConfiguration.copyTo(config); | ||
} | ||
return config; | ||
} | ||
|
||
@Override | ||
public void save(final Configuration configuration) throws IOException { | ||
final FileConfiguration fileConfiguration = new FileConfiguration(); | ||
fileConfiguration.copyFrom(configuration); | ||
|
||
try (final Writer writer = Files.newBufferedWriter(this.configFile)) { | ||
this.gson.toJson(fileConfiguration, writer); | ||
} | ||
} | ||
} |