diff --git a/build.gradle b/build.gradle index e8ad4fc..b1c6b8a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import org.apache.tools.ant.filters.ReplaceTokens + plugins { id "java" id "maven-publish" @@ -32,7 +34,7 @@ dependencies { processResources { from(sourceSets.main.resources.srcDirs) { - filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [name: rootProject.name, version: project.version.toString(), description: project.description] + filter ReplaceTokens, tokens: [name: rootProject.name, version: project.version.toString(), description: project.description] } } @@ -73,7 +75,7 @@ license { } ext { - year = 2020 + year = 2021 } } @@ -89,7 +91,7 @@ sourceSets { publishing { repositories { maven { - if (version.contains("-DEV-")) { + if (version.contains("-DEV")) { url = uri("https://repo.extendedclip.com/content/repositories/dev/") } else { url = uri("https://repo.extendedclip.com/content/repositories/placeholderapi/") diff --git a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java index 16c216e..1e71a45 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java +++ b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java @@ -22,7 +22,6 @@ package me.clip.placeholderapi.expansion; import java.util.Collections; import java.util.List; -import java.util.Objects; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderHook; import org.bukkit.Bukkit; @@ -368,5 +367,4 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { public String getLink() { return null; } - } diff --git a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java index bbf01e9..872215c 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java +++ b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java @@ -46,18 +46,25 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Unmodifiable; import java.io.File; +import java.lang.reflect.Modifier; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Level; +import java.util.stream.Collectors; public final class LocalExpansionManager implements Listener { @NotNull private static final String EXPANSIONS_FOLDER_NAME = "expansions"; + @NotNull + private static final Set ABSTRACT_EXPANSION_METHODS = Arrays.stream(PlaceholderExpansion.class.getDeclaredMethods()) + .filter(method -> Modifier.isAbstract(method.getModifiers())) + .map(method -> new MethodSignature(method.getName(), method.getParameterTypes())) + .collect(Collectors.toSet()); @NotNull private final File folder; @@ -153,20 +160,33 @@ public final class LocalExpansionManager implements Listener { @NotNull final Class clazz) { try { final PlaceholderExpansion expansion = createExpansionInstance(clazz); - if (expansion == null || !expansion.register()) { + + Objects.requireNonNull(expansion.getAuthor(), "The expansion author is null!"); + Objects.requireNonNull(expansion.getIdentifier(), "The expansion identifier is null!"); + Objects.requireNonNull(expansion.getVersion(), "The expansion version is null!"); + + if (!expansion.register()) { return Optional.empty(); } return Optional.of(expansion); - } catch (final LinkageError ex) { - plugin.getLogger().severe("Failed to load Expansion class " + clazz.getSimpleName() + - " (Is a dependency missing?)"); - plugin.getLogger().severe("Cause: " + ex.getClass().getSimpleName() + " " + ex.getMessage()); + } catch (LinkageError | NullPointerException ex) { + final String reason; + + if (ex instanceof LinkageError) { + reason = " (Is a dependency missing?)"; + } else { + reason = " - One of its properties is null which is not allowed!"; + } + + plugin.getLogger().severe("Failed to load expansion class " + clazz.getSimpleName() + + reason); + plugin.getLogger().log(Level.SEVERE, "", ex); } return Optional.empty(); } - + @ApiStatus.Internal public boolean register(@NotNull final PlaceholderExpansion expansion) { final String identifier = expansion.getIdentifier().toLowerCase(); @@ -341,6 +361,16 @@ public final class LocalExpansionManager implements Listener { if (expansionClass == null) { plugin.getLogger().severe("Failed to load Expansion: " + file.getName() + ", as it does not have" + " a class which extends PlaceholderExpansion."); + return null; + } + + Set expansionMethods = Arrays.stream(expansionClass.getDeclaredMethods()) + .map(method -> new MethodSignature(method.getName(), method.getParameterTypes())) + .collect(Collectors.toSet()); + if (!expansionMethods.containsAll(ABSTRACT_EXPANSION_METHODS)) { + plugin.getLogger().severe("Failed to load Expansion: " + file.getName() + ", as it does not have the" + + " required methods declared for a PlaceholderExpansion."); + return null; } return expansionClass; @@ -365,8 +395,8 @@ public final class LocalExpansionManager implements Listener { if (ex.getCause() instanceof LinkageError) { throw ((LinkageError) ex.getCause()); } - - plugin.getLogger().warning("There was an issue with loading an expansion"); + + plugin.getLogger().warning("There was an issue with loading an expansion."); return null; } diff --git a/src/main/java/me/clip/placeholderapi/expansion/manager/MethodSignature.java b/src/main/java/me/clip/placeholderapi/expansion/manager/MethodSignature.java new file mode 100644 index 0000000..8a7d735 --- /dev/null +++ b/src/main/java/me/clip/placeholderapi/expansion/manager/MethodSignature.java @@ -0,0 +1,37 @@ +package me.clip.placeholderapi.expansion.manager; + +import java.util.Arrays; +import java.util.Objects; + +public final class MethodSignature { + private final String name; + private final Class[] params; + + protected MethodSignature(String name, Class[] params) { + this.name = name; + this.params = params; + } + + public String getName() { + return name; + } + + public Class[] getParams() { + return params; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MethodSignature that = (MethodSignature) o; + return Objects.equals(name, that.name) && Arrays.equals(params, that.params); + } + + @Override + public int hashCode() { + int result = Objects.hash(name); + result = 31 * result + Arrays.hashCode(params); + return result; + } +} diff --git a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java index bce51b8..2163be2 100644 --- a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java +++ b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java @@ -23,6 +23,7 @@ package me.clip.placeholderapi.updatechecker; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; +import java.util.Arrays; import javax.net.ssl.HttpsURLConnection; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.util.Msg; @@ -90,17 +91,24 @@ public class UpdateChecker implements Listener { return false; } - String plV = toReadable(pluginVersion); - String spV = toReadable(spigotVersion); - return plV.compareTo(spV) < 0; + int[] plV = toReadable(pluginVersion); + int[] spV = toReadable(spigotVersion); + + if (plV[0] < spV[0]) { + return true; + } else if ((plV[1] < spV[1])) { + return true; + } else { + return plV[2] < spV[2]; + } } - private String toReadable(String version) { - if (version.contains("-DEV-")) { - version = version.split("-DEV-")[0]; + private int[] toReadable(String version) { + if (version.contains("-DEV")) { + version = version.split("-DEV")[0]; } - return version.replaceAll("\\.", ""); + return Arrays.stream(version.split("\\.")).mapToInt(Integer::parseInt).toArray(); } @EventHandler(priority = EventPriority.MONITOR)