Skip to content

Commit

Permalink
#573, 585 Extended the join and leave command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shynixn committed Dec 19, 2024
1 parent 0f57ced commit 1e85842
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 29 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ tasks.register("pluginJarLatest", ShadowJar::class.java) {
dependsOn("relocatePluginJar")
from(zipTree(File("./build/libs/" + (tasks.getByName("relocatePluginJar") as Jar).archiveName)))
archiveName = "${baseName}-${version}-latest.${extension}"
// destinationDir = File("C:\\temp\\plugins")
destinationDir = File("C:\\temp\\plugins")

exclude("com/github/shynixn/blockball/lib/com/github/shynixn/mcutils/packet/nms/v1_8_R3/**")
exclude("com/github/shynixn/blockball/lib/com/github/shynixn/mcutils/packet/nms/v1_9_R2/**")
Expand Down Expand Up @@ -251,7 +251,7 @@ tasks.register("languageFile") {
implContents.add("")
implContents.add("class BlockBallLanguageImpl : Language, LanguageProviderImpl() {")
implContents.add(" override val names: List<String>\n" +
" get() = listOf(\"en_us\", \"es_es\")")
" get() = listOf(\"en_us\", \"es_es\", \"zh_cn\")")

for (i in 0 until lines.size) {
val key = lines[i]
Expand Down
13 changes: 8 additions & 5 deletions docs/wiki/docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,26 @@ Enables or disables your game. If a game is disabled, nobody can join.
### /blockball join

```
/blockball join <name> [team]
/blockball join <name> [team] [player]
```

Lets the player executing the command join the game. The optional team argument allows to directly join a specific team.
If the team is full, the other team will be chosen. If no team is specified, a random team will be selected.
Lets the player executing the command join the game. If no team is specified, a random team will be selected.
If the player has already joined a game, this command can also be used to switch teams.

* Name: Identifier of a game
* Team: Name of the team. Is always red or blue.
* Team: Optional name of the team. Is ``red``, ``blue`` or ``referee``.
* Player: Optional name of the player to join. Requires the ``blockball.edit`` permission to use.

### /blockball leave

```
/blockball leave
/blockball leave [player]
```

Lets the player executing the command leave the game.

* Player: Optional name of the player to leave. Requires the ``blockball.edit`` permission to use.

### /blockball axe

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.shynixn.blockball.contract.Language

class BlockBallLanguageImpl : Language, LanguageProviderImpl() {
override val names: List<String>
get() = listOf("en_us", "es_es")
get() = listOf("en_us", "es_es", "zh_cn")
override var gameAlreadyExistsMessage = LanguageItem("&0&l[&f&lBlockBall&0&l]&c Game %1$1s already exists.")

override var commandUsage = LanguageItem("&0&l[&f&lBlockBall&0&l]&7 Use /blockball help to see more info about the plugin.")
Expand Down Expand Up @@ -190,4 +190,6 @@ class BlockBallLanguageImpl : Language, LanguageProviderImpl() {
override var commandPlaceHolderToolTip = LanguageItem("Resolves a given placeholder.")

override var commandPlaceHolderMessage = LanguageItem("Evaluated placeholder: %1$1s")

override var playerNotFoundMessage = LanguageItem("&0&l[&f&lBlockBall&0&l]&c Player %1$1s not found.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@ interface Language : LanguageProvider {
var commandPlaceHolderToolTip: LanguageItem

var commandPlaceHolderMessage: LanguageItem

var playerNotFoundMessage: LanguageItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.github.shynixn.mcutils.common.selection.AreaSelectionService
import com.github.shynixn.mcutils.sign.SignService
import com.google.inject.Inject
import kotlinx.coroutines.runBlocking
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Player
Expand All @@ -46,6 +47,31 @@ class BlockBallCommandExecutor @Inject constructor(
private val arenaTabs: suspend (s: CommandSender) -> List<String> = {
arenaRepository.getAll().map { e -> e.name }
}
private val onlinePlayerTabs: (suspend (CommandSender) -> List<String>) = {
Bukkit.getOnlinePlayers().map { e -> e.name }
}
private val playerMustExist = object : Validator<Player> {
override suspend fun transform(
sender: CommandSender, prevArgs: List<Any>, openArgs: List<String>
): Player? {
try {
val playerId = openArgs[0]
val player = Bukkit.getPlayer(playerId)

if (player != null) {
return player
}
return Bukkit.getPlayer(UUID.fromString(playerId))
} catch (e: Exception) {
return null
}
}

override suspend fun message(sender: CommandSender, prevArgs: List<Any>, openArgs: List<String>): String {
return language.playerNotFoundMessage.text.format(openArgs[0])
}
}

private val teamTabs: suspend (s: CommandSender) -> List<String> = {
val tabs = ArrayList<Team>()
tabs.add(Team.RED)
Expand Down Expand Up @@ -232,17 +258,27 @@ class BlockBallCommandExecutor @Inject constructor(
builder().argument("name").validator(gameMustExistValidator).tabs(arenaTabs)
.executePlayer({ language.commandSenderHasToBePlayer.text }) { sender, arena ->
joinGame(
sender, arena.name
sender, sender, arena.name
)
}.argument("team").validator(teamValidator).tabs(teamTabs)
.executePlayer({ language.commandSenderHasToBePlayer.text }) { sender, arena, team ->
joinGame(sender, arena.name, team)
joinGame(sender, sender, arena.name, team)
}
.argument("player").validator(playerMustExist).tabs(onlinePlayerTabs)
.permission { Permission.EDIT_GAME.permission }.permissionMessage { language.noPermissionMessage.text }
.execute { sender, arena, team, player ->
joinGame(sender, player, arena.name, team)
}
}
subCommand("leave") {
noPermission()
toolTip { language.commandLeaveToolTip.text }
builder().executePlayer({ language.commandSenderHasToBePlayer.text }) { sender -> leaveGame(sender) }
.argument("player").validator(playerMustExist).tabs(onlinePlayerTabs)
.permission { Permission.EDIT_GAME.permission }.permissionMessage { language.noPermissionMessage.text }
.execute { _, player ->
leaveGame(player)
}
}
helpCommand()
subCommand("axe") {
Expand Down Expand Up @@ -599,12 +635,26 @@ class BlockBallCommandExecutor @Inject constructor(
sender.sendMessage(footerBuilder.toString())
}

private fun joinGame(player: Player, name: String, team: Team? = null) {
private fun joinGame(sender: CommandSender, player: Player, name: String, team: Team? = null): Boolean {
for (game in gameService.getAll()) {
if (game.getPlayers().contains(player)) {
if (game.arena.name.equals(name, true)) {
// Do not leave, if it is the same game.
return
// It is the same game.
val previousTeam = game.ingamePlayersStorage[player]!!.team

if (game.status == GameState.JOINABLE && team != null && previousTeam != team) {
// Switching teams.
game.leave(player)
val joinResult = joinGame(sender, player, name, team)

if (!joinResult) {
game.join(player, previousTeam)
return false
}
return true
}

return false
}

game.leave(player)
Expand All @@ -614,45 +664,45 @@ class BlockBallCommandExecutor @Inject constructor(
val game = gameService.getByName(name)

if (game == null) {
language.sendMessage(language.gameDoesNotExistMessage, player, name)
return
language.sendMessage(language.gameDoesNotExistMessage, sender, name)
return false
}

if (!player.hasPermission(
if (!sender.hasPermission(
Permission.JOIN.permission.replace(
"[name]", game.arena.name
)
) && !player.hasPermission(Permission.JOIN.permission.replace("[name]", "*"))
) && !sender.hasPermission(Permission.JOIN.permission.replace("[name]", "*"))
) {
language.sendMessage(language.noPermissionForGameMessage, player, game.arena.name)
return
language.sendMessage(language.noPermissionForGameMessage, sender, game.arena.name)
return false
}

if (team != null && team == Team.REFEREE) {
if (game !is SoccerRefereeGame) {
language.sendMessage(language.gameIsNotARefereeGame, player)
return
language.sendMessage(language.gameIsNotARefereeGame, sender)
return false
}

if (!player.hasPermission(Permission.REFEREE_JOIN.permission)) {
language.sendMessage(language.noPermissionForGameMessage, player, game.arena.name)
return
if (!sender.hasPermission(Permission.REFEREE_JOIN.permission)) {
language.sendMessage(language.noPermissionForGameMessage, sender, game.arena.name)
return false
}
}

val joinResult = game.join(player, team)

if (team != null && joinResult == JoinResult.TEAM_FULL) {
if (team == Team.BLUE) {
return joinGame(player, name, Team.RED)
return joinGame(sender, player, name, Team.RED)
} else {
return joinGame(player, name, Team.BLUE)
return joinGame(sender, player, name, Team.BLUE)
}
}

if (joinResult == JoinResult.GAME_FULL || joinResult == JoinResult.GAME_ALREADY_RUNNING) {
language.sendMessage(language.gameIsFullMessage, player)
return
language.sendMessage(language.gameIsFullMessage, sender)
return false
}

if (joinResult == JoinResult.SUCCESS_BLUE) {
Expand All @@ -662,6 +712,7 @@ class BlockBallCommandExecutor @Inject constructor(
} else if (joinResult == JoinResult.SUCCESS_REFEREE) {
language.sendMessage(language.joinTeamRefereeMessage, player)
}
return true
}

private fun leaveGame(player: Player) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ commandToggleToolTip:
text: "Enables or disables your game. If a game is disabled, nobody can join."
commandJoinToolTip:
type: "CHAT"
text: "Lets the player executing the command join the game. The optional team argument allows to directly join a specific team. If the team is full, the other team will be chosen. If no team is specified, a random team will be selected."
text: "Lets the player executing the command join the game. If no team is specified, a random team will be selected. If the player has already joined a game, this command can also be used to switch teams."
commandLeaveToolTip:
type: "CHAT"
text: "Lets the player executing the command leave the game."
Expand Down Expand Up @@ -306,3 +306,6 @@ commandPlaceHolderToolTip:
commandPlaceHolderMessage:
type: "CHAT"
text: "Evaluated placeholder: %1$1s"
playerNotFoundMessage:
type: "CHAT"
text: "&0&l[&f&lBlockBall&0&l]&c Player %1$1s not found."

0 comments on commit 1e85842

Please sign in to comment.