Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-11-20 06:50:08 +01:00
Small refactor to UpdateUtil
Dieser Commit ist enthalten in:
Ursprung
ceae2c2094
Commit
7b8a33cc78
@ -21,7 +21,7 @@ import com.google.gson.JsonObject;
|
|||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
import com.viaversion.viaversion.api.Via;
|
import com.viaversion.viaversion.api.Via;
|
||||||
import com.viaversion.viaversion.util.GsonUtil;
|
import com.viaversion.viaversion.util.GsonUtil;
|
||||||
|
import com.viaversion.viaversion.util.Pair;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@ -29,90 +29,83 @@ import java.net.HttpURLConnection;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
public final class UpdateUtil {
|
public final class UpdateUtil {
|
||||||
|
|
||||||
private static final String PREFIX = "§a§l[ViaVersion] §a";
|
private static final String PREFIX = "§a§l[ViaVersion] §a";
|
||||||
private static final String URL = "https://update.viaversion.com";
|
private static final String URL = "https://update.viaversion.com/";
|
||||||
private static final String PLUGIN = "/ViaVersion/";
|
private static final String PLUGIN = "ViaVersion/";
|
||||||
|
|
||||||
public static void sendUpdateMessage(final UUID uuid) {
|
public static void sendUpdateMessage(final UUID uuid) {
|
||||||
Via.getPlatform().runAsync(() -> {
|
Via.getPlatform().runAsync(() -> {
|
||||||
final String message = getUpdateMessage(false);
|
final Pair<Level, String> message = getUpdateMessage(false);
|
||||||
if (message != null) {
|
if (message != null) {
|
||||||
Via.getPlatform().runSync(() -> Via.getPlatform().sendMessage(uuid, PREFIX + message));
|
Via.getPlatform().runSync(() -> Via.getPlatform().sendMessage(uuid, PREFIX + message.value()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendUpdateMessage() {
|
public static void sendUpdateMessage() {
|
||||||
Via.getPlatform().runAsync(() -> {
|
Via.getPlatform().runAsync(() -> {
|
||||||
final String message = getUpdateMessage(true);
|
final Pair<Level, String> message = getUpdateMessage(true);
|
||||||
if (message != null) {
|
if (message != null) {
|
||||||
Via.getPlatform().runSync(() -> Via.getPlatform().getLogger().warning(message));
|
Via.getPlatform().runSync(() -> Via.getPlatform().getLogger().log(message.key(), message.value()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static @Nullable String getUpdateMessage(boolean console) {
|
private static @Nullable Pair<Level, String> getUpdateMessage(boolean console) {
|
||||||
if (Via.getPlatform().getPluginVersion().equals("${version}")) {
|
if (Via.getPlatform().getPluginVersion().equals("${version}")) {
|
||||||
return "You are using a debug/custom version, consider updating.";
|
return new Pair<>(Level.WARNING, "You are using a debug/custom version, consider updating.");
|
||||||
}
|
}
|
||||||
String newestString = getNewestVersion();
|
|
||||||
if (newestString == null) {
|
String newestString;
|
||||||
if (console) {
|
try {
|
||||||
return "Could not check for updates, check your connection.";
|
newestString = getNewestVersion();
|
||||||
} else {
|
} catch (IOException | JsonParseException ignored) {
|
||||||
return null;
|
return console ? new Pair<>(Level.WARNING, "Could not check for updates, check your connection.") : null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Version current;
|
Version current;
|
||||||
try {
|
try {
|
||||||
current = new Version(Via.getPlatform().getPluginVersion());
|
current = new Version(Via.getPlatform().getPluginVersion());
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
return "You are using a custom version, consider updating.";
|
return new Pair<>(Level.INFO, "You are using a custom version, consider updating.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Version newest = new Version(newestString);
|
Version newest = new Version(newestString);
|
||||||
if (current.compareTo(newest) < 0) {
|
if (current.compareTo(newest) < 0) {
|
||||||
return "There is a newer plugin version available: " + newest + ", you're on: " + current;
|
return new Pair<>(Level.WARNING, "There is a newer plugin version available: " + newest + ", you're on: " + current);
|
||||||
} else if (console && current.compareTo(newest) != 0) {
|
} else if (console && current.compareTo(newest) != 0) {
|
||||||
String tag = current.getTag().toLowerCase(Locale.ROOT);
|
String tag = current.getTag().toLowerCase(Locale.ROOT);
|
||||||
if (tag.startsWith("dev") || tag.startsWith("snapshot")) {
|
if (tag.endsWith("dev") || tag.endsWith("snapshot")) {
|
||||||
return "You are running a development version of the plugin, please report any bugs to GitHub.";
|
return new Pair<>(Level.INFO, "You are running a development version of the plugin, please report any bugs to GitHub.");
|
||||||
} else {
|
} else {
|
||||||
return "You are running a newer version of the plugin than is released!";
|
return new Pair<>(Level.WARNING, "You are running a newer version of the plugin than is released!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static @Nullable String getNewestVersion() {
|
private static String getNewestVersion() throws IOException {
|
||||||
try {
|
URL url = new URL(URL + PLUGIN);
|
||||||
URL url = new URL(URL + PLUGIN);
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setUseCaches(false);
|
||||||
connection.setUseCaches(false);
|
connection.addRequestProperty("User-Agent", "ViaVersion " + Via.getPlatform().getPluginVersion() + " " + Via.getPlatform().getPlatformName());
|
||||||
connection.addRequestProperty("User-Agent", "ViaVersion " + Via.getPlatform().getPluginVersion() + " " + Via.getPlatform().getPlatformName());
|
connection.addRequestProperty("Accept", "application/json");
|
||||||
connection.addRequestProperty("Accept", "application/json");
|
connection.setDoOutput(true);
|
||||||
connection.setDoOutput(true);
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
StringBuilder builder = new StringBuilder();
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||||
String input;
|
String input;
|
||||||
StringBuilder builder = new StringBuilder();
|
while ((input = reader.readLine()) != null) {
|
||||||
while ((input = br.readLine()) != null) {
|
|
||||||
builder.append(input);
|
builder.append(input);
|
||||||
}
|
}
|
||||||
br.close();
|
|
||||||
JsonObject statistics;
|
|
||||||
try {
|
|
||||||
statistics = GsonUtil.getGson().fromJson(builder.toString(), JsonObject.class);
|
|
||||||
} catch (JsonParseException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return statistics.get("name").getAsString();
|
|
||||||
} catch (IOException e) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonObject statistics = GsonUtil.getGson().fromJson(builder.toString(), JsonObject.class);
|
||||||
|
return statistics.get("name").getAsString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren