From 6436cacb04d8d27093e79bff4d2416a0018bd8ab Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 6 Jul 2021 11:34:02 +0200 Subject: [PATCH] Prototyping BackupCommand Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 7 ++ .../features/backup/BackupCommand.java | 90 +++++++++++++++++++ .../de/steamwar/bausystem/region/Region.java | 18 ++-- 3 files changed, 108 insertions(+), 7 deletions(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index 2eaea2da..b8c5755c 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -99,6 +99,13 @@ AUTOSTART_MESSAGE_RESULT1 = §eZeit §7bis zur §eExplosion §7am Gegner§8:§e AUTOSTART_MESSAGE_RESULT2 = §eZeitdifferenz in ticks §7bis 60 Sekunden§8:§e {0} AUTOSTART_MESSAGE_RESULT3 = §7Positiv, wenn zu wenig, negativ wenn zu viel +# Backup +BACKUP_REGION_NO_REGION=§cDu bist in keiner Region +BACKUP_NO_PERMS=§cDu darfst hier nicht das Backup System verwenden +BACKUP_SUCCESS=§7Das Backup wurde erstellt +BACKUP_FAILURE=§cDas Backup erstellen ist schiefgegangen +BACKUP_NO_CHANGE=§7Die Region hat keine Veränderung + # Bau BAU_COMMAND_HELP1 = §8/§ebau togglewe §8[§7Player§8] §8- §7Editiere die WorldEdit Rechte eines Spielers BAU_COMMAND_HELP2 = §8/§ebau toggleworld §8[§7Player§8] §8- §7Editiere die Welt Rechte eines Spielers diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java index 51bd8b0d..37263776 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java @@ -19,9 +19,21 @@ package de.steamwar.bausystem.features.backup; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.Permission; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.bausystem.region.Region; +import de.steamwar.bausystem.region.tags.Tag; import de.steamwar.command.SWCommand; +import de.steamwar.command.SWCommandUtils; +import de.steamwar.command.TypeMapper; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; @Linked(LinkageType.COMMAND) public class BackupCommand extends SWCommand { @@ -30,4 +42,82 @@ public class BackupCommand extends SWCommand { super("backup", "bu"); } + @Register(help = true) + public void genericHelp(Player p, String... args) { + BauSystem.MESSAGE.sendPrefixless("COMMAND_HELP_HEAD", p, "Backup"); + BauSystem.MESSAGE.sendPrefixless("DETONATOR_HELP_1", p); + } + + static boolean checkGlobalRegion(Region region, Player p) { + if (region.isGlobal()) { + BauSystem.MESSAGE.send("BACKUP_REGION_NO_REGION", p); + return true; + } + return false; + } + + private boolean permissionCheck(Player player, Permission permission) { + if (Permission.hasPermission(player, permission)) { + return true; + } + BauSystem.MESSAGE.send("BACKUP_NO_PERMS", player); + return false; + } + + @Register("create") + public void backupCreate(Player p) { + if (!permissionCheck(p, Permission.WORLDEDIT)) return; + Region region = Region.getRegion(p.getLocation()); + if (checkGlobalRegion(region, p)) { + return; + } + if (!region.get(Tag.CHANGED)) { + BauSystem.MESSAGE.send("BACKUP_NO_CHANGE", p); + return; + } + if (region.backup()) { + BauSystem.MESSAGE.send("BACKUP_SUCCESS", p); + } else { + BauSystem.MESSAGE.send("BACKUP_FAILURE", p); + } + } + + @Register("load") + public void backupLoad(Player p, @Mapper("backupName") String backupName) { + if (!permissionCheck(p, Permission.WORLDEDIT)) return; + Region region = Region.getRegion(p.getLocation()); + if (checkGlobalRegion(region, p)) { + return; + } + System.out.println(backupName); + } + + @Register("list") + public void backupList(Player p) { + Region region = Region.getRegion(p.getLocation()); + if (checkGlobalRegion(region, p)) { + return; + } + listbackup(p).forEach(s -> { + p.sendMessage("HERE: " + s); + }); + } + + @Mapper(value = "backupName", local = true) + public TypeMapper backupMapper() { + return SWCommandUtils.createMapper(s -> s, (commandSender, s) -> listbackup((Player) commandSender)); + } + + private List listbackup(Player p) { + Region region = Region.getRegion(p.getLocation()); + if (checkGlobalRegion(region, p)) { + return Collections.emptyList(); + } + try { + return region.listBackup().stream().map(s -> s.substring(0, s.length() - 6)).collect(Collectors.toList()); + } catch (NullPointerException e) { + return Collections.emptyList(); + } + } + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index 70956ff5..91e7c806 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -29,8 +29,6 @@ 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; @@ -42,10 +40,9 @@ import yapion.hierarchy.types.YAPIONValue; import java.io.File; import java.io.IOException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -57,7 +54,7 @@ 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"); + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss' 'dd.MM.yyyy"); public static Region getRegion(Location location) { return REGION_MAP.values().stream() @@ -473,4 +470,11 @@ public class Region { //noinspection unchecked return VersionedCallable.call(new VersionedCallable<>(() -> Region_15.backup(minPoint, maxPoint, backupFile), 15)); } + + public List listBackup() { + final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name); + //noinspection ResultOfMethodCallIgnored + definedBackupFolder.mkdirs(); + return Arrays.asList(Objects.requireNonNull(definedBackupFolder.list((dir, name) -> name.endsWith(".schem")))); + } } \ No newline at end of file