tweaks724/src/main/java/eu/m724/tweaks/updater/PluginScanner.java
2024-12-31 13:34:17 +01:00

59 lines
1.7 KiB
Java

/*
* Copyright (C) 2024 Minecon724
* Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file
* in the project root for the full license text.
*/
package eu.m724.tweaks.updater;
import eu.m724.tweaks.updater.cache.SpigotResource;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class PluginScanner {
private final Plugin thisPlugin;
PluginScanner(Plugin thisPlugin) {
this.thisPlugin = thisPlugin;
}
public Set<SpigotResource> load() throws IOException {
File installedPluginsYml = new File(thisPlugin.getDataFolder(), "installed_plugins.yml");
if (!installedPluginsYml.exists()) {
thisPlugin.saveResource("installed_plugins.yml", false);
}
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(installedPluginsYml);
Plugin[] plugins = thisPlugin.getServer().getPluginManager().getPlugins();
Set<SpigotResource> spigotResources = new HashSet<>();
for (Plugin plugin : plugins) {
String pluginName = plugin.getName();
if (!configuration.isSet(pluginName)) {
configuration.set(pluginName, -1);
continue;
}
int pluginId = configuration.getInt(pluginName);
if (pluginId > 0) {
spigotResources.add(
new SpigotResource(plugin, pluginId, pluginName)
);
}
}
configuration.save(installedPluginsYml);
return spigotResources;
}
}