From eac3433ed14c545f4bf77d5c89cc9724997fdf6f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 22 Sep 2022 19:08:17 +0200 Subject: [PATCH] Add MaxVersion --- src/de/steamwar/linkage/LinkageProcessor.java | 23 +++++++++++-- src/de/steamwar/linkage/MaxVersion.java | 32 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/de/steamwar/linkage/MaxVersion.java diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 14249ad..57d2cc8 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -37,6 +37,7 @@ import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; import java.io.*; import java.io.Writer; +import java.lang.annotation.Annotation; import java.util.*; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -289,17 +290,25 @@ public class LinkageProcessor extends AbstractProcessor { } private void specialElements(TypeElement typeElement, BuildPlan buildPlan, Consumer stringConsumer, Runnable inner) { + MinVersion minVersion = typeElement.getAnnotation(MinVersion.class); + MaxVersion maxVersion = typeElement.getAnnotation(MaxVersion.class); + EventMode eventMode = typeElement.getAnnotation(EventMode.class); + DiscordMode discordMode = typeElement.getAnnotation(DiscordMode.class); if (context == LinkageType.Context.SPIGOT) { - MinVersion minVersion = typeElement.getAnnotation(MinVersion.class); + errorOnNonNull(typeElement, eventMode, discordMode); if (minVersion != null) { buildPlan.addImport("de.steamwar.core.Core"); stringConsumer.accept("if (Core.getVersion() >= " + minVersion.value() + ") {"); } + if (maxVersion != null) { + buildPlan.addImport("de.steamwar.core.Core"); + stringConsumer.accept("if (Core.getVersion() < " + maxVersion.value() + ") {"); + } inner.run(); + if (maxVersion != null) stringConsumer.accept("}"); if (minVersion != null) stringConsumer.accept("}"); } else { - EventMode eventMode = typeElement.getAnnotation(EventMode.class); - DiscordMode discordMode = typeElement.getAnnotation(DiscordMode.class); + errorOnNonNull(typeElement, minVersion, maxVersion); if (eventMode != null) { buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); stringConsumer.accept("if (" + eventMode.value().getPrefix() + "BungeeCore.EVENT_MODE) {"); @@ -313,4 +322,12 @@ public class LinkageProcessor extends AbstractProcessor { if (eventMode != null) stringConsumer.accept("}"); } } + + private void errorOnNonNull(TypeElement typeElement, Annotation... annotations) { + for (Annotation annotation : annotations) { + if (annotation != null) { + messager.printMessage(Diagnostic.Kind.ERROR, annotation.annotationType().getSimpleName() + " is not supported in " + context.name(), typeElement); + } + } + } } diff --git a/src/de/steamwar/linkage/MaxVersion.java b/src/de/steamwar/linkage/MaxVersion.java new file mode 100644 index 0000000..589a67f --- /dev/null +++ b/src/de/steamwar/linkage/MaxVersion.java @@ -0,0 +1,32 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@AllowedContexts(LinkageType.Context.SPIGOT) +@Retention(RetentionPolicy.SOURCE) +@Target({ElementType.TYPE}) +public @interface MaxVersion { + int value(); +}