Skip to content

Commit

Permalink
Merge branch 'main' of github.com:unnamed/nmessage
Browse files Browse the repository at this point in the history
  • Loading branch information
yusshu committed Aug 17, 2023
2 parents 54b768c + 0d6cd33 commit 04192b0
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 12 deletions.
20 changes: 10 additions & 10 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ Add the necessary dependencies
```kotlin
dependencies {
// Core API, necessary for everything
implementation("me.yushust.message:core:7.1.1")
implementation("me.yushust.message:core:7.1.3")

// .PROPERTIES source type
implementation("me.yushust.message:sourcetype-properties:7.1.1")
implementation("me.yushust.message:sourcetype-properties:7.1.3")

// .JSON source type
implementation("me.yushust.message:sourcetype-gson-json:7.1.1")
implementation("me.yushust.message:sourcetype-gson-json:7.1.3")

// Bukkit YAML source type and helpers
implementation("me.yushust.message:sourcetype-bukkit-yml:7.1.1")
implementation("me.yushust.message:sourcetype-bukkit-yml:7.1.3")

// BungeeCord YAML source type and helpers
implementation("me.yushust.message:sourcetype-bungee-yml:7.1.1")
implementation("me.yushust.message:sourcetype-bungee-yml:7.1.3")
}
```

Expand All @@ -54,34 +54,34 @@ Add the necessary dependencies
<dependency>
<groupId>me.yushust.message</groupId>
<artifactId>core</artifactId>
<version>7.1.1</version>
<version>7.1.3</version>
</dependency>

<!-- .PROPERTIES source type -->
<dependency>
<groupId>me.yushust.message</groupId>
<artifactId>sourcetype-properties</artifactId>
<version>7.1.1</version>
<version>7.1.3</version>
</dependency>

<!-- .JSON source type -->
<dependency>
<groupId>me.yushust.message</groupId>
<artifactId>sourcetype-gson-json</artifactId>
<version>7.1.1</version>
<version>7.1.3</version>
</dependency>

<!-- Bukkit YAML source type and helpers -->
<dependency>
<groupId>me.yushust.message</groupId>
<artifactId>sourcetype-bukkit-yml</artifactId>
<version>7.1.1</version>
<version>7.1.3</version>
</dependency>

<!-- BungeeCord YAML source type and helpers -->
<dependency>
<groupId>me.yushust.message</groupId>
<artifactId>sourcetype-bungee-yml</artifactId>
<version>7.1.1</version>
<version>7.1.3</version>
</dependency>
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=me.yushust.message
version=7.1.3
version=7.2.0

repositoryName=unnamedRepository
snapshotRepository=https\://repo.unnamed.team/repository/unnamed-snapshots/
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ include("core")
include("sourcetype-properties")
include("sourcetype-gson-json")
include("sourcetype-bukkit-yml")
include("sourcetype-bungee-yml")
include("sourcetype-bungee-yml")
include("sourcetype-configurate-yml")
8 changes: 8 additions & 0 deletions sourcetype-configurate-yml/build.gradle.kts
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")
}
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");
}

}
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);
}
}

}
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);
}
}
}

0 comments on commit 04192b0

Please sign in to comment.