-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:unnamed/nmessage
- Loading branch information
Showing
7 changed files
with
153 additions
and
12 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
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,8 @@ | ||
plugins { | ||
id("message.java-conventions") | ||
} | ||
|
||
dependencies { | ||
implementation(project(":core")) | ||
implementation("org.spongepowered:configurate-yaml:4.0.0") | ||
} |
33 changes: 33 additions & 0 deletions
33
.../src/main/java/me/yushust/message/source/configurate/yml/ConfigurateYamlMessageAdapt.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,33 @@ | ||
package me.yushust.message.source.configurate.yml; | ||
|
||
import me.yushust.message.source.MessageSource; | ||
import java.io.File; | ||
|
||
/** | ||
* Facade that adds static factory | ||
* methods for creating {@link ConfigurateYamlMessageSource} | ||
*/ | ||
public class ConfigurateYamlMessageAdapt { | ||
|
||
private ConfigurateYamlMessageAdapt() {} | ||
|
||
/** | ||
* Creates a new {@link ConfigurateYamlMessageSource} for the | ||
* specified {@code folder} and using the provided {@code fileFormat} | ||
* to get the filenames using its language | ||
*/ | ||
public static MessageSource newYamlSource(File folder, String fileFormat) { | ||
return new ConfigurateYamlMessageSource(folder, fileFormat); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ConfigurateYamlMessageSource} for the | ||
* specified {@code folder} and using the default file format | ||
* ("lang_%lang%.yml") to get the filenames using its language | ||
*/ | ||
|
||
public static MessageSource newYamlSource(File folder) { | ||
return newYamlSource(folder, "lang_%lang%.yml"); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...src/main/java/me/yushust/message/source/configurate/yml/ConfigurateYamlMessageSource.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,76 @@ | ||
package me.yushust.message.source.configurate.yml; | ||
|
||
import me.yushust.message.source.AbstractCachedFileSource; | ||
import me.yushust.message.source.MessageSource; | ||
import me.yushust.message.util.Validate; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.configurate.ConfigurationNode; | ||
import org.spongepowered.configurate.serialize.SerializationException; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
|
||
public class ConfigurateYamlMessageSource | ||
extends AbstractCachedFileSource<ConfigurationNode> | ||
implements MessageSource { | ||
|
||
private final File folder; | ||
|
||
public ConfigurateYamlMessageSource(File folder, String fileFormat) { | ||
super(fileFormat); | ||
this.folder = Validate.isNotNull(folder, "folder"); | ||
|
||
if (!folder.exists() && !folder.mkdirs()) { | ||
throw new IllegalStateException("Cannot create container folder (" + folder.getName() + ')'); | ||
} | ||
} | ||
|
||
private @Nullable ConfigurationNode loadImpl(File file, String filename) { | ||
if (file.exists()) { | ||
return YamlParse.fromFile(file); | ||
} | ||
|
||
try (InputStream resource = getClass().getClassLoader().getResourceAsStream(filename)) { | ||
if (resource == null) { | ||
return null; | ||
} | ||
|
||
try (InputStream stream = new BufferedInputStream(resource)) { | ||
Files.copy(stream, file.toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return YamlParse.fromFile(file); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void load(String language) { | ||
String filename = getFilename(language); | ||
File file = new File(folder, filename); | ||
ConfigurationNode config = loadImpl(file, filename); | ||
cache.put(language, config); | ||
} | ||
|
||
@Override | ||
protected @Nullable ConfigurationNode getSource(String filename) { | ||
File file = new File(folder, filename); | ||
return loadImpl(file, filename); | ||
} | ||
|
||
@Override | ||
protected @Nullable Object getValue(ConfigurationNode source, String path) { | ||
try { | ||
return source.node((Object[]) path.split("\\.")).get(Object.class); | ||
} catch (SerializationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...pe-configurate-yml/src/main/java/me/yushust/message/source/configurate/yml/YamlParse.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,23 @@ | ||
package me.yushust.message.source.configurate.yml; | ||
|
||
import org.spongepowered.configurate.ConfigurateException; | ||
import org.spongepowered.configurate.ConfigurationNode; | ||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader; | ||
|
||
import java.io.File; | ||
|
||
public class YamlParse { | ||
|
||
private YamlParse() {} | ||
|
||
public static ConfigurationNode fromFile(File file) { | ||
try { | ||
return YamlConfigurationLoader.builder() | ||
.file(file) | ||
.build() | ||
.load(); | ||
} catch (ConfigurateException e) { | ||
throw new RuntimeException("Cannot load YamlConfiguration", e); | ||
} | ||
} | ||
} |