Advertisement
manusoftar

FileIO

Aug 1st, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. package com.manusoftar.minefinder.io;
  2.  
  3. import java.awt.Point;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11.  
  12. import net.minecraft.nbt.CompressedStreamTools;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.util.BlockPos;
  16. import net.minecraftforge.common.DimensionManager;
  17. import net.minecraftforge.common.util.Constants.NBT;
  18.  
  19. public final class FileIO {
  20.      
  21.       private static NBTTagCompound ublocks;
  22.       private static NBTTagCompound mines;
  23.        
  24.       private static File getDataFile() {
  25.             return new File(DimensionManager.getCurrentSaveRootDirectory(), "minefinder/mines.dat");
  26.       }
  27.      
  28.       public static LinkedList<BlockPos> readUnbrekableBlocks() {
  29.               LinkedList<BlockPos> blocks = new LinkedList<BlockPos>();
  30.               try {
  31.                   InputStream is = new FileInputStream(getDataFile());
  32.                  
  33.                   NBTTagCompound nbc  = CompressedStreamTools.readCompressed(is);
  34.              
  35.                   if (nbc.hasKey("UnbreakableBlocks")){
  36.                       NBTTagList list = nbc.getTagList("UnbreakableBlocks", NBT.TAG_COMPOUND);
  37.                       for ( int i = 0; i < list.tagCount(); i++){
  38.                             NBTTagCompound ntc = list.getCompoundTagAt(i);
  39.                             int x,y,z;
  40.                             x = ntc.getInteger("X");
  41.                             y = ntc.getInteger("Y");
  42.                             z = ntc.getInteger("Z");
  43.                            
  44.                             BlockPos bp = new BlockPos(x,y,z);
  45.                            
  46.                             blocks.push(bp);
  47.                       }
  48.                       return blocks;
  49.                   }
  50.                  
  51.               } catch (Exception ex){
  52.                   ex.printStackTrace();
  53.               }
  54.              
  55.               return null;
  56.       }
  57.      
  58.      
  59.       public static LinkedList<Point> readMinesFound(){
  60.               LinkedList<Point> blocks = new LinkedList<Point>();
  61.               try {
  62.                   InputStream is = new FileInputStream(getDataFile());
  63.                  
  64.                   NBTTagCompound nbc  = CompressedStreamTools.readCompressed(is);
  65.              
  66.                   if (nbc.hasKey("UnbreakableBlocks")){
  67.                       NBTTagList list = nbc.getTagList("UnbreakableBlocks", NBT.TAG_COMPOUND);
  68.                       for ( int i = 0; i < list.tagCount(); i++){
  69.                             NBTTagCompound ntc = list.getCompoundTagAt(i);
  70.                             int x,y,z;
  71.                             x = ntc.getInteger("X");
  72.                             z = ntc.getInteger("Z");
  73.                            
  74.                             Point pt = new Point(x,z);
  75.                            
  76.                             blocks.push(pt);
  77.                       }
  78.                       return blocks;
  79.                   }
  80.                  
  81.               } catch (Exception ex){
  82.                   ex.printStackTrace();
  83.               }
  84.              
  85.               return null;
  86.       }
  87.      
  88.      
  89.       public static void prepareUnbreakableBlocks(LinkedList<BlockPos> blocks){
  90.              try {
  91.                  Iterator<BlockPos> it = blocks.iterator();
  92.                  
  93.                  InputStream is = new FileInputStream(getDataFile());
  94.                  
  95.                  NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
  96.                  NBTTagList list = new NBTTagList();
  97.                
  98.                  if (nbt.hasKey("UnbreakableBlocks")) {
  99.                      list = nbt.getTagList("UnbreakableBlocks", NBT.TAG_COMPOUND);
  100.                  }
  101.                  
  102.                  while (it.hasNext()){
  103.                         BlockPos pos = it.next();
  104.                         NBTTagCompound ntc = new NBTTagCompound();
  105.                         ntc.setInteger("X", pos.getX());
  106.                         ntc.setInteger("Y", pos.getY());
  107.                         ntc.setInteger("Z", pos.getZ());
  108.                         list.appendTag(ntc);    
  109.                  }
  110.                  
  111.                  NBTTagCompound nbc = new NBTTagCompound();
  112.                  nbc.setTag("UnbreakableBlocks", list);
  113.                  
  114.                  //OutputStream os = new FileOutputStream(getDataFile());
  115.                  //CompressedStreamTools.writeCompressed(nbc, os);
  116.                  ublocks = (NBTTagCompound) nbc.copy();
  117.                  
  118.                  
  119.              } catch (Exception ex) {
  120.                  ex.printStackTrace();
  121.              }
  122.       }
  123.      
  124.       public static void prepareMinesFound(LinkedList<Point> blocks){
  125.              try {
  126.                  Iterator<Point> it = blocks.iterator();
  127.                  
  128.                  InputStream is = new FileInputStream(getDataFile());
  129.                  
  130.                  NBTTagCompound nbt = CompressedStreamTools.readCompressed(is);
  131.                  NBTTagList list = new NBTTagList();
  132.                
  133.                  if (nbt.hasKey("MinesFound")){
  134.                      list = nbt.getTagList("MinesFound", NBT.TAG_COMPOUND);
  135.                  }
  136.                  
  137.                  while (it.hasNext()){
  138.                         Point pos = it.next();
  139.                         NBTTagCompound ntc = new NBTTagCompound();
  140.                         ntc.setInteger("X", pos.x);
  141.                         ntc.setInteger("Y", pos.y);
  142.                         list.appendTag(ntc);    
  143.                  }
  144.                  
  145.                  NBTTagCompound nbc = new NBTTagCompound();
  146.                  nbc.setTag("MinesFound", list);
  147.                  
  148.                  /*OutputStream os = new FileOutputStream(getDataFile());
  149.                  CompressedStreamTools.writeCompressed(nbc, os);*/
  150.                  
  151.                  mines = (NBTTagCompound) nbc.copy();
  152.                  
  153.                  
  154.              } catch (Exception ex) {
  155.                  ex.printStackTrace();
  156.              }
  157.       }
  158.      
  159.      
  160.       public static void saveChanges(){
  161.              try {
  162.                  OutputStream os = new FileOutputStream(getDataFile());
  163.                  NBTTagCompound nbc = new NBTTagCompound();
  164.                  
  165.                  nbc.setTag("", mines);
  166.                  nbc.setTag("",ublocks);
  167.                  
  168.                  
  169.              } catch (Exception ex) {
  170.                  
  171.              }
  172.       }
  173.      
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement