Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/src/main/java/dev/triassic/template/TemplateImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package dev.triassic.template;

import dev.triassic.template.annotation.PlatformSpecific;
import dev.triassic.template.command.CommandRegistry;
import dev.triassic.template.command.Commander;
import dev.triassic.template.configuration.ConfigurationManager;
Expand Down Expand Up @@ -49,18 +50,20 @@ public TemplateImpl(final TemplatePlugin plugin) {
/**
* Called when the bootstrapped plugin is done initializing.
*/
@PlatformSpecific(PlatformType.BUKKIT)
public void initialize() {
final long startTime = System.currentTimeMillis();

try {
this.config = ConfigurationManager.load(dataDirectory, TemplateConfiguration.class);
this.config = ConfigurationManager.load(
dataDirectory, TemplateConfiguration.class, platformType);
} catch (IOException e) {
logger.error("Failed to load configuration", e);
return;
}

this.commandRegistry = new CommandRegistry(this, commandManager);
commandRegistry.registerAll();
commandRegistry.registerAll(platformType);

logger.info("Enabled in {}ms", System.currentTimeMillis() - startTime);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-License-Identifier: CC0-1.0
*
* Dedicated to the public domain under CC0 1.0 Universal.
*
* You can obtain a full copy of the license at:
* https://creativecommons.org/publicdomain/zero/1.0/
*/

package dev.triassic.template.annotation;

import dev.triassic.template.util.PlatformType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks a class, method, or field as being specific to one or more platforms.
* Can be used for runtime checks or documentation.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR})
public @interface PlatformSpecific {

/**
* The platforms this class/method/field is intended for.
*/
PlatformType[] value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
package dev.triassic.template.command;

import dev.triassic.template.TemplateImpl;
import dev.triassic.template.annotation.PlatformSpecific;
import dev.triassic.template.command.defaults.ReloadCommand;
import dev.triassic.template.util.PlatformType;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.incendo.cloud.CommandManager;
Expand All @@ -27,11 +29,31 @@ public final class CommandRegistry {
/**
* Registers all commands with the command manager.
*/
public void registerAll() {
public void registerAll(PlatformType platform) {
final List<TemplateCommand> commands = List.of(
new ReloadCommand(instance)
);

commands.forEach(command -> command.register(commandManager));
commands.forEach(command -> {
if (isSupported(command.getClass(), platform)) {
command.register(commandManager);
} else {
System.out.println("Skipping " + command.getClass().getSimpleName()
+ " (not supported on " + platform + ")");
}
});
}

private static boolean isSupported(Class<?> clazz, PlatformType platform) {
PlatformSpecific annotation = clazz.getAnnotation(PlatformSpecific.class);
if (annotation == null) {
return true;
}
for (PlatformType allowed : annotation.value()) {
if (allowed == platform) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
package dev.triassic.template.command.defaults;

import dev.triassic.template.TemplateImpl;
import dev.triassic.template.annotation.PlatformSpecific;
import dev.triassic.template.command.Commander;
import dev.triassic.template.command.TemplateCommand;
import dev.triassic.template.configuration.ConfigurationManager;
import dev.triassic.template.configuration.TemplateConfiguration;
import dev.triassic.template.util.PlatformType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -23,6 +25,7 @@
/**
* A command that reloads the plugin's configuration.
*/
@PlatformSpecific({PlatformType.BUKKIT, PlatformType.PAPER})
public final class ReloadCommand extends TemplateCommand {

private final Logger logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
package dev.triassic.template.configuration;

import dev.triassic.template.BuildParameters;
import dev.triassic.template.annotation.PlatformSpecific;
import dev.triassic.template.util.PlatformType;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicReference;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.objectmapping.meta.Processor;
import org.spongepowered.configurate.serialize.TypeSerializerCollection;
import org.spongepowered.configurate.yaml.NodeStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;

Expand Down Expand Up @@ -57,14 +63,22 @@ public final class ConfigurationManager<T> {
*/
public static <T> ConfigurationManager<T> load(
Path path,
final Class<T> clazz
final Class<T> clazz,
final PlatformType platform
) throws IOException {
path = path.resolve("config.yml");

final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
.indent(2)
.nodeStyle(NodeStyle.BLOCK)
.defaultOptions(opts -> opts.header(HEADER))
.defaultOptions(opts -> opts
.header(HEADER)
.serializers(TypeSerializerCollection.defaults().childBuilder()
.registerAnnotatedObjects(ObjectMapper.factoryBuilder()
.addProcessor(PlatformSpecific.class, platformSpecific(platform))
.build()
)
.build()))
.path(path)
.build();

Expand All @@ -78,6 +92,19 @@ public static <T> ConfigurationManager<T> load(
return new ConfigurationManager<>(clazz, loader, new AtomicReference<>(config));
}

private static Processor.Factory<PlatformSpecific, Object> platformSpecific(
PlatformType currentPlatform
) {
return (annotation, fieldType) -> (value, destination) -> {
boolean allowed = Arrays.asList(annotation.value()).contains(currentPlatform);

if (!allowed) {
System.out.println("Removing " + destination.key());
destination.parent().removeChild(destination.key());
}
};
}

/**
* Asynchronously reloads the configuration from disk.
* The current configuration object is updated with the newly loaded data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

package dev.triassic.template.configuration;

import dev.triassic.template.annotation.PlatformSpecific;
import dev.triassic.template.util.PlatformType;
import lombok.Getter;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
Expand All @@ -21,6 +23,10 @@
@SuppressWarnings("FieldMayBeFinal")
public class TemplateConfiguration {

@Comment("Should only appear on Bukkit-like platforms.")
@PlatformSpecific({PlatformType.BUKKIT, PlatformType.PAPER})
private String exampleString = "hello, bukkit!";

/**
* The version of the configuration file.
*/
Expand Down
Loading