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;
|
2021-04-17 17:44:58 +02:00
|
|
|
import java.util.*;
|
2021-04-17 14:36:14 +02:00
|
|
|
|
|
|
|
@UtilityClass
|
|
|
|
public class LinkageUtils {
|
|
|
|
|
2021-04-17 17:44:58 +02:00
|
|
|
private Map<Class<?>, Object> objectMap = new HashMap<>();
|
|
|
|
|
2021-04-17 15:05:30 +02:00
|
|
|
public void link() {
|
2021-04-17 17:59:02 +02:00
|
|
|
internalLinkOrUnlink(false, Linked.class);
|
|
|
|
internalLinkOrUnlink(false, Linked.Linkages.class);
|
2021-04-17 17:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void unlink() {
|
2021-04-17 17:59:02 +02:00
|
|
|
internalLinkOrUnlink(true, Linked.Linkages.class);
|
|
|
|
internalLinkOrUnlink(true, Linked.class);
|
2021-04-17 17:44:58 +02:00
|
|
|
}
|
|
|
|
|
2021-04-17 17:59:02 +02:00
|
|
|
private void internalLinkOrUnlink(boolean unlink, Class<?> toLink) {
|
|
|
|
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de.steamwar.bausystem/" + toLink.getTypeName().replace("$", "."))))) {
|
2021-04-17 14:36:14 +02:00
|
|
|
bufferedReader.lines().forEach(s -> {
|
|
|
|
try {
|
2021-04-17 17:44:58 +02:00
|
|
|
linkOrUnlink(Class.forName(s), unlink);
|
2021-04-17 14:36:14 +02:00
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
// ignored
|
2021-04-17 23:17:11 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2021-04-17 14:36:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 17:44:58 +02:00
|
|
|
private void linkOrUnlink(Class<?> clazz, boolean unlink) {
|
2021-04-17 15:05:30 +02:00
|
|
|
Linked[] linkages = clazz.getDeclaredAnnotationsByType(Linked.class);
|
|
|
|
if (linkages.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-17 17:44:58 +02:00
|
|
|
List<LinkageType> linkageTypeList = new ArrayList<>();
|
2021-04-17 15:05:30 +02:00
|
|
|
for (Linked linked : linkages) {
|
|
|
|
if (linked == null) {
|
2021-04-17 16:39:40 +02:00
|
|
|
continue;
|
2021-04-17 15:05:30 +02:00
|
|
|
}
|
|
|
|
LinkageType linkageType = linked.value();
|
|
|
|
if (linkageType.getLinkagePredicate().test(clazz)) {
|
2021-04-17 17:44:58 +02:00
|
|
|
linkageTypeList.add(linked.value());
|
2021-04-17 15:05:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 17:59:02 +02:00
|
|
|
|
|
|
|
linkageTypeList.removeIf(linkageType -> linkageType.isUnlink() != unlink);
|
2021-04-17 17:44:58 +02:00
|
|
|
if (linkageTypeList.isEmpty()) {
|
2021-04-17 15:05:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-17 17:44:58 +02:00
|
|
|
linkageTypeList.sort(Comparator.comparingInt(LinkageType::getOrder));
|
|
|
|
Object object = objectMap.computeIfAbsent(clazz, LinkageUtils::constructInstance);
|
|
|
|
linkageTypeList.forEach(linkageType -> linkageType.getLinkageConsumer().accept(object));
|
2021-04-17 15:05:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-17 14:36:14 +02:00
|
|
|
private Object constructInstance(Class<?> clazz) {
|
|
|
|
try {
|
|
|
|
Constructor<?> constructor = clazz.getDeclaredConstructor();
|
2021-04-17 16:43:27 +02:00
|
|
|
constructor.setAccessible(true); //NOSONAR
|
2021-04-17 14:36:14 +02:00
|
|
|
return constructor.newInstance();
|
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
|
|
|
|
throw new SecurityException(e.getMessage(), e.getCause());
|
|
|
|
}
|
|
|
|
}
|
2021-04-17 16:43:27 +02:00
|
|
|
}
|