Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
LioRael committed Jan 30, 2022
1 parent 9ea63f0 commit efd570b
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 106 deletions.
23 changes: 17 additions & 6 deletions src/main/kotlin/com/faithl/level/api/event/ExpUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ import taboolib.platform.type.BukkitProxyEvent
* @author Leosouthey
* @since 2021/12/18-19:07
**/
data class ExpUpdateEvent(
val level: Level,
val target: String,
val changeType: ChangeType,
var value: Int
) : BukkitProxyEvent()
class ExpUpdateEvent {

class Before(
val level: Level,
val target: String,
val changeType: ChangeType,
var value: Int
) : BukkitProxyEvent()

class After(
val level: Level,
val target: String,
val changeType: ChangeType,
var value: Int
) : BukkitProxyEvent()

}
50 changes: 35 additions & 15 deletions src/main/kotlin/com/faithl/level/api/event/LevelUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,42 @@ import taboolib.platform.type.BukkitProxyEvent
* @author Leosouthey
* @since 2021/12/18-19:02
**/
data class LevelUpdateEvent(
val level: Level,
val target: String,
var oldLevel: Int,
var newLevel: Int,
) : BukkitProxyEvent() {
class LevelUpdateEvent {

val type: ChangeType
get() {
return if (newLevel - oldLevel > 0) {
ChangeType.ADD
} else if (newLevel == oldLevel) {
ChangeType.NONE
} else {
ChangeType.TAKE
class Before(
val level: Level,
val target: String,
var oldLevel: Int,
var newLevel: Int
) : BukkitProxyEvent() {
val type: ChangeType
get() {
return if (newLevel - oldLevel > 0) {
ChangeType.ADD
} else if (newLevel == oldLevel) {
ChangeType.NONE
} else {
ChangeType.TAKE
}
}
}
}

class After(
val level: Level,
val target: String,
var oldLevel: Int,
var newLevel: Int
) : BukkitProxyEvent() {
val type: ChangeType
get() {
return if (newLevel - oldLevel > 0) {
ChangeType.ADD
} else if (newLevel == oldLevel) {
ChangeType.NONE
} else {
ChangeType.TAKE
}
}
}

}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/faithl/level/api/event/UpdateFailureEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.faithl.level.api.event

import com.faithl.level.internal.core.Level
import taboolib.platform.type.BukkitProxyEvent

/**
* @author Leosouthey
* @since 2022/1/30-19:32
**/
class UpdateFailureEvent(val level: Level, val message: String) : BukkitProxyEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.faithl.level.internal.command.impl
import com.faithl.level.FaithlLevel
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.subCommand
import taboolib.module.lang.Language
import taboolib.module.lang.sendLang

object CommandReload {
Expand All @@ -14,6 +15,7 @@ object CommandReload {
*/
val command = subCommand {
execute<ProxyCommandSender> { sender, _, _ ->
Language.reload()
FaithlLevel.reload()
sender.sendLang("command-reload")
}
Expand Down
60 changes: 24 additions & 36 deletions src/main/kotlin/com/faithl/level/internal/core/LevelHandler.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.faithl.level.internal.core

import com.faithl.level.internal.core.impl.Basic
import com.faithl.level.internal.core.impl.Temp
import com.faithl.level.internal.util.eval
import com.faithl.level.internal.util.sendMessage
import taboolib.common.platform.function.getProxyPlayer
import taboolib.common.util.asList
import taboolib.common5.Coerce
Expand All @@ -19,7 +17,7 @@ import taboolib.platform.compat.replacePlaceholder
**/
object LevelHandler {

fun getNeedExp(target: String, levelData: Int, data: Any, eval: Boolean = false): Int? {
fun getNeedExp(target: String, levelData: Int, data: Any): Int? {
val result = getValue(levelData, data, "data.increase")
val player = getProxyPlayer(target)
if (result.state == ValueResult.State.FAILURE) {
Expand All @@ -28,27 +26,17 @@ object LevelHandler {
}
return null
}
if (!Coerce.asInteger(result.value).isPresent) {
return null
}
return if (player != null) {
if (eval) {
println(
Coerce.toInteger(
(result.value as String).replacePlaceholder(player.cast()).eval()
)
)
Coerce.toInteger(
(result.value as String).replacePlaceholder(player.cast()).eval()
)
} else {
Coerce.toInteger((result.value as String).replacePlaceholder(player.cast()))
}
Coerce.toInteger(
((result.value ?: return null) as String).replacePlaceholder(player.cast()).eval()
)
} else {
if (eval) {
Coerce.toInteger(
(result.value as String).eval()
)
} else {
Coerce.toInteger(result.value as String)
}
Coerce.toInteger(
((result.value ?: return null) as String).eval()
)
}
}

Expand All @@ -67,6 +55,13 @@ object LevelHandler {
if (data is ConfigurationSection) {
val sortedData = data.getConfigurationSection(node)?.getKeys(false)?.map { Coerce.toInteger(it) }?.sorted()
if (sortedData != null) {
if (level >= sortedData.last()) {
return ValueResult(
ValueResult.State.FAILURE,
null,
ValueResult.Reason.LEVEL_MAX
)
}
for (key in sortedData) {
if (level <= key) {
return ValueResult(ValueResult.State.SUCCESS, data["${node}.$key"])
Expand All @@ -76,18 +71,18 @@ object LevelHandler {
if (fixed != null) {
return ValueResult(ValueResult.State.SUCCESS, fixed)
}
if (level > sortedData.last()) {
}
}
if (data is org.bukkit.configuration.ConfigurationSection) {
val sortedData = data.getConfigurationSection(node)?.getKeys(false)?.map { Coerce.toInteger(it) }?.sorted()
if (sortedData != null) {
if (level >= sortedData.last()) {
return ValueResult(
ValueResult.State.FAILURE,
null,
ValueResult.Reason.LEVEL_MAX
)
}
}
}
if (data is org.bukkit.configuration.ConfigurationSection) {
val sortedData = data.getConfigurationSection(node)?.getKeys(false)?.map { Coerce.toInteger(it) }?.sorted()
if (sortedData != null) {
for (key in sortedData) {
if (level <= key) {
return ValueResult(ValueResult.State.SUCCESS, data["${node}.$key"])
Expand All @@ -97,20 +92,13 @@ object LevelHandler {
if (fixed != null) {
return ValueResult(ValueResult.State.SUCCESS, fixed)
}
if (level > sortedData.last()) {
return ValueResult(
ValueResult.State.FAILURE,
null,
ValueResult.Reason.LEVEL_MAX
)
}
}
}
return ValueResult(ValueResult.State.FAILURE, null, ValueResult.Reason.INVALID_TYPE)
}

fun isLevelUp(level: Temp, target: String, value: Int): Boolean {
val expNeeded = getNeedExp(target, level.getLevel(target), level.config, true) ?: return false
val expNeeded = getNeedExp(target, level.getLevel(target), level.config) ?: return false
val exp = level.getExp(target)
return exp + value >= expNeeded
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.faithl.level.internal.core
* @author Leosouthey
* @since 2022/1/21-19:59
**/
class ValueResult(val state: State = State.SUCCESS, val value: Any? = null, val reason: Reason? = null) {
data class ValueResult(val state: State = State.SUCCESS, val value: Any? = null, val reason: Reason? = null) {

enum class State {
SUCCESS, FAILURE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import taboolib.library.configuration.ConfigurationSection
internal object Condition {

@SubscribeEvent(priority = EventPriority.HIGHEST)
fun e(e: LevelUpdateEvent) {
fun e(e: LevelUpdateEvent.Before) {
if (e.level is Basic) {
kotlin.runCatching {
getProxyPlayer(e.target)?.let {
Expand All @@ -37,7 +37,7 @@ internal object Condition {
}

@SubscribeEvent(priority = EventPriority.HIGH)
fun e(e: ExpUpdateEvent) {
fun e(e: ExpUpdateEvent.Before) {
if (e.changeType == ChangeType.ADD && e.level is Basic) {
runCatching {
val config = e.level.config
Expand Down
32 changes: 16 additions & 16 deletions src/main/kotlin/com/faithl/level/internal/core/function/LevelMax.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@ import taboolib.library.configuration.ConfigurationSection
object LevelMax {

@SubscribeEvent
fun e(e: ExpUpdateEvent) {
fun e(e: ExpUpdateEvent.Before) {
if (e.changeType == ChangeType.ADD) {
if (e.level is Temp) {
if (LevelHandler.getNeedExp(e.target, e.level.getLevel(e.target) + 1, e.level.config, true) == null) {
if (LevelHandler.getNeedExp(e.target, e.level.getLevel(e.target), e.level.config) == null) {
getProxyPlayer(e.target)?.let {
LevelHandler.getValue(
e.level.getLevel(e.target),
e.level.config,
"event.player-level-max.message"
).value?.let { message ->
val message = LevelHandler.getNodeValue(e.level.config, "event.player-level-max.message")
if (message != null) {
if (message is ConfigurationSection) {
message.sendMessage(it)
}
e.isCancelled = (LevelHandler.getNodeValue(e.level.config, "overflow") ?: false) as Boolean
if (message is org.bukkit.configuration.ConfigurationSection) {
message.sendMessage(it)
}
}
}
e.isCancelled = (LevelHandler.getNodeValue(e.level.config, "overflow") ?: false) as Boolean
}
}
}
}

@SubscribeEvent
fun e(e: LevelUpdateEvent) {
fun e(e: LevelUpdateEvent.Before) {
if (e.newLevel - e.oldLevel > 0) {
if (e.level is Temp) {
if (LevelHandler.getNeedExp(e.target, e.newLevel, e.level.config, true) == null) {
if (LevelHandler.getNeedExp(e.target, e.oldLevel, e.level.config) == null) {
getProxyPlayer(e.target)?.let {
LevelHandler.getValue(
e.newLevel,
e.level.config,
"event.player-level-max.message"
).value?.let { message ->
val message = LevelHandler.getNodeValue(e.level.config, "event.player-level-max.message")
if (message != null) {
if (message is ConfigurationSection) {
message.sendMessage(it)
}
e.isCancelled = true
if (message is org.bukkit.configuration.ConfigurationSection) {
message.sendMessage(it)
}
}
}
e.isCancelled = true
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/com/faithl/level/internal/core/impl/Pure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class Pure() : Temp() {
config = 100
}

constructor(conf: Int): this(){
constructor(conf: Int) : this() {
config = conf
}

Expand Down Expand Up @@ -60,14 +60,14 @@ open class Pure() : Temp() {
}

@SubscribeEvent
fun e(e: ExpUpdateEvent) {
fun e(e: ExpUpdateEvent.Before) {
if (e.level is Pure) {
save(e.target, e.level)
}
}

@SubscribeEvent
fun e(e: LevelUpdateEvent) {
fun e(e: LevelUpdateEvent.Before) {
if (e.level is Pure) {
save(e.target, e.level)
}
Expand All @@ -77,6 +77,7 @@ open class Pure() : Temp() {
fun e(e: PlayerQuitEvent) {
FaithlLevelAPI.registeredLevels.values.forEach {
if (it is Pure) {
save(e.player.name, it)
it.levelData.remove(e.player.name)
}
}
Expand Down
Loading

0 comments on commit efd570b

Please sign in to comment.