Advertisement
Corosus

Untitled

Sep 4th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. package CoroAI.util;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6.  
  7. import net.minecraft.nbt.CompressedStreamTools;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.server.MinecraftServer;
  10. import net.minecraft.util.ChunkCoordinates;
  11. import net.minecraft.util.EnumOS;
  12. import net.minecraft.world.World;
  13. import net.minecraft.world.WorldServer;
  14. import net.minecraftforge.common.DimensionManager;
  15. import cpw.mods.fml.client.FMLClientHandler;
  16. import cpw.mods.fml.relauncher.Side;
  17. import cpw.mods.fml.relauncher.SideOnly;
  18.  
  19. public class CoroUtilFile {
  20.     public static String lastWorldFolder = "";
  21.    
  22.     public static NBTTagCompound getExtraWorldNBT(String fileName) {
  23.         NBTTagCompound data = new NBTTagCompound();
  24.         //try load
  25.        
  26.         String saveFolder = getWorldSaveFolderPath() + getWorldFolderName();
  27.        
  28.         if ((new File(saveFolder + fileName)).exists()) {
  29.             try {
  30.                 data = CompressedStreamTools.readCompressed(new FileInputStream(saveFolder + fileName));
  31.             } catch (Exception ex) {
  32.                 System.out.println("CoroUtilFile: getExtraWorldNBT: Error loading " + saveFolder + fileName);
  33.             }
  34.            
  35.             //NBTTagList var14 = gameData.getTagList("playerData");
  36.         }
  37.        
  38.         return data;
  39.     }
  40.    
  41.     public static void setExtraWorldNBT(String fileName, NBTTagCompound data) {
  42.         try {
  43.            
  44.             String saveFolder = getWorldSaveFolderPath() + getWorldFolderName();
  45.            
  46.             //Write out to file
  47.             FileOutputStream fos = new FileOutputStream(saveFolder + fileName);
  48.             CompressedStreamTools.writeCompressed(data, fos);
  49.             fos.close();
  50.            
  51.         } catch (Exception ex) {
  52.             ex.printStackTrace();
  53.         }
  54.     }
  55.    
  56.     //this must be used while server is active
  57.     public static String getWorldFolderName() {
  58.         World world = DimensionManager.getWorld(0);
  59.        
  60.         if (world != null) {
  61.             lastWorldFolder = ((WorldServer)world).getChunkSaveLocation().getName();
  62.             return lastWorldFolder + File.separator;
  63.         }
  64.        
  65.         return lastWorldFolder + File.separator;
  66.     }
  67.    
  68.     public static String getSaveFolderPath() {
  69.         if (MinecraftServer.getServer() == null || MinecraftServer.getServer().isSinglePlayer()) {
  70.             return getClientSidePath() + File.separator;
  71.         } else {
  72.             return new File(".").getAbsolutePath() + File.separator;
  73.         }
  74.        
  75.     }
  76.    
  77.     public static String getWorldSaveFolderPath() {
  78.         if (MinecraftServer.getServer() == null || MinecraftServer.getServer().isSinglePlayer()) {
  79.             return getClientSidePath() + File.separator + "saves" + File.separator;
  80.         } else {
  81.             return new File(".").getAbsolutePath() + File.separator;
  82.         }
  83.        
  84.     }
  85.    
  86.     @SideOnly(Side.CLIENT)
  87.     public static String getClientSidePath() {
  88.         return FMLClientHandler.instance().getClient().mcDataDir/*getAppDir("minecraft")*/.getPath();
  89.     }
  90.    
  91.     public static void writeCoords(String name, ChunkCoordinates coords, NBTTagCompound nbt) {
  92.         nbt.setInteger(name + "X", coords.posX);
  93.         nbt.setInteger(name + "Y", coords.posY);
  94.         nbt.setInteger(name + "Z", coords.posZ);
  95.     }
  96.    
  97.     public static ChunkCoordinates readCoords(String name, NBTTagCompound nbt) {
  98.         if (nbt.hasKey(name + "X")) {
  99.             return new ChunkCoordinates(nbt.getInteger(name + "X"), nbt.getInteger(name + "Y"), nbt.getInteger(name + "Z"));
  100.         } else {
  101.             return null;
  102.         }
  103.     }
  104.    
  105.     //this is here just in case, new best way is now Minecraft.mcDataDir
  106.     public static File getAppDir(String par0Str)
  107.     {
  108.         String s1 = System.getProperty("user.home", ".");
  109.         File file1;
  110.  
  111.         switch (EnumOSHelper.field_90049_a[getOs().ordinal()])
  112.         {
  113.             case 1:
  114.             case 2:
  115.                 file1 = new File(s1, '.' + par0Str + '/');
  116.                 break;
  117.             case 3:
  118.                 String s2 = System.getenv("APPDATA");
  119.  
  120.                 if (s2 != null)
  121.                 {
  122.                     file1 = new File(s2, "." + par0Str + '/');
  123.                 }
  124.                 else
  125.                 {
  126.                     file1 = new File(s1, '.' + par0Str + '/');
  127.                 }
  128.  
  129.                 break;
  130.             case 4:
  131.                 file1 = new File(s1, "Library/Application Support/" + par0Str);
  132.                 break;
  133.             default:
  134.                 file1 = new File(s1, par0Str + '/');
  135.         }
  136.  
  137.         if (!file1.exists() && !file1.mkdirs())
  138.         {
  139.             throw new RuntimeException("The working directory could not be created: " + file1);
  140.         }
  141.         else
  142.         {
  143.             return file1;
  144.         }
  145.     }
  146.  
  147.     public static EnumOS getOs()
  148.     {
  149.         String s = System.getProperty("os.name").toLowerCase();
  150.         return s.contains("win") ? EnumOS.WINDOWS : (s.contains("mac") ? EnumOS.MACOS : (s.contains("solaris") ? EnumOS.SOLARIS : (s.contains("sunos") ? EnumOS.SOLARIS : (s.contains("linux") ? EnumOS.LINUX : (s.contains("unix") ? EnumOS.LINUX : EnumOS.UNKNOWN)))));
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement