-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create & List Commands #135
base: develop
Are you sure you want to change the base?
Changes from all commits
45ad34f
e786123
9014360
74bebc9
9adfe1a
dc7ceac
72e1aab
bca28b9
b2f3e2c
ecc1974
bc56e81
534e22c
b560da2
c9b4c9a
a235cd1
141bbd3
37ba2fe
911b789
fdbf004
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.dansplugins.fiefs.command.fiefs.create | ||
|
||
import com.dansplugins.fiefs.Fiefs | ||
import com.dansplugins.fiefs.fief.Fief | ||
import org.bukkit.ChatColor | ||
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 | ||
|
||
class FiefsCreateCommand(private val plugin: Fiefs) : CommandExecutor, TabCompleter { | ||
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean { | ||
if (sender !is Player) { | ||
sender.sendMessage("${ChatColor.RED}This command can only be run by a player.") | ||
return false | ||
} | ||
val name = args.firstOrNull() | ||
if (name == null) { | ||
sender.sendMessage("${ChatColor.RED}Please specify a name for the fief.") | ||
return false | ||
} | ||
if (plugin.fiefRepository.getFief(name) != null) { | ||
sender.sendMessage("${ChatColor.RED}A fief with that name already exists.") | ||
return false | ||
} | ||
|
||
// player must be in a faction | ||
val mfPlayer = plugin.medievalFactions.services.playerService.getPlayer(sender) | ||
val faction = plugin.medievalFactions.services.factionService.getFaction(mfPlayer?.id ?: return false) | ||
if (faction == null) { | ||
sender.sendMessage("${ChatColor.RED}You must be in a faction to create a fief.") | ||
return false | ||
} | ||
|
||
// player must not be in a fief already | ||
val playersFief = plugin.fiefRepository.getPlayersFief(mfPlayer.id) | ||
if (playersFief != null) { | ||
sender.sendMessage("${ChatColor.RED}You're already in a fief. You must leave it first.") | ||
return false | ||
} | ||
|
||
val newFief = Fief(name, mfPlayer.id, faction.id) | ||
plugin.fiefRepository.addFief(newFief) | ||
sender.sendMessage("${ChatColor.GREEN}Fief created.") | ||
return true | ||
} | ||
|
||
override fun onTabComplete( | ||
sender: CommandSender, | ||
command: Command, | ||
alias: String, | ||
args: Array<out String> | ||
): List<String> { | ||
return emptyList() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.dansplugins.fiefs.command.fiefs.list | ||
|
||
import com.dansplugins.fiefs.Fiefs | ||
import org.bukkit.ChatColor | ||
import org.bukkit.command.Command | ||
import org.bukkit.command.CommandExecutor | ||
import org.bukkit.command.CommandSender | ||
import org.bukkit.command.TabCompleter | ||
|
||
class FiefsListCommand(private val plugin: Fiefs) : CommandExecutor, TabCompleter { | ||
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean { | ||
if (plugin.fiefRepository.getFiefs().isEmpty()) { | ||
sender.sendMessage("${ChatColor.RED}There are no fiefs.") | ||
return false | ||
} | ||
sender.sendMessage("${ChatColor.AQUA}=== Fiefs ===") | ||
for (fief in plugin.fiefRepository.getFiefs()) { | ||
sender.sendMessage("${ChatColor.AQUA}" + fief.getName()) | ||
} | ||
return true | ||
} | ||
|
||
override fun onTabComplete( | ||
sender: CommandSender, | ||
command: Command, | ||
alias: String, | ||
args: Array<out String> | ||
): List<String> { | ||
return emptyList() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.dansplugins.fiefs.fief | ||
|
||
import com.dansplugins.factionsystem.faction.MfFactionId | ||
import com.dansplugins.factionsystem.player.MfPlayerId | ||
import java.util.UUID | ||
|
||
data class Fief(private val name: String, private val ownerMfPlayerId: MfPlayerId, private val mfFactionid: MfFactionId) { | ||
private var id = MfFiefId.generate() | ||
private var members: MutableList<MfPlayerId> = mutableListOf() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless you're replacing the entire list, this can be a val. |
||
|
||
fun getId(): MfFiefId { | ||
return id | ||
} | ||
|
||
fun addMember(mfPlayerId: MfPlayerId) { | ||
members.add(mfPlayerId) | ||
} | ||
|
||
fun removeMember(mfPlayerId: MfPlayerId) { | ||
members.remove(mfPlayerId) | ||
} | ||
|
||
fun isMember(mfPlayerId: MfPlayerId): Boolean { | ||
return members.contains(mfPlayerId) || ownerMfPlayerId == mfPlayerId | ||
} | ||
|
||
fun getMembers(): List<MfPlayerId> { | ||
return members | ||
} | ||
|
||
fun getOwnerId(): MfPlayerId { | ||
return ownerMfPlayerId | ||
} | ||
|
||
fun getName(): String { | ||
return name | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.dansplugins.fiefs.fief | ||
|
||
import com.dansplugins.factionsystem.player.MfPlayerId | ||
import com.dansplugins.fiefs.Fiefs | ||
import java.util.* | ||
|
||
/** | ||
* Stores all Fiefs in a list and provides methods for accessing and modifying the list. | ||
*/ | ||
class FiefRepository(private val plugin: Fiefs) { | ||
private val fiefs = mutableListOf<Fief>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume eventually this will be backed by a DB, negating the need for the list in the repository, but this is fine for testing purposes. |
||
|
||
/** | ||
* Adds a Fief to the list. | ||
* @param fief The Fief to add. | ||
*/ | ||
fun addFief(fief: Fief) { | ||
fiefs.add(fief) | ||
} | ||
|
||
/** | ||
* Removes a Fief from the list. | ||
* @param fief The Fief to remove. | ||
*/ | ||
fun removeFief(fief: Fief) { | ||
fiefs.remove(fief) | ||
} | ||
|
||
/** | ||
* Gets a Fief from the list. | ||
* @param name The name of the Fief to get. | ||
* @return The Fief with the given name. | ||
*/ | ||
fun getFief(name: String): Fief? { | ||
for (fief in fiefs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The more functional approach to this adds a good deal of brevity and is clearer in its intent imo: You can also use Can multiple fiefs have the same name? |
||
if (fief.getName() == name) { | ||
return fief | ||
} | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* Gets a Fief from the list. | ||
* @param mfFiefId The ID of the Fief to get. | ||
* @return The Fief with the given UUID. | ||
*/ | ||
fun getFief(mfFiefId: MfFiefId): Fief? { | ||
for (fief in fiefs) { | ||
if (fief.getId() == mfFiefId) { | ||
return fief | ||
} | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* Gets a player's fief from the list. | ||
* @param mfPlayerId The ID of the player whose fief to get. | ||
* @return The Fief which the player is a member of. | ||
* @return null if the player is not a member of any fief. | ||
*/ | ||
fun getPlayersFief(mfPlayerId: MfPlayerId): Fief? { | ||
for (fief in fiefs) { | ||
if (fief.isMember(mfPlayerId)) { | ||
return fief | ||
} | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* Gets a list of all Fiefs. | ||
* @return A list of all Fiefs. | ||
*/ | ||
fun getFiefs(): List<Fief> { | ||
return fiefs | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.dansplugins.fiefs.fief | ||
|
||
import java.util.* | ||
|
||
@JvmInline | ||
value class MfFiefId(val value: String) { | ||
companion object { | ||
fun generate() = MfFiefId(UUID.randomUUID().toString()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.dansplugins.fiefs.command | ||
|
||
import com.dansplugins.fiefs.Fiefs | ||
import com.dansplugins.fiefs.command.fiefs.FiefsCommand | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.bukkit.ChatColor | ||
import org.bukkit.command.Command | ||
import org.bukkit.command.CommandSender | ||
import org.junit.jupiter.api.Test | ||
|
||
class TestFiefsCommand { | ||
|
||
private val mockPlugin = mockk<Fiefs>() { | ||
every { description.version } returns "1.0.0" | ||
every { description.authors } returns listOf("Dan") | ||
every { description.description } returns "A plugin about fiefs." | ||
} | ||
|
||
@Test | ||
fun testFiefsCommandNoArguments() { | ||
// prepare | ||
val mockSender = mockk<CommandSender>() { | ||
every { sendMessage(any<String>()) } returns Unit | ||
} | ||
val mockCommand = mockk<Command>() | ||
val args = emptyArray<String>() | ||
val label = "fiefs" | ||
val fiefsCommand = FiefsCommand(mockPlugin) | ||
|
||
// execute | ||
fiefsCommand.onCommand(mockSender, mockCommand, label, args) | ||
|
||
// verify | ||
val expectedAuthors = mockPlugin.description.authors | ||
val expectedDescription = mockPlugin.description.description | ||
verify(exactly = 1) { mockSender.sendMessage("${ChatColor.AQUA}Fiefs v1.0.0") } | ||
verify(exactly = 1) { mockSender.sendMessage("${ChatColor.AQUA}Author: $expectedAuthors") } | ||
verify(exactly = 1) { mockSender.sendMessage("${ChatColor.AQUA}Description: $expectedDescription") } | ||
verify(exactly = 1) { mockSender.sendMessage("${ChatColor.GREEN}Type /fiefs help for a list of commands.") } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming the fief repository will read these from the database, you may wish to be careful about threading - commands are run on the main thread, and database queries perform I/O which would cause lag spikes each time the command was run.
Additionally, I'd recommend adding another layer of abstraction in a fief service type thing (which contains any domain logic) so your command isn't interfacing directly with the database.