package ru.tehkode.permissions.backends.file;

import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;

public class FileConfig extends YamlConfiguration {
	private final File file;
	private boolean saveSuppressed;

	public FileConfig(File file) {
		super();
		this.options().pathSeparator(FileBackend.PATH_SEPARATOR);
		this.file = file;
	}

	public File getFile() {
		return file;
	}

	public void load() throws IOException, InvalidConfigurationException {
		this.load(file);
	}

	public void save() throws IOException {
		if (!saveSuppressed) {
			this.save(file);
		}
	}

	public boolean isSaveSuppressed() {
		return saveSuppressed;
	}

	void setSaveSuppressed(boolean saveSuppressed) {
		this.saveSuppressed = saveSuppressed;
	}

	@Override
	public void loadFromString(String contents) throws InvalidConfigurationException {
		synchronized (this) {
			super.loadFromString(contents);
		}
	}

	@Override
	public String saveToString() {
		synchronized (this) {
			return super.saveToString();
		}
	}
}

@@ -286,16 +286,63 @@
 ile file
+, tempFile, oldFile;%0A%09private final Object lock
 ;%0A%09priva
@@ -407,325 +407,907 @@
 %7B%0A%09%09
-super();%0A%09%09this.options().pathSeparator(FileBackend.PATH_SEPARATOR);%0A%09%09this.file = file;%0A%09%7D%0A%0A%09public File getFile() %7B%0A%09%09return file;%0A%09%7D%0A%0A%09public void load() throws IOException, InvalidConfigurationException %7B%0A%09%09this.load(file);%0A%09%7D%0A%0A%09public void save() throws IOException %7B%0A%09%09if (!saveSuppressed) %7B%0A%09%09%09this.save(file);
+this(file, new Object());%0A%09%7D%0A%0A%09public FileConfig(File file, Object lock) %7B%0A%09%09super();%0A%09%09this.lock = lock;%0A%09%09this.options().pathSeparator(FileBackend.PATH_SEPARATOR);%0A%09%09this.file = file;%0A%09%09this.tempFile = new File(file.getPath() + %22.tmp%22);%0A%09%09this.oldFile = new File(file.getPath() + %22.old%22);%0A%09%7D%0A%0A%09public File getFile() %7B%0A%09%09return file;%0A%09%7D%0A%0A%09public void load() throws IOException, InvalidConfigurationException %7B%0A%09%09this.load(file);%0A%09%7D%0A%0A%09public void save() throws IOException %7B%0A%09%09if (!saveSuppressed) %7B%0A%09%09%09this.save(tempFile);%0A%09%09%09oldFile.delete();%0A%09%09%09if (!file.exists() %7C%7C file.renameTo(oldFile)) %7B%0A%09%09%09%09if (!tempFile.renameTo(file)) %7B%0A%09%09%09%09%09throw new IOException(%22Unable to overwrite config with temporary file! New config is at %22 + tempFile + %22, old config at%22 + oldFile);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09if (!oldFile.delete()) %7B%0A%09%09%09%09%09%09throw new IOException(%22Unable to delete old file %22 + oldFile);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D
 %0A%09%09%7D
@@ -1578,28 +1578,28 @@
 nchronized (
-this
+lock
 ) %7B%0A%09%09%09super
@@ -1692,20 +1692,20 @@
 onized (
-this
+lock
 ) %7B%0A%09%09%09r

Make config saving more robust in case of server crash