From 5d30e0854fcc4c69bd16a504efd6f5b9d5f344c4 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Fri, 4 Dec 2020 12:15:56 +0100 Subject: [PATCH] Add java version checker --- .../plotsquared/bukkit/BukkitPlatform.java | 4 + .../bukkit/util/JavaVersionCheck.java | 74 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/util/JavaVersionCheck.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index ead142a36..1b057069b 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -156,6 +156,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import static com.plotsquared.bukkit.util.JavaVersionCheck.checkJavaVersion; import static com.plotsquared.core.util.PremiumVerification.getDownloadID; import static com.plotsquared.core.util.PremiumVerification.getResourceID; import static com.plotsquared.core.util.PremiumVerification.getUserID; @@ -242,6 +243,9 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl return; } + // Check whether the server runs on 11 or greater + checkJavaVersion(); + // We create the injector after PlotSquared has been initialized, so that we have access // to generated instances and settings this.injector = Guice diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/JavaVersionCheck.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/JavaVersionCheck.java new file mode 100644 index 000000000..8f67eaa8c --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/JavaVersionCheck.java @@ -0,0 +1,74 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 3 of the License, or + * (at your option) any later version. + * + * 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.bukkit.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JavaVersionCheck { + + private static final Logger logger = LoggerFactory.getLogger("P2/" + JavaVersionCheck.class.getSimpleName()); + + public JavaVersionCheck() {} + + public static void checkJavaVersion() { + final String javaVersion = System.getProperty("java.version"); + final int dotIndex = javaVersion.indexOf('.'); + + if (javaVersion.startsWith("1.")) { + JavaVersionCheck.notify(javaVersion, logger); + return; + } + + final int endIndex = dotIndex == -1 ? javaVersion.length() : dotIndex; + final String version = javaVersion.substring(0, endIndex); + + final int javaVersionNumber; + try { + javaVersionNumber = Integer.parseInt(version); + } catch (final NumberFormatException e) { + logger.error("Failed to determine Java version. Could not parse {}", version, e); + JavaVersionCheck.notify(javaVersion, logger); + return; + } + + if (javaVersionNumber < 11) { + JavaVersionCheck.notify(javaVersion, logger); + } + } + + public static void notify(final String version, final Logger logger) { + logger.error("************************************************************"); + logger.error("* WARNING - YOU ARE RUNNING AN OUTDATED VERSION OF JAVA."); + logger.error("* PLOTSQUARED WILL STOP BEING COMPATIBLE WITH THIS VERSION OF"); + logger.error("* JAVA WHEN MINECRAFT 1.17 IS RELEASED."); + logger.error("*"); + logger.error("* Please update the version of Java to 11."); + logger.error("*"); + logger.error("* Current Java version: " + version); + logger.error("************************************************************"); + } +}