Skip to content

Commit

Permalink
add save setting to file
Browse files Browse the repository at this point in the history
  • Loading branch information
nutyworks committed Apr 6, 2020
1 parent 08806f5 commit 8bf5af0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ class FiNoteBlockCommand(val plugin: FiNoteBlockPlugin) : AbstractExecutorComple
}

override fun tabComplete(): MutableList<String> {
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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,75 @@ class UserSettings(val uuid: UUID) {
companion object {
val default = HashMap<String, SettingOption>().apply {
//
put("displayBossBar", SettingOption(2, true, hashBijectiveOf<Any, Any>().apply {
set(true, secondary = true)
set(false, secondary = false)
put("displayBossBar", SettingOption(true, hashBijectiveOf<Any, Any>().apply {
set(primary = true, secondary = true)
set(primary = false, secondary = false)
}))
put("bossBarProgress", SettingOption(3, "percentage", hashBijectiveOf<Any, Any>().apply {
put("bossBarProgress", SettingOption("percentage", hashBijectiveOf<Any, Any>().apply {
set("percentage", true)
set("time", false)
}))
}

val settingIndex = ArrayList<String>().apply {
add("displayBossBar")
add("bossBarProgress")
}
}

private val settings = HashMap<String, Any>()
private val file = File("${FiNoteBlockPlugin.instance.dataFolder}\\userdata\\$uuid.dat")

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() {
val file = File("${FiNoteBlockPlugin.instance.dataFolder}\\userdata\\$uuid.dat")
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? {
Expand All @@ -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) {
Expand All @@ -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<Any, Any>)
class SettingOption(val defaultValue: Any, val acceptableValues: HashBijective<Any, Any>)

0 comments on commit 8bf5af0

Please sign in to comment.