Simplified AddonsManager#sortAddons()

This commit is contained in:
Florian CUNY 2018-10-28 14:09:35 +01:00
parent 12d1909a22
commit 2f7b8a6764

View File

@ -227,19 +227,22 @@ public class AddonsManager {
} }
private void sortAddons() { private void sortAddons() {
// Check that any dependencies exist // Lists all available addons as names.
List<String> names = addons.stream().map(a -> a.getDescription().getName()).collect(Collectors.toList()); List<String> names = addons.stream().map(a -> a.getDescription().getName()).collect(Collectors.toList());
Iterator<Addon> ita = addons.iterator();
while (ita.hasNext()) { // Check that any dependencies exist
Addon a = ita.next(); Iterator<Addon> addonsIterator = addons.iterator();
for (String dep : a.getDescription().getDependencies()) { while (addonsIterator.hasNext()) {
if (!names.contains(dep)) { Addon a = addonsIterator.next();
plugin.logError(a.getDescription().getName() + " has dependency on " + dep + " that does not exist. Addon will not load!"); for (String dependency : a.getDescription().getDependencies()) {
ita.remove(); if (!names.contains(dependency)) {
plugin.logError(a.getDescription().getName() + " has dependency on " + dependency + " that does not exist. Addon will not load!");
addonsIterator.remove();
break; break;
} }
} }
} }
// Load dependencies or soft dependencies // Load dependencies or soft dependencies
Map<String,Addon> sortedAddons = new LinkedHashMap<>(); Map<String,Addon> sortedAddons = new LinkedHashMap<>();
// Start with nodes with no dependencies // Start with nodes with no dependencies
@ -249,30 +252,20 @@ public class AddonsManager {
List<Addon> remaining = addons.stream().filter(a -> !sortedAddons.containsKey(a.getDescription().getName())).collect(Collectors.toList()); List<Addon> remaining = addons.stream().filter(a -> !sortedAddons.containsKey(a.getDescription().getName())).collect(Collectors.toList());
// Run through remaining addons // Run through remaining addons
int index = 0; remaining.forEach(addon -> {
while (index < 10 && !remaining.isEmpty()) { // Get the addon's dependencies.
index++; List<String> dependencies = new ArrayList<>(addon.getDescription().getDependencies());
Iterator<Addon> it = remaining.iterator(); dependencies.addAll(addon.getDescription().getSoftDependencies());
while (it.hasNext()) {
Addon a = it.next(); // Remove already sorted addons (dependencies) from the list
// Check if dependencies are loaded - make a list of all hard and soft deps dependencies.removeIf(sortedAddons::containsKey);
List<String> deps = new ArrayList<>(a.getDescription().getDependencies());
deps.addAll(a.getDescription().getSoftDependencies()); if (dependencies.stream().noneMatch(dependency -> addon.getDescription().getDependencies().contains(dependency))) {
Iterator<String> depIt = deps.iterator(); sortedAddons.put(addon.getDescription().getName(), addon);
while(depIt.hasNext()) {
String dep = depIt.next();
if (sortedAddons.containsKey(dep)) {
depIt.remove();
}
}
if (deps.stream().noneMatch(s -> a.getDescription().getDependencies().contains(s))) {
// Add addons loaded
sortedAddons.put(a.getDescription().getName(), a);
it.remove();
}
} }
} });
addons.clear(); addons.clear();
sortedAddons.values().forEach(addons::add); addons.addAll(sortedAddons.values());
} }
} }