Files
FalsePatternLib/src/main/java/com/falsepattern/lib/updates/UpdateChecker.java
T

88 lines
3.3 KiB
Java
Raw Normal View History

2022-07-31 20:41:56 +02:00
/*
2022-07-30 15:21:11 +02:00
* Copyright (C) 2022 FalsePattern
* All Rights Reserved
*
* The above copyright notice, this permission notice and the word "SNEED"
* shall be included in all copies or substantial portions of the Software.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-06-09 20:14:11 +02:00
package com.falsepattern.lib.updates;
2022-06-10 00:15:08 +02:00
import com.falsepattern.lib.StableAPI;
2022-08-17 20:08:46 +02:00
import com.falsepattern.lib.internal.impl.updates.UpdateCheckerImpl;
2022-07-31 20:41:56 +02:00
import net.minecraft.util.IChatComponent;
2022-06-09 20:14:11 +02:00
import java.util.List;
2022-06-19 22:42:52 +02:00
import java.util.concurrent.CompletableFuture;
2022-06-09 20:14:11 +02:00
2022-06-10 00:15:08 +02:00
@StableAPI(since = "0.8.0")
2022-08-17 20:08:46 +02:00
public final class UpdateChecker {
2022-06-09 20:14:11 +02:00
/**
* Checks for updates. The URL should be a JSON file that contains a list of mods, each with a mod ID, one or more
* versions, and a URL for the user to check for updates in case the current and latest versions are different.
* The JSON file must have the following format:
* <pre>{@code
* [
* {
* "modID": "modid",
* "latestVersion": ["1.0.0", "1.0.0-foo"],
* "updateURL": "https://example.com/mods/mymod"
* },
* {
* "modID": "modid2",
* "latestVersion": ["0.2.0", "0.3.0-alpha"],
* "updateURL": "https://example.com/mods/mymod2"
* },
* ...etc, one json object per mod.
* ]
* }</pre>
2022-07-31 20:41:56 +02:00
*
2022-06-09 20:14:11 +02:00
* @param url The URL to check
2022-07-31 20:41:56 +02:00
*
2022-06-09 20:14:11 +02:00
* @return A list of mods that were both available on the URL and installed
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-06-19 22:42:52 +02:00
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
2022-08-17 20:08:46 +02:00
return UpdateCheckerImpl.fetchUpdatesAsync(url);
2022-06-19 22:42:52 +02:00
}
/**
* Same this as {@link #fetchUpdatesAsync(String)}, but returns the result in a blocking fashion.
2022-07-31 20:41:56 +02:00
*
2022-06-19 22:42:52 +02:00
* @param url The URL to check
2022-07-31 20:41:56 +02:00
*
2022-06-19 22:42:52 +02:00
* @return A future that will contain the update info about mods that were both available on the URL and installed
2022-07-31 20:41:56 +02:00
*
2022-06-19 22:42:52 +02:00
* @throws UpdateCheckException If the update checker is disabled in config, the URL is invalid, or
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
2022-06-19 22:42:52 +02:00
public static List<ModUpdateInfo> fetchUpdates(String url) throws UpdateCheckException {
2022-08-17 20:08:46 +02:00
return UpdateCheckerImpl.fetchUpdates(url);
2022-06-09 20:14:11 +02:00
}
/**
* Formats the raw list of updates into lines of chat messages you can send to players.
2022-07-31 20:41:56 +02:00
*
* @param initiator Who/what/which mod did this update check
2022-07-31 20:41:56 +02:00
* @param updates The list of updates to convert
*
* @return A list of chat messages that can be sent to players
*/
2022-08-06 13:12:55 +02:00
@StableAPI.Expose
public static List<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) {
2022-08-17 20:08:46 +02:00
return UpdateCheckerImpl.updateListToChatMessages(initiator, updates);
}
2022-06-09 20:14:11 +02:00
}