コード例 #1
0
 public void save() {
   try {
     save(configFile);
   } catch (IOException ex) {
     LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
   }
 }
コード例 #2
0
    @Override
    public void run() {
      // long startTime = System.nanoTime();
      synchronized (configFile) {
        if (pendingDiskWrites.get() > 1) {
          // Writes can be skipped, because they are stored in a queue (in the executor).
          // Only the last is actually written.
          pendingDiskWrites.decrementAndGet();
          // LOGGER.log(Level.INFO, configFile + " skipped writing in " + (System.nanoTime() -
          // startTime) + " nsec.");
          return;
        }
        try {
          Files.createParentDirs(configFile);

          if (!configFile.exists()) {
            try {
              LOGGER.log(Level.INFO, tl("creatingEmptyConfig", configFile.toString()));
              if (!configFile.createNewFile()) {
                LOGGER.log(Level.SEVERE, tl("failedToCreateConfig", configFile.toString()));
                return;
              }
            } catch (IOException ex) {
              LOGGER.log(Level.SEVERE, tl("failedToCreateConfig", configFile.toString()), ex);
              return;
            }
          }

          final FileOutputStream fos = new FileOutputStream(configFile);
          try {
            final OutputStreamWriter writer = new OutputStreamWriter(fos, UTF8);

            try {
              writer.write(data);
            } finally {
              writer.close();
            }
          } finally {
            fos.close();
          }
        } catch (IOException e) {
          LOGGER.log(Level.SEVERE, e.getMessage(), e);
        } finally {
          // LOGGER.log(Level.INFO, configFile + " written to disk in " + (System.nanoTime() -
          // startTime) + " nsec.");
          pendingDiskWrites.decrementAndGet();
        }
      }
    }
コード例 #3
0
  public void save() {
    File file = null;
    for (String s : worlds) {
      //////////// --------- Tubes ------------ ///////////////
      if (tubes != null && tubes.get(s) != null) {
        file = new File(path + File.separator + s + "Tubes.loc");
        new File(path).mkdir();
        if (!file.exists()) {
          try {
            file.createNewFile();
          } catch (IOException e) {
            // e.printStackTrace();
          }
        }

        ArrayList<String> t = new ArrayList<String>();

        for (Location x : tubes.get(s)) {
          t.add(
              x.getWorld().getName()
                  + ","
                  + x.getBlockX()
                  + ","
                  + x.getBlockY()
                  + ","
                  + x.getBlockZ());
        }

        try {
          ObjectOutputStream oos =
              new ObjectOutputStream(new FileOutputStream(path + File.separator + s + "Tubes.loc"));
          oos.writeObject(t);
          oos.flush();
          oos.close();
        } catch (Exception e) {
          // e.printStackTrace();
        }
      }

      //////////// --------- Tubes End ------------ ///////////////
      //////////// --------- cornucopia ------------ ///////////////

      if (cornucopia.get(s) != null) {
        file = new File(path + File.separator + s + "Corn.loc");
        new File(path).mkdir();
        if (!file.exists()) {
          try {
            file.createNewFile();
          } catch (IOException e) {
            // e.printStackTrace();
          }
        }

        String c = "";
        c =
            (cornucopia.get(s).getWorld().getName()
                + ","
                + cornucopia.get(s).getBlockX()
                + ","
                + cornucopia.get(s).getBlockY()
                + ","
                + cornucopia.get(s).getBlockZ());

        cornucopia
            .get(s)
            .getWorld()
            .setSpawnLocation(
                cornucopia.get(s).getBlockX(),
                cornucopia.get(s).getBlockY(),
                cornucopia.get(s).getBlockZ());

        try {
          ObjectOutputStream oos =
              new ObjectOutputStream(new FileOutputStream(path + File.separator + s + "Corn.loc"));
          oos.writeObject(c);
          oos.flush();
          oos.close();
        } catch (Exception e) {
          // e.printStackTrace();
        }
      }
      //////////// --------- cornucopia End ------------ ///////////////
    }

    //////////// --------- Waiting ------------ ///////////////
    if (waiting != null) {
      file = new File(path + File.separator + "Wait.loc");
      new File(path).mkdir();
      if (!file.exists()) {
        try {
          file.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      String w = "";
      w =
          (waiting.getWorld().getName()
              + ","
              + waiting.getBlockX()
              + ","
              + waiting.getBlockY()
              + ","
              + waiting.getBlockZ());

      try {
        ObjectOutputStream oos =
            new ObjectOutputStream(new FileOutputStream(path + File.separator + "Wait.loc"));
        oos.writeObject(w);
        oos.flush();
        oos.close();
      } catch (Exception e) {
        // e.printStackTrace();
      }
    }

    //////////// --------- Waiting End ------------ ///////////////
    //////////// --------- Score ------------ ///////////////

    if (score != null) {
      file = new File(path + File.separator + "Score.loc");
      new File(path).mkdir();
      if (!file.exists()) {
        try {
          file.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      try {
        ObjectOutputStream oos =
            new ObjectOutputStream(new FileOutputStream(path + File.separator + "Score.loc"));
        oos.writeObject(score);
        oos.flush();
        oos.close();
      } catch (Exception e) {
        // e.printStackTrace();
      }
    }
    //////////// --------- Score End ------------ ///////////////
    //////////// --------- Shops ------------ ///////////////

    if (items != null) {
      file = new File(path + File.separator + "Shops.loc");
      new File(path).mkdir();
      if (!file.exists()) {
        try {
          file.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      ArrayList<String> it = new ArrayList<String>();

      for (ShopItem i : items) {
        String ss =
            i.id
                + ","
                + i.price
                + ","
                + i.holder.getLocation().getWorld().getName()
                + ","
                + i.holder.getLocation().getX()
                + ","
                + i.holder.getLocation().getY()
                + ","
                + i.holder.getLocation().getZ();
        it.add(ss);
      }

      try {
        ObjectOutputStream oos =
            new ObjectOutputStream(new FileOutputStream(path + File.separator + "Shops.loc"));
        oos.writeObject(it);
        oos.flush();
        oos.close();
      } catch (Exception e) {
        // e.printStackTrace();
      }
    }
    //////////// --------- Shops End ------------ ///////////////
    //////////// --------- Shop ------------ ///////////////
    if (waiting != null) {
      file = new File(path + File.separator + "Shop.loc");
      new File(path).mkdir();
      if (!file.exists()) {
        try {
          file.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      String s = "";
      s =
          (shop.getWorld().getName()
              + ","
              + shop.getBlockX()
              + ","
              + shop.getBlockY()
              + ","
              + shop.getBlockZ());

      try {
        ObjectOutputStream oos =
            new ObjectOutputStream(new FileOutputStream(path + File.separator + "Shop.loc"));
        oos.writeObject(s);
        oos.flush();
        oos.close();
      } catch (Exception e) {
        // e.printStackTrace();
      }
    }

    //////////// --------- Shop End ------------ ///////////////
  }
コード例 #4
0
  public synchronized void load() {
    if (pendingDiskWrites.get() != 0) {
      LOGGER.log(
          Level.INFO, "File {0} not read, because it''s not yet written to disk.", configFile);
      return;
    }
    if (!configFile.getParentFile().exists()) {
      if (!configFile.getParentFile().mkdirs()) {
        LOGGER.log(Level.SEVERE, tl("failedToCreateConfig", configFile.toString()));
      }
    }
    // This will delete files where the first character is 0. In most cases they are broken.
    if (configFile.exists() && configFile.length() != 0) {
      try {
        final InputStream input = new FileInputStream(configFile);
        try {
          if (input.read() == 0) {
            input.close();
            configFile.delete();
          }
        } catch (IOException ex) {
          LOGGER.log(Level.SEVERE, null, ex);
        } finally {
          try {
            input.close();
          } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
          }
        }
      } catch (FileNotFoundException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
      }
    }

    if (!configFile.exists()) {
      if (legacyFileExists()) {
        convertLegacyFile();
      } else if (altFileExists()) {
        convertAltFile();
      } else if (templateName != null) {
        LOGGER.log(Level.INFO, tl("creatingConfigFromTemplate", configFile.toString()));
        createFromTemplate();
      } else {
        return;
      }
    }

    try {
      final FileInputStream inputStream = new FileInputStream(configFile);
      try {
        long startSize = configFile.length();
        if (startSize > Integer.MAX_VALUE) {
          throw new InvalidConfigurationException("File too big");
        }
        ByteBuffer buffer = ByteBuffer.allocate((int) startSize);
        int length;
        while ((length = inputStream.read(bytebuffer)) != -1) {
          if (length > buffer.remaining()) {
            ByteBuffer resize =
                ByteBuffer.allocate(buffer.capacity() + length - buffer.remaining());
            int resizePosition = buffer.position();
            buffer.rewind();
            resize.put(buffer);
            resize.position(resizePosition);
            buffer = resize;
          }
          buffer.put(bytebuffer, 0, length);
        }
        buffer.rewind();
        final CharBuffer data = CharBuffer.allocate(buffer.capacity());
        CharsetDecoder decoder = UTF8.newDecoder();
        CoderResult result = decoder.decode(buffer, data, true);
        if (result.isError()) {
          buffer.rewind();
          data.clear();
          LOGGER.log(
              Level.INFO,
              "File "
                  + configFile.getAbsolutePath().toString()
                  + " is not utf-8 encoded, trying "
                  + Charset.defaultCharset().displayName());
          decoder = Charset.defaultCharset().newDecoder();
          result = decoder.decode(buffer, data, true);
          if (result.isError()) {
            throw new InvalidConfigurationException(
                "Invalid Characters in file " + configFile.getAbsolutePath().toString());
          } else {
            decoder.flush(data);
          }
        } else {
          decoder.flush(data);
        }
        final int end = data.position();
        data.rewind();
        super.loadFromString(data.subSequence(0, end).toString());
      } finally {
        inputStream.close();
      }
    } catch (IOException ex) {
      LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
    } catch (InvalidConfigurationException ex) {
      File broken =
          new File(configFile.getAbsolutePath() + ".broken." + System.currentTimeMillis());
      configFile.renameTo(broken);
      LOGGER.log(
          Level.SEVERE,
          "The file "
              + configFile.toString()
              + " is broken, it has been renamed to "
              + broken.toString(),
          ex.getCause());
    }
  }