tweaks724/src/main/java/eu/m724/tweaks/updater/UpdaterManager.java
Minecon724 d55afa26ea
Some checks failed
/ deploy (push) Failing after 5m14s
Move updater cache to its own directory
2024-12-01 14:50:28 +01:00

56 lines
1.9 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.ResourceVersion;
import eu.m724.tweaks.updater.cache.SpigotResource;
import eu.m724.tweaks.updater.cache.VersionedResource;
import org.bukkit.plugin.Plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class UpdaterManager {
static File cacheFile;
private final Plugin plugin;
public UpdaterManager(Plugin plugin) {
this.plugin = plugin;
cacheFile = new File(plugin.getDataFolder(), "cache/updater");
}
public void init() throws IOException {
// scan installed plugins
Set<SpigotResource> resources = new PluginScanner(plugin).load();
cacheFile.getParentFile().mkdirs(); // TODO move this somewhere else
// load installed versions from cache
Set<ResourceVersion> installedVersions;
try (FileInputStream inputStream = new FileInputStream(cacheFile)) {
installedVersions = VersionCheckCache.loadAll(inputStream);
} catch (FileNotFoundException e) {
installedVersions = new HashSet<>();
}
final Set<ResourceVersion> ivf = installedVersions;
Set<VersionedResource> versionedResources = resources.stream()
.map(res -> new VersionedResource(
res, ivf.stream().filter(iv -> iv.resourceId() == res.resourceId()).findFirst().orElse(null), null
)).collect(Collectors.toSet());
new UpdateChecker(plugin, versionedResources)
.runTaskTimerAsynchronously(plugin, 600, 12 * 3600 * 20);
}
}