fix multiple exceptions with update checker and clean up code

This commit is contained in:
MWHunter committed 2023-02-25 23:25:56 -06:00
1 parent 70eaec04c7
commit 33c0d9d934
4 files changed
+32 -111

No files matched your search

@@ -1,45 +0,0 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2021 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.updatechecker;
import net.minecraft.util.com.google.gson.JsonObject;
import net.minecraft.util.com.google.gson.JsonParser;
import java.io.IOException;
public class LowLevelUpdateCheckerLegacy implements LowLevelUpdateChecker {
@Override
public String getLatestRelease() {
String jsonResponse;
try {
jsonResponse = getLatestReleaseJson();
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("Failed to parse packetevents version!");
}
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(jsonResponse).getAsJsonObject();
String versionName = null;
if (jsonObject != null) {
versionName = jsonObject.get("name").getAsString();
}
return versionName;
}
}
@@ -1,45 +0,0 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2021 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.retrooper.packetevents.updatechecker;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.IOException;
public class LowLevelUpdateCheckerModern implements LowLevelUpdateChecker {
@Override
public String getLatestRelease() {
String jsonResponse;
try {
jsonResponse = getLatestReleaseJson();
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("Failed to parse packetevents version!");
}
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(jsonResponse).getAsJsonObject();
String versionName = null;
if (jsonObject != null) {
versionName = jsonObject.get("name").getAsString();
}
return versionName;
}
}
@@ -19,7 +19,6 @@
package io.github.retrooper.packetevents.updatechecker;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.utils.server.ServerVersion;
import io.github.retrooper.packetevents.utils.version.PEVersion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@@ -31,14 +30,10 @@ import org.bukkit.ChatColor;
* @since 1.6.9
*/
public class UpdateChecker {
private final LowLevelUpdateChecker lowLevelUpdateChecker;
private final UpdaterCheck lowLevelUpdateChecker;
public UpdateChecker() {
if (PacketEvents.get().getServerUtils().getVersion().isOlderThan(ServerVersion.v_1_8)) {
lowLevelUpdateChecker = new LowLevelUpdateCheckerLegacy();
} else {
lowLevelUpdateChecker = new LowLevelUpdateCheckerModern();
}
lowLevelUpdateChecker = new UpdaterCheck();
}
public String checkLatestReleasedVersion() {
@@ -50,23 +45,26 @@ public class UpdateChecker {
*/
public UpdateCheckerStatus checkForUpdate() {
PEVersion localVersion = PacketEvents.get().getVersion();
PEVersion newVersion;
try {
newVersion = new PEVersion(checkLatestReleasedVersion());
} catch (Exception ex) {
newVersion = null;
String version = checkLatestReleasedVersion();
if (version == null) {
return UpdateCheckerStatus.FAILED;
}
if (newVersion != null && localVersion.isOlderThan(newVersion)) {
inform("There is an update available for the PacketEvents API! Your build: (" + localVersion.toString() + ") | Latest released build: (" + newVersion.toString() + ")");
PEVersion newVersion = new PEVersion(checkLatestReleasedVersion());
if (localVersion.isOlderThan(newVersion)) {
inform("There is an update available for the PacketEvents API! Your build: (" + localVersion + ") | Latest released build: (" + newVersion + ")");
return UpdateCheckerStatus.OUTDATED;
} else if (newVersion != null && localVersion.isNewerThan(newVersion)) {
inform("You are on a dev or pre released build of PacketEvents. Your build: (" + localVersion.toString() + ") | Latest released build: (" + newVersion.toString() + ")");
} else if (localVersion.isNewerThan(newVersion)) {
inform("You are on a dev or pre released build of PacketEvents. Your build: (" + localVersion + ") | Latest released build: (" + newVersion + ")");
return UpdateCheckerStatus.PRE_RELEASE;
} else if (localVersion.equals(newVersion)) {
inform("You are on the latest released version of PacketEvents. (" + newVersion.toString() + ")");
inform("You are on the latest released version of PacketEvents. (" + newVersion + ")");
return UpdateCheckerStatus.UP_TO_DATE;
} else {
report("Something went wrong while checking for an update. Your build: (" + localVersion.toString() + ") | Latest released build: (" + newVersion.toString() + ")");
report("Something went wrong while checking for an update. Your build: (" + localVersion + ") | Latest released build: (" + newVersion + ")");
return UpdateCheckerStatus.FAILED;
}
}
@@ -24,10 +24,23 @@ import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public interface LowLevelUpdateChecker {
String getLatestRelease();
public class UpdaterCheck {
public String getLatestRelease() {
try {
String jsonResponse = getLatestReleaseJson();
default String getLatestReleaseJson() throws IOException {
String targetPart = "\"name\":";
int nameIndex = jsonResponse.indexOf(targetPart);
int stringStart = nameIndex + targetPart.length();
int stringEnd = jsonResponse.indexOf(",", stringStart);
return jsonResponse.substring(stringStart + 1, stringEnd - 1);
} catch (Exception e) {
return null;
}
}
private String getLatestReleaseJson() throws IOException {
URLConnection connection = new URL("https://api.github.com/repos/retrooper/packetevents/releases/latest").openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));