SteamWar/BauSystem2.0
Archiviert
12
0

First impl of BackupScheduler

Dieser Commit ist enthalten in:
Zeanon 2021-07-05 19:32:10 +02:00
Ursprung 9eef410148
Commit 71f784451f
3 geänderte Dateien mit 116 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -23,9 +23,13 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitWorld; import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader; import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operations; import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.math.transform.AffineTransform;
@ -36,8 +40,10 @@ import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Level;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -98,4 +104,28 @@ public class Region_15 {
} }
} }
} }
boolean backup(Point minPoint, Point maxPoint, File file) {
BukkitWorld bukkitWorld = new BukkitWorld(Bukkit.getWorlds().get(0));
CuboidRegion region = new CuboidRegion(bukkitWorld, RegionUtils_15.toBlockVector3(minPoint), RegionUtils_15.toBlockVector3(maxPoint));
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(bukkitWorld, -1)) {
ForwardExtentCopy copy = new ForwardExtentCopy(
e, region, clipboard, region.getMinimumPoint()
);
copy.setCopyingEntities(false);
copy.setCopyingBiomes(false);
Operations.complete(copy);
try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) {
writer.write(clipboard);
}
return true;
} catch (WorldEditException | IOException e) {
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
return false;
}
}
} }

Datei anzeigen

@ -0,0 +1,61 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 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;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.linkage.Enable;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.Linked;
import de.steamwar.bausystem.region.tags.Tag;
import java.util.Iterator;
import org.bukkit.scheduler.BukkitRunnable;
@Linked(LinkageType.ENABLE_LINK)
public class BackupScheduler implements Enable {
@Override
public void enable() {
new BukkitRunnable() {
@Override
public void run() {
final Iterator<Region> regions = Region.getREGION_MAP().values().stream().filter(region -> region.get(Tag.CHANGED)).iterator();
BackupScheduler.this.doBackup(regions);
}
}.runTaskTimer(BauSystem.getInstance(), 0, 20*60*30);
}
public void doBackup(final Iterator<Region> regionIterator) {
new BukkitRunnable() {
@Override
public void run() {
if (!regionIterator.hasNext()) {
this.cancel();
return;
}
final Region region = regionIterator.next();
if (region.backup()) {
region.remove(Tag.CHANGED);
}
}
}.runTaskTimer(BauSystem.getInstance(), 0, 20);
}
}

Datei anzeigen

@ -27,10 +27,14 @@ import de.steamwar.bausystem.region.tags.Tag;
import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.shared.SizedStack; import de.steamwar.bausystem.shared.SizedStack;
import de.steamwar.core.VersionedCallable;
import de.steamwar.sql.Schematic; import de.steamwar.sql.Schematic;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONObject;
import yapion.hierarchy.types.YAPIONType; import yapion.hierarchy.types.YAPIONType;
@ -50,7 +54,10 @@ import static de.steamwar.bausystem.region.RegionUtils.paste;
@Getter @Getter
public class Region { public class Region {
@Getter
private static final Map<String, Region> REGION_MAP = new HashMap<>(); private static final Map<String, Region> REGION_MAP = new HashMap<>();
private static final File backupFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "backup");
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH-mm-ss'#'dd-MM-yyyy");
public static Region getRegion(Location location) { public static Region getRegion(Location location) {
return REGION_MAP.values().stream() return REGION_MAP.values().stream()
@ -289,10 +296,20 @@ public class Region {
setLinkedRegion(region -> region.flagStorage.set(tag)); setLinkedRegion(region -> region.flagStorage.set(tag));
} }
public void remove(Tag tag) {
if (flagStorage.remove(tag)) {
RegionUtils.save(this);
}
}
public Flag.Value<?> get(Flag flagType) { public Flag.Value<?> get(Flag flagType) {
return flagStorage.get(flagType); return flagStorage.get(flagType);
} }
public boolean get(Tag tagType) {
return flagStorage.is(tagType);
}
public <T extends Enum<T> & Flag.Value<T>> T getPlain(Flag flagType) { public <T extends Enum<T> & Flag.Value<T>> T getPlain(Flag flagType) {
return (T) flagStorage.get(flagType).getValue(); return (T) flagStorage.get(flagType).getValue();
} }
@ -448,4 +465,12 @@ public class Region {
} }
} }
public boolean backup() {
final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name);
//noinspection ResultOfMethodCallIgnored
definedBackupFolder.mkdirs();
final File backupFile = new File(definedBackupFolder, LocalDateTime.now().format(formatter) + ".schem");
//noinspection unchecked
return VersionedCallable.call(new VersionedCallable<>(() -> Region_15.backup(minPoint, maxPoint, backupFile), 15));
}
} }