From 1cb289817384870716ec8cd83649c6958c228266 Mon Sep 17 00:00:00 2001 From: Szum123321 Date: Fri, 3 Apr 2020 11:26:14 +0200 Subject: [PATCH] Added option to not backup files. --- .../szum123321/textile_backup/ConfigHandler.java | 8 ++++++-- .../szum123321/textile_backup/TextileBackup.java | 4 ++-- .../textile_backup/commands/BlacklistCommand.java | 14 +++++++------- .../textile_backup/commands/WhitelistCommand.java | 14 +++++++------- .../szum123321/textile_backup/core/Compressor.java | 4 +++- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/ConfigHandler.java b/src/main/java/net/szum123321/textile_backup/ConfigHandler.java index 29f0c69..c9ca13c 100644 --- a/src/main/java/net/szum123321/textile_backup/ConfigHandler.java +++ b/src/main/java/net/szum123321/textile_backup/ConfigHandler.java @@ -38,6 +38,10 @@ public class ConfigHandler { @Comment("\nA path to backup folder\n") public String path = "backup/"; + @Comment("\nThis setting allows you to exclude files form being backuped.\n"+ + "Be very careful when setting it, as it is easy to make your backuped world unusable!\n") + public Set fileBlacklist = new HashSet<>(); + @Comment("\nShould every world has its won backup folder?\n") public boolean perWorldBackup = false; @@ -63,10 +67,10 @@ public class ConfigHandler { public boolean alwaysSingleplayerAllowed = true; @Comment("\nPlayers allowed to run backup commands without sufficient permission level\n") - public Set whitelist = new HashSet<>(); + public Set playerWhitelist = new HashSet<>(); @Comment("\nPlayers banned from running backup commands besides their sufficient permission level\n") - public Set blacklist = new HashSet<>(); + public Set playerBlocklist = new HashSet<>(); @Comment("\nFormat of date&time used to name backup files.\n") public String dateTimeFormat = "dd.MM.yyyy_HH-mm-ss"; diff --git a/src/main/java/net/szum123321/textile_backup/TextileBackup.java b/src/main/java/net/szum123321/textile_backup/TextileBackup.java index 636faa2..ff0b631 100644 --- a/src/main/java/net/szum123321/textile_backup/TextileBackup.java +++ b/src/main/java/net/szum123321/textile_backup/TextileBackup.java @@ -52,9 +52,9 @@ public class TextileBackup implements ModInitializer { LiteralArgumentBuilder.literal("backup") .requires((ctx) -> { try { - return ((config.whitelist.contains(ctx.getEntityOrThrow().getEntityName()) || + return ((config.playerWhitelist.contains(ctx.getEntityOrThrow().getEntityName()) || ctx.hasPermissionLevel(config.permissionLevel)) && - !config.blacklist.contains(ctx.getEntityOrThrow().getEntityName())) || + !config.playerBlocklist.contains(ctx.getEntityOrThrow().getEntityName())) || (ctx.getMinecraftServer().isSinglePlayer() && config.alwaysSingleplayerAllowed); }catch (Exception e){ //Command was called from server console. diff --git a/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java b/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java index d5bf80d..a9a3521 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/BlacklistCommand.java @@ -40,7 +40,7 @@ public class BlacklistCommand { builder.append("Currently on the blacklist are: "); - for(String name : TextileBackup.config.blacklist){ + for(String name : TextileBackup.config.playerBlocklist){ builder.append(name); builder.append(", "); } @@ -53,10 +53,10 @@ public class BlacklistCommand { private static int executeAdd(CommandContext ctx) throws CommandSyntaxException { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); - if(TextileBackup.config.blacklist.contains(player.getEntityName())) { + if(TextileBackup.config.playerBlocklist.contains(player.getEntityName())) { ctx.getSource().sendFeedback(new TranslatableText("Player: %s is already blacklisted.", player.getEntityName()), false); }else{ - TextileBackup.config.blacklist.add(player.getEntityName()); + TextileBackup.config.playerBlocklist.add(player.getEntityName()); ConfigManager.saveConfig(TextileBackup.config); StringBuilder builder = new StringBuilder(); @@ -65,8 +65,8 @@ public class BlacklistCommand { builder.append(player.getEntityName()); builder.append(" added to the blacklist"); - if(TextileBackup.config.whitelist.contains(player.getEntityName())){ - TextileBackup.config.whitelist.remove(player.getEntityName()); + if(TextileBackup.config.playerWhitelist.contains(player.getEntityName())){ + TextileBackup.config.playerWhitelist.remove(player.getEntityName()); builder.append(" and removed form the whitelist"); } @@ -81,10 +81,10 @@ public class BlacklistCommand { private static int executeRemove(CommandContext ctx) throws CommandSyntaxException { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); - if(!TextileBackup.config.blacklist.contains(player.getEntityName())) { + if(!TextileBackup.config.playerBlocklist.contains(player.getEntityName())) { ctx.getSource().sendFeedback(new TranslatableText("Player: %s newer was blacklisted.", player.getEntityName()), false); }else{ - TextileBackup.config.blacklist.remove(player.getEntityName()); + TextileBackup.config.playerBlocklist.remove(player.getEntityName()); ConfigManager.saveConfig(TextileBackup.config); StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java b/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java index 2d8fde0..83947e1 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/WhitelistCommand.java @@ -40,7 +40,7 @@ public class WhitelistCommand { builder.append("Currently on the whitelist are: "); - for(String name : TextileBackup.config.whitelist){ + for(String name : TextileBackup.config.playerWhitelist){ builder.append(name); builder.append(", "); } @@ -53,10 +53,10 @@ public class WhitelistCommand { private static int executeAdd(CommandContext ctx) throws CommandSyntaxException { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); - if(TextileBackup.config.whitelist.contains(player.getEntityName())) { + if(TextileBackup.config.playerWhitelist.contains(player.getEntityName())) { ctx.getSource().sendFeedback(new TranslatableText("Player: %s is already whitelisted.", player.getEntityName()), false); }else{ - TextileBackup.config.whitelist.add(player.getEntityName()); + TextileBackup.config.playerWhitelist.add(player.getEntityName()); ConfigManager.saveConfig(TextileBackup.config); StringBuilder builder = new StringBuilder(); @@ -65,8 +65,8 @@ public class WhitelistCommand { builder.append(player.getEntityName()); builder.append(" added to the whitelist"); - if(TextileBackup.config.blacklist.contains(player.getEntityName())){ - TextileBackup.config.blacklist.remove(player.getEntityName()); + if(TextileBackup.config.playerBlocklist.contains(player.getEntityName())){ + TextileBackup.config.playerBlocklist.remove(player.getEntityName()); builder.append(" and removed form the blacklist"); } @@ -81,10 +81,10 @@ public class WhitelistCommand { private static int executeRemove(CommandContext ctx) throws CommandSyntaxException { PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player"); - if(!TextileBackup.config.whitelist.contains(player.getEntityName())) { + if(!TextileBackup.config.playerWhitelist.contains(player.getEntityName())) { ctx.getSource().sendFeedback(new TranslatableText("Player: %s newer was on the whitelist.", player.getEntityName()), false); }else{ - TextileBackup.config.whitelist.remove(player.getEntityName()); + TextileBackup.config.playerWhitelist.remove(player.getEntityName()); ConfigManager.saveConfig(TextileBackup.config); StringBuilder builder = new StringBuilder(); diff --git a/src/main/java/net/szum123321/textile_backup/core/Compressor.java b/src/main/java/net/szum123321/textile_backup/core/Compressor.java index f4f4031..663e54b 100644 --- a/src/main/java/net/szum123321/textile_backup/core/Compressor.java +++ b/src/main/java/net/szum123321/textile_backup/core/Compressor.java @@ -27,6 +27,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; +import java.time.LocalDateTime; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -40,10 +41,11 @@ public class Compressor { ZipOutputStream arc = new ZipOutputStream(new FileOutputStream(out)); arc.setLevel(TextileBackup.config.compression); + arc.setComment("Created on: " + Utilities.getDateTimeFormatter().format(LocalDateTime.now())); int rootPathLength = input.toString().length() + 1; - Files.walk(input.toPath()).filter(path -> !path.equals(input.toPath()) && path.toFile().isFile()).forEach(path -> { + Files.walk(input.toPath()).filter(path -> !path.equals(input.toPath()) && path.toFile().isFile() && !TextileBackup.config.fileBlacklist.contains(path.toString().substring(rootPathLength))).forEach(path -> { try{ File file = path.toAbsolutePath().toFile();