From 0c97efbf08987e2d28003dc2a33b987686b1ccce Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Sun, 26 May 2024 17:59:21 +0300 Subject: [PATCH 1/2] feat: use java 8 and spigot api SADLY --- build.gradle.kts | 10 ++++++++-- .../expansion/server/util/ServerUtil.java | 20 ++++++++++++------- .../expansion/server/util/VersionHelper.java | 20 +++++++++++-------- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d92f4c3..bd6fefe 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,16 +7,22 @@ group = "at.helpch.placeholderapi.expansion" version = "2.7.2" repositories { - maven("https://papermc.io/repo/repository/maven-public/") + mavenCentral() + maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") } dependencies { - compileOnly("io.papermc.paper:paper-api:1.17.1-R0.1-SNAPSHOT") + compileOnly("org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT") compileOnly("me.clip:placeholderapi:2.11.3") } tasks { + java { + targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_1_8 + } + shadowJar { archiveFileName.set("PAPI-Expansion-Server-${project.version}.jar") } diff --git a/src/main/java/at/helpch/placeholderapi/expansion/server/util/ServerUtil.java b/src/main/java/at/helpch/placeholderapi/expansion/server/util/ServerUtil.java index 4579938..d9b1e2d 100644 --- a/src/main/java/at/helpch/placeholderapi/expansion/server/util/ServerUtil.java +++ b/src/main/java/at/helpch/placeholderapi/expansion/server/util/ServerUtil.java @@ -4,19 +4,22 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Map; import java.util.TreeMap; public final class ServerUtil { private static final Map variants = new TreeMap<>(); - private static final String variant; - private static final String build; // TPS stuff private static final Object craftServer; private static final Field tpsField; - private static boolean hasTpsMethod; // Paper and its forks have Bukkit#getTps + private static Method getTpsMethod; // Paper and its forks have Bukkit#getTPS + // ---- + + private static final String variant; + private static final String build; static { variants.put("net.pl3x.purpur.PurpurConfig", "Purpur"); @@ -78,8 +81,7 @@ private static Object getCraftServer() { private static Field getTpsHandler() { try { - Bukkit.class.getMethod("getTPS"); - hasTpsMethod = true; + getTpsMethod = Bukkit.class.getMethod("getTPS"); return null; } catch (NoSuchMethodException ignored) { } @@ -138,8 +140,12 @@ public static String getBuild() { } public static double[] getTps() { - if (hasTpsMethod) { - return Bukkit.getTPS(); + if (getTpsMethod != null) { + try { + return (double[]) getTpsMethod.invoke(null); + } catch (IllegalAccessException | InvocationTargetException ignored) { + return new double[]{0, 0, 0}; + } } if (craftServer == null || tpsField == null) { diff --git a/src/main/java/at/helpch/placeholderapi/expansion/server/util/VersionHelper.java b/src/main/java/at/helpch/placeholderapi/expansion/server/util/VersionHelper.java index b8caee4..93c9913 100644 --- a/src/main/java/at/helpch/placeholderapi/expansion/server/util/VersionHelper.java +++ b/src/main/java/at/helpch/placeholderapi/expansion/server/util/VersionHelper.java @@ -27,6 +27,8 @@ import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,11 +41,10 @@ public final class VersionHelper { private static final int V1_17 = 1_17_0; + private static final boolean IS_PAPER = checkPaper(); public static final String MINECRAFT_VERSION = getMinecraftVersion(); private static final int CURRENT_VERSION = getCurrentVersion(); - private static final boolean IS_PAPER = checkPaper(); - public static final boolean IS_1_17_OR_HIGHER = CURRENT_VERSION >= V1_17; /** @@ -92,13 +93,16 @@ private static int getCurrentVersion() { } private static String getMinecraftVersion() { - try { - // Paper method from 2020 - returns the version like 1.20.1 - return Bukkit.getMinecraftVersion(); - } catch (NoSuchMethodError ignored) { - // The version is formatted as 1.20.1-R0.1-SNAPSHOT - return Bukkit.getBukkitVersion().split("-")[0]; + if (IS_PAPER) { + try { + // Paper method from 2020 - returns the version like 1.20.1 + final Method method = Bukkit.class.getDeclaredMethod("getMinecraftVersion"); + return (String) method.invoke(Bukkit.getServer()); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { } } + + // The version is formatted as 1.20.1-R0.1-SNAPSHOT + return Bukkit.getBukkitVersion().split("-")[0]; } public static String getNmsVersion() { From 99d8d9732cc684554004d32177af75ab6240e8ce Mon Sep 17 00:00:00 2001 From: iGabyTM Date: Sun, 22 Sep 2024 11:52:11 +0300 Subject: [PATCH 2/2] feat: catch DateTimeException when parsing a cooldown format LocalDateTime requires the time to be present --- .../expansion/server/util/TimeFormatter.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/at/helpch/placeholderapi/expansion/server/util/TimeFormatter.java b/src/main/java/at/helpch/placeholderapi/expansion/server/util/TimeFormatter.java index 943878a..05de674 100644 --- a/src/main/java/at/helpch/placeholderapi/expansion/server/util/TimeFormatter.java +++ b/src/main/java/at/helpch/placeholderapi/expansion/server/util/TimeFormatter.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; @@ -46,9 +47,16 @@ public TimeFormatter( try { return DateTimeFormatter.ofPattern(pattern, timeLocale); } catch (IllegalArgumentException e) { - Logging.warn("Could not parse DateTimeFormatter from string \"{0}\"", pattern); - return null; + Logging.error(e, "Could not parse DateTimeFormatter from pattern \"{0}\"", pattern); + } catch (DateTimeException e) { + if (e.getMessage().contains("obtain LocalTime from TemporalAccessor")) { + Logging.error(e, "The pattern \"{0}\" might be missing the time definition, try to add 'HH' at the end and set it to '00' (beginning of the day)", pattern); + } else { + Logging.error(e, "Could not parse DateTimeFormatter from pattern \"{0}\"", pattern); + } } + + return null; }); }