From 4c968f012dea657440e5240e29034066ac2fcbce Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 05:03:47 -0600 Subject: [PATCH 1/9] Cleanup build.gradle (cherry picked from commit 7750ba7033dc9fdf40d55fcd9ecc20a94819a7c1) --- build.gradle | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index e8ad4fc..ffe47d3 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import org.apache.tools.ant.filters.ReplaceTokens + plugins { id "java" id "maven-publish" @@ -6,7 +8,7 @@ plugins { } group "me.clip" -version "2.10.10-DEV-${System.getProperty("BUILD_NUMBER")}" +version "2.10.10-DEV" description "An awesome placeholder provider!" @@ -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/") From 0c102a18238452ca7a560e4ae38bff4a6160302b Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 05:04:36 -0600 Subject: [PATCH 2/9] Remove the @NotNull's because they are overridden by expansion devs anyways, and it turns out people can make them null anyways. This adds a hashcode check and pretty error for it. (cherry picked from commit f5386d4ca55c23b54f3c5f952f08040b0967f4a3) --- .../expansion/PlaceholderExpansion.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java index 16c216e..4133017 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; @@ -46,7 +45,6 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return placeholder identifier that is associated with this expansion */ - @NotNull public abstract String getIdentifier(); /** @@ -54,7 +52,6 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return name of the author for this expansion */ - @NotNull public abstract String getAuthor(); /** @@ -62,7 +59,6 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return current version of this expansion */ - @NotNull public abstract String getVersion(); /** @@ -70,7 +66,6 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return {@link #getIdentifier()} by default, name of this expansion if specified */ - @NotNull public String getName() { return getIdentifier(); } @@ -90,7 +85,6 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return placeholder list that this expansion provides */ - @NotNull public List getPlaceholders() { return Collections.emptyList(); } @@ -369,4 +363,18 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { return null; } + @Override + public int hashCode() { + if (this.getIdentifier() == null) { + throw new NullPointerException("Identifier must not be null! Error occurred in: " + this.getClass().getName()); + } + if (this.getAuthor() == null) { + throw new NullPointerException("Author must not be null! Error occurred in: " + this.getClass().getName()); + } + if (this.getVersion() == null) { + throw new NullPointerException("Version must not be null! Error occurred in: " + this.getClass().getName()); + } + + return super.hashCode(); + } } From 14d885392c1d913c5b44b78ecbae20651f253a82 Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 05:05:18 -0600 Subject: [PATCH 3/9] Fix version checker to compare the individual semver numbers instead of combining it together. (cherry picked from commit ef5cd9d37680cc6cc1321ef2e04af09b44a2c681) --- .../updatechecker/UpdateChecker.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java index bce51b8..44be055 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,22 @@ 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) From ac9b0c42b7593001eb177bb2ee60b433c36948ff Mon Sep 17 00:00:00 2001 From: Star Date: Wed, 30 Jun 2021 05:33:06 -0600 Subject: [PATCH 4/9] Update src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java Co-authored-by: Andre_601 <11576465+Andre601@users.noreply.github.com> (cherry picked from commit ccf4f5934386b005831757d298e2da9d8f46cb1a) --- .../me/clip/placeholderapi/updatechecker/UpdateChecker.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java index 44be055..2163be2 100644 --- a/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java +++ b/src/main/java/me/clip/placeholderapi/updatechecker/UpdateChecker.java @@ -98,7 +98,9 @@ public class UpdateChecker implements Listener { return true; } else if ((plV[1] < spV[1])) { return true; - } else return plV[2] < spV[2]; + } else { + return plV[2] < spV[2]; + } } private int[] toReadable(String version) { From 679ef900913d4c2f542b0b6b22fcb1324b1f1668 Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 05:43:14 -0600 Subject: [PATCH 5/9] Re-add NotNull's (cherry picked from commit 2bfd8e7e5efb25f648b8a6de934b1eff675481b1) --- .../clip/placeholderapi/expansion/PlaceholderExpansion.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java index 4133017..4fac2a1 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java +++ b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java @@ -45,6 +45,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return placeholder identifier that is associated with this expansion */ + @NotNull public abstract String getIdentifier(); /** @@ -52,6 +53,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return name of the author for this expansion */ + @NotNull public abstract String getAuthor(); /** @@ -59,6 +61,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return current version of this expansion */ + @NotNull public abstract String getVersion(); /** @@ -66,6 +69,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return {@link #getIdentifier()} by default, name of this expansion if specified */ + @NotNull public String getName() { return getIdentifier(); } @@ -85,6 +89,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { * * @return placeholder list that this expansion provides */ + @NotNull public List getPlaceholders() { return Collections.emptyList(); } From 252802dcbe68e610c08de18cc587f31bf5d8fc53 Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 10:13:12 -0600 Subject: [PATCH 6/9] Removed nullcheck from hashcode, and moved it up the chain. --- .../expansion/PlaceholderExpansion.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java index 4fac2a1..1e71a45 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java +++ b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java @@ -367,19 +367,4 @@ public abstract class PlaceholderExpansion extends PlaceholderHook { public String getLink() { return null; } - - @Override - public int hashCode() { - if (this.getIdentifier() == null) { - throw new NullPointerException("Identifier must not be null! Error occurred in: " + this.getClass().getName()); - } - if (this.getAuthor() == null) { - throw new NullPointerException("Author must not be null! Error occurred in: " + this.getClass().getName()); - } - if (this.getVersion() == null) { - throw new NullPointerException("Version must not be null! Error occurred in: " + this.getClass().getName()); - } - - return super.hashCode(); - } } From 94bf5fea57ed67cae96f9e3adcca30d637479e93 Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 10:13:29 -0600 Subject: [PATCH 7/9] POJO to help with checking that expansions have all required methods implemented. --- .../expansion/manager/MethodSignature.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/me/clip/placeholderapi/expansion/manager/MethodSignature.java 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; + } +} From 9b6b558002cc9b23fc69c60884d52df55e6c0142 Mon Sep 17 00:00:00 2001 From: Starmism Date: Wed, 30 Jun 2021 10:13:47 -0600 Subject: [PATCH 8/9] Add method implementation checking and nullchecking for fields. --- .../manager/LocalExpansionManager.java | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) 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; } From c4a2256fff7ec2fb15ad34fc4f8231e6b02f3408 Mon Sep 17 00:00:00 2001 From: Starmism Date: Thu, 1 Jul 2021 07:54:42 -0600 Subject: [PATCH 9/9] Appease the Pigman --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index ffe47d3..b1c6b8a 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group "me.clip" -version "2.10.10-DEV" +version "2.10.10-DEV-${System.getProperty("BUILD_NUMBER")}" description "An awesome placeholder provider!"