Hayo Im developing for a minecraft server written in kotlin
The current issue im running into is if there is a better programing pattern in kotlin than if/else blocks for mechanics that can be controlled/configured by the user.
Heres a snippet, it was suggested that one of the controller mechanics (drifting) be toggleable by the user.
fun setDirectControlEnabled(enabled: Boolean) {
isDirectControlEnabled = enabled
if (enabled) {
sendMessage(dcMessage)
val player: Player = (controller as? PlayerController)?.player ?: return
player.walkSpeed = 0.009f
val playerLoc = player.location
directControlCenter = playerLoc.toBlockLocation().add(0.5, playerLoc.y.rem(1)+0.001, 0.5)
if(!PlayerCache[player].playerConfig.dcDrift) { // the configuration to check
player.teleport(directControlCenter!!) // the code to run
}//skipping the teleportation causes the user to drift
// more code
The suggested snippet isnt too bad, but as more settings are added, or there are nested settings, Im worried that the codebase will be become muddled with code that mostly covers setting control than actually handling the intended mechanic.
if (configOne) {
// do something
if (configTwo) {
// also do
}
} else {
//do this instead
}
Im fairly new to kotlin, is there a code style/pattern that minimizes obfuscation?
Ive tried looking into decorators and annotations, although both of those things dont seem like the thing I want.