diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 684f35c..01b88a9 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,22 +4,22 @@
-
-
-
-
-
+
+
+
+
+
-
-
+
+
+
+
-
-
@@ -145,7 +145,7 @@
"ignore.virus.scanning.warn.message": "true",
"jdk.selected.JAVA_MODULE": "corretto-17",
"kotlin-language-version-configured": "true",
- "last_opened_file_path": "C:/Development/EntityLib",
+ "last_opened_file_path": "C:/Development/EntityLib/buildSrc/src/main/resources",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -165,11 +165,16 @@
}
+
+
-
-
+
+
+
+
+
@@ -431,8 +436,21 @@
-
+
+
+
+
+
+
+
+ 1720354581808
+
+
+
+ 1720354581808
+
+
@@ -442,6 +460,10 @@
+
+
+
+
diff --git a/api/build.gradle.kts b/api/build.gradle.kts
index af548d8..f90ab8c 100644
--- a/api/build.gradle.kts
+++ b/api/build.gradle.kts
@@ -1,6 +1,7 @@
plugins {
entitylib.`java-conventions`
- id("java-library")
+ `java-library`
+ `el-version`
}
dependencies {
@@ -9,3 +10,9 @@ dependencies {
compileOnlyApi(libs.bundles.adventure)
compileOnlyApi(libs.packetevents.api)
}
+
+tasks {
+ generateVersionsFile {
+ packageName = "me.tofaa.entitylib.utils"
+ }
+}
diff --git a/api/src/main/java/me/tofaa/entitylib/APIConfig.java b/api/src/main/java/me/tofaa/entitylib/APIConfig.java
index 0db6d93..9631dd4 100644
--- a/api/src/main/java/me/tofaa/entitylib/APIConfig.java
+++ b/api/src/main/java/me/tofaa/entitylib/APIConfig.java
@@ -30,7 +30,7 @@ public final class APIConfig {
@Blocking
public boolean requiresUpdate() throws IOException {
if (!checkForUpdates) return false;
- GithubUpdater updater = new GithubUpdater("Tofaa2", "EntityLib", EntityLib.getVersion());
+ GithubUpdater updater = new GithubUpdater("Tofaa2", "EntityLib");
return !updater.isLatestVersion();
}
diff --git a/api/src/main/java/me/tofaa/entitylib/EntityLib.java b/api/src/main/java/me/tofaa/entitylib/EntityLib.java
index 0cfbd3f..9ccda22 100644
--- a/api/src/main/java/me/tofaa/entitylib/EntityLib.java
+++ b/api/src/main/java/me/tofaa/entitylib/EntityLib.java
@@ -1,5 +1,9 @@
package me.tofaa.entitylib;
+import com.github.retrooper.packetevents.util.PEVersion;
+import me.tofaa.entitylib.utils.ELVersion;
+import me.tofaa.entitylib.utils.ELVersions;
+
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;
@@ -15,6 +19,7 @@ public final class EntityLib {
EntityLib.platform = platform;
platform.setupApi(settings);
api = platform.getAPI();
+
if (api.getSettings().shouldCheckForUpdate()) {
try {
if (api.getSettings().isDebugMode()) {
@@ -45,7 +50,7 @@ public final class EntityLib {
return platform;
}
- public static String getVersion() {
- return "2.3.1-SNAPSHOT";
+ public static ELVersion getVersion() {
+ return ELVersions.CURRENT;
}
}
diff --git a/api/src/main/java/me/tofaa/entitylib/utils/ELVersion.java b/api/src/main/java/me/tofaa/entitylib/utils/ELVersion.java
new file mode 100644
index 0000000..ea05cb3
--- /dev/null
+++ b/api/src/main/java/me/tofaa/entitylib/utils/ELVersion.java
@@ -0,0 +1,187 @@
+package me.tofaa.entitylib.utils;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Objects;
+
+public class ELVersion implements Comparable {
+
+ private final int major;
+ private final int minor;
+ private final int patch;
+ private final boolean snapshot;
+
+ /**
+ * Constructs a {@link ELVersion} instance.
+ *
+ * @param major the major version number.
+ * @param minor the minor version number.
+ * @param patch the patch version number.
+ * @param snapshot whether the version is a snapshot.
+ */
+ public ELVersion(final int major, final int minor, final int patch, final boolean snapshot) {
+ this.major = major;
+ this.minor = minor;
+ this.patch = patch;
+ this.snapshot = snapshot;
+ }
+
+ /**
+ * Constructs a {@link ELVersion} instance with snapshot defaulted to false.
+ *
+ * @param major the major version number.
+ * @param minor the minor version number.
+ * @param patch the patch version number.
+ */
+ public ELVersion(final int major, final int minor, final int patch) {
+ this(major, minor, patch, false);
+ }
+
+ /**
+ * Constructs a {@link ELVersion} instance from a version string.
+ *
+ * @param version the version string (e.g., "1.8.9-SNAPSHOT").
+ * @throws IllegalArgumentException if the version string format is incorrect.
+ */
+ public static ELVersion fromString(@NotNull final String version) {
+ String versionWithoutSnapshot = version.replace("-SNAPSHOT", "");
+ String[] parts = versionWithoutSnapshot.split("\\.");
+
+ if (parts.length < 2 || parts.length > 3) {
+ throw new IllegalArgumentException("Version string must be in the format 'major.minor[.patch][-SNAPSHOT]' found '" + version + "' instead.");
+ }
+
+ int major = Integer.parseInt(parts[0]);
+ int minor = Integer.parseInt(parts[1]);
+ int patch = parts.length > 2 ? Integer.parseInt(parts[2]) : 0;
+ boolean snapshot = version.contains("-SNAPSHOT");
+
+ return new ELVersion(major, minor, patch, snapshot);
+ }
+
+ /**
+ * Gets the major version number.
+ *
+ * @return the major version number.
+ */
+ public int major() {
+ return major;
+ }
+
+ /**
+ * Gets the minor version number.
+ *
+ * @return the minor version number.
+ */
+ public int minor() {
+ return minor;
+ }
+
+ /**
+ * Gets the patch version number.
+ *
+ * @return the patch version number.
+ */
+ public int patch() {
+ return patch;
+ }
+
+ /**
+ * Checks if the version is a snapshot.
+ *
+ * @return true if snapshot, false otherwise.
+ */
+ public boolean snapshot() {
+ return snapshot;
+ }
+
+ /**
+ * Compares this {@link ELVersion} with another {@link ELVersion}.
+ *
+ * @param other the other {@link ELVersion}.
+ * @return a negative integer, zero, or a positive integer as this version is less than,
+ * equal to, or greater than the specified version.
+ */
+ @Override
+ public int compareTo(@NotNull final ELVersion other) {
+ int majorCompare = Integer.compare(this.major, other.major);
+ if (majorCompare != 0) return majorCompare;
+
+ int minorCompare = Integer.compare(this.minor, other.minor);
+ if (minorCompare != 0) return minorCompare;
+
+ int patchCompare = Integer.compare(this.patch, other.patch);
+ if (patchCompare != 0) return patchCompare;
+
+ return Boolean.compare(other.snapshot, this.snapshot);
+ }
+
+ /**
+ * Checks if the provided object is equal to this {@link ELVersion}.
+ *
+ * @param obj the object to compare.
+ * @return true if the provided object is equal to this {@link ELVersion}, false otherwise.
+ */
+ @Override
+ public boolean equals(@NotNull final Object obj) {
+ if (this == obj) return true;
+ if (!(obj instanceof ELVersion)) return false;
+ ELVersion other = (ELVersion) obj;
+
+ return this.major == other.major &&
+ this.minor == other.minor &&
+ this.patch == other.patch &&
+ this.snapshot == other.snapshot;
+ }
+
+ /**
+ * Checks if this version is newer than the provided version.
+ *
+ * @param otherVersion the other {@link ELVersion}.
+ * @return true if this version is newer, false otherwise.
+ */
+ public boolean isNewerThan(@NotNull final ELVersion otherVersion) {
+ return this.compareTo(otherVersion) > 0;
+ }
+
+ /**
+ * Checks if this version is older than the provided version.
+ *
+ * @param otherVersion the other {@link ELVersion}.
+ * @return true if this version is older, false otherwise.
+ */
+ public boolean isOlderThan(@NotNull final ELVersion otherVersion) {
+ return this.compareTo(otherVersion) < 0;
+ }
+
+ /**
+ * Returns a hash code value for this {@link ELVersion}.
+ *
+ * @return a hash code value.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(major, minor, patch, snapshot);
+ }
+
+ /**
+ * Creates and returns a copy of this {@link ELVersion}.
+ *
+ * @return a clone of this instance.
+ */
+ @Override
+ public ELVersion clone() {
+ return new ELVersion(major, minor, patch, snapshot);
+ }
+
+ /**
+ * Converts the {@link ELVersion} to a string representation.
+ *
+ * @return a string representation of the version.
+ */
+ @Override
+ public String toString() {
+ return major + "." + minor + "." + patch + (snapshot ? "-SNAPSHOT" : "");
+ }
+}
+
diff --git a/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java b/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
index 4d029fc..63b423c 100644
--- a/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
+++ b/api/src/main/java/me/tofaa/entitylib/utils/GithubUpdater.java
@@ -14,40 +14,39 @@ public final class GithubUpdater {
private final String org;
private final String repo;
- private final String currentVersion;
+ private final ELVersion currentVersion;
- public GithubUpdater(String org, String repo, String currentVersion) {
+ public GithubUpdater(String org, String repo) {
this.org = org;
this.repo = repo;
- this.currentVersion = currentVersion;
+ this.currentVersion = ELVersions.CURRENT;
}
@Blocking
public boolean isLatestVersion() throws IOException {
- String latest = getLatestVersion();
- return latest != null && latest.equals(currentVersion);
+ ELVersion latest = getLatestVersion();
+ return currentVersion.isNewerThan(latest);
}
-
@Blocking
- public String getLatestVersion() throws IOException {
- URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest");
- URLConnection connection = url.openConnection();
- connection.addRequestProperty("User-Agent", "Mozilla/5.0");
- InputStreamReader isr = new InputStreamReader(connection.getInputStream());
- BufferedReader reader = new BufferedReader(isr);
- String response = reader.readLine();
- JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class);
- reader.close();
- isr.close();
- if (json.has("name")) {
- return json.get("name").getAsString();
- }
- throw new IOException("Could not find name attribute in github api fetch");
+ public ELVersion getLatestVersion() throws IOException {
+ URL url = new URL("https://api.github.com/repos/" + org + "/" + repo + "/releases/latest");
+ URLConnection connection = url.openConnection();
+ connection.addRequestProperty("User-Agent", "Mozilla/5.0");
+ InputStreamReader isr = new InputStreamReader(connection.getInputStream());
+ BufferedReader reader = new BufferedReader(isr);
+ String response = reader.readLine();
+ JsonObject json = AdventureSerializer.getGsonSerializer().serializer().fromJson(response, JsonObject.class);
+
+ if (json.has("name")) {
+ return ELVersion.fromString(json.get("name").getAsString().replaceFirst("^[vV]", ""));
+ }
+ throw new IOException("Could not find name attribute in github api fetch");
}
+ @Deprecated
public String getCurrentVersion() {
- return currentVersion;
+ return currentVersion.toString();
}
public String getOrg() {
diff --git a/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts
index 4ed76ee..0808888 100644
--- a/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts
+++ b/buildSrc/src/main/kotlin/entitylib.java-conventions.gradle.kts
@@ -1,6 +1,6 @@
plugins {
java
- id("maven-publish")
+ `maven-publish`
}
group = rootProject.group
diff --git a/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionPlugin.kt b/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionPlugin.kt
new file mode 100644
index 0000000..0e5ab27
--- /dev/null
+++ b/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionPlugin.kt
@@ -0,0 +1,31 @@
+package me.tofaa.entitylib.version
+
+import org.gradle.api.Plugin
+import org.gradle.api.Project
+import org.gradle.api.tasks.SourceSet
+import org.gradle.api.tasks.SourceSetContainer
+import org.gradle.kotlin.dsl.getByName
+import org.gradle.kotlin.dsl.register
+
+class ELVersionPlugin : Plugin {
+
+ override fun apply(target: Project) {
+ val task = target.tasks.register(ELVersionTask.TASK_NAME) {
+ group = target.rootProject.name.toString()
+
+ version = target.version.toString()
+ outputDir = target.layout.buildDirectory.dir("generated/sources/src/java/main")
+ }
+
+ target.afterEvaluate {
+ val sourceSets = target.extensions.getByName("sourceSets")
+
+ sequenceOf(SourceSet.MAIN_SOURCE_SET_NAME, SourceSet.TEST_SOURCE_SET_NAME).forEach {
+ sourceSets.getByName(it).java.srcDir(task.flatMap { it.outputDir })
+ }
+
+ task.get().generate()
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionTask.kt b/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionTask.kt
new file mode 100644
index 0000000..cacab79
--- /dev/null
+++ b/buildSrc/src/main/kotlin/me/tofaa/entitylib/version/ELVersionTask.kt
@@ -0,0 +1,78 @@
+package me.tofaa.entitylib.version
+
+import org.gradle.api.DefaultTask
+import org.gradle.api.file.Directory
+import org.gradle.api.provider.Provider
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.OutputDirectory
+import org.gradle.api.tasks.TaskAction
+
+abstract class ELVersionTask : DefaultTask() {
+
+ companion object {
+ const val TASK_NAME = "generateVersionsFile"
+ }
+
+ @get:Input
+ abstract var packageName: String
+
+ @get:Input
+ abstract var version: String
+
+ @get:OutputDirectory
+ abstract var outputDir: Provider
+
+ @TaskAction
+ fun generate() {
+ val dir = outputDir.get().dir(packageName.replace('.', '/'))
+ dir.asFile.mkdirs()
+
+ val file = dir.file("ELVersions.java").asFile
+ if (!file.exists()) {
+ file.createNewFile()
+ }
+
+ val ver = Version.fromString(version)
+ logger.info("Generating ELVersions.java with version $ver")
+
+ file.writeText("""
+ /**
+ * This file is generated by the auto-version task. Modifying it will have no effect.
+ */
+ package $packageName;
+
+ public final class ELVersions {
+
+ public static final String RAW = "$version";
+ public static final ELVersion CURRENT = new ELVersion(${ver.major}, ${ver.minor}, ${ver.patch}, ${ver.snapShot});
+ public static final ELVersion UNKNOWN = new ELVersion(0, 0, 0);
+
+ private ELVersions() {
+ throw new IllegalStateException();
+ }
+ }
+ """.trimIndent())
+ }
+
+ private data class Version(
+ val major: Int,
+ val minor: Int,
+ val patch: Int,
+ val snapShot: Boolean
+ ) {
+ companion object {
+ private val REGEX = Regex("""(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?""")
+
+ fun fromString(version: String): Version {
+ val match = REGEX.matchEntire(version) ?: throw IllegalArgumentException("Invalid version: $version")
+ return Version(
+ match.groupValues[1].toInt(),
+ match.groupValues[2].toInt(),
+ match.groupValues[3].toInt(),
+ match.groupValues[4].isNotEmpty()
+ )
+ }
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/buildSrc/src/main/resources/META-INF/gradle-plugins/el-version.properties b/buildSrc/src/main/resources/META-INF/gradle-plugins/el-version.properties
new file mode 100644
index 0000000..095e231
--- /dev/null
+++ b/buildSrc/src/main/resources/META-INF/gradle-plugins/el-version.properties
@@ -0,0 +1 @@
+implementation-class=me.tofaa.entitylib.version.ELVersionPlugin
\ No newline at end of file
diff --git a/common/build.gradle.kts b/common/build.gradle.kts
index d6eb1d7..e2cf686 100644
--- a/common/build.gradle.kts
+++ b/common/build.gradle.kts
@@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
- id("java-library")
+ `java-library`
}
dependencies {
diff --git a/model-engine-addon/build.gradle.kts b/model-engine-addon/build.gradle.kts
index 1722ca7..7b901a0 100644
--- a/model-engine-addon/build.gradle.kts
+++ b/model-engine-addon/build.gradle.kts
@@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
- id("java-library")
+ `java-library`
}
repositories {
diff --git a/platforms/spigot/build.gradle.kts b/platforms/spigot/build.gradle.kts
index 1c14384..e6ac43b 100644
--- a/platforms/spigot/build.gradle.kts
+++ b/platforms/spigot/build.gradle.kts
@@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
- id("java-library")
+ `java-library`
}
repositories {
diff --git a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java
index ffe81e5..7aaa98e 100644
--- a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java
+++ b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java
@@ -45,8 +45,9 @@ public class SpigotEntityLibPlatform extends AbstractPlatform {
if (settings.shouldUseBstats()) {
PacketEventsAPI pe = (PacketEventsAPI)api.getPacketEvents();
Metrics metrics = new Metrics((JavaPlugin) pe.getPlugin(), 21916);
- metrics.addCustomChart(new Metrics.SimplePie("entitylib-version", EntityLib::getVersion));
+ metrics.addCustomChart(new Metrics.SimplePie("entitylib-version", () -> EntityLib.getVersion().toString()));
}
+
}
diff --git a/platforms/standalone/build.gradle.kts b/platforms/standalone/build.gradle.kts
index 42f388c..48225d7 100644
--- a/platforms/standalone/build.gradle.kts
+++ b/platforms/standalone/build.gradle.kts
@@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
- id("java-library")
+ `java-library`
}
dependencies {
diff --git a/platforms/velocity/build.gradle.kts b/platforms/velocity/build.gradle.kts
index 7c4791f..78ce677 100644
--- a/platforms/velocity/build.gradle.kts
+++ b/platforms/velocity/build.gradle.kts
@@ -1,6 +1,6 @@
plugins {
entitylib.`java-conventions`
- id("java-library")
+ `java-library`
}
repositories {