From ca589bd07c57b44a1332ce7650c8d995a5e2b25c Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Mon, 29 Sep 2025 10:17:49 +0200 Subject: [PATCH] Add Linkage to CommonCore and implement SpigotLinker used in BauSystem --- .../src/de/steamwar/bausystem/BauSystem.java | 160 ++++-------------- .../features/bau/BauInfoBauGuiItem.java | 1 - .../features/loadtimer/LoadtimerListener.java | 1 - .../features/region/ColorCommand.java | 2 - .../region/RegionScoreboardElement.java | 3 - .../features/script/event/EventListener.java | 2 - .../features/techhider/TechHiderCommand.java | 4 - .../util/items/NightVisionBauGuiItem.java | 1 - .../linkage/specific/ScoreboardItem.java | 37 ---- CommonCore/Linkage/build.gradle.kts | 26 +++ .../de/steamwar/linkage/AbstractLinker.java | 152 +++++++++++++++++ .../src/de/steamwar/linkage/Linked.java | 0 .../de/steamwar/linkage/LinkedInstance.java | 0 .../src/de/steamwar/linkage/MaxVersion.java | 0 .../src/de/steamwar/linkage/MinVersion.java | 0 .../src/de/steamwar/linkage/PluginCheck.java | 0 .../src/de/steamwar/linkage/api/Disable.java | 0 .../src/de/steamwar/linkage/api/Enable.java | 0 CommonCore/build.gradle.kts | 1 + .../src/de/steamwar/linkage/SpigotLinker.java | 82 +++++++++ settings.gradle.kts | 3 +- 21 files changed, 297 insertions(+), 178 deletions(-) delete mode 100644 BauSystem/BauSystem_Main/src/de/steamwar/bausystem/linkage/specific/ScoreboardItem.java create mode 100644 CommonCore/Linkage/build.gradle.kts create mode 100644 CommonCore/Linkage/src/de/steamwar/linkage/AbstractLinker.java rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/Linked.java (100%) rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/LinkedInstance.java (100%) rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/MaxVersion.java (100%) rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/MinVersion.java (100%) rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/PluginCheck.java (100%) rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/api/Disable.java (100%) rename {BauSystem/BauSystem_Main => CommonCore/Linkage}/src/de/steamwar/linkage/api/Enable.java (100%) create mode 100644 SpigotCore/SpigotCore_Main/src/de/steamwar/linkage/SpigotLinker.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index af84b780..4d473261 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -38,41 +38,29 @@ import de.steamwar.bausystem.utils.TickListener; import de.steamwar.bausystem.utils.TickManager; import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.command.AbstractValidator; -import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommandUtils; import de.steamwar.core.CRIUSleepEvent; -import de.steamwar.core.Core; import de.steamwar.core.WorldEditRendererCUIEditor; -import de.steamwar.linkage.LinkedInstance; -import de.steamwar.linkage.MaxVersion; -import de.steamwar.linkage.MinVersion; -import de.steamwar.linkage.PluginCheck; -import de.steamwar.linkage.api.Disable; -import de.steamwar.linkage.api.Enable; +import de.steamwar.linkage.AbstractLinker; +import de.steamwar.linkage.SpigotLinker; import de.steamwar.message.Message; -import de.steamwar.network.packets.PacketHandler; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; -import java.io.*; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.logging.Level; -import java.util.stream.Collectors; public class BauSystem extends JavaPlugin implements Listener { @@ -83,7 +71,7 @@ public class BauSystem extends JavaPlugin implements Listener { @Getter private static BauSystem instance; - private final Map, Object> instances = new HashMap<>(); + private SpigotLinker linker; @Override public void onEnable() { @@ -96,115 +84,43 @@ public class BauSystem extends JavaPlugin implements Listener { SWUtils.setBausystem(instance); RegionSystem.INSTANCE.load(); - /* - try { - PrototypeLoader.load(); - RegionLoader.load(); - } catch (SecurityException e) { - Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); - Bukkit.shutdown(); - System.exit(1); - return; - } - - new Updater(PrototypeLoader.file, PrototypeLoader::load); - new Updater(RegionLoader.file, RegionLoader::load); - */ SWCommandUtils.addValidator(Player.class, validator(Permission.BUILD)); SWCommandUtils.addValidator(CommandSender.class, validator(Permission.BUILD)); SWCommandUtils.addValidator("supervisor", validator(Permission.SUPERVISOR)); SWCommandUtils.addValidator("owner", validator(Permission.OWNER)); - instances.put(BauServer.class, BauServer.getInstance()); - List> classes = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/META-INF/annotations/de.steamwar.linkage.Linked"))) - .lines() - .map(s -> { - try { - return Class.forName(s, false, BauSystem.class.getClassLoader()); - } catch (ClassNotFoundException | NoClassDefFoundError e) { - if (e.getMessage().equals(s)) { - Bukkit.shutdown(); - throw new SecurityException(e.getMessage(), e); - } - return null; - } - }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - classes.forEach(clazz -> { - MinVersion minVersion = clazz.getAnnotation(MinVersion.class); - MaxVersion maxVersion = clazz.getAnnotation(MaxVersion.class); - PluginCheck[] pluginChecks = clazz.getAnnotationsByType(PluginCheck.class); - if (minVersion != null && Core.getVersion() < minVersion.value()) { - return; - } - if (maxVersion != null && Core.getVersion() > maxVersion.value()) { - return; - } - for (PluginCheck pluginCheck : pluginChecks) { - if (pluginCheck.has() == PluginCheck.Has.THIS && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) != null) { - continue; + linker = new SpigotLinker(BauSystem.getInstance(), BauSystem.MESSAGE) { + @Override + protected void linkObject(Object any) { + super.linkObject(any); + if (any instanceof LuaLib) { + SteamWarLuaPlugin.add((LuaLib) any); } - if (pluginCheck.has() == PluginCheck.Has.NOT && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) == null) { - continue; + if (any instanceof ScoreboardElement) { + BauScoreboard.addElement((ScoreboardElement) any); } - return; - } - - Object any; - try { - any = clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - getLogger().log(Level.SEVERE, e.getMessage(), e); - Bukkit.shutdown(); - throw new SecurityException(e.getMessage()); - } - - instances.put(clazz, any); - if (any instanceof Enable) { - ((Enable) any).enable(); - } - if (any instanceof SWCommand) { - ((SWCommand) any).setMessage(BauSystem.MESSAGE); - } - if (any instanceof Listener) { - Bukkit.getPluginManager().registerEvents((Listener) any, BauSystem.getInstance()); - } - if (any instanceof PacketHandler) { - ((PacketHandler) any).register(); - } - if (any instanceof LuaLib) { - SteamWarLuaPlugin.add((LuaLib) any); - } - if (any instanceof ScoreboardElement) { - BauScoreboard.addElement((ScoreboardElement) any); - } - if (any instanceof BauGuiItem) { - BauGUI.addItem((BauGuiItem) any); - } - if (any instanceof PanzernAlgorithm) { - Panzern.add((PanzernAlgorithm) any); - } - if (any instanceof ConfigConverter) { - Config.addConfigConverter((ConfigConverter) any); - } - if (any instanceof BoundingBoxLoader) { - ((BoundingBoxLoader) any).load(); - } - }); - - instances.forEach((clazz, o) -> { - for (Field field : clazz.getFields()) { - if (field.getAnnotation(LinkedInstance.class) != null) { - try { - field.set(o, instances.get(field.getType())); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } + if (any instanceof BauGuiItem) { + BauGUI.addItem((BauGuiItem) any); + } + if (any instanceof PanzernAlgorithm) { + Panzern.add((PanzernAlgorithm) any); + } + if (any instanceof ConfigConverter) { + Config.addConfigConverter((ConfigConverter) any); + } + if (any instanceof BoundingBoxLoader) { + ((BoundingBoxLoader) any).load(); } } - }); + }; + try { + linker.addLinkableInstance(BauServer.getInstance()); + linker.link(); + } catch (AbstractLinker.LinkException e) { + getLogger().log(Level.SEVERE, "Could not link a class.", e); + Bukkit.shutdown(); + } TickListener.impl.init(); @@ -221,15 +137,7 @@ public class BauSystem extends JavaPlugin implements Listener { @Override public void onDisable() { - instances.forEach((aClass, o) -> { - if (o instanceof Listener) { - HandlerList.unregisterAll((Listener) o); - } - if (o instanceof Disable) { - ((Disable) o).disable(); - } - }); - + linker.unlink(); WorldData.write(); RegionSystem.INSTANCE.save(); Config.getInstance().saveAll(); diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauInfoBauGuiItem.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauInfoBauGuiItem.java index 8a3cffb4..6553f44a 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauInfoBauGuiItem.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauInfoBauGuiItem.java @@ -26,7 +26,6 @@ import de.steamwar.bausystem.linkage.specific.BauGuiItem; import de.steamwar.bausystem.region.FlagOptional; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.flags.Flag; -import de.steamwar.core.Core; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; import de.steamwar.sql.SteamwarUser; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerListener.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerListener.java index c528b9ad..d9cf2118 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerListener.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerListener.java @@ -23,7 +23,6 @@ import de.steamwar.bausystem.region.Region; import de.steamwar.core.TrickyTrialsWrapper; import de.steamwar.linkage.Linked; import org.bukkit.Material; -import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPlaceEvent; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java index 6c086ded..4b03cf8a 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java @@ -25,8 +25,6 @@ import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.flags.ColorMode; import de.steamwar.bausystem.region.flags.Flag; -import de.steamwar.bausystem.region.utils.RegionExtensionType; -import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.command.SWCommand; import de.steamwar.command.TypeValidator; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java index b810bb1e..9164040f 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionScoreboardElement.java @@ -23,12 +23,9 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.utils.ScoreboardElement; import de.steamwar.linkage.Linked; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; -import java.io.File; import java.util.List; -import java.util.Optional; @Linked public class RegionScoreboardElement implements ScoreboardElement { diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java index 54ddac4e..33946536 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/script/event/EventListener.java @@ -25,8 +25,6 @@ import de.steamwar.bausystem.features.script.ScriptRunner; import de.steamwar.bausystem.features.script.lua.SteamWarGlobalLuaPlugin; import de.steamwar.bausystem.features.script.lua.libs.StorageLib; import de.steamwar.bausystem.region.Region; -import de.steamwar.bausystem.region.utils.RegionExtensionType; -import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.core.TrickyTrialsWrapper; import de.steamwar.linkage.Linked; import org.bukkit.Bukkit; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java index 5f205e79..91a3d3a0 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java @@ -34,16 +34,12 @@ import de.steamwar.linkage.LinkedInstance; import de.steamwar.techhider.TechHider; import net.md_5.bungee.api.ChatMessageType; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; -import java.io.File; import java.util.*; import java.util.stream.Collectors; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/NightVisionBauGuiItem.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/NightVisionBauGuiItem.java index 03ebb495..fb73dd8b 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/NightVisionBauGuiItem.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/NightVisionBauGuiItem.java @@ -28,7 +28,6 @@ import de.steamwar.linkage.Linked; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; -import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionEffectType; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/linkage/specific/ScoreboardItem.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/linkage/specific/ScoreboardItem.java deleted file mode 100644 index 6a5dad27..00000000 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/linkage/specific/ScoreboardItem.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2021 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.bausystem.linkage.specific; - -import de.steamwar.bausystem.region.Region; -import org.bukkit.entity.Player; - -public interface ScoreboardItem { - - /** - * Returns one Scoreboard line. If {@code null} result will be ignored. - * If return value contains {@code '?'} it will be replaced to the color - * code of the current {@link Region}. - * - * @param player the player to create the scoreboard line for - * @param region the region the player is in - * @return the String to send, can be {@code null} - */ - String getString(Player player, Region region); -} diff --git a/CommonCore/Linkage/build.gradle.kts b/CommonCore/Linkage/build.gradle.kts new file mode 100644 index 00000000..dd139c6b --- /dev/null +++ b/CommonCore/Linkage/build.gradle.kts @@ -0,0 +1,26 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2024 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 . + */ + +plugins { + steamwar.java +} + +dependencies { + compileOnly(libs.classindex) +} diff --git a/CommonCore/Linkage/src/de/steamwar/linkage/AbstractLinker.java b/CommonCore/Linkage/src/de/steamwar/linkage/AbstractLinker.java new file mode 100644 index 00000000..ab401aa2 --- /dev/null +++ b/CommonCore/Linkage/src/de/steamwar/linkage/AbstractLinker.java @@ -0,0 +1,152 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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 de.steamwar.linkage.api.Disable; +import de.steamwar.linkage.api.Enable; +import lombok.NonNull; +import lombok.experimental.StandardException; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +public abstract class AbstractLinker { + + protected final T plugin; + private Map, Object> instances = new HashMap<>(); + + protected AbstractLinker(@NonNull T plugin) { + this.plugin = plugin; + instances.put(plugin.getClass(), plugin); + } + + @StandardException + public static class LinkException extends Exception { + } + + public final void link() throws LinkException { + List> classes; + try { + classes = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream("/META-INF/annotations/de.steamwar.linkage.Linked"))) + .lines() + .map(s -> { + try { + return Class.forName(s, false, plugin.getClass().getClassLoader()); + } catch (ClassNotFoundException | NoClassDefFoundError e) { + throw new SecurityException(e.getMessage(), e); + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } catch (SecurityException e) { + Throwable cause = e.getCause(); + throw new LinkException(cause.getMessage(), cause); + } + + try { + classes.forEach(clazz -> { + MinVersion minVersion = clazz.getAnnotation(MinVersion.class); + MaxVersion maxVersion = clazz.getAnnotation(MaxVersion.class); + if (!versionCheck(clazz, minVersion, maxVersion)) return; + PluginCheck[] pluginChecks = clazz.getAnnotationsByType(PluginCheck.class); + for (PluginCheck pluginCheck : pluginChecks) { + if (!pluginCheck(clazz, pluginCheck)) return; + } + + Object any; + try { + any = clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new SecurityException(e.getMessage()); + } + instances.put(clazz, any); + + if (any instanceof Enable) { + ((Enable) any).enable(); + } + linkObject(any); + }); + } catch (SecurityException e) { + Throwable cause = e.getCause(); + throw new LinkException(cause.getMessage(), cause); + } + + try { + instances.forEach((clazz, o) -> { + for (Field field : clazz.getFields()) { + if (field.getAnnotation(LinkedInstance.class) != null) { + System.out.println("Setting " + field.getName() + " to " + instances.get(field.getType())); + try { + field.set(o, instances.get(field.getType())); + } catch (IllegalAccessException e) { + throw new SecurityException(e); + } + } + } + }); + } catch (SecurityException e) { + Throwable cause = e.getCause(); + throw new LinkException(cause.getMessage(), cause); + } + } + + public final void unlink() { + instances.forEach((aClass, any) -> { + unlinkObject(any); + if (any instanceof Disable) { + ((Disable) any).disable(); + } + }); + } + + public final void addLinkableInstance(@NonNull Object instance) { + instances.put(instance.getClass(), instance); + } + + /** + * @return {@code true} if the clazz passes the checks {@code false} otherwise + */ + protected boolean versionCheck(@NonNull Class clazz, MinVersion minVersion, MaxVersion maxVersion) { + return true; + } + + /** + * @return {@code true} if the clazz passes the checks {@code false} otherwise + */ + protected boolean pluginCheck(@NonNull Class clazz, PluginCheck pluginCheck) { + return true; + } + + /** + * There is no need in calling {@link Enable#enable()} by this method. + */ + protected abstract void linkObject(Object any); + + /** + * There is no need in calling {@link Disable#disable()} ()} by this method. + */ + protected abstract void unlinkObject(Object any); +} diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/Linked.java b/CommonCore/Linkage/src/de/steamwar/linkage/Linked.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/Linked.java rename to CommonCore/Linkage/src/de/steamwar/linkage/Linked.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/LinkedInstance.java b/CommonCore/Linkage/src/de/steamwar/linkage/LinkedInstance.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/LinkedInstance.java rename to CommonCore/Linkage/src/de/steamwar/linkage/LinkedInstance.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/MaxVersion.java b/CommonCore/Linkage/src/de/steamwar/linkage/MaxVersion.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/MaxVersion.java rename to CommonCore/Linkage/src/de/steamwar/linkage/MaxVersion.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/MinVersion.java b/CommonCore/Linkage/src/de/steamwar/linkage/MinVersion.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/MinVersion.java rename to CommonCore/Linkage/src/de/steamwar/linkage/MinVersion.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/PluginCheck.java b/CommonCore/Linkage/src/de/steamwar/linkage/PluginCheck.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/PluginCheck.java rename to CommonCore/Linkage/src/de/steamwar/linkage/PluginCheck.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/api/Disable.java b/CommonCore/Linkage/src/de/steamwar/linkage/api/Disable.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/api/Disable.java rename to CommonCore/Linkage/src/de/steamwar/linkage/api/Disable.java diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/linkage/api/Enable.java b/CommonCore/Linkage/src/de/steamwar/linkage/api/Enable.java similarity index 100% rename from BauSystem/BauSystem_Main/src/de/steamwar/linkage/api/Enable.java rename to CommonCore/Linkage/src/de/steamwar/linkage/api/Enable.java diff --git a/CommonCore/build.gradle.kts b/CommonCore/build.gradle.kts index 8e2df2bc..7105676f 100644 --- a/CommonCore/build.gradle.kts +++ b/CommonCore/build.gradle.kts @@ -25,4 +25,5 @@ dependencies { api(project(":CommonCore:SQL")) api(project(":CommonCore:Network")) api(project(":CommonCore:Data")) + api(project(":CommonCore:Linkage")) } diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/linkage/SpigotLinker.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/linkage/SpigotLinker.java new file mode 100644 index 00000000..35abb954 --- /dev/null +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/linkage/SpigotLinker.java @@ -0,0 +1,82 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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 de.steamwar.command.SWCommand; +import de.steamwar.core.Core; +import de.steamwar.message.Message; +import de.steamwar.network.packets.PacketHandler; +import lombok.NonNull; +import org.bukkit.Bukkit; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +public class SpigotLinker extends AbstractLinker { + + private final Message message; + + public SpigotLinker(@NonNull JavaPlugin plugin, Message message) { + super(plugin); + this.message = message; + } + + @Override + protected boolean versionCheck(@NonNull Class clazz, MinVersion minVersion, MaxVersion maxVersion) { + if (minVersion != null && Core.getVersion() < minVersion.value()) { + return false; + } + if (maxVersion != null && Core.getVersion() > maxVersion.value()) { + return false; + } + return true; + } + + @Override + protected boolean pluginCheck(@NonNull Class clazz, PluginCheck pluginCheck) { + if (pluginCheck.has() == PluginCheck.Has.THIS && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) != null) { + return true; + } + if (pluginCheck.has() == PluginCheck.Has.NOT && Bukkit.getPluginManager().getPlugin(pluginCheck.value()) == null) { + return true; + } + return false; + } + + @Override + protected void linkObject(Object any) { + if (any instanceof SWCommand) { + ((SWCommand) any).setMessage(message); + } + if (any instanceof Listener) { + Bukkit.getPluginManager().registerEvents((Listener) any, plugin); + } + if (any instanceof PacketHandler) { + ((PacketHandler) any).register(); + } + } + + @Override + protected void unlinkObject(Object any) { + if (any instanceof Listener) { + HandlerList.unregisterAll((Listener) any); + } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 3bb2a9d8..7be8fc09 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -187,7 +187,8 @@ include( "CommonCore", "CommonCore:Data", "CommonCore:SQL", - "CommonCore:Network" + "CommonCore:Network", + "CommonCore:Linkage" ) include(