70 lines
2.2 KiB
Java
70 lines
2.2 KiB
Java
package net.therosegarden.firefighter;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
|
|
final class FirefighterDutyManager {
|
|
private final RoseFirefighterPlugin plugin;
|
|
private final JobsFacade jobs;
|
|
private final File file;
|
|
private final Set<UUID> offDuty = new HashSet<>();
|
|
|
|
FirefighterDutyManager(RoseFirefighterPlugin plugin, JobsFacade jobs) {
|
|
this.plugin = plugin;
|
|
this.jobs = jobs;
|
|
this.file = new File(plugin.getDataFolder(), "duty-status.yml");
|
|
load();
|
|
}
|
|
|
|
boolean isOnDuty(Player player) {
|
|
return player != null && jobs.isFirefighter(player) && !offDuty.contains(player.getUniqueId());
|
|
}
|
|
|
|
boolean setOnDuty(Player player, boolean onDuty) {
|
|
if (player == null || !jobs.isFirefighter(player)) {
|
|
return false;
|
|
}
|
|
if (onDuty) {
|
|
offDuty.remove(player.getUniqueId());
|
|
} else {
|
|
offDuty.add(player.getUniqueId());
|
|
}
|
|
save();
|
|
return true;
|
|
}
|
|
|
|
private void load() {
|
|
offDuty.clear();
|
|
if (!file.isFile()) {
|
|
return;
|
|
}
|
|
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file);
|
|
for (String value : yaml.getStringList("off-duty")) {
|
|
try {
|
|
offDuty.add(UUID.fromString(value));
|
|
} catch (IllegalArgumentException ignored) {
|
|
plugin.getLogger().warning("Ignoring invalid off-duty UUID: " + value);
|
|
}
|
|
}
|
|
}
|
|
|
|
void save() {
|
|
YamlConfiguration yaml = new YamlConfiguration();
|
|
yaml.set("off-duty", offDuty.stream().map(UUID::toString).sorted().toList());
|
|
try {
|
|
if (!plugin.getDataFolder().exists() && !plugin.getDataFolder().mkdirs()) {
|
|
plugin.getLogger().warning("Could not create plugin data folder for duty status.");
|
|
return;
|
|
}
|
|
yaml.save(file);
|
|
} catch (IOException ex) {
|
|
plugin.getLogger().severe("Could not save Firefighter duty status: " + ex.getMessage());
|
|
}
|
|
}
|
|
}
|