Skip to content

Commit

Permalink
Migrate command
Browse files Browse the repository at this point in the history
list command fix
  • Loading branch information
SergeyDertan committed May 2, 2019
1 parent 5c30207 commit 84c62c1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
55 changes: 55 additions & 0 deletions src/main/kotlin/Sergey_Dertan/SVault/command/MigrateCommand.kt
Original file line number Diff line number Diff line change
@@ -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<String>()
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<out String>): 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/Sergey_Dertan/SVault/main/SVaultMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions src/main/kotlin/Sergey_Dertan/SVault/vault/VaultManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
fun getVaultList(player: String): List<String> {
val list = mutableListOf<String>()
this.vaults.getOrDefault(player, Collections.emptyMap()).keys.forEach { list.add(it) }
return list.toTypedArray()
return list
}

private fun sendFakeChest(player: Player, name: String): Vector3 {
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/lang/eng.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
7 changes: 7 additions & 0 deletions src/main/resources/lang/rus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
4 changes: 3 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ permissions:
svault.command.save:
default: op
svault.command.vault:
default: true
default: true
svault.command.migrate:
default: op

0 comments on commit 84c62c1

Please sign in to comment.