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

View File

@ -33,6 +33,7 @@ import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.utility.Util; import com.comphenix.protocol.utility.Util;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
@ -44,6 +45,7 @@ import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
@ -58,6 +60,8 @@ import org.bukkit.plugin.java.JavaPlugin;
*/ */
public class ProtocolLib extends JavaPlugin { public class ProtocolLib extends JavaPlugin {
public static boolean isFolia = false;
// Every possible error or warning report type // Every possible error or warning report type
public static final ReportType REPORT_CANNOT_DELETE_CONFIG = new ReportType( public static final ReportType REPORT_CANNOT_DELETE_CONFIG = new ReportType(
"Cannot delete old ProtocolLib configuration."); "Cannot delete old ProtocolLib configuration.");
@ -303,6 +307,13 @@ public class ProtocolLib extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
isFolia = true;
} catch (ClassNotFoundException e) {
isFolia = false;
}
try { try {
Server server = this.getServer(); Server server = this.getServer();
PluginManager manager = server.getPluginManager(); PluginManager manager = server.getPluginManager();
@ -491,11 +502,10 @@ public class ProtocolLib extends JavaPlugin {
// Attempt to create task // Attempt to create task
try { if (isFolia) {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
this.packetTask = 1; this.packetTask = 1;
server.getGlobalRegionScheduler().runAtFixedRate(this, task -> packetTaskRegistrator(), ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY); server.getGlobalRegionScheduler().runAtFixedRate(this, task -> packetTaskRegistrator(), ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY);
} catch (ClassNotFoundException e) { } else {
this.packetTask = server.getScheduler().scheduleSyncRepeatingTask(this, this::packetTaskRegistrator, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY); this.packetTask = server.getScheduler().scheduleSyncRepeatingTask(this, this::packetTaskRegistrator, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY);
} }
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
@ -575,10 +585,9 @@ public class ProtocolLib extends JavaPlugin {
// Clean up // Clean up
if (this.packetTask >= 0) { if (this.packetTask >= 0) {
try { if (isFolia) {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
this.getServer().getGlobalRegionScheduler().cancelTasks(this); this.getServer().getGlobalRegionScheduler().cancelTasks(this);
} catch (ClassNotFoundException e) { } else {
this.getServer().getScheduler().cancelTask(this.packetTask); this.getServer().getScheduler().cancelTask(this.packetTask);
} }
this.packetTask = -1; this.packetTask = -1;

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;
@ -80,10 +81,9 @@ public final class SpigotUpdater extends Updater {
} 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);
} catch (ClassNotFoundException e) { } else {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener); plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener);
} }
} }