From 15fbb0241cac402f8958ce810ec33beb8aa8efe1 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 17 Apr 2021 14:36:14 +0200 Subject: [PATCH] Add LinkageUtils Add LinkedCommand Add LinkedListener --- BauSystem_Main/build.gradle | 11 ++++ .../src/de/steamwar/bausystem/BauSystem.java | 14 ++++ .../bausystem/linkage/LinkageUtils.java | 66 +++++++++++++++++++ .../bausystem/linkage/LinkedCommand.java | 33 ++++++++++ .../bausystem/linkage/LinkedListener.java | 33 ++++++++++ build.gradle | 2 +- gradle.properties | 1 - 7 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java diff --git a/BauSystem_Main/build.gradle b/BauSystem_Main/build.gradle index 1de23ece..0f115ae6 100644 --- a/BauSystem_Main/build.gradle +++ b/BauSystem_Main/build.gradle @@ -51,8 +51,19 @@ dependencies { annotationProcessor 'org.projectlombok:lombok:1.18.6' testAnnotationProcessor 'org.projectlombok:lombok:1.18.6' + implementation 'org.atteo.classindex:classindex:3.4' + testImplementation 'org.atteo.classindex:classindex:3.4' + annotationProcessor 'org.atteo.classindex:classindex:3.4' + testAnnotationProcessor 'org.atteo.classindex:classindex:3.4' + compileOnly files("${projectDir}/../lib/Spigot-1.15.jar") compileOnly files("${projectDir}/../lib/WorldEdit-1.15.jar") compileOnly files("${projectDir}/../lib/ProtocolLib.jar") compileOnly files("${projectDir}/../lib/SpigotCore.jar") } + +processResources { + from("build/classes/java/main/META-INF/annotations/") { + into("de.steamwar.bausystem") + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index b480c822..fd7eb15f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -20,7 +20,11 @@ package de.steamwar.bausystem; import de.steamwar.bausystem.config.BauServer; +import de.steamwar.bausystem.linkage.LinkageUtils; +import de.steamwar.bausystem.linkage.LinkedCommand; +import de.steamwar.bausystem.linkage.LinkedListener; import lombok.Getter; +import org.bukkit.Bukkit; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; @@ -35,6 +39,16 @@ public class BauSystem extends JavaPlugin implements Listener { public void onEnable() { instance = this; + LinkageUtils.link(LinkedCommand.class, clazz -> clazz.getSuperclass() == de.steamwar.command.SWCommand.class, o -> {}); + LinkageUtils.link(LinkedListener.class, clazz -> { + for (Class interfaceClazz : clazz.getInterfaces()) { + if (interfaceClazz == Listener.class) { + return true; + } + } + return false; + }, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.instance)); + new BauServer(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java new file mode 100644 index 00000000..c91c6e2f --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java @@ -0,0 +1,66 @@ +/* + * 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; + +import de.steamwar.bausystem.BauSystem; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.function.Consumer; +import java.util.function.Predicate; + +@UtilityClass +public class LinkageUtils { + + public void link(Class source, Predicate> loadPredicate, Consumer loader) { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de/steamwar/baussystem/" + source.getTypeName())))) { + bufferedReader.lines().forEach(s -> { + try { + Class clazz = Class.forName(s); + if (!loadPredicate.test(clazz)) { + return; + } + loader.accept(constructInstance(clazz)); + } catch (ClassNotFoundException e) { + // ignored + } + }); + } catch (IOException e) { + Bukkit.shutdown(); + return; + } + } + + private Object constructInstance(Class clazz) { + try { + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + return constructor.newInstance(); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { + throw new SecurityException(e.getMessage(), e.getCause()); + } + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java new file mode 100644 index 00000000..40f853f7 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.atteo.classindex.IndexAnnotated; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@IndexAnnotated +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.TYPE}) +public @interface LinkedCommand { +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java new file mode 100644 index 00000000..afd81a20 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.atteo.classindex.IndexAnnotated; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@IndexAnnotated +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.TYPE}) +public @interface LinkedListener { +} diff --git a/build.gradle b/build.gradle index 0c16e144..e7f83ccd 100644 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ if (file("steamwar.properties").exists()) { } ext { - buildName = 'BauSystem' + buildName = 'BauSystem2.0' artifactName = 'bausystem' uberJarName = "${buildName}-all.jar" diff --git a/gradle.properties b/gradle.properties index 7a24a334..dc56501c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,5 +19,4 @@ org.gradle.daemon = true org.gradle.parallel = true -org.gradle.configureondemand = true org.gradle.workers.max = 4 \ No newline at end of file