2021-04-17 14:36:14 +02:00
|
|
|
/*
|
|
|
|
* 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();
|
2021-04-17 14:37:26 +02:00
|
|
|
} catch (NullPointerException e) {
|
|
|
|
// Ignored
|
2021-04-17 14:36:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|