From c03763f0bee180d0b3d45ad97fd43797c9731f62 Mon Sep 17 00:00:00 2001 From: PeytonPlayz595 <106421860+PeytonPlayz595@users.noreply.github.com> Date: Thu, 8 Feb 2024 21:31:59 -0500 Subject: [PATCH] Save/load files --- .../java/net/minecraft/client/Minecraft.java | 18 +-- .../java/net/minecraft/src/ChunkLoader.java | 62 +++----- .../java/net/minecraft/src/GameSettings.java | 25 +-- .../net/minecraft/src/GameWindowListener.java | 27 ---- .../net/minecraft/src/GuiDeleteWorld.java | 4 +- .../net/minecraft/src/GuiSelectWorld.java | 8 +- src/main/java/net/minecraft/src/World.java | 146 +++++++++--------- .../java/net/minecraft/src/WorldProvider.java | 4 +- .../net/minecraft/src/WorldProviderHell.java | 7 +- 9 files changed, 120 insertions(+), 181 deletions(-) delete mode 100644 src/main/java/net/minecraft/src/GameWindowListener.java diff --git a/src/main/java/net/minecraft/client/Minecraft.java b/src/main/java/net/minecraft/client/Minecraft.java index 3410599..cf1b50e 100644 --- a/src/main/java/net/minecraft/client/Minecraft.java +++ b/src/main/java/net/minecraft/client/Minecraft.java @@ -1,6 +1,5 @@ package net.minecraft.client; -import java.io.File; import net.minecraft.src.AxisAlignedBB; import net.minecraft.src.Block; import net.minecraft.src.EffectRenderer; @@ -10,8 +9,6 @@ import net.minecraft.src.EntityRenderer; import net.minecraft.src.FontRenderer; import net.minecraft.src.GLAllocation; import net.minecraft.src.GameSettings; -import net.minecraft.src.GameWindowListener; -import net.minecraft.src.GuiChat; import net.minecraft.src.GuiConflictWarning; import net.minecraft.src.GuiGameOver; import net.minecraft.src.GuiIngame; @@ -90,7 +87,6 @@ public class Minecraft implements Runnable { public MovingObjectPosition objectMouseOver = null; public GameSettings gameSettings; public MouseHelper mouseHelper; - public File field_6297_D; public static long[] field_9240_E = new long[512]; public static long[] field_9239_F = new long[512]; public static int field_9238_G = 0; @@ -98,7 +94,6 @@ public class Minecraft implements Runnable { private int field_9233_W; private TextureWaterFX field_9232_X = new TextureWaterFX(); private TextureLavaFX field_9231_Y = new TextureLavaFX(); - private static File minecraftDir = null; public volatile boolean running = true; public String field_6292_I = ""; boolean field_6291_J = false; @@ -125,8 +120,7 @@ public class Minecraft implements Runnable { public void startGame() { RenderManager.instance.field_4236_f = new ItemRenderer(this); - this.field_6297_D = getMinecraftDir(); - this.gameSettings = new GameSettings(this, this.field_6297_D); + this.gameSettings = new GameSettings(this); this.renderEngine = new RenderEngine(this.gameSettings); this.fontRenderer = new FontRenderer(this.gameSettings, "/font/default.png", this.renderEngine); this.loadScreen(); @@ -211,14 +205,6 @@ public class Minecraft implements Runnable { var9.draw(); } - public static File getMinecraftDir() { - if(minecraftDir == null) { - minecraftDir = new File("minecraft"); - } - - return minecraftDir; - } - public void displayGuiScreen(GuiScreen var1) { if(!(this.currentScreen instanceof GuiUnused)) { if(this.currentScreen != null) { @@ -775,7 +761,7 @@ public class Minecraft implements Runnable { public void func_6247_b(String var1) { this.func_6261_a((World)null); System.gc(); - World var2 = new World(new File(getMinecraftDir(), "saves"), var1); + World var2 = new World("saves", var1); if(var2.field_1033_r) { this.func_6263_a(var2, "Generating level"); } else { diff --git a/src/main/java/net/minecraft/src/ChunkLoader.java b/src/main/java/net/minecraft/src/ChunkLoader.java index f5bb61d..eb8adc0 100644 --- a/src/main/java/net/minecraft/src/ChunkLoader.java +++ b/src/main/java/net/minecraft/src/ChunkLoader.java @@ -1,51 +1,35 @@ package net.minecraft.src; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Iterator; +import org.lwjgl.opengl.GL11; + public class ChunkLoader implements IChunkLoader { - private File saveDir; + private String saveDir; private boolean createIfNecessary; - public ChunkLoader(File var1, boolean var2) { + public ChunkLoader(String var1, boolean var2) { this.saveDir = var1; this.createIfNecessary = var2; } - private File chunkFileForXZ(int var1, int var2) { + private String chunkFileForXZ(int var1, int var2) { String var3 = "c." + Integer.toString(var1, 36) + "." + Integer.toString(var2, 36) + ".dat"; String var4 = Integer.toString(var1 & 63, 36); String var5 = Integer.toString(var2 & 63, 36); - File var6 = new File(this.saveDir, var4); - if(!var6.exists()) { - if(!this.createIfNecessary) { - return null; - } - - var6.mkdir(); - } - - var6 = new File(var6, var5); - if(!var6.exists()) { - if(!this.createIfNecessary) { - return null; - } - - var6.mkdir(); - } - - var6 = new File(var6, var3); - return !var6.exists() && !this.createIfNecessary ? null : var6; + String var6 = this.saveDir + "/" + var4 + "/" + var3; + return var6; } public Chunk loadChunk(World var1, int var2, int var3) throws IOException { - File var4 = this.chunkFileForXZ(var2, var3); - if(var4 != null && var4.exists()) { + String var4 = this.chunkFileForXZ(var2, var3); + if(GL11.readFile(var4) != null) { try { - FileInputStream var5 = new FileInputStream(var4); + byte[] data = GL11.readFile(var4); + ByteArrayInputStream var5 = new ByteArrayInputStream(data); NBTTagCompound var6 = CompressedStreamTools.func_1138_a(var5); if(!var6.hasKey("Level")) { System.out.println("Chunk file at " + var2 + "," + var3 + " is missing level data, skipping"); @@ -76,26 +60,28 @@ public class ChunkLoader implements IChunkLoader { public void saveChunk(World var1, Chunk var2) throws IOException { var1.func_663_l(); - File var3 = this.chunkFileForXZ(var2.xPosition, var2.zPosition); - if(var3.exists()) { - var1.sizeOnDisk -= var3.length(); + String var3 = this.chunkFileForXZ(var2.xPosition, var2.zPosition); + if(GL11.readFile(var3) != null) { + var1.sizeOnDisk -= GL11.getFileSize(var3); } try { - File var4 = new File(this.saveDir, "tmp_chunk.dat"); - FileOutputStream var5 = new FileOutputStream(var4); + String var4 = this.saveDir + "/tmp_chunk.dat"; + ByteArrayOutputStream var5 = new ByteArrayOutputStream(); NBTTagCompound var6 = new NBTTagCompound(); NBTTagCompound var7 = new NBTTagCompound(); var6.setTag("Level", var7); this.storeChunkInCompound(var2, var1, var7); CompressedStreamTools.writeGzippedCompoundToOutputStream(var6, var5); + var5.flush(); + GL11.writeFile(var4, var5.toByteArray()); var5.close(); - if(var3.exists()) { - var3.delete(); + if(GL11.readFile(var3) != null) { + GL11.deleteFile(var3); } - var4.renameTo(var3); - var1.sizeOnDisk += var3.length(); + GL11.renameFile(var4, var3); + var1.sizeOnDisk += GL11.getFileSize(var3); } catch (Exception var8) { var8.printStackTrace(); } diff --git a/src/main/java/net/minecraft/src/GameSettings.java b/src/main/java/net/minecraft/src/GameSettings.java index edd10d3..6ef6e5b 100644 --- a/src/main/java/net/minecraft/src/GameSettings.java +++ b/src/main/java/net/minecraft/src/GameSettings.java @@ -1,12 +1,13 @@ package net.minecraft.src; import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStreamReader; import java.io.PrintWriter; import net.minecraft.client.Minecraft; import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.GL11; public class GameSettings { private static final String[] RENDER_DISTANCES = new String[]{"FAR", "NORMAL", "SHORT", "TINY"}; @@ -33,15 +34,13 @@ public class GameSettings { public KeyBinding keyBindSneak = new KeyBinding("Sneak", 42); public KeyBinding[] keyBindings = new KeyBinding[]{this.keyBindForward, this.keyBindLeft, this.keyBindBack, this.keyBindRight, this.keyBindJump, this.keyBindSneak, this.keyBindDrop, this.keyBindInventory, this.keyBindChat, this.keyBindToggleFog}; protected Minecraft mc; - private File optionsFile; public int numberOfOptions = 10; public int difficulty = 2; public boolean thirdPersonView = false; public String field_12259_z = ""; - public GameSettings(Minecraft var1, File var2) { + public GameSettings(Minecraft var1) { this.mc = var1; - this.optionsFile = new File(var2, "options.txt"); this.loadOptions(); } @@ -119,11 +118,14 @@ public class GameSettings { public void loadOptions() { try { - if(!this.optionsFile.exists()) { + byte[] fileData = GL11.readFile("options.txt"); + if(fileData == null) { return; } - BufferedReader var1 = new BufferedReader(new FileReader(this.optionsFile)); + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData); + InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream, "UTF-8"); + BufferedReader var1 = new BufferedReader(inputStreamReader); String var2 = ""; while(true) { @@ -201,7 +203,8 @@ public class GameSettings { public void saveOptions() { try { - PrintWriter var1 = new PrintWriter(new FileWriter(this.optionsFile)); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + PrintWriter var1 = new PrintWriter(byteArrayOutputStream); var1.println("music:" + this.musicVolume); var1.println("sound:" + this.soundVolume); var1.println("invertYMouse:" + this.invertMouse); @@ -218,6 +221,10 @@ public class GameSettings { for(int var2 = 0; var2 < this.keyBindings.length; ++var2) { var1.println("key_" + this.keyBindings[var2].keyDescription + ":" + this.keyBindings[var2].keyCode); } + + var1.flush(); + byte[] fileData = byteArrayOutputStream.toByteArray(); + GL11.writeFile("options.txt", fileData); var1.close(); } catch (Exception var3) { diff --git a/src/main/java/net/minecraft/src/GameWindowListener.java b/src/main/java/net/minecraft/src/GameWindowListener.java deleted file mode 100644 index 5ef7d0f..0000000 --- a/src/main/java/net/minecraft/src/GameWindowListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.minecraft.src; - -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import net.minecraft.client.Minecraft; - -public final class GameWindowListener extends WindowAdapter { - final Minecraft mc; - final Thread mcThread; - - public GameWindowListener(Minecraft var1, Thread var2) { - this.mc = var1; - this.mcThread = var2; - } - - public void windowClosing(WindowEvent var1) { - this.mc.shutdown(); - - try { - this.mcThread.join(); - } catch (InterruptedException var3) { - var3.printStackTrace(); - } - - System.exit(0); - } -} diff --git a/src/main/java/net/minecraft/src/GuiDeleteWorld.java b/src/main/java/net/minecraft/src/GuiDeleteWorld.java index 5da0735..9f1e4e0 100644 --- a/src/main/java/net/minecraft/src/GuiDeleteWorld.java +++ b/src/main/java/net/minecraft/src/GuiDeleteWorld.java @@ -1,6 +1,5 @@ package net.minecraft.src; -import java.io.File; import net.minecraft.client.Minecraft; public class GuiDeleteWorld extends GuiSelectWorld { @@ -23,8 +22,7 @@ public class GuiDeleteWorld extends GuiSelectWorld { public void deleteWorld(boolean var1, int var2) { if(var1) { - File var3 = Minecraft.getMinecraftDir(); - World.deleteWorld(var3, this.getWorldName(var2)); + World.deleteWorld(this.getWorldName(var2)); } this.mc.displayGuiScreen(this.parentScreen); diff --git a/src/main/java/net/minecraft/src/GuiSelectWorld.java b/src/main/java/net/minecraft/src/GuiSelectWorld.java index 0a6eb68..10af130 100644 --- a/src/main/java/net/minecraft/src/GuiSelectWorld.java +++ b/src/main/java/net/minecraft/src/GuiSelectWorld.java @@ -1,6 +1,5 @@ package net.minecraft.src; -import java.io.File; import net.minecraft.client.Minecraft; public class GuiSelectWorld extends GuiScreen { @@ -13,10 +12,8 @@ public class GuiSelectWorld extends GuiScreen { } public void initGui() { - File var1 = Minecraft.getMinecraftDir(); - for(int var2 = 0; var2 < 5; ++var2) { - NBTTagCompound var3 = World.func_629_a(var1, "World" + (var2 + 1)); + NBTTagCompound var3 = World.func_629_a("World" + (var2 + 1)); if(var3 == null) { this.controlList.add(new GuiButton(var2, this.width / 2 - 100, this.height / 6 + 24 * var2, "- empty -")); } else { @@ -31,8 +28,7 @@ public class GuiSelectWorld extends GuiScreen { } protected String getWorldName(int var1) { - File var2 = Minecraft.getMinecraftDir(); - return World.func_629_a(var2, "World" + var1) != null ? "World" + var1 : null; + return World.func_629_a("World" + var1) != null ? "World" + var1 : null; } public void initGui2() { diff --git a/src/main/java/net/minecraft/src/World.java b/src/main/java/net/minecraft/src/World.java index bb42061..1be0cad 100644 --- a/src/main/java/net/minecraft/src/World.java +++ b/src/main/java/net/minecraft/src/World.java @@ -1,12 +1,15 @@ package net.minecraft.src; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +//import java.io.File; +//import java.io.FileInputStream; +//import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -14,6 +17,8 @@ import java.util.Random; import java.util.Set; import java.util.TreeSet; +import org.lwjgl.opengl.GL11; + public class World implements IBlockAccess { public boolean field_4214_a; private List field_1051_z; @@ -40,8 +45,8 @@ public class World implements IBlockAccess { public final WorldProvider worldProvider; protected List worldAccesses; private IChunkProvider chunkProvider; - public File field_9433_s; - public File field_9432_t; + public String field_9433_s; + public String field_9432_t; public long randomSeed; private NBTTagCompound nbtCompoundPlayer; public long sizeOnDisk; @@ -54,52 +59,42 @@ public class World implements IBlockAccess { private int field_9426_L; private List field_1012_M; - public static NBTTagCompound func_629_a(File var0, String var1) { - File var2 = new File(var0, "saves"); - File var3 = new File(var2, var1); - if(!var3.exists()) { - return null; - } else { - File var4 = new File(var3, "level.dat"); - if(var4.exists()) { - try { - NBTTagCompound var5 = CompressedStreamTools.func_1138_a(new FileInputStream(var4)); - NBTTagCompound var6 = var5.getCompoundTag("Data"); - return var6; - } catch (Exception var7) { - var7.printStackTrace(); - } - } + public static NBTTagCompound func_629_a(String var1) { + byte[] data = GL11.readFile("saves/" + var1 + "/level.dat"); + if(!(data == null)) { + try { + NBTTagCompound var5 = CompressedStreamTools.func_1138_a(new ByteArrayInputStream(data)); + NBTTagCompound var6 = var5.getCompoundTag("Data"); + return var6; + } catch (Exception var7) { + var7.printStackTrace(); + } + } + return null; + } - return null; - } - } - - public static void deleteWorld(File var0, String var1) { - File var2 = new File(var0, "saves"); - File var3 = new File(var2, var1); - if(var3.exists()) { - deleteFiles(var3.listFiles()); - var3.delete(); - } - } - - private static void deleteFiles(File[] var0) { - for(int var1 = 0; var1 < var0.length; ++var1) { - if(var0[var1].isDirectory()) { - deleteFiles(var0[var1].listFiles()); - } - - var0[var1].delete(); - } - - } + public static void deleteWorld(String var1) { + String path = "saves/" + var1 + "/"; + Collection files = GL11.listFiles(path, true, false); + + for(GL11.FileEntry entry : files) { + byte[] data = GL11.readFile(entry.path); + if(data != null) { + GL11.deleteFile(entry.path); + } + } + + byte[] data = GL11.readFile("saves/" + var1 + "/"); + if(data != null) { + GL11.deleteFile("saves/" + var1 + "/"); + } + } public WorldChunkManager func_4075_a() { return this.worldProvider.worldChunkMgr; } - public World(File var1, String var2) { + public World(String var1, String var2) { this(var1, var2, (new Random()).nextLong()); } @@ -181,11 +176,11 @@ public class World implements IBlockAccess { this.calculateInitialSkylight(); } - public World(File var1, String var2, long var3) { + public World(String var1, String var2, long var3) { this(var1, var2, var3, (WorldProvider)null); } - public World(File var1, String var2, long var3, WorldProvider var5) { + public World(String var1, String var2, long var3, WorldProvider var5) { this.field_4214_a = false; this.field_1051_z = new ArrayList(); this.loadedEntityList = new ArrayList(); @@ -214,17 +209,17 @@ public class World implements IBlockAccess { this.field_1012_M = new ArrayList(); this.field_9433_s = var1; this.field_9431_w = var2; - var1.mkdirs(); - this.field_9432_t = new File(var1, var2); - this.field_9432_t.mkdirs(); + this.field_9432_t = var1 + "/" + var2; try { - File var6 = new File(this.field_9432_t, "session.lock"); - DataOutputStream var7 = new DataOutputStream(new FileOutputStream(var6)); + ByteArrayOutputStream data = new ByteArrayOutputStream(); + DataOutputStream var7 = new DataOutputStream(data); try { var7.writeLong(this.field_1054_E); } finally { + var7.flush(); + GL11.writeFile(this.field_9432_t + "/session.lock", data.toByteArray()); var7.close(); } } catch (IOException var16) { @@ -233,11 +228,12 @@ public class World implements IBlockAccess { } Object var17 = new WorldProvider(); - File var18 = new File(this.field_9432_t, "level.dat"); - this.field_1033_r = !var18.exists(); - if(var18.exists()) { + String var18 = this.field_9432_t + "/level.dat"; + this.field_1033_r = GL11.readFile(var18) == null; + byte[] data = GL11.readFile(var18); + if(data != null) { try { - NBTTagCompound var8 = CompressedStreamTools.func_1138_a(new FileInputStream(var18)); + NBTTagCompound var8 = CompressedStreamTools.func_1138_a(new ByteArrayInputStream(data)); NBTTagCompound var9 = var8.getCompoundTag("Data"); this.randomSeed = var9.getLong("RandomSeed"); this.spawnX = var9.getInteger("SpawnX"); @@ -285,7 +281,7 @@ public class World implements IBlockAccess { this.calculateInitialSkylight(); } - protected IChunkProvider func_4081_a(File var1) { + protected IChunkProvider func_4081_a(String var1) { return new ChunkProviderLoadOrGenerate(this, this.worldProvider.getChunkLoader(var1), this.worldProvider.getChunkProvider()); } @@ -367,22 +363,25 @@ public class World implements IBlockAccess { var3.setTag("Data", var1); try { - File var4 = new File(this.field_9432_t, "level.dat_new"); - File var5 = new File(this.field_9432_t, "level.dat_old"); - File var6 = new File(this.field_9432_t, "level.dat"); - CompressedStreamTools.writeGzippedCompoundToOutputStream(var3, new FileOutputStream(var4)); - if(var5.exists()) { - var5.delete(); + String var4 = field_9432_t + "/level.dat_new"; + String var5 = field_9432_t + "/level.dat_old"; + String var6 = field_9432_t + "/level.dat"; + ByteArrayOutputStream data = new ByteArrayOutputStream(); + CompressedStreamTools.writeGzippedCompoundToOutputStream(var3, data); + GL11.writeFile(var4, data.toByteArray()); + + if(GL11.readFile(var5) != null) { + GL11.deleteFile(var5); } - var6.renameTo(var5); - if(var6.exists()) { - var6.delete(); + GL11.renameFile(var6, var5); + if(GL11.readFile(var6) != null) { + GL11.deleteFile(var6); } - var4.renameTo(var6); - if(var4.exists()) { - var4.delete(); + GL11.renameFile(var4, var6); + if(GL11.readFile(var4) != null) { + GL11.deleteFile(var4); } } catch (Exception var7) { var7.printStackTrace(); @@ -1530,10 +1529,10 @@ public class World implements IBlockAccess { } else { ++this.field_4204_J; - boolean var2; try { int var1 = 5000; + boolean var2; while(this.field_1051_z.size() > 0) { --var1; if(var1 <= 0) { @@ -1545,11 +1544,10 @@ public class World implements IBlockAccess { } var2 = false; + return var2; } finally { --this.field_4204_J; } - - return var2; } } @@ -1954,8 +1952,8 @@ public class World implements IBlockAccess { public void func_663_l() { try { - File var1 = new File(this.field_9432_t, "session.lock"); - DataInputStream var2 = new DataInputStream(new FileInputStream(var1)); + String var1 = this.field_9432_t + "/session.lock"; + DataInputStream var2 = new DataInputStream(new ByteArrayInputStream(GL11.readFile(var1))); try { if(var2.readLong() != this.field_1054_E) { diff --git a/src/main/java/net/minecraft/src/WorldProvider.java b/src/main/java/net/minecraft/src/WorldProvider.java index bd168ff..187acb4 100644 --- a/src/main/java/net/minecraft/src/WorldProvider.java +++ b/src/main/java/net/minecraft/src/WorldProvider.java @@ -1,7 +1,5 @@ package net.minecraft.src; -import java.io.File; - public class WorldProvider { public World worldObj; public WorldChunkManager worldChunkMgr; @@ -36,7 +34,7 @@ public class WorldProvider { return new ChunkProviderGenerate(this.worldObj, this.worldObj.randomSeed); } - public IChunkLoader getChunkLoader(File var1) { + public IChunkLoader getChunkLoader(String var1) { return new ChunkLoader(var1, true); } diff --git a/src/main/java/net/minecraft/src/WorldProviderHell.java b/src/main/java/net/minecraft/src/WorldProviderHell.java index a2fcfa3..f07336e 100644 --- a/src/main/java/net/minecraft/src/WorldProviderHell.java +++ b/src/main/java/net/minecraft/src/WorldProviderHell.java @@ -1,7 +1,5 @@ package net.minecraft.src; -import java.io.File; - public class WorldProviderHell extends WorldProvider { public void registerWorldChunkManager() { this.worldChunkMgr = new WorldChunkManagerHell(MobSpawnerBase.hell, 1.0D, 0.0D); @@ -29,9 +27,8 @@ public class WorldProviderHell extends WorldProvider { return new ChunkProviderHell(this.worldObj, this.worldObj.randomSeed); } - public IChunkLoader getChunkLoader(File var1) { - File var2 = new File(var1, "DIM-1"); - var2.mkdirs(); + public IChunkLoader getChunkLoader(String var1) { + String var2 = var1 + "/DIM-1/"; return new ChunkLoader(var2, true); }