diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 57d2cc8..41abf0e 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -294,6 +294,7 @@ public class LinkageProcessor extends AbstractProcessor { MaxVersion maxVersion = typeElement.getAnnotation(MaxVersion.class); EventMode eventMode = typeElement.getAnnotation(EventMode.class); DiscordMode discordMode = typeElement.getAnnotation(DiscordMode.class); + PluginCheck[] pluginChecks = typeElement.getAnnotationsByType(PluginCheck.class); if (context == LinkageType.Context.SPIGOT) { errorOnNonNull(typeElement, eventMode, discordMode); if (minVersion != null) { @@ -304,7 +305,14 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addImport("de.steamwar.core.Core"); stringConsumer.accept("if (Core.getVersion() < " + maxVersion.value() + ") {"); } + if (pluginChecks.length != 0) { + buildPlan.addImport("org.bukkit.Bukkit"); + stringConsumer.accept(Arrays.stream(pluginChecks).map(pluginCheck -> { + return "Bukkit.getPluginManager().getPlugin(\"" + pluginCheck.value() + "\") " + (pluginCheck.has() == PluginCheck.Has.THIS ? "=" : "!") + "= null"; + }).collect(Collectors.joining(" && ", "if (", ") {"))); + } inner.run(); + if (pluginChecks.length != 0) stringConsumer.accept("}"); if (maxVersion != null) stringConsumer.accept("}"); if (minVersion != null) stringConsumer.accept("}"); } else { @@ -317,7 +325,14 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addImport("de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig"); stringConsumer.accept("if (SteamwarDiscordBotConfig.loaded) {"); } + if (pluginChecks.length != 0) { + buildPlan.addImport("net.md_5.bungee.BungeeCord"); + stringConsumer.accept(Arrays.stream(pluginChecks).map(pluginCheck -> { + return "BungeeCord.getPluginManager().getPlugin(\"" + pluginCheck.value() + "\") " + (pluginCheck.has() == PluginCheck.Has.THIS ? "=" : "!") + "= null"; + }).collect(Collectors.joining(" && ", "if (", ") {"))); + } inner.run(); + if (pluginChecks.length != 0) stringConsumer.accept("}"); if (discordMode != null) stringConsumer.accept("}"); if (eventMode != null) stringConsumer.accept("}"); } diff --git a/src/de/steamwar/linkage/PluginCheck.java b/src/de/steamwar/linkage/PluginCheck.java new file mode 100644 index 0000000..6b9931a --- /dev/null +++ b/src/de/steamwar/linkage/PluginCheck.java @@ -0,0 +1,42 @@ +/* + * 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.*; + +@AllowedContexts({LinkageType.Context.BUNGEE, LinkageType.Context.SPIGOT}) +@Retention(RetentionPolicy.SOURCE) +@Target({ElementType.TYPE}) +@Repeatable(PluginCheck.PluginChecks.class) +public @interface PluginCheck { + Has has() default Has.THIS; + String value(); + + enum Has { + THIS, + NOT + } + + @Retention(RetentionPolicy.SOURCE) + @Target({ElementType.TYPE}) + @interface PluginChecks { + @SuppressWarnings("unused") PluginCheck[] value() default {}; + } +}