public static void traverseTags_treatitems(Tag orig, boolean convert) {
    if (orig instanceof CompoundTag) {
      CompoundTag ctagorig = (CompoundTag) orig;
      Collection alltags = ctagorig.getAllTags();
      Iterator it = alltags.iterator();
      level++;
      while (it.hasNext()) {
        traverseTags_treatitems((Tag) it.next(), convert);
      }
      level--;
    }
    if (orig instanceof ListTag) {
      ListTag listTagorig = (ListTag) orig;
      Field list = null;
      Iterator it = null;
      try {
        list = ListTag.class.getDeclaredField("list");
        list.setAccessible(true);
        it = ((List) list.get(listTagorig)).iterator();
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (NoSuchFieldException e) {
        e.printStackTrace();
      } catch (SecurityException e) {
        e.printStackTrace();
      }
      level++;
      while (it.hasNext()) {
        // Logger.log(new String(new char[level]).replace("\0", "-")+orig.getId());
        Tag tag = (Tag) it.next();
        if (tag instanceof CompoundTag) {
          CompoundTag ctag = (CompoundTag) tag;
          if (listTagorig.getName().toLowerCase().indexOf("item") == -1) {
            traverseTags_treatitems(ctag, convert);
          } else {
            int ntype = 1;
            int id0 = (int) ctag.getShort("id");
            int id1 = (int) (Integer) ConverterMain.options.getconvertmaps()[1].get(id0)[0];
            count(ntype, id0, id1, convert);

            if (convert) {
              int typefound = (int) (Integer) ConverterMain.options.getconvertmaps()[1].get(id0)[1];
              if (typefound >= 0) {
                if (id0 != id1 && !converteds[ntype].contains(id0)) {
                  ctag.putShort("id", (short) id1);
                }
              }
            }
          }
        }
      }
      level--;
    }
    // Logger.log(new String(new char[level]).replace("\0", "-")+orig.getId());
  }
Пример #2
0
 @Override
 public Tag copy() {
   ListTag<T> res = new ListTag<T>(getName());
   res.type = type;
   for (T t : list) {
     @SuppressWarnings("unchecked")
     T copy = (T) t.copy();
     res.list.add(copy);
   }
   return res;
 }
  public void analyzeorconvert() {
    // Doing the meat of the conversion. Assumes the ConverterMain.convertmap is done
    // HashMap<List<Integer>, List<Integer>> convertmap=ConverterMain.getInstance().convertmap;
    HashMap<Integer, Integer> convertermaps[] = new HashMap[2];

    File rootfolderorig = ConverterMain.options.getsave(0).getRootfolder();
    File rootfolderdest = ConverterMain.options.getsave(1).getRootfolder();
    File cachefile = ConverterMain.options.getsave(0).getcachefile();

    if (!rootfolderorig.exists() || !rootfolderdest.isDirectory()) {
      Logger.logln("ERROR with the provided save origin or destination");
      return;
    }
    try {
      // File rootfolderdestnew=new
      // File(rootfolderdest,rootfolderorig.getName()+"_convertedto_"+ConverterMain.options.getminecraftversion(1));
      if (convert) {
        if (new File(rootfolderdest, "level.dat").exists()) {
          int response =
              JOptionPane.showConfirmDialog(
                  null,
                  "there's already a converted save. Destroy it?",
                  "Confirm",
                  JOptionPane.YES_NO_OPTION,
                  JOptionPane.QUESTION_MESSAGE);
          if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
            Logger.logln("INFO Aborting conversion");
            return;
          }
          Logger.logln("WARNING there's already a converted save there. We will destroy it");
          rootfolderdest.delete();
        }

        try {
          copyDirectory(rootfolderorig, rootfolderdest);
        } catch (IOException e) {
          Logger.logln("ERROR copying data from the old to the new level folder");
          return;
        }
      } else {
        if (cachefile.exists() && !MainFrame.redoanalyzesaveCheck.isSelected()) {
          try {
            FileInputStream fis = new FileInputStream(cachefile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            for (int ntype = 0; ntype < ConverterMain.Ntype; ntype++) {
              ConverterMain.options.getsave(0).listofidsinsave[ntype] =
                  (List<Object>) ois.readObject();
              ConverterMain.options.getsave(0).nidsinsave[ntype] =
                  (TreeMap<Object, Integer>) ois.readObject();
            }
            ois.close();
            ConverterMain.isdone.put("analyzesave", true);
            Logger.logln("Finished reading the cached analyze map for the origin save");
            // Print();
            return;
          } catch (IOException e) {
            Logger.logln("Couldn't read the ID map  cache");
          } catch (ClassNotFoundException e) {
            Logger.logln("Couldn't read the ID map  cache");
          }
        }
      }

      FilenameFilter directoryFilter =
          new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
              File file = new File(dir, name);
              return file.isDirectory();
            }
          };

      class FileWalker {
        TreeSet<File> regionfiles = new TreeSet<File>();

        public void walk(File root) {
          File[] list = root.listFiles();

          for (File f : list) {
            if (f.isDirectory()) {
              walk(f);
            } else {
              if (f.getName().endsWith(".mca")) {
                regionfiles.add(root);
              }
            }
          }
        }
      }

      for (int ntype = 0; ntype < ConverterMain.Ntype; ntype++) {
        problematicids[ntype] = new ArrayList<Object>();
        removedids[ntype] = new ArrayList<Object>();
        converteds[ntype] = new ArrayList<Object>();
        nconverteds[ntype] = new TreeMap<Object, Integer>();
        ConverterMain.options.getsave(0).listofidsinsave[ntype] = new ArrayList<Object>();
        ConverterMain.options.getsave(0).nidsinsave[ntype] = new TreeMap<Object, Integer>();
      }

      FileWalker filewalker0 = new FileWalker();
      filewalker0.walk(rootfolderorig);
      List<Object> list = Arrays.asList(filewalker0.regionfiles.toArray());
      Collections.shuffle(list);
      Iterator it = list.iterator();

      // for (int nr=0;nr<regions.length;nr++){
      while (it.hasNext()) {
        File oldregionfolder = (File) it.next();

        String relative = rootfolderorig.toURI().relativize(oldregionfolder.toURI()).getPath();

        File newregionfolder = new File(rootfolderdest, relative);
        if (!oldregionfolder.exists() || !oldregionfolder.isDirectory()) {
          Logger.logln("ERROR with the regions directory");
          return;
        }

        File[] regionfiles = newregionfolder.listFiles();
        if (convert) {
          Logger.logln(
              "Converting " + relative + " files from " + oldregionfolder.getCanonicalPath());
          Logger.logln("                          to " + newregionfolder.getCanonicalPath());
        } else {
          Logger.logln(
              "Analyzing " + relative + " files from " + oldregionfolder.getCanonicalPath());
        }
        for (int nf = 0; nf < regionfiles.length; nf++) {
          currentregionname = regionfiles[nf].getName();
          RegionFile newregion = new RegionFile(regionfiles[nf]);
          RegionFile oldregion =
              new RegionFile(new File(oldregionfolder, regionfiles[nf].getName()));
          Logger.logln(
              "Treating "
                  + relative
                  + " file "
                  + (nf + 1)
                  + "/"
                  + regionfiles.length
                  + "   "
                  + currentregionname);
          MainFrame.updateframe();
          int blabla = 1;
          for (xc = 0; xc < 32; xc++) {
            for (zc = 0; zc < 32; zc++) {
              if (stop) {
                return;
              }
              if (oldregion.hasChunk(xc, zc)) {
                // System.out.println(xc+" "+zc);
                try {
                  DataInputStream dis = oldregion.getChunkDataInputStream(xc, zc);
                  DataOutputStream dos = newregion.getChunkDataOutputStream(xc, zc);
                  CompoundTag comptag = NbtIo.read(dis);
                  dis.close();
                  CompoundTag comptag2 = (CompoundTag) comptag.copy();
                  CompoundTag leveldata = comptag2.getCompound("Level");

                  heightmap = leveldata.getIntArray("HeightMap");

                  currentmode = "Blocks";
                  ListTag sectionsdata = leveldata.getList("Sections");
                  for (int ns = 0; ns < sectionsdata.size(); ns++) {
                    // CompoundTag tag=;
                    treatblocks((CompoundTag) sectionsdata.get(ns), convert);
                    // tag.putByteArray("Blocks", convertblocks(tag.getByteArray("Blocks")));
                    // tag.putByteArray("Add", convertblocksAdd(tag.getByteArray("Add")));
                  }
                  // long t0=System.currentTimeMillis();

                  currentmode = "Entities_Items";
                  traverseTags_treatitems(leveldata.getList("Entities"), convert);

                  currentmode = "Entities";
                  treatentities(leveldata.getList("Entities"), 2, convert);

                  //									currentmode="TileEntities";
                  //									treatentities(leveldata.getList("TileEntities"),3,convert);

                  currentmode = "TileEntities_Items";
                  traverseTags_treatitems(leveldata.getList("TileEntities"), convert);

                  // long t1=System.currentTimeMillis();
                  // System.out.println(" "+(t1-t0));

                  if (convert) {
                    NbtIo.write(comptag2, dos);
                    dos.flush();
                    dos.close();
                  }
                  //									dis=oldregion.getChunkDataInputStream(xc,zc);
                  //									CompoundTag comptag3=NbtIo.read(dis);
                  //									System.out.println(comptag3.equals(comptag));

                } catch (IOException e) {
                  Logger.logln("Unrecoverable Problem reading chunk data. Possibly corrupt level");
                }
              }
            }
          }
        }
      }

      for (int ntype = 0; ntype < ConverterMain.Ntype; ntype++) {
        Iterator it2 = nconverteds[ntype].keySet().iterator();
        while (it2.hasNext()) {
          Integer id = (Integer) it2.next();
          if (nconverteds[ntype].get(id) > 0) {
            Logger.logln(
                "Converted "
                    + nconverteds[ntype].get(id)
                    + "("
                    + ConverterMain.idmaps[ntype].get(0).get(id)
                    + ") "
                    + ConverterMain.typenames[ntype]
                    + " with ID :"
                    + id
                    + " to "
                    + ConverterMain.options.getconvertmaps()[ntype].get(id)
                    + "("
                    + ConverterMain.idmaps[ntype].get(1).get(id)
                    + ")");
          }
        }
      }
      if (!convert) {
        Logger.logln("Finished the analysis.");
        FileOutputStream fos = new FileOutputStream(cachefile);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        for (int ntype = 0; ntype < ConverterMain.Ntype; ntype++) {
          oos.writeObject(ConverterMain.options.getsave(0).listofidsinsave[ntype]);
          oos.writeObject(ConverterMain.options.getsave(0).nidsinsave[ntype]);
        }
        oos.close();
        // Print();
      } else {
        Logger.logln("Finished the conversion ! Hopefully.");
      }
      for (int ntype = 0; ntype < ConverterMain.Ntype; ntype++) {
        problematicids[ntype].clear();
        converteds[ntype].clear();
        nconverteds[ntype].clear();
      }
      ConverterMain.options.save();

    } catch (IOException e) {
      Logger.logln("ERROR accessing the save folder");
      return;
    }
    return;
  }