Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
join game by command & can set game time and waiting time
Browse files Browse the repository at this point in the history
  • Loading branch information
hirosukt committed Sep 25, 2022
1 parent 5542f55 commit 8a25041
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
20 changes: 12 additions & 8 deletions src/main/java/love/chihuyu/FishBattleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package love.chihuyu

import love.chihuyu.FishingBattle.Companion.plugin
import love.chihuyu.database.User
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.Sound
import org.bukkit.entity.Fish
import org.bukkit.scoreboard.Criteria
import org.bukkit.scoreboard.DisplaySlot
import org.jetbrains.exposed.sql.insert
Expand All @@ -14,8 +16,8 @@ import org.jetbrains.exposed.sql.update
object FishBattleManager {

fun updateScoreboard() {
plugin.server.onlinePlayers.forEach {
val board = it.scoreboard
FishData.data.keys.forEach {
val board = plugin.server.scoreboardManager!!.newScoreboard
board.getObjective(DisplaySlot.SIDEBAR)?.unregister()
val obj = board.registerNewObjective("fish_battle", Criteria.create("fished"), " ${ChatColor.GOLD}${ChatColor.BOLD}${ChatColor.ITALIC}Fish Battle${ChatColor.RESET} ")

Expand All @@ -38,6 +40,7 @@ object FishBattleManager {
scores.forEachIndexed { index, s -> obj.getScore(s).score = -index }

obj.displaySlot = DisplaySlot.SIDEBAR
Bukkit.getPlayer(it.uniqueId)?.scoreboard = board
}
}

Expand Down Expand Up @@ -65,17 +68,18 @@ object FishBattleManager {

fun broadcastRanking() {
val sortedList = FishData.data.toList().sortedByDescending { it.second }
plugin.server.onlinePlayers.forEach {
it.playSound(it, Sound.ENTITY_ENDER_DRAGON_DEATH, 1f, 1f)
it.sendTitle(
FishData.data.keys.forEach {
val player = Bukkit.getPlayer(it.uniqueId) ?: return@forEach
player.playSound(player, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1f, 1f)
player.sendTitle(
"${ChatColor.RED}${ChatColor.BOLD}${ChatColor.ITALIC}Game Over!",
"Winner is ${ChatColor.BOLD}${sortedList[0].first.name}",
20, 100, 20
)

transaction {
val user = User.select { User.uuid eq it.uniqueId }.single()
if (user[User.highscore] < (FishData.data[it] ?: 0)) it.sendMessage("${ChatColor.LIGHT_PURPLE}You beated high score!\n${user[User.highscore]}pt -> ${FishData.data[it]}pt")
if (user[User.highscore] < (FishData.data[it] ?: 0)) player.sendMessage("${ChatColor.LIGHT_PURPLE}You beated high score!\n${user[User.highscore]}pt -> ${FishData.data[it]}pt")
}
}

Expand All @@ -93,6 +97,6 @@ object FishBattleManager {
}

messages.add("\n")
plugin.server.broadcastMessage(messages.joinToString("\n"))
FishData.data.keys.forEach { (Bukkit.getPlayer(it.uniqueId) ?: return@forEach).sendMessage(messages.joinToString("\n") + "\n") }
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/love/chihuyu/FishingBattle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FishingBattle : JavaPlugin(), Listener {
val result = event.caught as? Item
val player = event.player

if (!isStarted || result == null) return
if (!isStarted || result == null || !FishData.data.containsKey(player)) return

val point = when (result.itemStack.type) {
Material.COD -> 1
Expand Down Expand Up @@ -110,8 +110,9 @@ class FishingBattle : JavaPlugin(), Listener {
}
}
}
if (!isStarted) return
if (player !in FishData.data) FishData.data[player] = 0
FishBattleManager.updateScoreboard()
server.bossBars.forEach { it.removePlayer(player) }
}
}
}
74 changes: 54 additions & 20 deletions src/main/java/love/chihuyu/commands/CommandFishBattle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package love.chihuyu.commands

import dev.jorel.commandapi.CommandAPICommand
import dev.jorel.commandapi.CommandPermission
import dev.jorel.commandapi.arguments.IntegerArgument
import dev.jorel.commandapi.executors.PlayerCommandExecutor
import love.chihuyu.FishBattleManager
import love.chihuyu.FishData
Expand All @@ -10,6 +11,10 @@ import love.chihuyu.FishingBattle.Companion.isStarted
import love.chihuyu.FishingBattle.Companion.owner
import love.chihuyu.FishingBattle.Companion.plugin
import love.chihuyu.utils.runTaskTimer
import net.md_5.bungee.api.chat.ClickEvent
import net.md_5.bungee.api.chat.HoverEvent
import net.md_5.bungee.api.chat.TextComponent
import net.md_5.bungee.api.chat.hover.content.Text
import org.bukkit.Bukkit
import org.bukkit.ChatColor
import org.bukkit.NamespacedKey
Expand All @@ -28,25 +33,36 @@ object CommandFishBattle {
CommandAPICommand("start")
.withPermission("fishbattle.start")
.withPermission(CommandPermission.NONE)
.withArguments(IntegerArgument("waitingSeconds"), IntegerArgument("gameSeconds"))
.executesPlayer(
PlayerCommandExecutor { sender, args ->
if (isStarted) {
sender.sendMessage("${ChatColor.RED}Game is already started.")
return@PlayerCommandExecutor
}

var remainCountdown = 6
plugin.server.onlinePlayers.forEach {
it.spigot().sendMessage(
TextComponent("${sender.name} started new game. ${ChatColor.GREEN}${ChatColor.BOLD}[Click to join]").apply {
this.clickEvent = ClickEvent(ClickEvent.Action.RUN_COMMAND, "/fb join")
this.hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, Text("Click to join game"))
}
)
}

var remainCountdown = args[0] as Int
val gameTime = args[1] as Int

plugin.runTaskTimer(0, 20) countdown@{
remainCountdown--

if (remainCountdown == 0) {
val endEpoch = nowEpoch() + 180
val endEpoch = nowEpoch() + gameTime
isStarted = true

plugin.server.onlinePlayers.forEach {
it.playSound(it, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1f, 1f)
FishData.data[it] = 0
FishData.data.keys.forEach {
val player = Bukkit.getPlayer(it.uniqueId) ?: return@forEach
player.playSound(player, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1f, 1f)
}

owner = sender
Expand All @@ -62,11 +78,12 @@ object CommandFishBattle {
BarStyle.SEGMENTED_6
)

bossBar.progress = (1.0 / 180.0) * (endEpoch - nowEpoch())
bossBar.progress = (1.0 / gameTime) * (endEpoch - nowEpoch())
bossBar.isVisible = true

plugin.server.onlinePlayers.forEach {
bossBar.addPlayer(it)
FishData.data.keys.forEach {
val player = Bukkit.getPlayer(it.uniqueId) ?: return@forEach
bossBar.addPlayer(player)
}

FishBattleManager.updateScoreboard()
Expand All @@ -92,15 +109,18 @@ object CommandFishBattle {
return@countdown
}

plugin.server.onlinePlayers.forEach {
it.playSound(it, Sound.UI_BUTTON_CLICK, 1f, 1f)
it.sendTitle(
"${ChatColor.LIGHT_PURPLE}${ChatColor.BOLD}${ChatColor.ITALIC}$remainCountdown",
"Fishing Battle",
0,
20,
0
)
if (remainCountdown <= 5) {
FishData.data.keys.forEach {
val player = Bukkit.getPlayer(it.uniqueId) ?: return@forEach
player.playSound(player, Sound.UI_BUTTON_CLICK, 1f, 1f)
player.sendTitle(
"${ChatColor.LIGHT_PURPLE}${ChatColor.BOLD}${ChatColor.ITALIC}$remainCountdown",
"Fishing Battle",
0,
20,
0
)
}
}
}
}
Expand Down Expand Up @@ -131,14 +151,28 @@ object CommandFishBattle {
isStarted = false
FishBattleManager.updateScoreboard()
}
)
),
CommandAPICommand("join")
.withPermission("fishbattle.join")
.withPermission(CommandPermission.NONE)
.executesPlayer(PlayerCommandExecutor { sender, args ->
FishData.data[sender] = 0
sender.sendMessage("${ChatColor.GREEN}You joined game.")
}),
CommandAPICommand("leave")
.withPermission("fishbattle.leave")
.withPermission(CommandPermission.NONE)
.executesPlayer(PlayerCommandExecutor { sender, args ->
FishData.data.remove(sender)
sender.sendMessage("${ChatColor.RED}You left game.")
})
)

private fun formatTime(timeSeconds: Long): String {
return "${"%02d".format(timeSeconds.floorDiv(60))}:" + "%02d".format(timeSeconds % 60)
return "${"%02d".format(timeSeconds.floorDiv(3600))}:${"%02d".format(timeSeconds.floorDiv(60))}:${"%02d".format(timeSeconds % 60)}"
}

private fun nowEpoch(): Long {
return Instant.now().epochSecond
}
}
}

0 comments on commit 8a25041

Please sign in to comment.