refactor: minor userdata dump refactor

feat/data-edit-commands
William 5 months ago
parent 2bdd3dae37
commit a8ca3314d8
No known key found for this signature in database

@ -60,34 +60,34 @@ public class UserDataCommand extends PluginCommand {
// Show the latest snapshot // Show the latest snapshot
private void viewLatestSnapshot(@NotNull CommandUser executor, @NotNull User user) { private void viewLatestSnapshot(@NotNull CommandUser executor, @NotNull User user) {
plugin.getDatabase().getLatestSnapshot(user).ifPresentOrElse( plugin.getDatabase().getLatestSnapshot(user).ifPresentOrElse(
data -> { data -> {
if (data.isInvalid()) { if (data.isInvalid()) {
plugin.getLocales().getLocale("error_invalid_data", data.getInvalidReason(plugin)) plugin.getLocales().getLocale("error_invalid_data", data.getInvalidReason(plugin))
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
DataSnapshotOverview.of(data.unpack(plugin), data.getFileSize(plugin), user, plugin) DataSnapshotOverview.of(data.unpack(plugin), data.getFileSize(plugin), user, plugin)
.show(executor); .show(executor);
}, },
() -> plugin.getLocales().getLocale("error_no_data_to_display") () -> plugin.getLocales().getLocale("error_no_data_to_display")
.ifPresent(executor::sendMessage) .ifPresent(executor::sendMessage)
); );
} }
// Show the specified snapshot // Show the specified snapshot
private void viewSnapshot(@NotNull CommandUser executor, @NotNull User user, @NotNull UUID version) { private void viewSnapshot(@NotNull CommandUser executor, @NotNull User user, @NotNull UUID version) {
plugin.getDatabase().getSnapshot(user, version).ifPresentOrElse( plugin.getDatabase().getSnapshot(user, version).ifPresentOrElse(
data -> { data -> {
if (data.isInvalid()) { if (data.isInvalid()) {
plugin.getLocales().getLocale("error_invalid_data", data.getInvalidReason(plugin)) plugin.getLocales().getLocale("error_invalid_data", data.getInvalidReason(plugin))
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
DataSnapshotOverview.of(data.unpack(plugin), data.getFileSize(plugin), user, plugin) DataSnapshotOverview.of(data.unpack(plugin), data.getFileSize(plugin), user, plugin)
.show(executor); .show(executor);
}, },
() -> plugin.getLocales().getLocale("error_invalid_version_uuid") () -> plugin.getLocales().getLocale("error_invalid_version_uuid")
.ifPresent(executor::sendMessage) .ifPresent(executor::sendMessage)
); );
} }
@ -96,7 +96,7 @@ public class UserDataCommand extends PluginCommand {
final List<DataSnapshot.Packed> dataList = plugin.getDatabase().getAllSnapshots(user); final List<DataSnapshot.Packed> dataList = plugin.getDatabase().getAllSnapshots(user);
if (dataList.isEmpty()) { if (dataList.isEmpty()) {
plugin.getLocales().getLocale("error_no_data_to_display") plugin.getLocales().getLocale("error_no_data_to_display")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
DataSnapshotList.create(dataList, user, plugin).displayPage(executor, page); DataSnapshotList.create(dataList, user, plugin).displayPage(executor, page);
@ -106,16 +106,16 @@ public class UserDataCommand extends PluginCommand {
private void deleteSnapshot(@NotNull CommandUser executor, @NotNull User user, @NotNull UUID version) { private void deleteSnapshot(@NotNull CommandUser executor, @NotNull User user, @NotNull UUID version) {
if (!plugin.getDatabase().deleteSnapshot(user, version)) { if (!plugin.getDatabase().deleteSnapshot(user, version)) {
plugin.getLocales().getLocale("error_invalid_version_uuid") plugin.getLocales().getLocale("error_invalid_version_uuid")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
plugin.getRedisManager().clearUserData(user); plugin.getRedisManager().clearUserData(user);
plugin.getLocales().getLocale("data_deleted", plugin.getLocales().getLocale("data_deleted",
version.toString().split("-")[0], version.toString().split("-")[0],
version.toString(), version.toString(),
user.getUsername(), user.getUsername(),
user.getUuid().toString()) user.getUuid().toString())
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
} }
// Restore a snapshot // Restore a snapshot
@ -123,7 +123,7 @@ public class UserDataCommand extends PluginCommand {
final Optional<DataSnapshot.Packed> optionalData = plugin.getDatabase().getSnapshot(user, version); final Optional<DataSnapshot.Packed> optionalData = plugin.getDatabase().getSnapshot(user, version);
if (optionalData.isEmpty()) { if (optionalData.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_version_uuid") plugin.getLocales().getLocale("error_invalid_version_uuid")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
@ -131,14 +131,14 @@ public class UserDataCommand extends PluginCommand {
final DataSnapshot.Packed data = optionalData.get().copy(); final DataSnapshot.Packed data = optionalData.get().copy();
if (data.isInvalid()) { if (data.isInvalid()) {
plugin.getLocales().getLocale("error_invalid_data", data.getInvalidReason(plugin)) plugin.getLocales().getLocale("error_invalid_data", data.getInvalidReason(plugin))
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
data.edit(plugin, (unpacked -> { data.edit(plugin, (unpacked -> {
unpacked.getHealth().ifPresent(status -> status.setHealth(Math.max(1, status.getHealth()))); unpacked.getHealth().ifPresent(status -> status.setHealth(Math.max(1, status.getHealth())));
unpacked.setSaveCause(DataSnapshot.SaveCause.BACKUP_RESTORE); unpacked.setSaveCause(DataSnapshot.SaveCause.BACKUP_RESTORE);
unpacked.setPinned( unpacked.setPinned(
plugin.getSettings().getSynchronization().doAutoPin(DataSnapshot.SaveCause.BACKUP_RESTORE) plugin.getSettings().getSynchronization().doAutoPin(DataSnapshot.SaveCause.BACKUP_RESTORE)
); );
})); }));
@ -148,7 +148,7 @@ public class UserDataCommand extends PluginCommand {
redis.getUserData(u).ifPresent(d -> redis.setUserData(u, s, RedisKeyType.TTL_1_YEAR)); redis.getUserData(u).ifPresent(d -> redis.setUserData(u, s, RedisKeyType.TTL_1_YEAR));
redis.sendUserDataUpdate(u, s); redis.sendUserDataUpdate(u, s);
plugin.getLocales().getLocale("data_restored", u.getUsername(), u.getUuid().toString(), plugin.getLocales().getLocale("data_restored", u.getUsername(), u.getUuid().toString(),
s.getShortId(), s.getId().toString()).ifPresent(executor::sendMessage); s.getShortId(), s.getId().toString()).ifPresent(executor::sendMessage);
}); });
} }
@ -157,7 +157,7 @@ public class UserDataCommand extends PluginCommand {
final Optional<DataSnapshot.Packed> optionalData = plugin.getDatabase().getSnapshot(user, version); final Optional<DataSnapshot.Packed> optionalData = plugin.getDatabase().getSnapshot(user, version);
if (optionalData.isEmpty()) { if (optionalData.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_version_uuid") plugin.getLocales().getLocale("error_invalid_version_uuid")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
@ -169,16 +169,17 @@ public class UserDataCommand extends PluginCommand {
plugin.getDatabase().pinSnapshot(user, data.getId()); plugin.getDatabase().pinSnapshot(user, data.getId());
} }
plugin.getLocales().getLocale(data.isPinned() ? "data_unpinned" : "data_pinned", data.getShortId(), plugin.getLocales().getLocale(data.isPinned() ? "data_unpinned" : "data_pinned", data.getShortId(),
data.getId().toString(), user.getUsername(), user.getUuid().toString()) data.getId().toString(), user.getUsername(), user.getUuid().toString())
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
} }
// Dump a snapshot // Dump a snapshot
private void dumpSnapshot(@NotNull CommandUser executor, @NotNull User user, @NotNull UUID version, boolean webDump) { private void dumpSnapshot(@NotNull CommandUser executor, @NotNull User user, @NotNull UUID version,
@NotNull DumpType type) {
final Optional<DataSnapshot.Packed> data = plugin.getDatabase().getSnapshot(user, version); final Optional<DataSnapshot.Packed> data = plugin.getDatabase().getSnapshot(user, version);
if (data.isEmpty()) { if (data.isEmpty()) {
plugin.getLocales().getLocale("error_invalid_version_uuid") plugin.getLocales().getLocale("error_invalid_version_uuid")
.ifPresent(executor::sendMessage); .ifPresent(executor::sendMessage);
return; return;
} }
@ -187,7 +188,8 @@ public class UserDataCommand extends PluginCommand {
final DataDumper dumper = DataDumper.create(userData, user, plugin); final DataDumper dumper = DataDumper.create(userData, user, plugin);
try { try {
plugin.getLocales().getLocale("data_dumped", userData.getShortId(), user.getUsername(), plugin.getLocales().getLocale("data_dumped", userData.getShortId(), user.getUsername(),
(webDump ? dumper.toWeb() : dumper.toFile())).ifPresent(executor::sendMessage); (type == DumpType.WEB ? dumper.toWeb() : dumper.toFile()))
.ifPresent(executor::sendMessage);
} catch (Throwable e) { } catch (Throwable e) {
plugin.log(Level.SEVERE, "Failed to dump user data", e); plugin.log(Level.SEVERE, "Failed to dump user data", e);
} }
@ -256,7 +258,7 @@ public class UserDataCommand extends PluginCommand {
final User user = ctx.getArgument("username", User.class); final User user = ctx.getArgument("username", User.class);
final UUID version = ctx.getArgument("version", UUID.class); final UUID version = ctx.getArgument("version", UUID.class);
final DumpType type = ctx.getArgument("type", DumpType.class); final DumpType type = ctx.getArgument("type", DumpType.class);
dumpSnapshot(user(sub, ctx), user, version, type == DumpType.WEB); dumpSnapshot(user(sub, ctx), user, version, type);
}, user("username"), uuid("version"), dumpType()); }, user("username"), uuid("version"), dumpType());
} }
@ -267,7 +269,7 @@ public class UserDataCommand extends PluginCommand {
case "web" -> DumpType.WEB; case "web" -> DumpType.WEB;
case "file" -> DumpType.FILE; case "file" -> DumpType.FILE;
default -> throw CommandSyntaxException.BUILT_IN_EXCEPTIONS default -> throw CommandSyntaxException.BUILT_IN_EXCEPTIONS
.dispatcherUnknownArgument().createWithContext(reader); .dispatcherUnknownArgument().createWithContext(reader);
}; };
}, (context, builder) -> { }, (context, builder) -> {
builder.suggest("web"); builder.suggest("web");

Loading…
Cancel
Save