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.config.BauServer;
import de.steamwar.bausystem.linkage.LinkageUtils; import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.linkage.LinkedCommand;
import de.steamwar.bausystem.linkage.LinkedListener;
import lombok.Getter; import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -39,15 +36,7 @@ public class BauSystem extends JavaPlugin implements Listener {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
LinkageUtils.link(LinkedCommand.class, clazz -> clazz.getSuperclass() == de.steamwar.command.SWCommand.class, o -> {}); LinkageUtils.link();
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(); 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.io.InputStreamReader;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.function.Consumer; import java.util.HashSet;
import java.util.function.Predicate; import java.util.Set;
@UtilityClass @UtilityClass
public class LinkageUtils { public class LinkageUtils {
public void link(Class<?> source, Predicate<Class<?>> loadPredicate, Consumer<Object> loader) { public void link() {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de/steamwar/baussystem/" + source.getTypeName())))) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de/steamwar/baussystem/" + Linked.class.getTypeName())))) {
bufferedReader.lines().forEach(s -> { bufferedReader.lines().forEach(s -> {
try { try {
Class<?> clazz = Class.forName(s); link(Class.forName(s));
if (!loadPredicate.test(clazz)) {
return;
}
loader.accept(constructInstance(clazz));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// ignored // 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) { private Object constructInstance(Class<?> clazz) {
try { try {
Constructor<?> constructor = clazz.getDeclaredConstructor(); Constructor<?> constructor = clazz.getDeclaredConstructor();

Datei anzeigen

@ -21,13 +21,18 @@ package de.steamwar.bausystem.linkage;
import org.atteo.classindex.IndexAnnotated; import org.atteo.classindex.IndexAnnotated;
import java.lang.annotation.ElementType; import java.lang.annotation.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@IndexAnnotated @IndexAnnotated
@Retention(RetentionPolicy.CLASS) @Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE}) @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 {
}