SteamWar/BauSystem2.0
Archiviert
12
0

Add LinkageUtils

Add LinkedCommand
Add LinkedListener
Dieser Commit ist enthalten in:
yoyosource 2021-04-17 14:36:14 +02:00
Ursprung 507733f77b
Commit 15fbb0241c
7 geänderte Dateien mit 158 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -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")
}
}

Datei anzeigen

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

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Class<?>> loadPredicate, Consumer<Object> 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());
}
}
}

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -45,7 +45,7 @@ if (file("steamwar.properties").exists()) {
}
ext {
buildName = 'BauSystem'
buildName = 'BauSystem2.0'
artifactName = 'bausystem'
uberJarName = "${buildName}-all.jar"

Datei anzeigen

@ -19,5 +19,4 @@
org.gradle.daemon = true
org.gradle.parallel = true
org.gradle.configureondemand = true
org.gradle.workers.max = 4