From 0d1f3b855f8959f221bdcf005626a613449001c6 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 5 Feb 2017 09:58:00 +0000 Subject: [PATCH] Ignore commented lines on dump imports & add annotations to export files - closes #169 --- .../common/commands/misc/ExportCommand.java | 24 +++++++++++++++++++ .../lucko/luckperms/common/data/Importer.java | 9 ++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java index 22e0faf1b..d65d7a2d1 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/misc/ExportCommand.java @@ -43,12 +43,16 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; import java.util.Set; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; public class ExportCommand extends SingleCommand { + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); + private static void write(BufferedWriter writer, String s) { try { writer.write(s); @@ -94,50 +98,70 @@ public class ExportCommand extends SingleCommand { try (FileWriter fWriter = new FileWriter(f, true); BufferedWriter writer = new BufferedWriter(fWriter)) { log.log("Starting."); + write(writer, "# LuckPerms Export File"); + write(writer, "# Generated by " + sender.getName() + " at " + DATE_FORMAT.format(new Date(System.currentTimeMillis()))); + write(writer, ""); + // Export Groups log.log("Starting group export."); // Create the actual groups first + write(writer, "# Create groups"); for (Group group : plugin.getGroupManager().getAll().values()) { write(writer, "/luckperms creategroup " + group.getName()); } + write(writer, ""); AtomicInteger groupCount = new AtomicInteger(0); for (Group group : plugin.getGroupManager().getAll().values()) { + write(writer, "# Export group: " + group.getName()); for (Node node : group.getNodes()) { write(writer, NodeFactory.nodeAsCommand(node, group.getName(), true)); } + write(writer, ""); log.logAllProgress("Exported {} groups so far.", groupCount.incrementAndGet()); } log.log("Exported " + groupCount.get() + " groups."); + write(writer, ""); + write(writer, ""); + // Export tracks log.log("Starting track export."); // Create the actual tracks first + write(writer, "# Create tracks"); for (Track track : plugin.getTrackManager().getAll().values()) { write(writer, "/luckperms createtrack " + track.getName()); } AtomicInteger trackCount = new AtomicInteger(0); for (Track track : plugin.getTrackManager().getAll().values()) { + write(writer, "# Export track: " + track.getName()); for (String group : track.getGroups()) { write(writer, "/luckperms track " + track.getName() + " append " + group); } + write(writer, ""); log.logAllProgress("Exported {} tracks so far.", trackCount.incrementAndGet()); } log.log("Exported " + trackCount.get() + " tracks."); + write(writer, ""); + write(writer, ""); + // Export users log.log("Starting user export. Finding a list of unique users to export."); Storage ds = plugin.getStorage(); Set users = ds.getUniqueUsers().join(); log.log("Found " + users.size() + " unique users to export."); + write(writer, "# Export users"); + AtomicInteger userCount = new AtomicInteger(0); for (UUID uuid : users) { plugin.getStorage().loadUser(uuid, "null").join(); User user = plugin.getUserManager().get(uuid); + write(writer, "# Export user: " + user.getUuid().toString() + " - " + user.getName()); boolean inDefault = false; for (Node node : user.getNodes()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/data/Importer.java b/common/src/main/java/me/lucko/luckperms/common/data/Importer.java index 2cd2fef0e..811fe4419 100644 --- a/common/src/main/java/me/lucko/luckperms/common/data/Importer.java +++ b/common/src/main/java/me/lucko/luckperms/common/data/Importer.java @@ -75,9 +75,12 @@ public class Importer { public void start(Sender executor, List commands) { this.executor = executor; this.commands = commands.stream() - .map(s -> s.startsWith("/") ? s.substring(1) : s) - .map(s -> s.startsWith("perms ") ? s.substring(6) : s) - .map(s -> s.startsWith("luckperms ") ? s.substring(10) : s) + .filter(s -> !s.isEmpty()) + .filter(s -> !s.startsWith("#")) + .filter(s -> !s.startsWith("//")) + .map(s -> s.startsWith("/") ? s.substring("/".length()) : s) + .map(s -> s.startsWith("perms ") ? s.substring("perms ".length()) : s) + .map(s -> s.startsWith("luckperms ") ? s.substring("luckperms ".length()) : s) .collect(Collectors.toList()); cmdResult = new HashMap<>();