diff --git a/src/main/kotlin/Sergey_Dertan/SVault/command/MigrateCommand.kt b/src/main/kotlin/Sergey_Dertan/SVault/command/MigrateCommand.kt new file mode 100644 index 0000000..9fd9632 --- /dev/null +++ b/src/main/kotlin/Sergey_Dertan/SVault/command/MigrateCommand.kt @@ -0,0 +1,55 @@ +package Sergey_Dertan.SVault.command + +import Sergey_Dertan.SVault.main.SVaultMain +import Sergey_Dertan.SVault.provider.DataProvider +import Sergey_Dertan.SVault.utils.DataProviderException +import cn.nukkit.command.CommandSender +import cn.nukkit.command.data.CommandParameter + +class MigrateCommand(private val main: SVaultMain) : SVaultCommand("migrate", main.vaultManager) { + + init { + val providers = mutableListOf() + DataProvider.Type.values().forEach { providers.add(it.name.toLowerCase()) } + providers.remove(DataProvider.Type.UNKNOWN.name.toLowerCase()) + + this.commandParameters = mapOf( + Pair("from-provider", arrayOf(CommandParameter("from", false, providers.toTypedArray()))), + Pair("to-provider", arrayOf(CommandParameter("to", false, providers.toTypedArray()))) + ) + } + + override fun execute(sender: CommandSender, s: String, args: Array): Boolean { + if (!this.testPermissionSilent(sender)) { + this.messenger.sendMessage(sender, "command.migrate.permission") + return false + } + if (args.size < 2) { + this.messenger.sendMessage(sender, "command.migrate.usage") + return false + } + val fr = DataProvider.Type.fromString(args[0]) //from + val tt = DataProvider.Type.fromString(args[1]) //to + if (fr == DataProvider.Type.UNKNOWN || tt == DataProvider.Type.UNKNOWN) { + this.messenger.sendMessage(sender, "command.migrate.unknown-provider") + return false + } + if (fr == tt) { + this.messenger.sendMessage(sender, "command.migrate.same-provider") + return false + } + val from: DataProvider + val to: DataProvider + try { + from = this.main.getProviderInstance(fr) + to = this.main.getProviderInstance(tt) + } catch (e: DataProviderException) { + this.messenger.sendMessage(sender, "command.migrate.error", "@provider", e.provider.name) + return false + } + val list = from.loadVaultList() + to.saveVaultList(list) + this.messenger.sendMessage(sender, "command.migrate.success", "@amount", list.size.toString()) + return true + } +} \ No newline at end of file diff --git a/src/main/kotlin/Sergey_Dertan/SVault/command/VaultListCommand.kt b/src/main/kotlin/Sergey_Dertan/SVault/command/VaultListCommand.kt index eb9ea6f..0716793 100644 --- a/src/main/kotlin/Sergey_Dertan/SVault/command/VaultListCommand.kt +++ b/src/main/kotlin/Sergey_Dertan/SVault/command/VaultListCommand.kt @@ -30,7 +30,7 @@ class VaultListCommand(vaultManager: VaultManager) : SVaultCommand("list", vault this.messenger.sendMessage(sender, "command.list.permission") return false } - this.messenger.sendMessage(sender, "command.list.list.other", arrayOf("@player", "@list"), arrayOf(args[0], this.vaultManager.getVaultList(args[0]).joinToString { "$it," })) + this.messenger.sendMessage(sender, "command.list.list.other", arrayOf("@player", "@list"), arrayOf(args[0], this.vaultManager.getVaultList(args[0]).joinToString())) } return true } diff --git a/src/main/kotlin/Sergey_Dertan/SVault/main/SVaultMain.kt b/src/main/kotlin/Sergey_Dertan/SVault/main/SVaultMain.kt index d154c96..fabcc62 100644 --- a/src/main/kotlin/Sergey_Dertan/SVault/main/SVaultMain.kt +++ b/src/main/kotlin/Sergey_Dertan/SVault/main/SVaultMain.kt @@ -10,6 +10,7 @@ import Sergey_Dertan.SVault.provider.database.MySQLDataProvider import Sergey_Dertan.SVault.provider.database.PostgreSQLDataProvider import Sergey_Dertan.SVault.provider.database.SQLiteDataProvider import Sergey_Dertan.SVault.settings.Settings +import Sergey_Dertan.SVault.utils.DataProviderException import Sergey_Dertan.SVault.utils.PlaceholdersLoader import Sergey_Dertan.SVault.utils.Utils.compareVersions import Sergey_Dertan.SVault.utils.Utils.httpGetRequestJson @@ -118,7 +119,7 @@ class SVaultMain : PluginBase() { else -> throw RuntimeException("Unknown provider") } } catch (e: Exception) { - throw RuntimeException("Cannot instantiate provider " + type.name, e) + throw DataProviderException("Cannot instantiate provider " + type.name, e, type) } } @@ -147,6 +148,7 @@ class SVaultMain : PluginBase() { this.registerCommand(OpenVaultCommand(this.vaultManager)) this.registerCommand(VaultListCommand(this.vaultManager)) this.registerCommand(SaveCommand(this)) + this.registerCommand(MigrateCommand(this)) } private fun initDataProvider(): Boolean { diff --git a/src/main/kotlin/Sergey_Dertan/SVault/utils/DataProviderException.kt b/src/main/kotlin/Sergey_Dertan/SVault/utils/DataProviderException.kt new file mode 100644 index 0000000..1161922 --- /dev/null +++ b/src/main/kotlin/Sergey_Dertan/SVault/utils/DataProviderException.kt @@ -0,0 +1,5 @@ +package Sergey_Dertan.SVault.utils + +import Sergey_Dertan.SVault.provider.DataProvider + +class DataProviderException(msg: String, e: Exception, val provider: DataProvider.Type) : RuntimeException(msg, e) \ No newline at end of file diff --git a/src/main/kotlin/Sergey_Dertan/SVault/vault/VaultManager.kt b/src/main/kotlin/Sergey_Dertan/SVault/vault/VaultManager.kt index a5a40b6..4c00e80 100644 --- a/src/main/kotlin/Sergey_Dertan/SVault/vault/VaultManager.kt +++ b/src/main/kotlin/Sergey_Dertan/SVault/vault/VaultManager.kt @@ -85,10 +85,10 @@ class VaultManager(private val settings: Settings, private val provider: DataPro fun vaultExists(owner: String, name: String): Boolean = this.vaults.getOrDefault(owner, Collections.emptyMap()).containsKey(name) - fun getVaultList(player: String): Array { + fun getVaultList(player: String): List { val list = mutableListOf() this.vaults.getOrDefault(player, Collections.emptyMap()).keys.forEach { list.add(it) } - return list.toTypedArray() + return list } private fun sendFakeChest(player: Player, name: String): Vector3 { diff --git a/src/main/resources/lang/eng.yml b/src/main/resources/lang/eng.yml index a31fec6..42d4c9d 100644 --- a/src/main/resources/lang/eng.yml +++ b/src/main/resources/lang/eng.yml @@ -58,4 +58,11 @@ command.list.in-game: "In game only" command.list.list.own: "Your vaults: {@list}" command.list.list.other: "{@player}`s vaults: {@list}" +command.migrate.permission: "You have no permission" +command.migrate.usage: "Required arguments: [FROM] [TO]" +command.migrate.description: "Migrate" +command.migrate.same-provider: "You entered two equal providers" +command.migrate.error: "Cannot instantiate data provider {@provider}" +command.migrate.success: "Successfully copied {@amount} vaults" + vault-inventory-name: "Vault {@name}" \ No newline at end of file diff --git a/src/main/resources/lang/rus.yml b/src/main/resources/lang/rus.yml index c1ee3cc..1780bdf 100644 --- a/src/main/resources/lang/rus.yml +++ b/src/main/resources/lang/rus.yml @@ -58,4 +58,11 @@ command.list.in-game: "Только в игре" command.list.list.own: "Ваши хранилища: {@list}" command.list.list.other: "Хранилища игрока {@player}: {@list}" +command.migrate.permission: "Недостаточно прав" +command.migrate.usage: "Необходимые аргументы: [ИСТОЧНИК] [ЦЕЛЬ]" +command.migrate.description: "Migrate" +command.migrate.same-provider: "Одинаковые поставщики данных" +command.migrate.error: "Не удалось создать поставщик данных {@provider}" +command.migrate.success: "Скопировано {@amount} хранилищ" + vault-inventory-name: "Хранилище {@name}" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3b150fe..705f29c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -29,4 +29,6 @@ permissions: svault.command.save: default: op svault.command.vault: - default: true \ No newline at end of file + default: true + svault.command.migrate: + default: op \ No newline at end of file