Skip to content

Commit

Permalink
Use Bukkit version to determine internal version
Browse files Browse the repository at this point in the history
Will eventually likely add an adaptor to check Paper's Server#getMinecraftVersion instead as it should be less likely to be broken by weird Bukkit implementations.
  • Loading branch information
Jikoo committed May 8, 2024
1 parent 8b71d35 commit 170ebaa
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 29 deletions.
29 changes: 29 additions & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<artifactId>spigot-api</artifactId>
<groupId>org.spigotmc</groupId>
</dependency>
<dependency>
<artifactId>planarwrappers</artifactId>
<groupId>com.github.jikoo</groupId>
</dependency>
</dependencies>

<build>
Expand All @@ -53,6 +57,31 @@
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<filters>
<filter>
<!-- Always shade entirety of required modules. -->
<artifact>com.lishid:openinv*</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>*:*</artifact>
<!-- Don't warn about file conflicts that'll be clobbered anyway. -->
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>com.github.jikoo.planarwrappers</pattern>
<shadedPattern>com.github.jikoo.openinv.planarwrappers</shadedPattern>
</relocation>
</relocations>
<minimizeJar>true</minimizeJar>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
134 changes: 105 additions & 29 deletions plugin/src/main/java/com/lishid/openinv/InternalAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.lishid.openinv;

import com.github.jikoo.planarwrappers.util.version.BukkitVersions;
import com.github.jikoo.planarwrappers.util.version.Version;
import com.lishid.openinv.internal.IAnySilentContainer;
import com.lishid.openinv.internal.IPlayerDataManager;
import com.lishid.openinv.internal.ISpecialEnderChest;
Expand All @@ -31,46 +33,120 @@
class InternalAccessor {

private final @NotNull Plugin plugin;
private final String version;
private final String versionPackage;
private boolean supported = false;
private IPlayerDataManager playerDataManager;
private IAnySilentContainer anySilentContainer;

InternalAccessor(@NotNull Plugin plugin) {
this.plugin = plugin;
this.versionPackage = getVersionPackage();
checkSupported();

String packageName = plugin.getServer().getClass().getPackage().getName();
this.version = packageName.substring(packageName.lastIndexOf('.') + 1);
/*
* TODO more loose versioning support:
* - Prefer classes if available
* - Use ASM to rewrite best class if not available? I.e. unversion packages on Paper
* - should verify OBC/NMS calls' existence!
* - could possibly be expanded to tentatively support unknown versions
*/
}

private void checkSupported() {
if ("nope".equals(this.versionPackage)) { // TODO this is a terrible way to handle things
return;
}
try {
Class.forName("com.lishid.openinv.internal." + this.version + ".SpecialPlayerInventory");
Class.forName("com.lishid.openinv.internal." + this.version + ".SpecialEnderChest");
Class.forName("com.lishid.openinv.internal." + this.versionPackage + ".SpecialPlayerInventory");
Class.forName("com.lishid.openinv.internal." + this.versionPackage + ".SpecialEnderChest");
this.playerDataManager = this.createObject(IPlayerDataManager.class, "PlayerDataManager");
this.anySilentContainer = this.createObject(IAnySilentContainer.class, "AnySilentContainer");
this.supported = InventoryAccess.isUsable();
} catch (Exception ignored) {}
}

public String getReleasesLink() {
private String getVersionPackage() {
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 20, 3))) { // Min supported version: 1.20.3
return "nope";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 20, 4))) { // 1.20.3, 1.20.4
return "v1_20_R3";
}
if (BukkitVersions.MINECRAFT.greaterThanOrEqual(Version.of(1, 21))) {
return "nope";
}
if (BukkitVersions.MINECRAFT.greaterThan(Version.of(1, 20, 6))) {
return "nope"; // TODO: use at your own risk flag?
}
// 1.20.5, 1.20.6
return "v1_20_R4";
}

return switch (version) {
case "1_4_5", "1_4_6", "v1_4_R1", "v1_5_R2", "v1_5_R3", "v1_6_R1", "v1_6_R2", "v1_6_R3",
"v1_7_R1", "v1_7_R2", "v1_7_R3", "v1_7_R4", "v1_8_R1", "v1_8_R2",
"v1_9_R1", "v1_9_R2", "v1_10_R1", "v1_11_R1", "v1_12_R1"
-> "https://github.com/lishid/OpenInv/releases/tag/4.0.0 (OpenInv-legacy)";
case "v1_13_R1" -> "https://github.com/lishid/OpenInv/releases/tag/4.0.0";
case "v1_13_R2" -> "https://github.com/lishid/OpenInv/releases/tag/4.0.7";
case "v1_14_R1" -> "https://github.com/lishid/OpenInv/releases/tag/4.1.1";
case "v1_16_R1" -> "https://github.com/lishid/OpenInv/releases/tag/4.1.4";
case "v1_8_R3", "v1_15_R1", "v1_16_R2" -> "https://github.com/lishid/OpenInv/releases/tag/4.1.5";
case "v1_16_R3" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.1.8";
case "v1_17_R1", "v1_18_R1" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.1.10";
case "v1_19_R1" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.2.2";
case "v1_18_R2", "v1_19_R2" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.3.0";
case "v1_20_R1" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.4.1";
case "v1_19_R3", "v1_20_R2" -> "https://github.com/Jikoo/OpenInv/releases/tag/4.4.3";
default -> "https://github.com/Jikoo/OpenInv/releases";
};
public String getReleasesLink() {
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 4, 4))) { // Good luck.
return "https://dev.bukkit.org/projects/openinv/files?&sort=datecreated";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 8, 8))) { // 1.8.8
return "https://github.com/lishid/OpenInv/releases/tag/4.1.5";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 13))) { // 1.4.4+ had versioned packages.
return "https://github.com/lishid/OpenInv/releases/tag/4.0.0 (OpenInv-legacy)";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 13))) { // 1.13
return "https://github.com/lishid/OpenInv/releases/tag/4.0.0";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 14))) { // 1.13.1, 1.13.2
return "https://github.com/lishid/OpenInv/releases/tag/4.0.7";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 14))) { // 1.14 to 1.14.1 had no revision bump.
return "https://github.com/lishid/OpenInv/releases/tag/4.0.0";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 14, 1))) { // 1.14.1 to 1.14.2 had no revision bump.
return "https://github.com/lishid/OpenInv/releases/tag/4.0.1";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 15))) { // 1.14.2
return "https://github.com/lishid/OpenInv/releases/tag/4.1.1";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 15, 1))) { // 1.15, 1.15.1
return "https://github.com/lishid/OpenInv/releases/tag/4.1.5";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 16))) { // 1.15.2
return "https://github.com/Jikoo/OpenInv/commit/502f661be39ee85d300851dd571f3da226f12345 (never released)";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 16, 1))) { // 1.16, 1.16.1
return "https://github.com/lishid/OpenInv/releases/tag/4.1.4";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 16, 3))) { // 1.16.2, 1.16.3
return "https://github.com/lishid/OpenInv/releases/tag/4.1.5";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 17))) { // 1.16.4, 1.16.5
return "https://github.com/Jikoo/OpenInv/releases/tag/4.1.8";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 18, 1))) { // 1.17, 1.18, 1.18.1
return "https://github.com/Jikoo/OpenInv/releases/tag/4.1.10";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 19))) { // 1.18.2
return "https://github.com/Jikoo/OpenInv/releases/tag/4.3.0";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 19))) { // 1.19
return "https://github.com/Jikoo/OpenInv/releases/tag/4.2.0";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 19, 1))) { // 1.19.1
return "https://github.com/Jikoo/OpenInv/releases/tag/4.2.2";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 19, 3))) { // 1.19.2, 1.19.3
return "https://github.com/Jikoo/OpenInv/releases/tag/4.3.0";
}
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 20))) { // 1.19.4
return "https://github.com/Jikoo/OpenInv/releases/tag/4.4.3";
}
if (BukkitVersions.MINECRAFT.lessThanOrEqual(Version.of(1, 20, 1))) { // 1.20, 1.20.1
return "https://github.com/Jikoo/OpenInv/releases/tag/4.4.1";
}
if (BukkitVersions.MINECRAFT.equals(Version.of(1, 20, 3))) { // 1.20.3
return "https://github.com/Jikoo/OpenInv/releases/tag/4.4.3";
}
return "https://github.com/Jikoo/OpenInv/releases";
}

private @NotNull <T> T createObject(
Expand All @@ -79,7 +155,7 @@ public String getReleasesLink() {
@NotNull Object @NotNull ... params)
throws ClassCastException, ReflectiveOperationException {
// Fetch internal class if it exists.
Class<?> internalClass = Class.forName("com.lishid.openinv.internal." + this.version + "." + className);
Class<?> internalClass = Class.forName("com.lishid.openinv.internal." + this.versionPackage + "." + className);

// Quick return: no parameters, no need to fiddle about finding the correct constructor.
if (params.length == 0) {
Expand Down Expand Up @@ -119,7 +195,7 @@ public String getReleasesLink() {
@NotNull Player player,
boolean online) throws InstantiationException {
if (!this.supported) {
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT));
}
try {
return this.createObject(assignableClass, className, player, online);
Expand All @@ -138,7 +214,7 @@ public String getReleasesLink() {
*/
public @NotNull IAnySilentContainer getAnySilentContainer() {
if (!this.supported) {
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT));
}
return this.anySilentContainer;
}
Expand All @@ -151,7 +227,7 @@ public String getReleasesLink() {
*/
public @NotNull IPlayerDataManager getPlayerDataManager() {
if (!this.supported) {
throw new IllegalStateException(String.format("Unsupported server version %s!", this.version));
throw new IllegalStateException(String.format("Unsupported server version %s!", BukkitVersions.MINECRAFT));
}
return this.playerDataManager;
}
Expand All @@ -162,7 +238,7 @@ public String getReleasesLink() {
* @return the version
*/
public @NotNull String getVersion() {
return this.version;
return BukkitVersions.MINECRAFT.toString();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencyManagement>
Expand Down Expand Up @@ -98,6 +102,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>planarwrappers</artifactId>
<groupId>com.github.jikoo</groupId>
<version>3.2.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 170ebaa

Please sign in to comment.