Skip to content

Commit

Permalink
feat: add platform module with config and commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Jan 21, 2022
1 parent c01f9b8 commit 3c8dc9e
Show file tree
Hide file tree
Showing 45 changed files with 2,783 additions and 0 deletions.
40 changes: 40 additions & 0 deletions platform/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
allprojects {
dependencies {
api project(':core')

testImplementation(testFixtures(project(":core")))

testFixturesImplementation(testFixtures(project(':api')))
testFixturesImplementation(testFixtures(project(':core')))

testFixturesImplementation 'org.spongepowered:configurate-core:4.1.2'
}
}

subprojects {
dependencies {
implementation project(':platform')

testImplementation(testFixtures(project(":platform")))

testFixturesImplementation(testFixtures(project(':platform')))
}
}

dependencies {
implementation 'cloud.commandframework:cloud-core:1.6.1'
implementation 'cloud.commandframework:cloud-annotations:1.6.1'

implementation 'net.kyori:adventure-platform-api:4.0.1'
implementation 'net.kyori:adventure-text-minimessage:4.2.0-SNAPSHOT'

implementation 'org.spongepowered:configurate-core:4.1.2'
implementation 'org.spongepowered:configurate-yaml:4.1.2'
implementation 'org.spongepowered:configurate-xml:4.1.2'
implementation 'org.spongepowered:configurate-hocon:4.1.2'
implementation 'org.spongepowered:configurate-gson:4.1.2'

implementation 'com.google.guava:guava:21.0'

testFixturesImplementation 'cloud.commandframework:cloud-core:1.6.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.platform.command;

import cloud.commandframework.CommandManager;
import cloud.commandframework.annotations.AnnotationParser;
import net.silthus.template.platform.sender.Sender;

public interface Command {

void register(CommandManager<Sender> commandManager, AnnotationParser<Sender> parser);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.platform.command;

import cloud.commandframework.CommandManager;
import cloud.commandframework.annotations.AnnotationParser;
import cloud.commandframework.arguments.parser.StandardParameters;
import cloud.commandframework.meta.CommandMeta;
import net.silthus.template.platform.sender.Sender;
import org.jetbrains.annotations.NotNull;

public final class Commands {
private final CommandManager<Sender> commandManager;
private final AnnotationParser<Sender> annotationParser;

public Commands(CommandManager<Sender> commandManager) {
this.commandManager = commandManager;
this.annotationParser = createAnnotationParser();
}

public void register(Command... commands) {
for (final Command command : commands) {
command.register(commandManager, annotationParser);
}
}

@NotNull
private AnnotationParser<Sender> createAnnotationParser() {
return new AnnotationParser<>(
this.commandManager,
Sender.class,
p -> CommandMeta.simple()
.with(CommandMeta.DESCRIPTION, p.get(StandardParameters.DESCRIPTION, "No description"))
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.silthus.template.platform.command.commands;

import cloud.commandframework.CommandManager;
import cloud.commandframework.annotations.AnnotationParser;
import cloud.commandframework.annotations.Argument;
import cloud.commandframework.annotations.CommandMethod;
import net.silthus.template.platform.command.Command;
import net.silthus.template.platform.config.Config;
import net.silthus.template.platform.sender.Sender;

import static net.silthus.template.platform.config.ConfigKeys.TEST;
import static net.silthus.template.platform.locale.Messages.TEST_MESSAGE;

public class ExampleCommand implements Command {
private final Config config;

public ExampleCommand(Config config) {this.config = config;}

@Override
public void register(CommandManager<Sender> commandManager, AnnotationParser<Sender> parser) {
parser.parse(this);
}

@CommandMethod("template test [text]")
public void example(Sender sender, @Argument("text") String text) {
if (text == null)
text = config.get(TEST);
TEST_MESSAGE.send(sender, text);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.platform.config;

import net.silthus.template.platform.config.key.ConfigKey;

public interface Config {

void load();

void save();

void reload();

<T> T get(ConfigKey<T> key);

<T> void set(ConfigKey<T> key, T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.platform.config;

import java.util.List;
import net.silthus.template.platform.config.key.ConfigKey;
import net.silthus.template.platform.config.key.KeyedConfiguration;

import static net.silthus.template.platform.config.key.ConfigKeyFactory.key;
import static net.silthus.template.platform.config.key.ConfigKeyFactory.modifiable;

public final class ConfigKeys {

private ConfigKeys() {
}

public static final ConfigKey<String> TEST = modifiable(key(
config -> config.getString("test", "Hi from template :)")),
(config, value) -> config.set("test", value)
);

/**
* A list of the keys defined in this class.
*/
private static final List<? extends ConfigKey<?>> KEYS = KeyedConfiguration.initialise(ConfigKeys.class);

public static List<? extends ConfigKey<?>> getKeys() {
return KEYS;
}

public static final class InvalidConfig extends RuntimeException {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.platform.config;

import net.silthus.template.platform.config.adapter.ConfigurationAdapter;
import net.silthus.template.platform.config.key.KeyedConfiguration;

public final class TemplateConfig extends KeyedConfiguration {

public TemplateConfig(ConfigurationAdapter adapter) {
super(adapter, ConfigKeys.getKeys());

init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* sChat, a Supercharged Minecraft Chat Plugin
* Copyright (C) Silthus <https://www.github.com/silthus>
* Copyright (C) sChat team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.silthus.template.platform.config.adapter;

import java.io.IOException;
import java.nio.file.Path;
import java.util.function.Function;
import net.kyori.adventure.text.Component;
import net.silthus.template.platform.config.serializers.MiniMessageComponentSerializer;
import net.silthus.template.platform.config.serializers.SettingsSerializer;
import net.silthus.template.pointer.Settings;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.ConfigurationOptions;
import org.spongepowered.configurate.loader.AbstractConfigurationLoader;
import org.spongepowered.configurate.loader.ConfigurationLoader;
import org.spongepowered.configurate.serialize.TypeSerializerCollection;

public abstract class ConfigurateAdapter<T extends AbstractConfigurationLoader.Builder<T, L>, L extends AbstractConfigurationLoader<?>> extends ConfigurateConfigSection implements ConfigurationAdapter {

private static final TypeSerializerCollection SERIALIZERS = TypeSerializerCollection.builder()
.register(Component.class, new MiniMessageComponentSerializer())
.register(Settings.class, new SettingsSerializer())
.build();

private static final Function<ConfigurationOptions, ConfigurationOptions> DEFAULT_OPTIONS = options ->
options.serializers(serializers -> serializers.registerAll(SERIALIZERS));

private final ConfigurationLoader<? extends ConfigurationNode> loader;

protected ConfigurateAdapter(Path path) {
final AbstractConfigurationLoader.Builder<T, L> loader = createLoader(path);
this.loader = loader.defaultOptions(DEFAULT_OPTIONS.apply(loader.defaultOptions())).build();
}

protected abstract AbstractConfigurationLoader.Builder<T, L> createLoader(Path path);

@Override
public void save() {
try {
loader.save(getRoot());
} catch (ConfigurateException e) {
throw new SaveFailed(e);
}
}

@Override
public void load() {
try {
setRoot(loader.load());
} catch (IOException e) {
throw new LoadFailed(e);
}
}
}
Loading

0 comments on commit 3c8dc9e

Please sign in to comment.