Fixed bukkit shutdown detection

Affects issues:
- Fixed #1970
This commit is contained in:
Risto Lahtela 2021-07-04 11:00:52 +03:00
parent d05a887878
commit f32cf95fc1

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.utilities.java.Reflection;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import net.playeranalytics.plugin.server.PluginLogger;
import org.bukkit.Bukkit;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -46,19 +47,49 @@ public class BukkitServerShutdownSave extends ServerShutdownSave {
@Override
protected boolean checkServerShuttingDownStatus() {
try {
return performCheck();
} catch (Exception | NoClassDefFoundError | NoSuchFieldError e) {
return false; // ShutdownHook handles save in case this fails upon plugin disable.
}
return isStoppedBefore1p17() || isStoppedV1p17() || isStoppedAfterV1p17();
}
private boolean performCheck() {
private boolean isStoppedBefore1p17() {
try {
// Special thanks to Fuzzlemann for figuring out the methods required for this check.
// https://github.com/plan-player-analytics/Plan/issues/769#issuecomment-433898242
Class<?> minecraftServerClass = Reflection.getMinecraftClass("MinecraftServer");
Object minecraftServer = Reflection.getField(minecraftServerClass, "SERVER", minecraftServerClass).get(null);
return Reflection.getField(minecraftServerClass, "isStopped", boolean.class).get(minecraftServer);
} catch (Exception | NoClassDefFoundError | NoSuchFieldError e) {
return false;
}
}
private boolean isStoppedV1p17() {
try {
// Special thanks to Fuzzlemann for figuring out the methods required for this check.
// https://github.com/plan-player-analytics/Plan/issues/769#issuecomment-433898242
Class<?> minecraftServerClass = Class.forName("net.minecraft.server.MinecraftServer");
Class<?> craftServerClass = Reflection.getCraftBukkitClass("CraftServer");
Object minecraftServer = Reflection.getField(craftServerClass, "console", minecraftServerClass).get(Bukkit.getServer());
return (Boolean) minecraftServerClass.getMethod("isStopped").invoke(minecraftServer);
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
e.printStackTrace();
return false;
}
}
private boolean isStoppedAfterV1p17() {
try {
// Special thanks to Fuzzlemann for figuring out the methods required for this check.
// https://github.com/plan-player-analytics/Plan/issues/769#issuecomment-433898242
Class<?> minecraftServerClass = Reflection.getMinecraftClass("MinecraftServer");
Class<?> craftServerClass = Reflection.getCraftBukkitClass("CraftServer");
Object minecraftServer = Reflection.getField(craftServerClass, "console", minecraftServerClass).get(Bukkit.getServer());
return (Boolean) minecraftServerClass.getMethod("isStopped").invoke(minecraftServer);
} catch (Exception | NoClassDefFoundError | NoSuchFieldError | NoSuchMethodError e) {
e.printStackTrace();
return false;
}
}
}