Fix some issues with the recent import/export changes

This commit is contained in:
Luck 2017-09-04 23:20:21 +01:00
parent 479bd91b74
commit 8faa4d137b
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 75 additions and 43 deletions

View File

@ -30,6 +30,7 @@ import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.locale.Message;
import me.lucko.luckperms.common.logging.ProgressLogger;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.Track;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.NodeFactory;
@ -54,6 +55,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Handles export operations
@ -102,31 +104,31 @@ public class Exporter implements Runnable {
AtomicInteger groupCount = new AtomicInteger(0);
plugin.getGroupManager().getAll().values().stream()
List<? extends Group> groups = plugin.getGroupManager().getAll().values().stream()
// export groups in order of weight
.sorted((o1, o2) -> {
int i = Integer.compare(o2.getWeight().orElse(0), o1.getWeight().orElse(0));
return i != 0 ? i : o1.getName().compareToIgnoreCase(o2.getName());
})
// create all groups initially
.peek(group -> {
if (!group.getName().equals("default")) {
write(writer, "/luckperms creategroup " + group.getName());
}
})
// then export the content of each group
.forEach(group -> {
if (groupCount.get() == 0) {
write(writer, "");
}
}).collect(Collectors.toList());
write(writer, "# Export group: " + group.getName());
for (Node node : group.getEnduringNodes().values()) {
write(writer, NodeFactory.nodeAsCommand(node, group.getName(), true, true));
}
write(writer, "");
log.logAllProgress("Exported {} groups so far.", groupCount.incrementAndGet());
});
for (Group group : groups) {
if (!group.getName().equals("default")) {
write(writer, "/luckperms creategroup " + group.getName());
}
}
for (Group group : groups) {
if (groupCount.get() == 0) {
write(writer, "");
}
write(writer, "# Export group: " + group.getName());
for (Node node : group.getEnduringNodes().values()) {
write(writer, NodeFactory.nodeAsCommand(node, group.getName(), true, true));
}
write(writer, "");
log.logAllProgress("Exported {} groups so far.", groupCount.incrementAndGet());
}
log.log("Exported " + groupCount.get() + " groups.");
@ -178,7 +180,7 @@ public class Exporter implements Runnable {
write(writer, "# Export users");
// divide into 16 pools.
Cycle<List<UUID>> userPools = new Cycle<>(Util.nInstances(16, ArrayList::new));
Cycle<List<UUID>> userPools = new Cycle<>(Util.nInstances(32, ArrayList::new));
for (UUID uuid : users) {
userPools.next().add(uuid);
}

View File

@ -36,7 +36,6 @@ import me.lucko.luckperms.common.commands.CommandResult;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.locale.Message;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.Cycle;
import java.util.ArrayList;
@ -89,12 +88,18 @@ public class Importer implements Runnable {
// form instances for all commands, and register them
int index = 1;
for (String command : commands) {
toExecute.add(new ImportCommand(commandManager.getPlugin(), index, command));
ImportCommand cmd = new ImportCommand(commandManager, index, command);
toExecute.add(cmd);
if (cmd.getCommand().startsWith("creategroup ") || cmd.getCommand().startsWith("createtrack")) {
cmd.process(); // process immediately
}
index++;
}
// divide commands up into pools
Cycle<List<ImportCommand>> commandPools = new Cycle<>(Util.nInstances(16, ArrayList::new));
Cycle<List<ImportCommand>> commandPools = new Cycle<>(Util.nInstances(128, ArrayList::new));
String lastTarget = null;
for (ImportCommand cmd : toExecute) {
@ -120,19 +125,7 @@ public class Importer implements Runnable {
// iterate through each user in the sublist, and grab their data.
for (ImportCommand cmd : subList) {
try {
CommandResult result = commandManager.onCommand(
cmd,
"lp",
Util.stripQuotes(Splitter.on(CommandManager.COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(cmd.getCommand()))
).get();
cmd.setResult(result);
} catch (Exception e) {
cmd.setResult(CommandResult.FAILURE);
e.printStackTrace();
}
cmd.process();
processedCount.incrementAndGet();
}
}, commandManager.getPlugin().getScheduler().async()));
@ -190,7 +183,7 @@ public class Importer implements Runnable {
private void sendProgress(int processedCount) {
int percent = (processedCount * 100) / commands.size();
int errors = (int) toExecute.stream().filter(v -> !v.getResult().asBoolean()).count();
int errors = (int) toExecute.stream().filter(v -> v.isCompleted() && !v.getResult().asBoolean()).count();
if (errors == 1) {
notify.forEach(s -> Message.IMPORT_PROGRESS_SIN.send(s, percent, processedCount, commands.size(), errors));
@ -203,18 +196,23 @@ public class Importer implements Runnable {
private static class ImportCommand extends ImporterSender {
private static final Splitter SPACE_SPLIT = Splitter.on(" ");
private final CommandManager commandManager;
private final int id;
private final String command;
private final String target;
@Setter
private boolean completed = false;
private final List<String> output = new ArrayList<>();
@Setter
private CommandResult result = CommandResult.FAILURE;
ImportCommand(LuckPermsPlugin plugin, int id, String command) {
super(plugin);
ImportCommand(CommandManager commandManager, int id, String command) {
super(commandManager.getPlugin());
this.commandManager = commandManager;
this.id = id;
this.command = command;
this.target = determineTarget(command);
@ -225,6 +223,27 @@ public class Importer implements Runnable {
output.add(s);
}
public void process() {
if (isCompleted()) {
return;
}
try {
CommandResult result = commandManager.onCommand(
this,
"lp",
Util.stripQuotes(Splitter.on(CommandManager.COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(getCommand()))
).get();
setResult(result);
} catch (Exception e) {
setResult(CommandResult.FAILURE);
e.printStackTrace();
}
setCompleted(true);
}
private static String determineTarget(String command) {
if (command.startsWith("user ") && command.length() > "user ".length()) {
String subCmd = command.substring("user ".length());
@ -256,6 +275,16 @@ public class Importer implements Runnable {
return "t:" + targetTrack;
}
if (command.startsWith("creategroup ") && command.length() > "creategroup ".length()) {
String targetGroup = command.substring("creategroup ".length());
return "g:" + targetGroup;
}
if (command.startsWith("createtrack ") && command.length() > "createtrack ".length()) {
String targetTrack = command.substring("createtrack ".length());
return "t:" + targetTrack;
}
return null;
}

View File

@ -392,8 +392,8 @@ public enum Message {
IMPORT_LOG_NOT_READABLE("Error: File {0} is not readable.", true),
IMPORT_LOG_FAILURE("An unexpected error occured whilst reading from the log file.", true),
IMPORT_PROGRESS("&b(Import) &b-> &f{0} &fpercent complete &7- &b{1}&f/&b{2} &foperations complete with &c{3} &ferrors.", true),
IMPORT_PROGRESS_SIN("&b(Import) &b-> &f{0} &fpercent complete &7- &b{1}&f/&b{2} &foperations complete with &c{3} &ferror.", true),
IMPORT_PROGRESS("&b(Import) &b-> &f{0}&f% complete &7- &b{1}&f/&b{2} &foperations complete with &c{3} &ferrors.", true),
IMPORT_PROGRESS_SIN("&b(Import) &b-> &f{0}&f% complete &7- &b{1}&f/&b{2} &foperations complete with &c{3} &ferror.", true),
IMPORT_START("&b(Import) &b-> &fStarting import process.", true),
IMPORT_END_COMPLETE("&b(Import) &a&lCOMPLETED &7- took &b{0} &7seconds - &7No errors.", true),

View File

@ -55,6 +55,7 @@ import java.util.stream.Collectors;
*/
@ToString(of = {"permission", "value", "override", "server", "world", "expireAt", "contexts"})
public final class ImmutableNode implements Node {
private static final int NODE_SEPARATOR_CHAR = Character.getNumericValue('.');
@Getter
private final String permission;
@ -149,7 +150,7 @@ public final class ImmutableNode implements Node {
}
isWildcard = this.permission.endsWith(".*");
wildcardLevel = (int) this.permission.chars().filter(num -> num == Character.getNumericValue('.')).count();
wildcardLevel = this.permission.chars().filter(num -> num == NODE_SEPARATOR_CHAR).sum();
isMeta = NodeFactory.isMetaNode(this.permission);
if (isMeta) {