Skip to content

Commit

Permalink
Expand preserved old data
Browse files Browse the repository at this point in the history
- Fix play timestamps being erroneously updated
- Fix vehicle possibly not being re-mounted
  • Loading branch information
Jikoo committed Nov 12, 2023
1 parent f9c7ed7 commit 79e8842
Showing 1 changed file with 68 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package com.lishid.openinv.internal.v1_20_R2;

import com.mojang.logging.LogUtils;
import java.io.File;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NumericTag;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.storage.PlayerDataStorage;
import org.bukkit.craftbukkit.v1_20_R2.CraftServer;
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public class OpenPlayer extends CraftPlayer {

Expand All @@ -48,13 +53,9 @@ public void saveData() {
setExtraData(playerData);

if (!isOnline()) {
// Special case: save old vehicle data
// Preserve certain data when offline.
CompoundTag oldData = worldNBTStorage.load(player);

if (oldData != null && oldData.contains("RootVehicle", 10)) {
// See net.minecraft.server.PlayerList#a(NetworkManager, EntityPlayer) and net.minecraft.server.EntityPlayer#b(NBTTagCompound)
playerData.put("RootVehicle", oldData.getCompound("RootVehicle"));
}
revertSpecialValues(playerData, oldData);
}

File file = File.createTempFile(player.getStringUUID() + "-", ".dat", worldNBTStorage.getPlayerDir());
Expand All @@ -67,4 +68,64 @@ public void saveData() {
}
}

private void revertSpecialValues(@NotNull CompoundTag newData, @Nullable CompoundTag oldData) {
if (oldData == null) {
return;
}

// Prevent vehicle deletion.
if (oldData.contains("RootVehicle", Tag.TAG_COMPOUND)) {
// See net.minecraft.server.players.PlayerList#save(ServerPlayer)
// See net.minecraft.server.level.ServerPlayer#addAdditionalSaveData(CompoundTag)
newData.putUUID("Attach", oldData.getUUID("Attach"));
newData.put("Entity", oldData.getCompound("Entity"));
newData.put("RootVehicle", oldData.getCompound("RootVehicle"));
}

// Revert automatic updates to play timestamps.
copyValue(newData, oldData, "bukkit", "lastPlayed", NumericTag.class);
copyValue(newData, oldData, "Paper", "LastSeen", NumericTag.class);
copyValue(newData, oldData, "Paper", "LastLogin", NumericTag.class);
}

private <T extends Tag> void copyValue(
@NotNull CompoundTag source,
@NotNull CompoundTag target,
@NotNull String container,
@NotNull String key,
@NotNull Class<T> tagType) {
CompoundTag oldContainer = getTag(source, container, CompoundTag.class);
CompoundTag newContainer = getTag(target, container, CompoundTag.class);

// Container being null means the server implementation doesn't store this data.
if (oldContainer == null || newContainer == null) {
return;
}

// If old tag exists, copy it to new location, removing otherwise.
setTag(newContainer, key, getTag(oldContainer, key, tagType));
}

private <T extends Tag> @Nullable T getTag(
@NotNull CompoundTag container,
@NotNull String key,
@NotNull Class<T> dataType) {
Tag value = container.get(key);
if (value == null || !dataType.isAssignableFrom(value.getClass())) {
return null;
}
return dataType.cast(value);
}

private <T extends Tag> void setTag(
@NotNull CompoundTag container,
@NotNull String key,
@Nullable T data) {
if (data == null) {
container.remove(key);
} else {
container.put(key, data);
}
}

}

0 comments on commit 79e8842

Please sign in to comment.