From 71f784451fe6d477fd733d18b70fc3183bdf5a52 Mon Sep 17 00:00:00 2001 From: Zeanon Date: Mon, 5 Jul 2021 19:32:10 +0200 Subject: [PATCH] First impl of BackupScheduler --- .../steamwar/bausystem/region/Region_15.java | 30 +++++++++ .../bausystem/region/BackupScheduler.java | 61 +++++++++++++++++++ .../de/steamwar/bausystem/region/Region.java | 25 ++++++++ 3 files changed, 116 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/region/BackupScheduler.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/region/Region_15.java b/BauSystem_15/src/de/steamwar/bausystem/region/Region_15.java index f9a1e19e..3e7ca830 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/region/Region_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/region/Region_15.java @@ -23,9 +23,13 @@ import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; 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.io.BuiltInClipboardFormat; import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; 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.math.BlockVector3; 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 java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.util.Objects; +import java.util.logging.Level; import lombok.experimental.UtilityClass; 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; + } + } } \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/BackupScheduler.java b/BauSystem_Main/src/de/steamwar/bausystem/region/BackupScheduler.java new file mode 100644 index 00000000..432f156d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/BackupScheduler.java @@ -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 . + */ + +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 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 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); + } +} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index 5bce6af0..70956ff5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -27,10 +27,14 @@ import de.steamwar.bausystem.region.tags.Tag; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; import de.steamwar.bausystem.shared.SizedStack; +import de.steamwar.core.VersionedCallable; import de.steamwar.sql.Schematic; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import lombok.AccessLevel; import lombok.Getter; import lombok.NonNull; +import org.bukkit.Bukkit; import org.bukkit.Location; import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; @@ -50,7 +54,10 @@ import static de.steamwar.bausystem.region.RegionUtils.paste; @Getter public class Region { + @Getter private static final Map 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) { return REGION_MAP.values().stream() @@ -289,10 +296,20 @@ public class Region { setLinkedRegion(region -> region.flagStorage.set(tag)); } + public void remove(Tag tag) { + if (flagStorage.remove(tag)) { + RegionUtils.save(this); + } + } + public Flag.Value get(Flag flagType) { return flagStorage.get(flagType); } + public boolean get(Tag tagType) { + return flagStorage.is(tagType); + } + public & Flag.Value> T getPlain(Flag flagType) { 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)); + } } \ No newline at end of file