From 77ef227048f0c05ee7d53ed2cd44927c75658f70 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 17 Apr 2021 15:05:30 +0200 Subject: [PATCH] Remove LinkedCommand.java Remove LinkedListener.java Add LinkageType Add Linked --- .../src/de/steamwar/bausystem/BauSystem.java | 13 +---- .../bausystem/linkage/LinkageType.java | 51 +++++++++++++++++++ .../bausystem/linkage/LinkageUtils.java | 39 ++++++++++---- .../{LinkedCommand.java => Linked.java} | 15 ++++-- .../bausystem/linkage/LinkedListener.java | 33 ------------ 5 files changed, 92 insertions(+), 59 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java rename BauSystem_Main/src/de/steamwar/bausystem/linkage/{LinkedCommand.java => Linked.java} (78%) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index fd7eb15f..aca76667 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -21,10 +21,7 @@ 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; @@ -39,15 +36,7 @@ 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)); + LinkageUtils.link(); new BauServer(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java new file mode 100644 index 00000000..8d1faf4f --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java @@ -0,0 +1,51 @@ +/* + * 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 de.steamwar.command.SWCommand; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.bukkit.Bukkit; +import org.bukkit.event.Listener; + +import java.util.function.Consumer; +import java.util.function.Predicate; + +@RequiredArgsConstructor +@AllArgsConstructor +public enum LinkageType { + LISTENER(clazz -> { + for (Class interfaceClazz : clazz.getInterfaces()) { + if (interfaceClazz == Listener.class) { + return true; + } + } + return false; + }, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())), + COMMAND(clazz -> clazz.getSuperclass() == SWCommand.class); + + @Getter + private final Predicate> linkagePredicate; + + @Getter + private Consumer linkageConsumer = o -> {}; +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java index 615ccdfe..801e8c65 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java @@ -28,21 +28,17 @@ 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; +import java.util.HashSet; +import java.util.Set; @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())))) { + public void link() { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de/steamwar/baussystem/" + Linked.class.getTypeName())))) { bufferedReader.lines().forEach(s -> { try { - Class clazz = Class.forName(s); - if (!loadPredicate.test(clazz)) { - return; - } - loader.accept(constructInstance(clazz)); + link(Class.forName(s)); } catch (ClassNotFoundException e) { // ignored } @@ -54,6 +50,31 @@ public class LinkageUtils { } } + private void link(Class clazz) { + Linked[] linkages = clazz.getDeclaredAnnotationsByType(Linked.class); + if (linkages.length == 0) { + return; + } + + Set linkageTypeSet = new HashSet<>(); + for (Linked linked : linkages) { + if (linked == null) { + return; + } + LinkageType linkageType = linked.value(); + if (linkageType.getLinkagePredicate().test(clazz)) { + linkageTypeSet.add(linked.value()); + } + } + + if (linkageTypeSet.isEmpty()) { + return; + } + + Object object = constructInstance(clazz); + linkageTypeSet.forEach(linkageType -> linkageType.getLinkageConsumer().accept(object)); + } + private Object constructInstance(Class clazz) { try { Constructor constructor = clazz.getDeclaredConstructor(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/Linked.java similarity index 78% rename from BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java rename to BauSystem_Main/src/de/steamwar/bausystem/linkage/Linked.java index 40f853f7..8206e9d7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/Linked.java @@ -21,13 +21,18 @@ 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; +import java.lang.annotation.*; @IndexAnnotated @Retention(RetentionPolicy.CLASS) @Target({ElementType.TYPE}) -public @interface LinkedCommand { +@Repeatable(Linked.Linkages.class) +public @interface Linked { + LinkageType value(); + + @Retention(RetentionPolicy.CLASS) + @Target({ElementType.TYPE}) + @interface Linkages { + Linked[] value() default {}; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java deleted file mode 100644 index afd81a20..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedListener.java +++ /dev/null @@ -1,33 +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; - -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 { -}