Add import/export support
This commit is contained in:
@@ -33,6 +33,7 @@ import me.lucko.luckperms.commands.group.GroupMainCommand;
|
||||
import me.lucko.luckperms.commands.group.ListGroups;
|
||||
import me.lucko.luckperms.commands.log.LogMainCommand;
|
||||
import me.lucko.luckperms.commands.misc.DebugCommand;
|
||||
import me.lucko.luckperms.commands.misc.ImportCommand;
|
||||
import me.lucko.luckperms.commands.misc.InfoCommand;
|
||||
import me.lucko.luckperms.commands.misc.SyncCommand;
|
||||
import me.lucko.luckperms.commands.track.CreateTrack;
|
||||
@@ -61,6 +62,7 @@ public class CommandManager {
|
||||
.add(new SyncCommand())
|
||||
.add(new InfoCommand())
|
||||
.add(new DebugCommand())
|
||||
.add(new ImportCommand())
|
||||
.add(new CreateGroup())
|
||||
.add(new DeleteGroup())
|
||||
.add(new ListGroups())
|
||||
|
||||
@@ -147,36 +147,30 @@ public abstract class SubCommand<T> {
|
||||
protected static void save(User user, Sender sender, LuckPermsPlugin plugin) {
|
||||
user.refreshPermissions();
|
||||
|
||||
plugin.getDatastore().saveUser(user, success -> {
|
||||
if (success) {
|
||||
Message.USER_SAVE_SUCCESS.send(sender);
|
||||
} else {
|
||||
Message.USER_SAVE_ERROR.send(sender);
|
||||
}
|
||||
});
|
||||
if (plugin.getDatastore().saveUser(user)) {
|
||||
Message.USER_SAVE_SUCCESS.send(sender);
|
||||
} else {
|
||||
Message.USER_SAVE_ERROR.send(sender);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void save(Group group, Sender sender, LuckPermsPlugin plugin) {
|
||||
plugin.getDatastore().saveGroup(group, success -> {
|
||||
if (success) {
|
||||
Message.GROUP_SAVE_SUCCESS.send(sender);
|
||||
} else {
|
||||
Message.GROUP_SAVE_ERROR.send(sender);
|
||||
}
|
||||
if (plugin.getDatastore().saveGroup(group)) {
|
||||
Message.GROUP_SAVE_SUCCESS.send(sender);
|
||||
} else {
|
||||
Message.GROUP_SAVE_ERROR.send(sender);
|
||||
}
|
||||
|
||||
plugin.runUpdateTask();
|
||||
});
|
||||
plugin.runUpdateTask();
|
||||
}
|
||||
|
||||
protected static void save(Track track, Sender sender, LuckPermsPlugin plugin) {
|
||||
plugin.getDatastore().saveTrack(track, success -> {
|
||||
if (success) {
|
||||
Message.TRACK_SAVE_SUCCESS.send(sender);
|
||||
} else {
|
||||
Message.TRACK_SAVE_ERROR.send(sender);
|
||||
}
|
||||
if (plugin.getDatastore().saveTrack(track)) {
|
||||
Message.TRACK_SAVE_SUCCESS.send(sender);
|
||||
} else {
|
||||
Message.TRACK_SAVE_ERROR.send(sender);
|
||||
}
|
||||
|
||||
plugin.runUpdateTask();
|
||||
});
|
||||
plugin.runUpdateTask();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class LogMainCommand extends MainCommand<Log> {
|
||||
.add(new LogRecent())
|
||||
.add(new LogSearch())
|
||||
.add(new LogNotify())
|
||||
// .add(new LogExport())
|
||||
.add(new LogExport())
|
||||
.add(new LogUserHistory())
|
||||
.add(new LogGroupHistory())
|
||||
.add(new LogTrackHistory())
|
||||
|
||||
@@ -23,13 +23,20 @@
|
||||
package me.lucko.luckperms.commands.log.subcommands;
|
||||
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.api.LogEntry;
|
||||
import me.lucko.luckperms.commands.CommandResult;
|
||||
import me.lucko.luckperms.commands.Predicate;
|
||||
import me.lucko.luckperms.commands.Sender;
|
||||
import me.lucko.luckperms.commands.SubCommand;
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.constants.Permission;
|
||||
import me.lucko.luckperms.data.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LogExport extends SubCommand<Log> {
|
||||
@@ -39,7 +46,83 @@ public class LogExport extends SubCommand<Log> {
|
||||
|
||||
@Override
|
||||
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Log log, List<String> args, String label) {
|
||||
// TODO: implement this
|
||||
return CommandResult.SUCCESS;
|
||||
File f = new File(plugin.getMainDir(), args.get(0));
|
||||
if (f.exists()) {
|
||||
Message.LOG_EXPORT_ALREADY_EXISTS.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.INVALID_ARGS;
|
||||
}
|
||||
|
||||
if (log.getContent().isEmpty()) {
|
||||
Message.LOG_EXPORT_EMPTY.send(sender);
|
||||
return CommandResult.STATE_ERROR;
|
||||
}
|
||||
|
||||
try {
|
||||
f.createNewFile();
|
||||
} catch (IOException e) {
|
||||
Message.LOG_EXPORT_FAILURE.send(sender);
|
||||
e.printStackTrace();
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
|
||||
if (!Files.isWritable(f.toPath())) {
|
||||
Message.LOG_EXPORT_NOT_WRITABLE.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
|
||||
List<String> data = new ArrayList<>();
|
||||
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (LogEntry e : log.getContent()) {
|
||||
b.setLength(0);
|
||||
b.append("/luckperms ");
|
||||
|
||||
if (e.getType() == 'U') {
|
||||
b.append("user ").append(e.getActed().toString()).append(" ").append(e.getAction());
|
||||
}
|
||||
|
||||
group:
|
||||
if (e.getType() == 'G') {
|
||||
if (e.getAction().equalsIgnoreCase("create")) {
|
||||
b.append("creategroup ").append(e.getActedName());
|
||||
break group;
|
||||
}
|
||||
|
||||
if (e.getAction().equalsIgnoreCase("delete")) {
|
||||
b.append("deletegroup ").append(e.getActedName());
|
||||
break group;
|
||||
}
|
||||
|
||||
b.append("group ").append(e.getActedName()).append(" ").append(e.getAction());
|
||||
}
|
||||
|
||||
track:
|
||||
if (e.getType() == 'T') {
|
||||
if (e.getAction().equalsIgnoreCase("create")) {
|
||||
b.append("createtrack ").append(e.getActedName());
|
||||
break track;
|
||||
}
|
||||
|
||||
if (e.getAction().equalsIgnoreCase("delete")) {
|
||||
b.append("deletetrack ").append(e.getActedName());
|
||||
break track;
|
||||
}
|
||||
|
||||
b.append("track ").append(e.getActedName()).append(" ").append(e.getAction());;
|
||||
}
|
||||
|
||||
data.add(b.toString());
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
Files.write(f.toPath(), data, Charset.defaultCharset());
|
||||
Message.LOG_EXPORT_SUCCESS.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.SUCCESS;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Message.LOG_EXPORT_FAILURE.send(sender);
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.commands.misc;
|
||||
|
||||
import me.lucko.luckperms.LuckPermsPlugin;
|
||||
import me.lucko.luckperms.commands.CommandResult;
|
||||
import me.lucko.luckperms.commands.Sender;
|
||||
import me.lucko.luckperms.commands.SingleMainCommand;
|
||||
import me.lucko.luckperms.constants.Message;
|
||||
import me.lucko.luckperms.constants.Permission;
|
||||
import me.lucko.luckperms.data.Importer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
public class ImportCommand extends SingleMainCommand {
|
||||
public ImportCommand() {
|
||||
super("Import", "/%s import <file>", 1, Permission.IMPORT);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandResult execute(LuckPermsPlugin plugin, Sender sender, List<String> args, String label) {
|
||||
if (args.size() == 0) {
|
||||
sendUsage(sender, label);
|
||||
return CommandResult.INVALID_ARGS;
|
||||
}
|
||||
|
||||
Importer importer = plugin.getImporter();
|
||||
|
||||
File f = new File(plugin.getMainDir(), args.get(0));
|
||||
if (!f.exists()) {
|
||||
Message.IMPORT_LOG_DOESNT_EXIST.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.INVALID_ARGS;
|
||||
}
|
||||
|
||||
if (!Files.isReadable(f.toPath())) {
|
||||
Message.IMPORT_LOG_NOT_READABLE.send(sender, f.getAbsolutePath());
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
|
||||
List<String> commands;
|
||||
|
||||
try {
|
||||
commands = Files.readAllLines(f.toPath(), Charset.defaultCharset());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Message.IMPORT_LOG_FAILURE.send(sender);
|
||||
return CommandResult.FAILURE;
|
||||
}
|
||||
|
||||
if (!importer.startRun()) {
|
||||
Message.IMPORT_ALREADY_RUNNING.send(sender);
|
||||
return CommandResult.STATE_ERROR;
|
||||
}
|
||||
|
||||
// Run the importer in its own thread.
|
||||
plugin.doAsync(() -> importer.start(sender, commands));
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user