Пример #1
0
  /**
   * Gets all settlement IDs in String form for reading.
   *
   * @return all filenames
   */
  public static String[] getAllChunkGroupIds() {

    File directory = new File(WriteReadType.SETTLEMENT_NORMAL.getDirectory());
    FilenameFilter filter =
        new FilenameFilter() {

          @Override
          public boolean accept(File dir, String name) {
            return name.endsWith(IOConstants.FILE_EXTENTENSION);
          }
        };

    if (!directory.exists()) {
      directory.mkdirs();
      Saga.info("Creating " + directory + " directory.");
    }

    String[] names = directory.list(filter);

    if (names == null) {
      Saga.severe("Could not retrieve faction names.");
      names = new String[0];
    }

    // Remove extensions:
    for (int i = 0; i < names.length; i++) {
      names[i] = names[i].replaceAll(IOConstants.FILE_EXTENTENSION, "");
    }

    return names;
  }
Пример #2
0
  /**
   * Moves the chunk group file to the folder for deleted factions.
   *
   * @param groupId
   */
  public static void deleteChunkGroup(String groupId) {

    // Create folders:
    File deletedDirectory = new File(WriteReadType.SETTLEMENT_DELETED.getDirectory());
    File factionDirectory = new File(WriteReadType.SETTLEMENT_NORMAL.getDirectory());
    File deletedFile =
        new File(
            WriteReadType.SETTLEMENT_DELETED.getDirectory()
                + groupId
                + IOConstants.FILE_EXTENTENSION);
    File factionFile =
        new File(
            WriteReadType.SETTLEMENT_NORMAL.getDirectory()
                + groupId
                + IOConstants.FILE_EXTENTENSION);

    if (!deletedDirectory.exists()) {
      deletedDirectory.mkdirs();
      Saga.info("Creating " + deletedDirectory + " directory.");
    }

    if (!factionDirectory.exists()) {
      factionDirectory.mkdirs();
      Saga.info("Creating " + factionDirectory + " directory.");
    }

    // Check if exists.
    if (!factionFile.exists()) {
      Saga.severe("Cant move " + factionFile + ", because it doesent exist.");
    }

    // Rename if target exists:
    for (int i = 1; i < 1000; i++) {
      if (deletedFile.exists()) {
        deletedFile.renameTo(
            new File(
                WriteReadType.SETTLEMENT_DELETED.getDirectory()
                    + groupId
                    + "("
                    + i
                    + ")"
                    + IOConstants.FILE_EXTENTENSION));
      } else {
        break;
      }
    }

    // Move file to deleted folder:
    boolean success = factionFile.renameTo(deletedFile);

    // Notify on failure:
    if (success) {
      Saga.info("Moved " + factionFile + " file to deleted factions folder.");
    } else {
      Saga.severe("Failed to move " + factionFile + " to deleted factions folder.");
    }
  }
Пример #3
0
  /**
   * Writes configuration.
   *
   * @param config configuration String
   * @param writeType write type
   * @param configType configuration type
   * @throws IOException thrown when read fails
   */
  private static void writeConfig(String config, WriteReadType writeType, String fileName)
      throws IOException {

    File directory = new File(writeType.getDirectory());
    File file = new File(writeType.getDirectory() + fileName);

    if (!directory.exists()) {
      directory.mkdirs();
      Saga.info("Creating " + directory + " directory.");
    }

    if (!file.exists()) {
      file.createNewFile();
      Saga.info("Creating " + file + " file.");
    }

    BufferedWriter out = new BufferedWriter(new FileWriter(file));
    out.write(config);
    out.close();
  }
Пример #4
0
  /**
   * Reads configuration.
   *
   * @param writeType write type
   * @param configType configuration type
   * @throws IOException thrown when read fails
   */
  private static String readConfig(WriteReadType writeType, String fileName) throws IOException {

    // Add directory if missing:
    File directory = new File(writeType.getDirectory());
    if (!directory.exists()) {
      directory.mkdirs();
      Saga.info("Creating " + directory + " directory.");
    }

    File file = new File(writeType.getDirectory() + fileName);
    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    fin = new FileInputStream(file);
    while ((ch = fin.read()) != -1) {
      strContent.append((char) ch);
    }
    fin.close();

    return strContent.toString();
  }