Files
spigot-quest-board/src/main/java/games/dmg/spigotquestboard/YamlPlayerCommandSettingsRepository.java
T
dmg be13b9b87c
Release / release (push) Successful in 2m8s
CI / build (push) Successful in 1m15s
feat(admin): control player quest commands
2026-09-05 08:36:45 -04:00

54 lines
1.9 KiB
Java

package games.dmg.spigotquestboard;
import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
final class YamlPlayerCommandSettingsRepository implements PlayerCommandSettingsRepository {
private static final String ENABLED_PATH = "player-commands.enabled";
private final Path path;
YamlPlayerCommandSettingsRepository(Path path) {
this.path = Objects.requireNonNull(path, "path");
}
@Override
public boolean loadEnabled() throws IOException {
if (!Files.exists(path)) {
return false;
}
YamlConfiguration yaml = new YamlConfiguration();
try {
yaml.load(path.toFile());
} catch (InvalidConfigurationException exception) {
throw new IOException("Invalid player command settings", exception);
}
return yaml.getBoolean(ENABLED_PATH, false);
}
@Override
public void saveEnabled(boolean enabled) throws IOException {
YamlConfiguration yaml = new YamlConfiguration();
yaml.set(ENABLED_PATH, enabled);
Path parent = path.toAbsolutePath().getParent();
Files.createDirectories(parent);
Path temporary = Files.createTempFile(parent, path.getFileName().toString(), ".tmp");
try {
yaml.save(temporary.toFile());
try {
Files.move(temporary, path, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException exception) {
Files.move(temporary, path, StandardCopyOption.REPLACE_EXISTING);
}
} finally {
Files.deleteIfExists(temporary);
}
}
}