Don't use always exceptions

This commit is contained in:
mani123 2023-04-17 21:17:29 +02:00
parent e00a0de496
commit 4af2927658
2 changed files with 654 additions and 645 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,22 @@
/** /**
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2015 dmulloy2 * Copyright (C) 2015 dmulloy2
* * <p>
* This program is free software; you can redistribute it and/or modify it under the terms of the * 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 2 of * GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version. * the License, or (at your option) any later version.
* * <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * 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. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* * <p>
* You should have received a copy of the GNU General Public License along with this program; * You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.updater; package com.comphenix.protocol.updater;
import com.comphenix.protocol.ProtocolLib;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.error.Report; import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.utility.Closer; import com.comphenix.protocol.utility.Closer;
@ -33,82 +34,81 @@ import java.net.URL;
*/ */
public final class SpigotUpdater extends Updater { public final class SpigotUpdater extends Updater {
private String remoteVersion; private String remoteVersion;
public SpigotUpdater(Plugin plugin, UpdateType type, boolean announce) { public SpigotUpdater(Plugin plugin, UpdateType type, boolean announce) {
super(plugin, type, announce); super(plugin, type, announce);
} }
@Override @Override
public void start(UpdateType type) { public void start(UpdateType type) {
waitForThread(); waitForThread();
this.type = type; this.type = type;
this.thread = new Thread(new SpigotUpdateRunnable()); this.thread = new Thread(new SpigotUpdateRunnable());
this.thread.start(); this.thread.start();
} }
@Override @Override
public String getResult() { public String getResult() {
waitForThread(); waitForThread();
return String.format(result.toString(), remoteVersion, plugin.getDescription().getVersion(), RESOURCE_URL); return String.format(result.toString(), remoteVersion, plugin.getDescription().getVersion(), RESOURCE_URL);
} }
private class SpigotUpdateRunnable implements Runnable { private class SpigotUpdateRunnable implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
String version = getSpigotVersion(); String version = getSpigotVersion();
remoteVersion = version; remoteVersion = version;
if (versionCheck(version)) { if (versionCheck(version)) {
result = UpdateResult.SPIGOT_UPDATE_AVAILABLE; result = UpdateResult.SPIGOT_UPDATE_AVAILABLE;
} else { } else {
result = UpdateResult.NO_UPDATE; result = UpdateResult.NO_UPDATE;
} }
} catch (Throwable ex) { } catch (Throwable ex) {
if (ProtocolLibrary.getConfig().isDebug()) { if (ProtocolLibrary.getConfig().isDebug()) {
ProtocolLibrary.getErrorReporter().reportDetailed( ProtocolLibrary.getErrorReporter().reportDetailed(
SpigotUpdater.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(ex).callerParam(this)); SpigotUpdater.this, Report.newBuilder(REPORT_CANNOT_UPDATE_PLUGIN).error(ex).callerParam(this));
} else { } else {
// People don't care // People don't care
// plugin.getLogger().log(Level.WARNING, "Failed to check for updates: " + ex); // plugin.getLogger().log(Level.WARNING, "Failed to check for updates: " + ex);
} }
ProtocolLibrary.disableUpdates(); ProtocolLibrary.disableUpdates();
} finally { } finally {
// Invoke the listeners on the main thread // Invoke the listeners on the main thread
for (Runnable listener : listeners) { for (Runnable listener : listeners) {
try { if (ProtocolLib.isFolia) {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); plugin.getServer().getGlobalRegionScheduler().execute(plugin, listener);
plugin.getServer().getGlobalRegionScheduler().execute(plugin, listener); } else {
} catch (ClassNotFoundException e) { plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener);
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener); }
} }
} }
} }
} }
}
private static final String RESOURCE_URL = "https://www.spigotmc.org/resources/protocollib.1997/"; private static final String RESOURCE_URL = "https://www.spigotmc.org/resources/protocollib.1997/";
private static final String UPDATE_URL = "https://api.spigotmc.org/legacy/update.php?resource=1997"; private static final String UPDATE_URL = "https://api.spigotmc.org/legacy/update.php?resource=1997";
private static final String ACTION = "GET"; private static final String ACTION = "GET";
public String getSpigotVersion() throws IOException { public String getSpigotVersion() throws IOException {
try (Closer closer = Closer.create()) { try (Closer closer = Closer.create()) {
HttpURLConnection con = (HttpURLConnection) new URL(UPDATE_URL).openConnection(); HttpURLConnection con = (HttpURLConnection) new URL(UPDATE_URL).openConnection();
con.setDoOutput(true); con.setDoOutput(true);
con.setRequestMethod(ACTION); con.setRequestMethod(ACTION);
InputStreamReader isr = closer.register(new InputStreamReader(con.getInputStream())); InputStreamReader isr = closer.register(new InputStreamReader(con.getInputStream()));
BufferedReader br = closer.register(new BufferedReader(isr)); BufferedReader br = closer.register(new BufferedReader(isr));
return br.readLine(); return br.readLine();
} }
} }
@Override @Override
public String getRemoteVersion() { public String getRemoteVersion() {
return remoteVersion; return remoteVersion;
} }
} }