Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.dansplugins.factionsystem.command.faction.claim.MfFactionClaimCommand
import com.dansplugins.factionsystem.command.faction.create.MfFactionCreateCommand
import com.dansplugins.factionsystem.command.faction.declareindependence.MfFactionDeclareIndependenceCommand
import com.dansplugins.factionsystem.command.faction.declarewar.MfFactionDeclareWarCommand
import com.dansplugins.factionsystem.command.faction.declinevassalization.MfFactionDeclineVassalizationCommand
import com.dansplugins.factionsystem.command.faction.denyapp.MfFactionDenyAppCommand
import com.dansplugins.factionsystem.command.faction.dev.MfFactionDevCommand
import com.dansplugins.factionsystem.command.faction.disband.MfFactionDisbandCommand
Expand Down Expand Up @@ -79,6 +80,7 @@ class MfFactionCommand(private val plugin: MedievalFactions) : CommandExecutor,
private val factionSetNameCommand = MfFactionSetNameCommand(plugin)
private val factionVassalizeCommand = MfFactionVassalizeCommand(plugin)
private val factionSwearFealtyCommand = MfFactionSwearFealtyCommand(plugin)
private val factionDeclineVassalizationCommand = MfFactionDeclineVassalizationCommand(plugin)
private val factionGrantIndependenceCommand = MfFactionGrantIndependenceCommand(plugin)
private val factionDeclareIndependenceCommand = MfFactionDeclareIndependenceCommand(plugin)
private val factionKickCommand = MfFactionKickCommand(plugin)
Expand Down Expand Up @@ -123,6 +125,7 @@ class MfFactionCommand(private val plugin: MedievalFactions) : CommandExecutor,
private val renameAliases = listOf("rename")
private val vassalizeAliases = listOf("vassalize", "vassalise", plugin.language["CmdFactionVassalize"])
private val swearFealtyAliases = listOf("swearfealty", plugin.language["CmdFactionSwearFealty"])
private val declineVassalizationAliases = listOf("declinevassalization", "declinevassalisation", plugin.language["CmdFactionDeclineVassalization"])
private val grantIndependenceAliases = listOf("grantindependence", plugin.language["CmdFactionGrantIndependence"])
private val declareIndependenceAliases = listOf("declareindependence", plugin.language["CmdFactionDeclareIndependence"])
private val kickAliases = listOf("kick", plugin.language["CmdFactionKick"])
Expand Down Expand Up @@ -167,6 +170,7 @@ class MfFactionCommand(private val plugin: MedievalFactions) : CommandExecutor,
renameAliases +
vassalizeAliases +
swearFealtyAliases +
declineVassalizationAliases +
grantIndependenceAliases +
declareIndependenceAliases +
kickAliases +
Expand Down Expand Up @@ -213,6 +217,7 @@ class MfFactionCommand(private val plugin: MedievalFactions) : CommandExecutor,
in renameAliases -> factionSetNameCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
in vassalizeAliases -> factionVassalizeCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
in swearFealtyAliases -> factionSwearFealtyCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
in declineVassalizationAliases -> factionDeclineVassalizationCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
in grantIndependenceAliases -> factionGrantIndependenceCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
in declareIndependenceAliases -> factionDeclareIndependenceCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
in kickAliases -> factionKickCommand.onCommand(sender, command, label, args.drop(1).toTypedArray())
Expand Down Expand Up @@ -276,6 +281,7 @@ class MfFactionCommand(private val plugin: MedievalFactions) : CommandExecutor,
in renameAliases -> factionSetNameCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
in vassalizeAliases -> factionVassalizeCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
in swearFealtyAliases -> factionSwearFealtyCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
in declineVassalizationAliases -> factionDeclineVassalizationCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
in grantIndependenceAliases -> factionGrantIndependenceCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
in declareIndependenceAliases -> factionDeclareIndependenceCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
in kickAliases -> factionKickCommand.onTabComplete(sender, command, label, args.drop(1).toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.dansplugins.factionsystem.command.faction.declinevassalization

import com.dansplugins.factionsystem.MedievalFactions
import com.dansplugins.factionsystem.faction.MfFaction
import com.dansplugins.factionsystem.player.MfPlayer
import com.dansplugins.factionsystem.relationship.MfFactionRelationshipType.VASSAL
import dev.forkhandles.result4k.onFailure
import org.bukkit.ChatColor.GREEN
import org.bukkit.ChatColor.RED
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabCompleter
import org.bukkit.entity.Player
import java.util.logging.Level.SEVERE

class MfFactionDeclineVassalizationCommand(private val plugin: MedievalFactions) : CommandExecutor, TabCompleter {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
if (!sender.hasPermission("mf.declinevassalization")) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationNoPermission"]}")
return true
}
if (args.isEmpty()) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationUsage"]}")
return true
}
if (sender !is Player) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationNotAPlayer"]}")
return true
}
plugin.server.scheduler.runTaskAsynchronously(
plugin,
Runnable {
val playerService = plugin.services.playerService
val mfPlayer = playerService.getPlayer(sender)
?: playerService.save(MfPlayer(plugin, sender)).onFailure {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationFailedToSavePlayer"]}")
plugin.logger.log(SEVERE, "Failed to save player: ${it.reason.message}", it.reason.cause)
return@Runnable
}
val factionService = plugin.services.factionService
val faction = factionService.getFaction(mfPlayer.id)
if (faction == null) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationMustBeInAFaction"]}")
return@Runnable
}
val role = faction.getRole(mfPlayer.id)
if (role == null || !role.hasPermission(faction, plugin.factionPermissions.swearFealty)) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationNoFactionPermission"]}")
return@Runnable
}
val target = factionService.getFaction(args.joinToString(" "))
if (target == null) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationInvalidTarget"]}")
return@Runnable
}
val factionRelationshipService = plugin.services.factionRelationshipService
val reverseRelationships = factionRelationshipService.getRelationships(target.id, faction.id)
val vassalizationRequest = reverseRelationships.firstOrNull { it.type == VASSAL }
if (vassalizationRequest == null) {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationNoVassalizationRequest"]}")
return@Runnable
}
factionRelationshipService.delete(vassalizationRequest.id).onFailure {
sender.sendMessage("$RED${plugin.language["CommandFactionDeclineVassalizationFailedToDeleteRelationship"]}")
plugin.logger.log(SEVERE, "Failed to delete faction relationship: ${it.reason.message}", it.reason.cause)
return@Runnable
}
sender.sendMessage("$GREEN${plugin.language["CommandFactionDeclineVassalizationSuccess", target.name]}")
plugin.server.scheduler.runTask(
plugin,
Runnable {
faction.sendMessage(
plugin.language["FactionVassalizationRequestDeclinedNotificationTitle", target.name],
plugin.language["FactionVassalizationRequestDeclinedNotificationBody", target.name]
)
target.sendMessage(
plugin.language["FactionVassalizationRequestRejectedNotificationTitle", faction.name],
plugin.language["FactionVassalizationRequestRejectedNotificationBody", faction.name]
)
}
)
}
)
return true
}

override fun onTabComplete(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): List<String> {
if (sender !is Player) return emptyList()
val playerService = plugin.services.playerService
val mfPlayer = playerService.getPlayer(sender) ?: return emptyList()
val factionService = plugin.services.factionService
val faction = factionService.getFaction(mfPlayer.id) ?: return emptyList()
val factionRelationshipService = plugin.services.factionRelationshipService
// Find factions that have sent vassalization requests TO this faction
val factionsWithRequests = factionService.factions.filter { otherFaction ->
val relationships = factionRelationshipService.getRelationships(otherFaction.id, faction.id)
relationships.any { it.type == VASSAL }
}
return when {
args.isEmpty() -> factionsWithRequests.map(MfFaction::name)
args.size == 1 ->
factionsWithRequests
.filter { it.name.lowercase().startsWith(args[0].lowercase()) }
.map(MfFaction::name)
else -> emptyList()
}
}
}
Loading
Loading