Created
April 16, 2014 00:06
-
-
Save maxpowa/d8943252154820a91ba7 to your computer and use it in GitHub Desktop.
An NBT save/load class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import net.minecraft.client.Minecraft; | |
import net.minecraft.crash.CrashReport; | |
import net.minecraft.nbt.CompressedStreamTools; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.util.ReportedException; | |
public class SaveController { | |
// Semi-hardcoded config folder location | |
protected static final String dirName = Minecraft.getMinecraft().mcDataDir + File.separator + "config" + File.separator + "War"; | |
// File object of aforementioned hardcoded config folder location | |
protected static File dir = new File(dirName); | |
// Returns the NBTTagCompound content of the specified file, in the specified directory | |
public static NBTTagCompound loadConfig(String name, String dirName) { | |
System.out.print("[BLA] Loading \"" + name + "\""); | |
if (dirName != null) { | |
Minecraft.getMinecraft(); | |
dir = new File(Minecraft.getMinecraft().mcDataDir + File.separator + dirName); | |
} | |
String fileName = name + ".dat"; | |
File file = new File(dir, fileName); | |
if (!file.exists()) { | |
System.out.println(" canceled, file does not exist."); | |
return null; | |
} else { | |
System.out.println(" successful."); | |
} | |
try { | |
return CompressedStreamTools.readCompressed(new FileInputStream(file)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
// Saves a given NBTTagCompound to the specified file, in the specified directory | |
public static void saveConfig(String name, String dirName, NBTTagCompound nbt) { | |
System.out.println("[BLA] Saving..."); | |
if (dirName != null) { | |
HUDRegistry.getMinecraftInstance(); | |
dir = new File(Minecraft.getMinecraft().mcDataDir + File.separator | |
+ dirName); | |
} | |
if (!dir.exists() && !dir.mkdirs()) | |
// You could replace these crashes by simply logging to console but meh. | |
throw new ReportedException(new CrashReport("Unable to create the configuration directories", new Throwable())); | |
String fileName = name + ".dat"; | |
File file = new File(dir, fileName); | |
try { | |
CompressedStreamTools.writeCompressed(nbt, fileOutputStream); | |
fileOutputStream.close(); | |
} catch (IOException e) { | |
// You could replace these crashes by simply logging to console but meh. | |
throw new ReportedException(new CrashReport("An error occured while saving", new Throwable())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment