Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-98 Move to okaeri-configs. #105

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,16 @@
### config.yml

```yaml
#
# ____ _ _ _____ ChatFormatter _ _
# / ___| |__ __ _| |_| ___|__ _ __ _ __ ___ __ _| |_| |_ ___ _ __
# | | | '_ \ / _` | __| |_ / _ \| '__| '_ ` _ \ / _` | __| __/ _ \ '__|
# | |___| | | | (_| | |_| _| (_) | | | | | | | | (_| | |_| || __/ |
# \____|_| |_|\__,_|\__|_| \___/|_| |_| |_| |_|\__,_|\__|\__\___|_|


#
#
# Do you want to receive updates about new versions of ChatFormatter?
receiveUpdates: true

# Chat format for ranks (Vault) Support mini-messages and legacy colors

# We're recommending to use webui for mini-messages: https://webui.adventure.kyori.net/

# documentation is here: https://docs.adventure.kyori.net/minimessage/format.html
receive-updates: true
# Example usages:
#
# Hover message:
Expand All @@ -108,25 +103,29 @@ receiveUpdates: true
# RGB and gradient message usage:
# <color:#ff00ee>Example message</color>
# <gradient:#ff00ee:#f79459>Example message</gradient>

defaultFormat: "{displayname} {arrow_right} {message}"
#
#
# Chat format for ranks (Vault) Support mini-messages and legacy colors
# We're recommending to use webui for mini-messages: https://webui.adventure.kyori.net/
# documentation is here: https://docs.adventure.kyori.net/minimessage/format.html
default-format: "{displayname} {arrow_right} {message}"
format:
default: "{member} &f{displayname} &8{arrow_right} {message} "
admin: "$template({admin}, &c)"

#
# It is used to shorten the text even more and keep the clean file!
templates:
- "$template($rank, $color) -> '$rank $color{displayname} &8{arrow_right} $color{message}'"

#
# Placeholders, it allows you to make a shorter text, you can use some prefixes, characters etc.
placeholders:
{displayname}: "<displayname>"
{message}: "<message>"
{prefix}: "<b><gradient:#29fbff:#38b3ff>ChatFormatter</gradient></b>"
{member}: "<b><color:#6e6764>Member</color></b>"
{admin}: "<b><color:#ff4400>Admin</color></b>"
{arrow_right}: "»"
{arrow_left}: "«"
'{displayname}': <displayname>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you force add "?

'{message}': <message>
'{prefix}': <b><gradient:#29fbff:#38b3ff>ChatFormatter</gradient></b>
'{member}': <b><color:#6e6764>Member</color></b>
'{admin}': <b><color:#ff4400>Admin</color></b>
'{arrow_right}': »
'{arrow_left}': «
```


Expand Down
7 changes: 4 additions & 3 deletions buildSrc/src/main/kotlin/eternalcode.java.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ version = "1.0.7"
repositories {
mavenCentral()
maven { url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") }
maven { url = uri("https://repo.panda-lang.org/releases") }
maven { url = uri("https://repo.panda-lang.org/releases/") }
maven { url = uri("https://storehouse.okaeri.eu/repository/maven-public/") }
maven { url = uri("https://repo.papermc.io/repository/maven-public/") }
maven { url = uri("https://repo.extendedclip.com/content/repositories/placeholderapi/") }
maven { url = uri("https://jitpack.io") }
maven { url = uri("https://repo.eternalcode.pl/releases") }
maven { url = uri("https://jitpack.io/") }
maven { url = uri("https://repo.eternalcode.pl/releases/") }
}

java {
Expand Down
13 changes: 7 additions & 6 deletions chatformatter-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
id("eternalcode.java")
id("com.github.johnrengelman.shadow")
Expand All @@ -17,10 +19,9 @@ dependencies {
testImplementation("net.kyori:adventure-platform-bukkit:$adventureVersion")
testImplementation("net.kyori:adventure-text-minimessage:$miniMessageVersion")

// CDN Configs
val cdnVersion = "1.14.4"
implementation("net.dzikoysk:cdn:$cdnVersion")
testImplementation("net.dzikoysk:cdn:$cdnVersion")
// Okaeri Config
val okaeriVersion = "5.0.0-beta.5"
implementation("eu.okaeri:okaeri-configs-yaml-snakeyaml:${okaeriVersion}")

// bStats
implementation("org.bstats:bstats-bukkit:3.0.2")
Expand Down Expand Up @@ -57,12 +58,12 @@ tasks {
val prefix = "com.eternalcode.formatter.libs"
listOf(
"com.eternalcode.gitcheck",
"net.dzikoysk",
"panda",
"org.panda_lang",
"net.kyori",
"org.bstats",
"org.json",
"eu.okaeri",
"org.yaml",
).forEach { pack ->
relocate(pack, "$prefix.$pack")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.eternalcode.formatter;

import com.eternalcode.formatter.config.ConfigManager;
import com.eternalcode.formatter.config.PluginConfig;
import com.google.common.base.Stopwatch;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.AudienceProvider;
Expand All @@ -23,12 +23,12 @@ class ChatFormatterCommand implements CommandExecutor, TabCompleter {
private static final String RELOAD_MESSAGE = "<b><gradient:#29fbff:#38b3ff>ChatFormatter:</gradient></b> <green>Successfully reloaded configs in %sms!";
public static final String RELOAD_PERMISSION = "chatformatter.reload";

private final ConfigManager configManager;
private final PluginConfig pluginConfig;
private final AudienceProvider provider;
private final MiniMessage miniMessage;

ChatFormatterCommand(ConfigManager configManager, AudienceProvider provider, MiniMessage miniMessage) {
this.configManager = configManager;
ChatFormatterCommand(PluginConfig pluginConfig, AudienceProvider provider, MiniMessage miniMessage) {
this.pluginConfig = pluginConfig;
this.provider = provider;
this.miniMessage = miniMessage;
}
Expand All @@ -43,7 +43,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (sender.hasPermission(RELOAD_PERMISSION)) {
Stopwatch stopwatch = Stopwatch.createStarted();

this.configManager.loadAndRenderConfigs();
this.pluginConfig.load();
long millis = stopwatch.elapsed(TimeUnit.MILLISECONDS);

Component deserialized = this.miniMessage.deserialize(String.format(RELOAD_MESSAGE, millis));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.eternalcode.formatter;

import com.eternalcode.formatter.config.ConfigManager;
import com.eternalcode.formatter.config.ConfigService;
import com.eternalcode.formatter.config.PluginConfig;
import com.eternalcode.formatter.legacy.LegacyPostProcessor;
import com.eternalcode.formatter.legacy.LegacyPreProcessor;
Expand All @@ -20,6 +20,8 @@
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

public class ChatFormatterPlugin implements ChatFormatterApi {
Expand All @@ -33,10 +35,10 @@ public ChatFormatterPlugin(Plugin plugin) {
Server server = plugin.getServer();
Stopwatch stopwatch = Stopwatch.createStarted();

ConfigManager configManager = new ConfigManager(plugin.getDataFolder());
configManager.loadAndRenderConfigs();
File dataFolder = plugin.getDataFolder();

PluginConfig pluginConfig = configManager.getPluginConfig();
ConfigService configService = new ConfigService();
PluginConfig pluginConfig = configService.create(PluginConfig.class, new File(dataFolder, "config.yml"));

this.placeholderRegistry = new PlaceholderRegistry();
this.placeholderRegistry.stack(pluginConfig);
Expand All @@ -56,7 +58,7 @@ public ChatFormatterPlugin(Plugin plugin) {

this.chatHandler = new ChatHandlerImpl(miniMessage, pluginConfig, this.rankProvider, this.placeholderRegistry, this.templateService);

server.getPluginCommand("chatformatter").setExecutor(new ChatFormatterCommand(configManager, audienceProvider, miniMessage));
server.getPluginCommand("chatformatter").setExecutor(new ChatFormatterCommand(pluginConfig, audienceProvider, miniMessage));
server.getPluginManager().registerEvents(new UpdaterController(updaterService, pluginConfig, audienceProvider, miniMessage), plugin);

ChatFormatterApiProvider.enable(this);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.eternalcode.formatter.config;

import eu.okaeri.configs.ConfigManager;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.serdes.commons.SerdesCommons;
import eu.okaeri.configs.yaml.snakeyaml.YamlSnakeYamlConfigurer;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;

import java.io.File;

public class ConfigService {

public <T extends OkaeriConfig> T create(Class<T> config, File file) {
T configFile = ConfigManager.create(config);

YamlSnakeYamlConfigurer yamlConfigurer = new YamlSnakeYamlConfigurer(this.createYaml());

configFile
.withConfigurer(yamlConfigurer)
.withSerdesPack(registry -> registry.register(new TemplateSerializer()))
.withBindFile(file)
.withRemoveOrphans(true)
.saveDefaults()
.load(true);

return configFile;
}

private Yaml createYaml() {
LoaderOptions loaderOptions = new LoaderOptions();
Constructor constructor = new Constructor(loaderOptions);

DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.AUTO);
dumperOptions.setIndent(2);
dumperOptions.setSplitLines(false);

Representer representer = new CustomRepresenter(dumperOptions);
Resolver resolver = new Resolver();

return new Yaml(constructor, representer, dumperOptions, loaderOptions, resolver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.eternalcode.formatter.config;

import com.google.common.collect.ImmutableMap;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


class CustomRepresenter extends Representer {

public CustomRepresenter(DumperOptions options) {
super(options);
this.representers.put(Integer.class, new RepresentInteger());
this.representers.put(ArrayList.class, new RepresentList());
this.representers.put(Boolean.class, new RepresentBoolean());
this.representers.put(String.class, new RepresentString());
this.representers.put(Map.class, new RepresentMap());
this.representers.put(HashMap.class, new RepresentMap());
this.representers.put(LinkedHashMap.class, new RepresentMap());
this.representers.put(ImmutableMap.class, new RepresentMap());
}

private class RepresentString implements Represent {
@Override
public Node representData(Object data) {
if (data instanceof String && ((String) data).contains(" ")) {
return representScalar(Tag.STR, (String) data, DumperOptions.ScalarStyle.DOUBLE_QUOTED);
}

return representScalar(Tag.STR, data.toString(), DumperOptions.ScalarStyle.PLAIN);
}
}

private class RepresentMap implements Represent {
public Node representData(Object data) {
return representMapping(Tag.MAP, (Map<?, ?>) data, DumperOptions.FlowStyle.BLOCK);
}
}

private class RepresentBoolean implements Represent {
@Override
public Node representData(Object data) {
return representScalar(Tag.BOOL, data.toString(), DumperOptions.ScalarStyle.PLAIN);
}
}

private class RepresentInteger implements Represent {
@Override
public Node representData(Object data) {
return representScalar(Tag.INT, data.toString(), DumperOptions.ScalarStyle.PLAIN);
}
}

private class RepresentList implements Represent {
@Override
public Node representData(Object data) {
return representSequence(getTag(data.getClass(), Tag.SEQ), (Iterable<?>) data, DumperOptions.FlowStyle.BLOCK);
}
}

}
Loading
Loading