|
|
|
|
@@ -0,0 +1,100 @@
|
|
|
|
|
/*
|
|
|
|
|
* This file is a part of the SteamWar software.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2020 SteamWar.de-Serverteam
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package de.steamwar.bausystem.region.dynamic;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
import de.steamwar.bausystem.region.FlagStorage;
|
|
|
|
|
import de.steamwar.bausystem.region.RegionBackups;
|
|
|
|
|
import de.steamwar.bausystem.region.flags.Flag;
|
|
|
|
|
import lombok.NonNull;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
@UtilityClass
|
|
|
|
|
public class RegionDataRepository {
|
|
|
|
|
|
|
|
|
|
private static File regionDataFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions");
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
regionDataFolder.mkdirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public File getRegionDirectory(DynamicRegion region) {
|
|
|
|
|
File file = new File(regionDataFolder, region.getID().toString());
|
|
|
|
|
file.mkdirs();
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadFlagStorage(DynamicRegion region, FlagStorage storage) {
|
|
|
|
|
File file = getRegionDirectory(region);
|
|
|
|
|
file = new File(file, "flags.json");
|
|
|
|
|
loadFlagStorage(file, storage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
|
private void loadFlagStorage(File file, FlagStorage storage) {
|
|
|
|
|
JsonObject jsonObject = JsonParser.parseReader(new FileReader(file)).getAsJsonObject();
|
|
|
|
|
for (Flag flag : Flag.getFlags()) {
|
|
|
|
|
Flag.Value<?> value;
|
|
|
|
|
try {
|
|
|
|
|
value = flag.valueOfValue(jsonObject.get(flag.name()).getAsString());
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
storage.getBackedMap().put(flag, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd'_'HH:mm:ss");
|
|
|
|
|
public RegionBackups getBackups(DynamicRegion region) {
|
|
|
|
|
if (region.getType().isGlobal()) {
|
|
|
|
|
return RegionBackups.EMPTY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File directory = new File(getRegionDirectory(region), "backup");
|
|
|
|
|
directory.mkdirs();
|
|
|
|
|
return new RegionBackups() {
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<Backup> create(BackupType backupType) {
|
|
|
|
|
return Optional.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public @NonNull List<Backup> list() {
|
|
|
|
|
return List.of();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
@Override
|
|
|
|
|
public Backup get(String name) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|