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