Skip to content

Commit 71f2026

Browse files
committed
Add a @ExcludePlatform annotation to exclude platforms, closes #2
1 parent 1cd2ecd commit 71f2026

File tree

6 files changed

+97
-6
lines changed

6 files changed

+97
-6
lines changed

core/src/main/java/dev/triassic/template/TemplateImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ public void initialize() {
5353
final long startTime = System.currentTimeMillis();
5454

5555
try {
56-
this.config = ConfigurationManager.load(dataDirectory, TemplateConfiguration.class);
56+
this.config = ConfigurationManager.load(
57+
dataDirectory, TemplateConfiguration.class, platformType);
5758
} catch (IOException e) {
5859
logger.error("Failed to load configuration", e);
5960
return;
6061
}
6162

6263
this.commandRegistry = new CommandRegistry(this, commandManager);
63-
commandRegistry.registerAll();
64+
commandRegistry.registerAll(platformType);
6465

6566
logger.info("Enabled in {}ms", System.currentTimeMillis() - startTime);
6667
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* SPDX-License-Identifier: CC0-1.0
3+
*
4+
* Dedicated to the public domain under CC0 1.0 Universal.
5+
*
6+
* You can obtain a full copy of the license at:
7+
* https://creativecommons.org/publicdomain/zero/1.0/
8+
*/
9+
10+
package dev.triassic.template.annotation;
11+
12+
import dev.triassic.template.util.PlatformType;
13+
import java.lang.annotation.ElementType;
14+
import java.lang.annotation.Retention;
15+
import java.lang.annotation.RetentionPolicy;
16+
import java.lang.annotation.Target;
17+
18+
/**
19+
* Excludes a command class or configuration field from certain platforms.
20+
*
21+
* <p>Use this to prevent commands or config nodes from being registered or loaded
22+
* on platforms where they are not supported. Elements without this annotation
23+
* are included on all platforms by default.</p>
24+
*/
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@Target({ElementType.TYPE, ElementType.FIELD})
27+
public @interface ExcludePlatform {
28+
29+
/**
30+
* The platforms on which the annotated element should be excluded.
31+
*
32+
* @return an array of {@link PlatformType} to exclude
33+
*/
34+
PlatformType[] value();
35+
}

core/src/main/java/dev/triassic/template/command/CommandRegistry.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
package dev.triassic.template.command;
1111

1212
import dev.triassic.template.TemplateImpl;
13+
import dev.triassic.template.annotation.ExcludePlatform;
1314
import dev.triassic.template.command.defaults.ReloadCommand;
15+
import dev.triassic.template.util.PlatformType;
16+
import java.util.Arrays;
1417
import java.util.List;
1518
import lombok.RequiredArgsConstructor;
1619
import org.incendo.cloud.CommandManager;
@@ -27,11 +30,21 @@ public final class CommandRegistry {
2730
/**
2831
* Registers all commands with the command manager.
2932
*/
30-
public void registerAll() {
33+
public void registerAll(final PlatformType platformType) {
3134
final List<TemplateCommand> commands = List.of(
3235
new ReloadCommand(instance)
3336
);
3437

35-
commands.forEach(command -> command.register(commandManager));
38+
commands.forEach(command -> {
39+
final ExcludePlatform exclude = command.getClass().getAnnotation(ExcludePlatform.class);
40+
if (exclude != null) {
41+
if (Arrays.asList(exclude.value()).contains(platformType)) {
42+
return;
43+
}
44+
}
45+
46+
command.register(commandManager);
47+
});
3648
}
49+
3750
}

core/src/main/java/dev/triassic/template/command/defaults/ReloadCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
package dev.triassic.template.command.defaults;
1111

1212
import dev.triassic.template.TemplateImpl;
13+
import dev.triassic.template.annotation.ExcludePlatform;
1314
import dev.triassic.template.command.Commander;
1415
import dev.triassic.template.command.TemplateCommand;
1516
import dev.triassic.template.configuration.ConfigurationManager;
1617
import dev.triassic.template.configuration.TemplateConfiguration;
18+
import dev.triassic.template.util.PlatformType;
1719
import net.kyori.adventure.text.Component;
1820
import net.kyori.adventure.text.format.NamedTextColor;
1921
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -23,6 +25,7 @@
2325
/**
2426
* A command that reloads the plugin's configuration.
2527
*/
28+
@ExcludePlatform({PlatformType.VELOCITY})
2629
public final class ReloadCommand extends TemplateCommand {
2730

2831
private final Logger logger;

core/src/main/java/dev/triassic/template/configuration/ConfigurationManager.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
package dev.triassic.template.configuration;
1111

1212
import dev.triassic.template.BuildParameters;
13+
import dev.triassic.template.annotation.ExcludePlatform;
14+
import dev.triassic.template.util.PlatformType;
1315
import java.io.IOException;
1416
import java.nio.file.Files;
1517
import java.nio.file.Path;
@@ -20,6 +22,9 @@
2022
import lombok.RequiredArgsConstructor;
2123
import org.spongepowered.configurate.CommentedConfigurationNode;
2224
import org.spongepowered.configurate.ConfigurateException;
25+
import org.spongepowered.configurate.objectmapping.ObjectMapper;
26+
import org.spongepowered.configurate.objectmapping.meta.Processor;
27+
import org.spongepowered.configurate.serialize.TypeSerializerCollection;
2328
import org.spongepowered.configurate.yaml.NodeStyle;
2429
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
2530

@@ -57,14 +62,22 @@ public final class ConfigurationManager<T> {
5762
*/
5863
public static <T> ConfigurationManager<T> load(
5964
Path path,
60-
final Class<T> clazz
65+
final Class<T> clazz,
66+
final PlatformType platformType
6167
) throws IOException {
6268
path = path.resolve("config.yml");
6369

6470
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
6571
.indent(2)
6672
.nodeStyle(NodeStyle.BLOCK)
67-
.defaultOptions(opts -> opts.header(HEADER))
73+
.defaultOptions(opts -> opts
74+
.header(HEADER)
75+
.serializers(TypeSerializerCollection.defaults().childBuilder()
76+
.registerAnnotatedObjects(ObjectMapper.factoryBuilder()
77+
.addProcessor(ExcludePlatform.class, excludePlatform(platformType))
78+
.build()
79+
)
80+
.build()))
6881
.path(path)
6982
.build();
7083

@@ -103,4 +116,17 @@ public CompletableFuture<Void> reload() {
103116
public T get() {
104117
return config.get();
105118
}
119+
120+
private static Processor.Factory<ExcludePlatform, Object> excludePlatform(
121+
final PlatformType platformType
122+
) {
123+
return (annotation, fieldType) -> (value, destination) -> {
124+
for (PlatformType platform : annotation.value()) {
125+
if (platformType.equals(platform)) {
126+
destination.parent().removeChild(destination.key());
127+
break;
128+
}
129+
}
130+
};
131+
}
106132
}

core/src/main/java/dev/triassic/template/configuration/TemplateConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package dev.triassic.template.configuration;
1111

12+
import dev.triassic.template.annotation.ExcludePlatform;
13+
import dev.triassic.template.util.PlatformType;
1214
import lombok.Getter;
1315
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
1416
import org.spongepowered.configurate.objectmapping.meta.Comment;
@@ -21,6 +23,17 @@
2123
@SuppressWarnings("FieldMayBeFinal")
2224
public class TemplateConfiguration {
2325

26+
/**
27+
* An example string that showcases the usage of {@link ExcludePlatform}.
28+
*
29+
* <p>Take note of how the {@link ExcludePlatform} annotation goes after
30+
* the {@link Comment} annotation, it is important to do it in this order,
31+
* otherwise the node will be recreated empty to add the comment.</p>
32+
*/
33+
@Comment("This string should not appear on Velocity and Bungeecord platforms.")
34+
@ExcludePlatform({PlatformType.BUNGEECORD, PlatformType.VELOCITY})
35+
private String exampleString = "This is an example string!";
36+
2437
/**
2538
* The version of the configuration file.
2639
*/

0 commit comments

Comments
 (0)