diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java index 314018852..d7ecea15e 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java @@ -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() { + return isStoppedBefore1p17() || isStoppedV1p17() || isStoppedAfterV1p17(); + } + + private boolean isStoppedBefore1p17() { try { - return performCheck(); + // 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; // ShutdownHook handles save in case this fails upon plugin disable. + return false; } } - private boolean performCheck() { - // 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); + 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 Reflection.getField(minecraftServerClass, "isStopped", boolean.class).get(minecraftServer); + 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; + } } } \ No newline at end of file