Skip to content

Commit

Permalink
Update to 1.20.6 (#198)
Browse files Browse the repository at this point in the history
* Fix version detection on Paper
* Fix offline player changes being overwritten
  • Loading branch information
Jikoo authored May 13, 2024
1 parent 8b71d35 commit b81eab8
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@

public class OpenPlayer extends CraftPlayer {

/**
* List of tags to always reset when saving.
*
* @see net.minecraft.world.entity.Entity#saveWithoutId(CompoundTag)
* @see net.minecraft.server.level.ServerPlayer#addAdditionalSaveData(CompoundTag)
* @see net.minecraft.world.entity.player.Player#addAdditionalSaveData(CompoundTag)
* @see net.minecraft.world.entity.LivingEntity#addAdditionalSaveData(CompoundTag)
*/
private static final Set<String> RESET_TAGS = Set.of(
// net.minecraft.world.Entity#saveWithoutId(CompoundTag)
// Entity#saveWithoutId(CompoundTag)
"CustomName",
"CustomNameVisible",
"Silent",
Expand All @@ -47,7 +55,7 @@ public class OpenPlayer extends CraftPlayer {
"HasVisualFire",
"Tags",
"Passengers",
// net.minecraft.server.level.ServerPlayer#addAdditionalSaveData(CompoundTag)
// ServerPlayer#addAdditionalSaveData(CompoundTag)
// Intentional omissions to prevent mount loss: Attach, Entity, and RootVehicle
"warden_spawn_tracker",
"enteredNetherPosition",
Expand All @@ -58,12 +66,12 @@ public class OpenPlayer extends CraftPlayer {
"SpawnAngle",
"SpawnDimension",
"raid_omen_position",
// net.minecraft.world.entity.player.Player#addAdditionalSaveData(CompoundTag)
// Player#addAdditionalSaveData(CompoundTag)
"ShoulderEntityLeft",
"ShoulderEntityRight",
"LastDeathLocation",
"current_explosion_impact_pos",
// net.minecraft.world.entity.LivingEntity#addAdditionalSaveData(CompoundTag)
// LivingEntity#addAdditionalSaveData(CompoundTag)
"ActiveEffects", // Backwards compat: Renamed from 1.19
"active_effects",
"SleepingX",
Expand All @@ -88,7 +96,7 @@ public void saveData() {
try {
PlayerDataStorage worldNBTStorage = player.server.getPlayerList().playerIo;

CompoundTag oldData = isOnline() ? null : worldNBTStorage.load(player).orElse(null);
CompoundTag oldData = isOnline() ? null : worldNBTStorage.load(player.getName().getString(), player.getStringUUID()).orElse(null);
CompoundTag playerData = getWritableTag(oldData);
playerData = player.saveWithoutId(playerData);
setExtraData(playerData);
Expand All @@ -99,11 +107,11 @@ public void saveData() {
}

Path playerDataDir = worldNBTStorage.getPlayerDir().toPath();
Path file = Files.createTempFile(playerDataDir, player.getStringUUID() + "-", ".dat");
NbtIo.writeCompressed(playerData, file);
Path tempFile = Files.createTempFile(playerDataDir, player.getStringUUID() + "-", ".dat");
NbtIo.writeCompressed(playerData, tempFile);
Path dataFile = playerDataDir.resolve(player.getStringUUID() + ".dat");
Path backupFile = playerDataDir.resolve(player.getStringUUID() + ".dat_old");
Util.safeReplaceFile(dataFile, file, backupFile);
Util.safeReplaceFile(dataFile, tempFile, backupFile);
} catch (Exception e) {
LogUtils.getLogger().warn("Failed to save player data for {}: {}", player.getScoreboardName(), e);
}
Expand Down
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
129 changes: 99 additions & 30 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 @@ -25,52 +27,119 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Constructor;

class InternalAccessor {

private final @NotNull Plugin plugin;
private final String version;
private final @Nullable 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);

private void checkSupported() {
if (versionPackage == null) {
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 @Nullable String getVersionPackage() {
if (BukkitVersions.MINECRAFT.lessThan(Version.of(1, 20, 3))) { // Min supported version: 1.20.3
return null;
}
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 null;
}
if (BukkitVersions.MINECRAFT.greaterThan(Version.of(1, 20, 6))) {
return null; // 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 +148,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 +188,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 +207,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 +220,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 +231,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 b81eab8

Please sign in to comment.