84 lines
2.7 KiB
Java
84 lines
2.7 KiB
Java
package de.winniepat.kingdomClashSurvival.managers;
|
|
|
|
import de.winniepat.kingdomClashSurvival.KingdomClashSurvival;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class StarterKitManager {
|
|
|
|
private final KingdomClashSurvival plugin;
|
|
private final File dataFile;
|
|
private final Set<UUID> claimedPlayers = new HashSet<>();
|
|
|
|
public StarterKitManager(KingdomClashSurvival plugin) {
|
|
this.plugin = plugin;
|
|
this.dataFile = new File(plugin.getDataFolder(), "claimed_starter_kits.txt");
|
|
loadData();
|
|
}
|
|
|
|
public void loadData() {
|
|
if (!dataFile.exists()) {
|
|
if (!plugin.getDataFolder().exists()) {
|
|
plugin.getDataFolder().mkdirs();
|
|
}
|
|
try {
|
|
dataFile.createNewFile();
|
|
} catch (IOException e) {
|
|
plugin.getLogger().severe("Could not create claimed_starter_kits.txt: " + e.getMessage());
|
|
}
|
|
return;
|
|
}
|
|
|
|
try {
|
|
claimedPlayers.clear();
|
|
Files.lines(dataFile.toPath())
|
|
.map(String::trim)
|
|
.filter(s -> !s.isEmpty())
|
|
.map(line -> {
|
|
try {
|
|
return UUID.fromString(line);
|
|
} catch (IllegalArgumentException e) {
|
|
plugin.getLogger().warning("Invalid UUID found in data file: " + line);
|
|
return null;
|
|
}
|
|
})
|
|
.filter(uuid -> uuid != null)
|
|
.forEach(claimedPlayers::add);
|
|
|
|
plugin.getLogger().info("Loaded " + claimedPlayers.size() + " claimed starter kits.");
|
|
} catch (IOException e) {
|
|
plugin.getLogger().severe("Could not load claimed starter kits data: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
public void saveData() {
|
|
List<String> uuidStrings = claimedPlayers.stream()
|
|
.map(UUID::toString)
|
|
.collect(Collectors.toList());
|
|
|
|
try {
|
|
Files.write(dataFile.toPath(), uuidStrings,
|
|
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
|
} catch (IOException e) {
|
|
plugin.getLogger().severe("Could not save claimed starter kits data: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
public boolean hasClaimed(UUID playerId) {
|
|
return claimedPlayers.contains(playerId);
|
|
}
|
|
|
|
public void setClaimed(UUID playerId) {
|
|
claimedPlayers.add(playerId);
|
|
saveData();
|
|
}
|
|
}
|