From 125394d2612a06ba3b91d5237b950e01710059ad Mon Sep 17 00:00:00 2001 From: Andret2344 Date: Tue, 31 May 2022 01:22:34 +0200 Subject: [PATCH] #89: Removed relocation checking --- README.md | 23 ++++++++++++-- base/build.gradle.kts | 3 +- .../src/main/java/org/bstats/MetricsBase.java | 19 ------------ .../java/org/bstats/RelocateCheckTest.java | 31 ------------------- 4 files changed, 21 insertions(+), 55 deletions(-) delete mode 100644 base/src/test/java/org/bstats/RelocateCheckTest.java diff --git a/README.md b/README.md index f41d62e..cb0f933 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,25 @@ This repository contains the code for all Java-based Metrics classes. ## Code Generation -The recommended way to include the Metrics classes is to use a build management tool like Gradle or Maven and -shade + relocate the required classes. +### Old way (pre 1.16) + +~~The recommended way to include the Metrics classes is to use a build management tool like Gradle or Maven and +shade + relocate the required classes.~~ + +### New way (1.16 and later) + +Since API 1.16, spigot implemented new solution to make plugins lighter. In order to get this library working, just add +it to `libraries` section of `plugin.yml` file and server will automatically download the dependency from Maven Central +and link it to all plugins that uses it. + +``` +libraries: +- org.bstats:bstats:3.0.0 +``` + +Reference: [Click here](https://www.spigotmc.org/wiki/plugin-yml/#optional-attributes) (scroll a bit down). + +--- However, to make bStats more accessible for beginners, a single file Metrics class is automatically generated on every release and pushed to the `single-file` branch. This file can simply be copy-and-pasted. @@ -30,4 +47,4 @@ For the GitHub Actions to properly work, one must configure the following [encry * `OSSRH_USERNAME`: The username used to publish the Maven Central * `OSSRH_TOKEN`: The password used to publish to Maven Central * `SIGNING_KEY`: The PGP private key used to sign the built artifacts (can be obtained by running `gpg --export-secret-keys -a > key.txt`) -* `SIGNING_PASSWORD`: The passphrase that protects the private key \ No newline at end of file +* `SIGNING_PASSWORD`: The passphrase that protects the private key diff --git a/base/build.gradle.kts b/base/build.gradle.kts index 4f47808..6e04b5f 100644 --- a/base/build.gradle.kts +++ b/base/build.gradle.kts @@ -23,5 +23,4 @@ java { tasks.test { systemProperty("metrics-version", version.toString()) - systemProperty("bstats.relocatecheck", "false") -} \ No newline at end of file +} diff --git a/base/src/main/java/org/bstats/MetricsBase.java b/base/src/main/java/org/bstats/MetricsBase.java index da1d0fe..c052c58 100644 --- a/base/src/main/java/org/bstats/MetricsBase.java +++ b/base/src/main/java/org/bstats/MetricsBase.java @@ -101,8 +101,6 @@ public MetricsBase( this.logSentData = logSentData; this.logResponseStatusText = logResponseStatusText; - checkRelocation(); - if (enabled) { // WARNING: Removing the option to opt-out will get your plugin banned from bStats startSubmitting(); } @@ -205,23 +203,6 @@ private void sendData(JsonObjectBuilder.JsonObject data) throws Exception { } } - /** - * Checks that the class was properly relocated. - */ - private void checkRelocation() { - // You can use the property to disable the check in your test environment - if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) { - // Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D - final String defaultPackage = new String( - new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's'}); - final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'}); - // We want to make sure no one just copy & pastes the example and uses the wrong package names - if (MetricsBase.class.getPackage().getName().startsWith(defaultPackage) || MetricsBase.class.getPackage().getName().startsWith(examplePackage)) { - throw new IllegalStateException("bStats Metrics class has not been relocated correctly!"); - } - } - } - /** * Gzips the given string. * diff --git a/base/src/test/java/org/bstats/RelocateCheckTest.java b/base/src/test/java/org/bstats/RelocateCheckTest.java deleted file mode 100644 index cf6bb0d..0000000 --- a/base/src/test/java/org/bstats/RelocateCheckTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.bstats; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class RelocateCheckTest { - - /** - * If the {@code bstats.relocatecheck} is not set to {@code false}, calling the constructor should throw - * an exception (because the class was not relocated for the test). - */ - @Test - public void shouldEnforceRelocation() { - System.getProperties().remove("bstats.relocatecheck"); - assertThrows(IllegalStateException.class, this::createDummyMetricsBase); - System.getProperties().setProperty("bstats.relocatecheck", "false"); - } - - @Test - public void shouldRespectDisabledRelocateCheck() { - // The bstats.relocatecheck system property is set for all tests by Gradle - assertDoesNotThrow(this::createDummyMetricsBase); - } - - private MetricsBase createDummyMetricsBase() { - return new MetricsBase("", "", -1, false, null, null, null, null, null, null, true, true, true); - } - -}