SteamWar/BauSystem2.0
Archiviert
12
0

Remove LinkedCommand.java

Remove LinkedListener.java
Add LinkageType
Add Linked
Dieser Commit ist enthalten in:
yoyosource 2021-04-17 15:05:30 +02:00
Ursprung d953144dfa
Commit 77ef227048
5 geänderte Dateien mit 92 neuen und 59 gelöschten Zeilen

Datei anzeigen

@ -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();
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Class<?>> linkagePredicate;
@Getter
private Consumer<Object> linkageConsumer = o -> {};
}

Datei anzeigen

@ -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<Class<?>> loadPredicate, Consumer<Object> 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<LinkageType> 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();

Datei anzeigen

@ -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 {};
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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 {
}