Skip to content

Commit

Permalink
Cleaned up SendApplicationTask class by extracting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccoystephenson committed Nov 18, 2024
1 parent cfa7604 commit d2f7975
Showing 1 changed file with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.dansplugins.factionsystem.command.faction.apply.tasks

import com.dansplugins.factionsystem.MedievalFactions
import com.dansplugins.factionsystem.faction.MfFaction
import com.dansplugins.factionsystem.faction.MfFactionApplication
import com.dansplugins.factionsystem.faction.MfFactionService
import com.dansplugins.factionsystem.player.MfPlayer
import com.dansplugins.factionsystem.player.MfPlayerService
import dev.forkhandles.result4k.onFailure
import net.md_5.bungee.api.ChatColor
import org.bukkit.entity.Player
Expand All @@ -17,29 +20,42 @@ class SendApplicationTask(
override fun run() {
val factionService = plugin.services.factionService
val playerService = plugin.services.playerService
val mfPlayer = playerService.getPlayer(sender)
?: playerService.save(MfPlayer(plugin, sender)).onFailure {
sender.sendMessage("${ChatColor.RED}${plugin.language["CommandFactionApplyFailedToSavePlayer"]}")
plugin.logger.log(SEVERE, "Failed to save player: ${it.reason.message}", it.reason.cause)
return
}
val faction = factionService.getFaction(mfPlayer.id)

val mfPlayer = getOrSavePlayer(playerService, sender) ?: return
val target = getTargetFaction(factionService, targetFactionName, sender) ?: return

if (isPlayerInTargetFaction(factionService, mfPlayer, target)) {
sender.sendMessage("${ChatColor.RED}${plugin.language["CommandFactionApplyCannotApplyToCurrentFaction"]}")
return
}

saveFactionApplication(factionService, target, mfPlayer, sender)
}

private fun getOrSavePlayer(playerService: MfPlayerService, sender: Player): MfPlayer? {
return playerService.getPlayer(sender) ?: playerService.save(MfPlayer(plugin, sender)).onFailure {
sender.sendMessage("${ChatColor.RED}${plugin.language["CommandFactionApplyFailedToSavePlayer"]}")
plugin.logger.log(SEVERE, "Failed to save player: ${it.reason.message}", it.reason.cause) as Nothing
}
}

private fun getTargetFaction(factionService: MfFactionService, targetFactionName: String, sender: Player): MfFaction? {
val target = factionService.getFaction(targetFactionName)
if (target == null) {
sender.sendMessage("${ChatColor.RED}${plugin.language["CommandFactionApplyInvalidTarget"]}")
return
}
if (faction != null) {
if (target.id.value == faction.id.value) {
sender.sendMessage("${ChatColor.RED}${plugin.language["CommandFactionApplyCannotApplyToCurrentFaction"]}")
return
}
}
return target
}

private fun isPlayerInTargetFaction(factionService: MfFactionService, mfPlayer: MfPlayer, target: MfFaction): Boolean {
val faction = factionService.getFaction(mfPlayer.id)
return faction != null && target.id.value == faction.id.value
}

private fun saveFactionApplication(factionService: MfFactionService, target: MfFaction, mfPlayer: MfPlayer, sender: Player) {
factionService.save(target.copy(applications = target.applications + MfFactionApplication(target.id, mfPlayer.id))).onFailure {
sender.sendMessage("${ChatColor.RED}${plugin.language["CommandFactionApplyFailedToSaveFaction"]}")
plugin.logger.log(SEVERE, "Failed to save faction: ${it.reason.message}", it.reason.cause)
return
plugin.logger.log(SEVERE, "Failed to save faction: ${it.reason.message}", it.reason.cause) as Nothing
}
}
}

0 comments on commit d2f7975

Please sign in to comment.