From 8bf5af01c91b6ac142a57a0fa5e7dd6b02b454b7 Mon Sep 17 00:00:00 2001 From: nutyworks Date: Mon, 6 Apr 2020 19:47:44 +0900 Subject: [PATCH] add save setting to file --- .../nutyworks/finoteblock/PlayerManager.kt | 4 +- .../finoteblock/command/FiNoteBlockCommand.kt | 4 +- .../noteblock/file/UserSettings.kt | 57 +++++++++++++------ 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/github/nutyworks/finoteblock/PlayerManager.kt b/src/main/java/com/github/nutyworks/finoteblock/PlayerManager.kt index aa241ec..5abc55d 100644 --- a/src/main/java/com/github/nutyworks/finoteblock/PlayerManager.kt +++ b/src/main/java/com/github/nutyworks/finoteblock/PlayerManager.kt @@ -17,8 +17,10 @@ class PlayerManager : Listener { @EventHandler private fun onPlayerJoin(event: PlayerJoinEvent) { val uuid = event.player.uniqueId + playerChannel[uuid] ?: moveChannel(uuid, "default", true) - FiNoteBlockPlugin.instance.playerManager.playerSetting[uuid] = UserSettings(uuid) + if (playerSetting[uuid] == null) + FiNoteBlockPlugin.instance.playerManager.playerSetting[uuid] = UserSettings(uuid) } fun moveChannel(uuid: UUID, channel: String, force: Boolean = false) { diff --git a/src/main/java/com/github/nutyworks/finoteblock/command/FiNoteBlockCommand.kt b/src/main/java/com/github/nutyworks/finoteblock/command/FiNoteBlockCommand.kt index 159c26f..56f94b8 100644 --- a/src/main/java/com/github/nutyworks/finoteblock/command/FiNoteBlockCommand.kt +++ b/src/main/java/com/github/nutyworks/finoteblock/command/FiNoteBlockCommand.kt @@ -110,7 +110,9 @@ class FiNoteBlockCommand(val plugin: FiNoteBlockPlugin) : AbstractExecutorComple } override fun tabComplete(): MutableList { - return setOf("playing", "playable").filter { it.startsWith(args[1]) }.toMutableList() + return if (args.size == 2) { + setOf("playing", "playable").filter { it.startsWith(args[1]) }.toMutableList() + } else mutableListOf() } } diff --git a/src/main/java/com/github/nutyworks/finoteblock/noteblock/file/UserSettings.kt b/src/main/java/com/github/nutyworks/finoteblock/noteblock/file/UserSettings.kt index 5afc783..49e1342 100644 --- a/src/main/java/com/github/nutyworks/finoteblock/noteblock/file/UserSettings.kt +++ b/src/main/java/com/github/nutyworks/finoteblock/noteblock/file/UserSettings.kt @@ -20,15 +20,20 @@ class UserSettings(val uuid: UUID) { companion object { val default = HashMap().apply { // - put("displayBossBar", SettingOption(2, true, hashBijectiveOf().apply { - set(true, secondary = true) - set(false, secondary = false) + put("displayBossBar", SettingOption(true, hashBijectiveOf().apply { + set(primary = true, secondary = true) + set(primary = false, secondary = false) })) - put("bossBarProgress", SettingOption(3, "percentage", hashBijectiveOf().apply { + put("bossBarProgress", SettingOption("percentage", hashBijectiveOf().apply { set("percentage", true) set("time", false) })) } + + val settingIndex = ArrayList().apply { + add("displayBossBar") + add("bossBarProgress") + } } private val settings = HashMap() @@ -36,23 +41,34 @@ class UserSettings(val uuid: UUID) { init { if (!file.exists()) create() + load() } fun create() { file.createNewFile() save() - load() } fun load() { val fis = file.inputStream() val dis = DataInputStream(fis) - + settingIndex.forEach { + val type = default[it]?.acceptableValues?.secondaryToPrimary?.keys?.first()?.javaClass?.typeName + val value = when (type) { + "java.lang.Boolean" -> dis.readBoolean() + "java.lang.Byte" -> dis.readByte() + "java.lang.Short" -> dis.readShort() + "java.lang.Integer" -> dis.readInt() + "java.lang.Long" -> dis.readLong() + "java.lang.String" -> dis.readUTF() + else -> default[it]?.defaultValue + } as Any + settings[it] = default[it]?.acceptableValues?.secondaryToPrimary?.get(value) as Any + } dis.close() fis.close() - TODO() } fun save() { @@ -60,9 +76,19 @@ class UserSettings(val uuid: UUID) { val fis = file.outputStream() val dos = DataOutputStream(fis) + settingIndex.forEach { + when (val value = default[it]?.acceptableValues?.primaryToSecondary?.get(settings[it] ?: default[it]?.defaultValue)) { + is Boolean -> dos.writeBoolean(value) + is Byte -> dos.writeByte(value.toInt()) + is Short -> dos.writeShort(value.toInt()) + is Int -> dos.writeInt(value) + is Long -> dos.writeLong(value) + is String -> dos.writeUTF(value) + } + } + dos.close() fis.close() - TODO() } fun get(key: String): Any? { @@ -72,8 +98,6 @@ class UserSettings(val uuid: UUID) { fun set(key: String, value: Any) { -// println("set() call") - if (!default.containsKey(key)) throw CommandFailException("Settings $key not exists.") val convertedValue = when (default[key]?.defaultValue?.javaClass?.typeName) { @@ -90,13 +114,14 @@ class UserSettings(val uuid: UUID) { else -> throw CommandFailException("Invalid value.") } -// println("${convertedValue.javaClass.typeName} != ${default[key]?.defaultValue?.javaClass?.typeName}") -// println(convertedValue) -// println(convertedValue.javaClass.typeName) - - if (default[key]?.acceptableValues?.primaryToSecondary?.keys?.contains(convertedValue) == true) + if (default[key]?.acceptableValues?.primaryToSecondary?.keys?.contains(convertedValue) == true) { settings[key] = convertedValue + } else { + throw CommandFailException("Invalid value.") + } + + save() } } -class SettingOption(val offset: Int, val defaultValue: Any, val acceptableValues: HashBijective) \ No newline at end of file +class SettingOption(val defaultValue: Any, val acceptableValues: HashBijective) \ No newline at end of file