Files
CatServer/patches/net/minecraft/world/storage/SaveHandler.java.patch

199 lines
7.3 KiB
Diff

--- ../src-base/minecraft/net/minecraft/world/storage/SaveHandler.java
+++ ../src-work/minecraft/net/minecraft/world/storage/SaveHandler.java
@@ -6,8 +6,11 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
@@ -19,6 +22,7 @@
import net.minecraft.world.gen.structure.template.TemplateManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
+import org.bukkit.craftbukkit.entity.CraftPlayer;
public class SaveHandler implements ISaveHandler, IPlayerFileData
{
@@ -31,6 +35,8 @@
private final TemplateManager structureTemplateManager;
protected final DataFixer dataFixer;
+ private UUID uuid = null;
+
public SaveHandler(File p_i46648_1_, String saveDirectoryNameIn, boolean p_i46648_3_, DataFixer dataFixerIn)
{
this.dataFixer = dataFixerIn;
@@ -73,7 +79,7 @@
catch (IOException ioexception)
{
ioexception.printStackTrace();
- throw new RuntimeException("Failed to check session lock, aborting");
+ throw new RuntimeException("Failed to check session lock for world located at " + this.worldDirectory + ", aborting. Stop the server and delete the session.lock in this world to prevent further issues."); // Spigot
}
}
@@ -93,7 +99,7 @@
{
if (datainputstream.readLong() != this.initializationTime)
{
- throw new MinecraftException("The save is being accessed from another location, aborting");
+ throw new MinecraftException("The save for world located at " + this.worldDirectory + " is being accessed from another location, aborting"); // Spigot
}
}
finally
@@ -103,7 +109,7 @@
}
catch (IOException var7)
{
- throw new MinecraftException("Failed to check session lock, aborting");
+ throw new MinecraftException("Failed to check session lock for world located at " + this.worldDirectory + ", aborting. Stop the server and delete the session.lock in this world to prevent further issues."); // Spigot
}
}
@@ -119,7 +125,7 @@
if (file1.exists())
{
- WorldInfo worldinfo = SaveFormatOld.getWorldData(file1, this.dataFixer);
+ WorldInfo worldinfo = SaveFormatOld.loadAndFix(file1, this.dataFixer, this);
if (worldinfo != null)
{
@@ -127,8 +133,9 @@
}
}
+ net.minecraftforge.fml.common.FMLCommonHandler.instance().confirmBackupLevelDatUse(this);
file1 = new File(this.worldDirectory, "level.dat_old");
- return file1.exists() ? SaveFormatOld.getWorldData(file1, this.dataFixer) : null;
+ return file1.exists() ? SaveFormatOld.loadAndFix(file1, this.dataFixer, this) : null;
}
public void saveWorldInfoWithPlayer(WorldInfo worldInformation, @Nullable NBTTagCompound tagCompound)
@@ -137,6 +144,8 @@
NBTTagCompound nbttagcompound1 = new NBTTagCompound();
nbttagcompound1.setTag("Data", nbttagcompound);
+ net.minecraftforge.fml.common.FMLCommonHandler.instance().handleWorldDataSave(this, worldInformation, nbttagcompound1);
+
try
{
File file1 = new File(this.worldDirectory, "level.dat_new");
@@ -189,6 +198,7 @@
}
file1.renameTo(file2);
+ net.minecraftforge.event.ForgeEventFactory.firePlayerSavingEvent(player, this.playersDirectory, player.getUniqueID().toString());
}
catch (Exception var5)
{
@@ -217,12 +227,35 @@
if (nbttagcompound != null)
{
+ if (player instanceof EntityPlayerMP) {
+ CraftPlayer playerCB = (CraftPlayer) player.getBukkitEntity();
+ // Only update first played if it is older than the one we have
+ long modified = new File(this.playersDirectory, player.getUniqueID().toString() + ".dat").lastModified();
+ if (modified < playerCB.getFirstPlayed()) {
+ playerCB.setFirstPlayed(modified);
+ }
+ }
player.readFromNBT(this.dataFixer.process(FixTypes.PLAYER, nbttagcompound));
}
+ net.minecraftforge.event.ForgeEventFactory.firePlayerLoadingEvent(player, playersDirectory, player.getUniqueID().toString());
return nbttagcompound;
}
+ public NBTTagCompound getPlayerData(String s) {
+ try {
+ File file1 = new File(this.playersDirectory, s + ".dat");
+
+ if (file1.exists()) {
+ return CompressedStreamTools.readCompressed((InputStream) (new FileInputStream(file1)));
+ }
+ } catch (Exception exception) {
+ LOGGER.warn("Failed to load player data for " + s);
+ }
+
+ return null;
+ }
+
public IPlayerFileData getPlayerNBTManager()
{
return this;
@@ -261,4 +294,66 @@
{
return this.structureTemplateManager;
}
+
+ public NBTTagCompound getPlayerNBT(net.minecraft.entity.player.EntityPlayerMP player)
+ {
+ try
+ {
+ File file1 = new File(this.playersDirectory, player.getUniqueID().toString() + ".dat");
+
+ if (file1.exists() && file1.isFile())
+ {
+ return CompressedStreamTools.readCompressed(new FileInputStream(file1));
+ }
+ }
+ catch (Exception exception)
+ {
+ LOGGER.warn("Failed to load player data for " + player.getName());
+ }
+ return null;
+ }
+
+ public UUID getUUID() {
+ if (uuid != null) return uuid;
+ File file1 = new File(this.worldDirectory, "uid.dat");
+ if (file1.exists()) {
+ DataInputStream dis = null;
+ try {
+ dis = new DataInputStream(new FileInputStream(file1));
+ return uuid = new UUID(dis.readLong(), dis.readLong());
+ } catch (IOException ex) {
+ LOGGER.warn("Failed to read " + file1 + ", generating new random UUID", ex);
+ } finally {
+ if (dis != null) {
+ try {
+ dis.close();
+ } catch (IOException ex) {
+ // NOOP
+ }
+ }
+ }
+ }
+ uuid = UUID.randomUUID();
+ DataOutputStream dos = null;
+ try {
+ dos = new DataOutputStream(new FileOutputStream(file1));
+ dos.writeLong(uuid.getMostSignificantBits());
+ dos.writeLong(uuid.getLeastSignificantBits());
+ } catch (IOException ex) {
+ LOGGER.warn("Failed to write " + file1, ex);
+ } finally {
+ if (dos != null) {
+ try {
+ dos.close();
+ } catch (IOException ex) {
+ // NOOP
+ }
+ }
+ }
+ return uuid;
+ }
+
+ public File getPlayerDir() {
+ return playersDirectory;
+ }
}