Dateien nach "src/main/java/me/viper/teamplugin/manager" hochladen
This commit is contained in:
90
src/main/java/me/viper/teamplugin/manager/BackupManager.java
Normal file
90
src/main/java/me/viper/teamplugin/manager/BackupManager.java
Normal file
@@ -0,0 +1,90 @@
|
||||
package me.viper.teamplugin.manager;
|
||||
|
||||
import me.viper.teamplugin.Main;
|
||||
import me.viper.teamplugin.util.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BackupManager {
|
||||
|
||||
// Liefert das Backup-Verzeichnis
|
||||
private static File backupsFolder() {
|
||||
String folderName = Main.getInstance().getConfig().getString("backup.folder", "backups");
|
||||
File folder = new File(Main.getInstance().getDataFolder(), folderName);
|
||||
if (!folder.exists()) folder.mkdirs();
|
||||
return folder;
|
||||
}
|
||||
|
||||
// Erstellt ein Backup der data.yml
|
||||
public static String createBackup() {
|
||||
File dataFile = new File(Main.getInstance().getDataFolder(), "data.yml");
|
||||
if (!dataFile.exists()) return null;
|
||||
|
||||
File backups = backupsFolder();
|
||||
|
||||
String timestamp = Utils.formatIsoNow().replace(":", "-");
|
||||
String backupName = "data-backup-" + timestamp + ".yml";
|
||||
File dest = new File(backups, backupName);
|
||||
|
||||
try {
|
||||
Files.copy(dataFile.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
pruneBackups(backups); // alte Backups löschen
|
||||
return backupName;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Stellt ein Backup wieder her
|
||||
public static boolean restoreBackup(String fileName) {
|
||||
File dataFile = new File(Main.getInstance().getDataFolder(), "data.yml");
|
||||
File backup = new File(backupsFolder(), fileName);
|
||||
|
||||
if (!backup.exists()) return false;
|
||||
|
||||
try {
|
||||
Files.copy(backup.toPath(), dataFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
// DataManager neu laden
|
||||
DataManager.getData(); // Lädt data.yml neu
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Listet alle Backups auf
|
||||
public static List<String> listBackups() {
|
||||
File backups = backupsFolder();
|
||||
if (!backups.exists()) return Collections.emptyList();
|
||||
|
||||
return Arrays.stream(Objects.requireNonNull(backups.listFiles((d, name) -> name.toLowerCase().endsWith(".yml"))))
|
||||
.filter(File::isFile)
|
||||
.map(File::getName)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
// Löscht alte Backups und behält nur die letzten X (Konfigurierbar)
|
||||
private static void pruneBackups(File backupsFolder) {
|
||||
int keep = Main.getInstance().getConfig().getInt("backup.keep", 10);
|
||||
List<File> files = Arrays.stream(Objects.requireNonNull(backupsFolder.listFiles((d, name) -> name.toLowerCase().endsWith(".yml"))))
|
||||
.filter(File::isFile)
|
||||
.sorted(Comparator.comparingLong(File::lastModified).reversed())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (int i = keep; i < files.size(); i++) {
|
||||
files.get(i).delete();
|
||||
}
|
||||
}
|
||||
}
|
180
src/main/java/me/viper/teamplugin/manager/DataManager.java
Normal file
180
src/main/java/me/viper/teamplugin/manager/DataManager.java
Normal file
@@ -0,0 +1,180 @@
|
||||
package me.viper.teamplugin.manager;
|
||||
|
||||
import me.viper.teamplugin.Main;
|
||||
import me.viper.teamplugin.util.Utils;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class DataManager {
|
||||
private static File file;
|
||||
private static FileConfiguration data;
|
||||
|
||||
public static void setup() {
|
||||
try {
|
||||
file = new File(Main.getInstance().getDataFolder(), "data.yml");
|
||||
if (!file.exists()) {
|
||||
Main.getInstance().getDataFolder().mkdirs();
|
||||
file.createNewFile();
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
data.createSection("Team");
|
||||
save();
|
||||
} else {
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
if (data.getConfigurationSection("Team") == null) {
|
||||
data.createSection("Team");
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure backup folder exists if enabled
|
||||
if (Main.getInstance().getConfig().getBoolean("backup.enabled", true)) {
|
||||
File backups = new File(Main.getInstance().getDataFolder(),
|
||||
Main.getInstance().getConfig().getString("backup.folder", "backups"));
|
||||
if (!backups.exists()) backups.mkdirs();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/** Reload data.yml from disk (used after restore). */
|
||||
public static void reloadData() {
|
||||
if (file == null) file = new File(Main.getInstance().getDataFolder(), "data.yml");
|
||||
data = YamlConfiguration.loadConfiguration(file);
|
||||
if (data.getConfigurationSection("Team") == null) {
|
||||
data.createSection("Team");
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
public static FileConfiguration getData() {
|
||||
if (data == null) setup();
|
||||
return data;
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
try {
|
||||
if (data != null && file != null) data.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addMember(String rank, String name) {
|
||||
List<String> list = data.getStringList("Team." + rank);
|
||||
if (!list.contains(name)) {
|
||||
list.add(name);
|
||||
data.set("Team." + rank, list);
|
||||
|
||||
// store join date if enabled
|
||||
if (Main.getInstance().getConfig().getBoolean("storeJoinDate", true)) {
|
||||
data.set("JoinDates." + name, Utils.formatIsoNow());
|
||||
}
|
||||
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean removeMember(String name) {
|
||||
boolean removed = false;
|
||||
if (data.getConfigurationSection("Team") != null) {
|
||||
for (String rank : data.getConfigurationSection("Team").getKeys(false)) {
|
||||
List<String> members = data.getStringList("Team." + rank);
|
||||
if (members.removeIf(s -> s.equalsIgnoreCase(name))) {
|
||||
data.set("Team." + rank, members);
|
||||
removed = true;
|
||||
}
|
||||
}
|
||||
if (removed) {
|
||||
data.set("JoinDates." + name, null);
|
||||
save();
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
// Backups
|
||||
public static String createBackup() {
|
||||
String folderName = Main.getInstance().getConfig().getString("backup.folder", "backups");
|
||||
File backupsFolder = new File(Main.getInstance().getDataFolder(), folderName);
|
||||
if (!backupsFolder.exists()) backupsFolder.mkdirs();
|
||||
|
||||
String timestamp = Utils.formatIsoNow().replace(":", "-");
|
||||
String fileName = "data-backup-" + timestamp + ".yml";
|
||||
File dest = new File(backupsFolder, fileName);
|
||||
|
||||
try {
|
||||
// ensure current file exists
|
||||
if (file == null) file = new File(Main.getInstance().getDataFolder(), "data.yml");
|
||||
if (!file.exists()) {
|
||||
// nothing to backup
|
||||
return null;
|
||||
}
|
||||
|
||||
Files.copy(file.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
// prune backups
|
||||
pruneBackups(backupsFolder);
|
||||
return fileName;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a backup by filename (relative to backup folder).
|
||||
* Returns true on success.
|
||||
*/
|
||||
public static boolean restoreBackup(String fileName) {
|
||||
String folderName = Main.getInstance().getConfig().getString("backup.folder", "backups");
|
||||
File backupsFolder = new File(Main.getInstance().getDataFolder(), folderName);
|
||||
File src = new File(backupsFolder, fileName);
|
||||
if (!src.exists()) return false;
|
||||
|
||||
try {
|
||||
if (file == null) file = new File(Main.getInstance().getDataFolder(), "data.yml");
|
||||
Files.copy(src.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
// reload data from the restored file
|
||||
reloadData();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> listBackups() {
|
||||
String folderName = Main.getInstance().getConfig().getString("backup.folder", "backups");
|
||||
File backupsFolder = new File(Main.getInstance().getDataFolder(), folderName);
|
||||
if (!backupsFolder.exists()) return Collections.emptyList();
|
||||
return Arrays.stream(Objects.requireNonNull(backupsFolder.listFiles()))
|
||||
.filter(File::isFile)
|
||||
.map(File::getName)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static void pruneBackups(File backupsFolder) {
|
||||
int keep = Main.getInstance().getConfig().getInt("backup.keep", 10);
|
||||
List<File> files = Arrays.stream(Objects.requireNonNull(backupsFolder.listFiles()))
|
||||
.filter(File::isFile)
|
||||
.sorted(Comparator.comparingLong(File::lastModified).reversed())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (int i = keep; i < files.size(); i++) {
|
||||
files.get(i).delete();
|
||||
}
|
||||
}
|
||||
|
||||
// helper
|
||||
public static String getJoinDate(String name) {
|
||||
return data.getString("JoinDates." + name, "");
|
||||
}
|
||||
}
|
40
src/main/java/me/viper/teamplugin/manager/LangManager.java
Normal file
40
src/main/java/me/viper/teamplugin/manager/LangManager.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package me.viper.teamplugin.manager;
|
||||
|
||||
import me.viper.teamplugin.Main;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class LangManager {
|
||||
private static File file;
|
||||
private static FileConfiguration cfg;
|
||||
|
||||
public static void setup() {
|
||||
file = new File(Main.getInstance().getDataFolder(), "lang.yml");
|
||||
if (!file.exists()) {
|
||||
Main.getInstance().saveResource("lang.yml", false);
|
||||
}
|
||||
cfg = YamlConfiguration.loadConfiguration(file);
|
||||
}
|
||||
|
||||
public static String get(String path) {
|
||||
if (cfg == null) setup();
|
||||
return cfg.getString(path, "Missing:" + path).replace("%prefix%", cfg.getString("prefix", ""));
|
||||
}
|
||||
|
||||
public static List<String> getList(String path) {
|
||||
if (cfg == null) setup();
|
||||
return cfg.getStringList(path);
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
try {
|
||||
if (cfg != null && file != null) cfg.save(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user