Skip to content

feat: use java 8 and spigot api #63

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.20.6-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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.OptionalInt;
import java.util.TreeMap;

public final class ServerUtil {

private static final Map<String, String> 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");
Expand Down Expand Up @@ -83,8 +86,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) { }

Expand Down Expand Up @@ -146,8 +148,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

/**
Expand Down Expand Up @@ -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() {
Expand Down