diff --git a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt index e4e5282..83cbd98 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt @@ -32,7 +32,6 @@ import dev.nikomaru.raceassist.race.event.SetCentralPointEvent import dev.nikomaru.raceassist.race.event.SetInsideCircuitEvent import dev.nikomaru.raceassist.race.event.SetOutsideCircuitEvent import dev.nikomaru.raceassist.utils.Lang -import org.bukkit.Bukkit import org.bukkit.configuration.file.YamlConfiguration import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.selectAll @@ -91,9 +90,9 @@ class RaceAssist : SuspendingJavaPlugin() { } private fun registerEvents() { - Bukkit.getPluginManager().registerEvents(SetInsideCircuitEvent(), this) - Bukkit.getPluginManager().registerEvents(SetOutsideCircuitEvent(), this) - Bukkit.getPluginManager().registerEvents(SetCentralPointEvent(), this) + server.pluginManager.registerSuspendingEvents(SetInsideCircuitEvent(), this) + server.pluginManager.registerSuspendingEvents(SetOutsideCircuitEvent(), this) + server.pluginManager.registerSuspendingEvents(SetCentralPointEvent(), this) server.pluginManager.registerSuspendingEvents(BetGuiClickEvent(), this) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/OpenBetGuiCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/OpenBetGuiCommand.kt index 1dc39ee..c478d4d 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/OpenBetGuiCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/OpenBetGuiCommand.kt @@ -21,17 +21,20 @@ import co.aikar.commands.annotation.CommandAlias import co.aikar.commands.annotation.CommandCompletion import co.aikar.commands.annotation.Single import co.aikar.commands.annotation.Subcommand +import com.github.shynixn.mccoroutine.launch +import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.bet.gui.BetChestGui import dev.nikomaru.raceassist.bet.gui.BetChestGui.Companion.AllPlayers import dev.nikomaru.raceassist.database.BetSetting import dev.nikomaru.raceassist.database.TempBetData import dev.nikomaru.raceassist.utils.Lang +import kotlinx.coroutines.Dispatchers import org.bukkit.entity.Player import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction @CommandAlias("ra|RaceAssist") class OpenBetGuiCommand : BaseCommand() { @@ -39,41 +42,39 @@ class OpenBetGuiCommand : BaseCommand() { @Subcommand("bet open") @CommandCompletion("@RaceID") fun openVending(player: Player, @Single raceID: String) { - if (!raceExist(raceID)) { - player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) - return - } - val vending = BetChestGui() - val canBet = transaction { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.canBet] } - if (!canBet) { - player.sendMessage(Lang.getText("now-cannot-bet-race", player.locale())) - return - } - - transaction { - TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } - player.openInventory(vending.getGUI(player, raceID)) + plugin!!.launch { + if (!raceExist(raceID)) { + player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) + return@launch + } + val vending = BetChestGui() + val canBet = newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.canBet] } + if (!canBet) { + player.sendMessage(Lang.getText("now-cannot-bet-race", player.locale())) + return@launch + } - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } + player.openInventory(vending.getGUI(player, raceID)) + } - transaction { - AllPlayers[raceID]?.forEach { jockey -> - TempBetData.insert { - it[TempBetData.raceID] = raceID - it[playerUUID] = player.uniqueId.toString() - it[TempBetData.jockey] = jockey.toString() - it[bet] = 0 + newSuspendedTransaction(Dispatchers.IO) { + AllPlayers[raceID]?.forEach { jockey -> + TempBetData.insert { + it[TempBetData.raceID] = raceID + it[playerUUID] = player.uniqueId.toString() + it[TempBetData.jockey] = jockey.toString() + it[bet] = 0 + } } } - } + } } - private fun raceExist(raceID: String): Boolean { - var exist = false - transaction { - exist = BetSetting.select { BetSetting.raceID eq raceID }.count() > 0 - } - return exist + private suspend fun raceExist(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + BetSetting.select { BetSetting.raceID eq raceID }.count() > 0 } + } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/SetBetCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/SetBetCommand.kt index 39388e4..49c7ff0 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/SetBetCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/SetBetCommand.kt @@ -36,7 +36,6 @@ import org.bukkit.entity.Player import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.update import java.text.MessageFormat import java.util.* @@ -151,7 +150,7 @@ class SetBetCommand : BaseCommand() { if (canRevert[player.uniqueId] == true) { - transaction { + newSuspendedTransaction(Dispatchers.Main) { BetList.select { BetList.raceID eq raceID }.forEach { @@ -250,7 +249,8 @@ class SetBetCommand : BaseCommand() { } } - private fun getRaceCreator(raceID: String) = transaction { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator] } + private suspend fun getRaceCreator(raceID: String) = + newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator] } private suspend fun raceExist(raceID: String): Boolean { var exist = false diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt index 65dba01..1bff3c0 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt @@ -46,7 +46,6 @@ import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.inventory.ItemStack import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.transactions.transaction import java.text.MessageFormat import java.time.LocalDateTime import java.util.* @@ -90,18 +89,17 @@ class BetGuiClickEvent : Listener { val selectedNowBet: Int = getNowBet(raceID, player, slot) player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 1.0f) - withContext(Dispatchers.IO) { - transaction { - TempBetData.update({ - (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get( - slot - ).toString()) - }) { - it[bet] = selectedNowBet + 10 - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.update({ + (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get( + slot + ).toString()) + }) { + it[bet] = selectedNowBet + 10 } } + val selectedAfterBet: Int = getNowBet(raceID, player, (slot)) val item = event.inventory.getItem(slot + 18)!! val itemMeta = item.itemMeta @@ -125,17 +123,16 @@ class BetGuiClickEvent : Listener { val selectedNowBet: Int = getNowBet(raceID, player, (slot - 9)) player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f) - withContext(Dispatchers.IO) { - transaction { - TempBetData.update({ - (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get( - slot - 9 - ).toString()) - }) { - it[bet] = selectedNowBet + 1 - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.update({ + (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.jockey eq AllPlayers[raceID]?.get( + slot - 9 + ).toString()) + }) { + it[bet] = selectedNowBet + 1 } } + val selectedAfterBet: Int = getNowBet(raceID, player, (slot - 9)) val item = event.inventory.getItem(slot + 9)!! val itemMeta = item.itemMeta @@ -168,17 +165,16 @@ class BetGuiClickEvent : Listener { } player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 0.7f) val uuid = player.uniqueId.toString() - withContext(Dispatchers.IO) { - transaction { - TempBetData.update({ - (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get( - slot - 27 - ).toString()) - }) { - it[bet] = selectedNowBet - 1 - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.update({ + (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get( + slot - 27 + ).toString()) + }) { + it[bet] = selectedNowBet - 1 } } + val selectedAfterBet: Int = getNowBet(raceID, player, (slot - 27)) val item = event.inventory.getItem(slot - 9)!! val itemMeta = item.itemMeta @@ -211,15 +207,13 @@ class BetGuiClickEvent : Listener { } player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 0.7f) val uuid = player.uniqueId.toString() - withContext(Dispatchers.IO) { - transaction { - TempBetData.update({ - (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get( - slot - 36 - ).toString()) - }) { - it[bet] = selectedNowBet - 10 - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.update({ + (TempBetData.raceID eq raceID) and (TempBetData.playerUUID eq uuid) and (TempBetData.jockey eq AllPlayers[raceID]?.get( + slot - 36 + ).toString()) + }) { + it[bet] = selectedNowBet - 10 } } @@ -242,19 +236,18 @@ class BetGuiClickEvent : Listener { //clear player.playSound(player.location, Sound.UI_BUTTON_CLICK, 1f, 1f) val uuid = player.uniqueId.toString() - withContext(Dispatchers.IO) { - transaction { - TempBetData.deleteWhere { (TempBetData.playerUUID eq uuid) and (TempBetData.raceID eq raceID) } - AllPlayers[raceID]?.forEach { jockey -> - TempBetData.insert { - it[TempBetData.raceID] = raceID - it[playerUUID] = uuid - it[TempBetData.jockey] = jockey.toString() - it[bet] = 0 - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.deleteWhere { (TempBetData.playerUUID eq uuid) and (TempBetData.raceID eq raceID) } + AllPlayers[raceID]?.forEach { jockey -> + TempBetData.insert { + it[TempBetData.raceID] = raceID + it[playerUUID] = uuid + it[TempBetData.jockey] = jockey.toString() + it[bet] = 0 } } } + for (i in 0 until limit + 1) { val item = event.inventory.getItem(i + 18)!! val itemMeta = item.itemMeta @@ -272,10 +265,8 @@ class BetGuiClickEvent : Listener { player.closeInventory() player.playSound(player.location, Sound.UI_BUTTON_CLICK, 0.5f, 1f) - withContext(Dispatchers.IO) { - transaction { - TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } - } + newSuspendedTransaction(Dispatchers.IO) { + TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } } } @@ -299,27 +290,26 @@ class BetGuiClickEvent : Listener { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.creator] })) - withContext(Dispatchers.Default) { - transaction { - var row = BetList.selectAll().count().toInt() - TempBetData.select { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } - .forEach { temp -> - if (temp[TempBetData.bet] != 0) { - BetList.insert { bet -> - bet[BetList.raceID] = raceID - bet[playerName] = player.name - bet[playerUUID] = player.uniqueId.toString() - bet[jockey] = Bukkit.getOfflinePlayer(UUID.fromString(temp[TempBetData.jockey])).name.toString() - bet[betting] = temp[TempBetData.bet] * betUnit - bet[timeStamp] = LocalDateTime.now() - bet[rowNum] = row + 1 - } - betProcess(player, row, temp, eco, owner) - row++ + + newSuspendedTransaction(Dispatchers.Default) { + var row = BetList.selectAll().count().toInt() + TempBetData.select { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } + .forEach { temp -> + if (temp[TempBetData.bet] != 0) { + BetList.insert { bet -> + bet[BetList.raceID] = raceID + bet[playerName] = player.name + bet[playerUUID] = player.uniqueId.toString() + bet[jockey] = Bukkit.getOfflinePlayer(UUID.fromString(temp[TempBetData.jockey])).name.toString() + bet[betting] = temp[TempBetData.bet] * betUnit + bet[timeStamp] = LocalDateTime.now() + bet[rowNum] = row + 1 } + betProcess(player, row, temp, eco, owner) + row++ } - TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } - } + } + TempBetData.deleteWhere { (TempBetData.playerUUID eq player.uniqueId.toString()) and (TempBetData.raceID eq raceID) } } putSheetsData(raceID) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt index 9aef786..310c798 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt @@ -16,13 +16,16 @@ package dev.nikomaru.raceassist.bet.gui +import com.github.shynixn.mccoroutine.launch import com.google.common.collect.ImmutableList +import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.bet.GuiComponent import dev.nikomaru.raceassist.database.BetList import dev.nikomaru.raceassist.database.BetSetting import dev.nikomaru.raceassist.database.PlayerList import dev.nikomaru.raceassist.files.Config.betUnit import dev.nikomaru.raceassist.utils.Lang +import kotlinx.coroutines.Dispatchers import net.kyori.adventure.text.Component import net.kyori.adventure.text.Component.text import net.kyori.adventure.text.format.TextColor @@ -33,7 +36,7 @@ import org.bukkit.inventory.Inventory import org.bukkit.inventory.ItemStack import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.text.MessageFormat import java.util.* import kotlin.math.floor @@ -41,7 +44,6 @@ import kotlin.math.floor class BetChestGui { fun getGUI(player: Player, raceID: String): Inventory { - val gui = Bukkit.createInventory(player, 45, GuiComponent.guiComponent()) val playerWools = ImmutableList.of( Material.RED_WOOL, @@ -52,31 +54,35 @@ class BetChestGui { Material.PINK_WOOL, Material.WHITE_WOOL ) - val rate: Int = transaction { - BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.returnPercent] - } val players: ArrayList = ArrayList() val odds: HashMap = HashMap() - AllPlayers[raceID] = ArrayList() - transaction { - PlayerList.select { PlayerList.raceID eq raceID }.forEach { - players.add(UUID.fromString(it[PlayerList.playerUUID])) - AllPlayers[raceID]!!.add(UUID.fromString(it[PlayerList.playerUUID])) - } - } var sum = 0 - transaction { - BetList.select { BetList.raceID eq raceID }.forEach { - sum += it[BetList.betting] + plugin!!.launch { + val rate: Int = newSuspendedTransaction(Dispatchers.IO) { + BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.returnPercent] } - } - players.forEach { jockey -> - transaction { - var jockeySum = 0 - BetList.select { (BetList.raceID eq raceID) and (BetList.jockey eq Bukkit.getOfflinePlayer(jockey).name!!) }.forEach { - jockeySum += it[BetList.betting] + + AllPlayers[raceID] = ArrayList() + newSuspendedTransaction(Dispatchers.IO) { + PlayerList.select { PlayerList.raceID eq raceID }.forEach { + players.add(UUID.fromString(it[PlayerList.playerUUID])) + AllPlayers[raceID]!!.add(UUID.fromString(it[PlayerList.playerUUID])) + } + } + + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { BetList.raceID eq raceID }.forEach { + sum += it[BetList.betting] + } + } + players.forEach { jockey -> + newSuspendedTransaction(Dispatchers.IO) { + var jockeySum = 0 + BetList.select { (BetList.raceID eq raceID) and (BetList.jockey eq Bukkit.getOfflinePlayer(jockey).name!!) }.forEach { + jockeySum += it[BetList.betting] + } + odds[jockey] = floor(((sum * (rate.toDouble() / 100)) / jockeySum) * 100) / 100 } - odds[jockey] = floor(((sum * (rate.toDouble() / 100)) / jockeySum) * 100) / 100 } } @@ -116,6 +122,7 @@ class BetChestGui { gui.setItem(i + 27, GuiComponent.onceDown(player.locale())) gui.setItem(i + 36, GuiComponent.tenTimesDown(player.locale())) } + val raceIDItem = ItemStack(Material.GRAY_STAINED_GLASS_PANE) val raceIDMeta = raceIDItem.itemMeta raceIDMeta.displayName(text(raceID, TextColor.fromHexString("#00ff7f"))) @@ -126,7 +133,10 @@ class BetChestGui { gui.setItem(35, GuiComponent.deny(player.locale())) gui.setItem(44, GuiComponent.accept(player.locale())) + + return gui + } companion object { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/AudiencesCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/AudiencesCommand.kt index b30c5b4..dcced9f 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/AudiencesCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/AudiencesCommand.kt @@ -21,10 +21,13 @@ import co.aikar.commands.annotation.CommandAlias import co.aikar.commands.annotation.CommandCompletion import co.aikar.commands.annotation.Single import co.aikar.commands.annotation.Subcommand +import com.github.shynixn.mccoroutine.launch +import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.database.PlayerList import dev.nikomaru.raceassist.database.PlayerList.playerUUID import dev.nikomaru.raceassist.database.RaceList import dev.nikomaru.raceassist.utils.Lang +import kotlinx.coroutines.Dispatchers import net.kyori.adventure.text.Component.text import net.kyori.adventure.text.format.NamedTextColor.GREEN import net.kyori.adventure.text.format.NamedTextColor.RED @@ -33,7 +36,7 @@ import org.bukkit.Bukkit import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.util.* @CommandAlias("ra|RaceAssist") @@ -43,19 +46,21 @@ class AudiencesCommand : BaseCommand() { @Subcommand("join") @CommandCompletion("@RaceID") private fun join(sender: Player, @Single raceID: String) { - if (!getRaceExist(raceID)) { - sender.sendMessage(text(Lang.getText("not-found-this-race", sender.locale()), TextColor.color(RED))) - return - } - if (audience[raceID]?.contains(sender.uniqueId) == true) { - sender.sendMessage(text(Lang.getText("already-joined", sender.locale()), TextColor.color(RED))) - return - } - if (!audience.containsKey(raceID)) { - audience[raceID] = ArrayList() + plugin!!.launch { + if (!getRaceExist(raceID)) { + sender.sendMessage(text(Lang.getText("not-found-this-race", sender.locale()), TextColor.color(RED))) + return@launch + } + if (audience[raceID]?.contains(sender.uniqueId) == true) { + sender.sendMessage(text(Lang.getText("already-joined", sender.locale()), TextColor.color(RED))) + return@launch + } + if (!audience.containsKey(raceID)) { + audience[raceID] = ArrayList() + } + audience[raceID]?.add(sender.uniqueId) + sender.sendMessage(text(Lang.getText("joined-group", sender.locale()), TextColor.color(GREEN))) } - audience[raceID]?.add(sender.uniqueId) - sender.sendMessage(text(Lang.getText("joined-group", sender.locale()), TextColor.color(GREEN))) } @Subcommand("leave") @@ -72,25 +77,23 @@ class AudiencesCommand : BaseCommand() { @Subcommand("list") @CommandCompletion("@RaceID") private fun list(sender: CommandSender, @Single raceID: String) { - val player = sender as Player - if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { - player.sendMessage(text(Lang.getText("only-race-creator-can-display", sender.locale()), TextColor.color(RED))) - return - } - sender.sendMessage(text(Lang.getText("participants-list", sender.locale()), TextColor.color(GREEN))) - transaction { - PlayerList.select { PlayerList.raceID eq raceID }.forEach { - sender.sendMessage(text(Bukkit.getOfflinePlayer(UUID.fromString(it[playerUUID])).name!!, TextColor.color(GREEN))) + plugin!!.launch { + val player = sender as Player + if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage(text(Lang.getText("only-race-creator-can-display", sender.locale()), TextColor.color(RED))) + return@launch + } + sender.sendMessage(text(Lang.getText("participants-list", sender.locale()), TextColor.color(GREEN))) + newSuspendedTransaction(Dispatchers.IO) { + PlayerList.select { PlayerList.raceID eq raceID }.forEach { + sender.sendMessage(text(Bukkit.getOfflinePlayer(UUID.fromString(it[playerUUID])).name!!, TextColor.color(GREEN))) + } } } } - private fun getRaceExist(raceID: String): Boolean { - var raceExist = false - transaction { - raceExist = RaceList.select { RaceList.raceID eq raceID }.count() > 0 - } - return raceExist + private suspend fun getRaceExist(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + RaceList.select { RaceList.raceID eq raceID }.count() > 0 } companion object { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlaceCommands.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlaceCommands.kt index 1372f75..59edb18 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlaceCommands.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlaceCommands.kt @@ -26,6 +26,7 @@ import dev.nikomaru.raceassist.race.commands.RaceCommand.Companion.getReverse import dev.nikomaru.raceassist.race.utils.InsideCircuit import dev.nikomaru.raceassist.race.utils.OutsideCircuit import dev.nikomaru.raceassist.utils.Lang +import kotlinx.coroutines.Dispatchers import net.kyori.adventure.text.Component.text import net.kyori.adventure.text.format.NamedTextColor.GREEN import net.kyori.adventure.text.format.NamedTextColor.RED @@ -34,7 +35,7 @@ import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.update import java.util.* import kotlin.math.atan2 @@ -47,28 +48,31 @@ class PlaceCommands : BaseCommand() { @Subcommand("reverse") @CommandCompletion("@RaceID") fun reverse(sender: Player, @Single raceID: String) { - val player = sender - if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { - player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) - return - } - val nowDirection = getDirection(raceID) + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { + sender.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) + return@launch + } + val nowDirection = getDirection(raceID) - transaction { - RaceList.update({ RaceList.raceID eq raceID }) { - it[reverse] = !nowDirection + newSuspendedTransaction(Dispatchers.IO) { + RaceList.update({ RaceList.raceID eq raceID }) { + it[reverse] = !nowDirection + } } + sender.sendMessage(text(Lang.getText("to-change-race-orientation", sender.locale()), TextColor.color(GREEN))) } - sender.sendMessage(text(Lang.getText("to-change-race-orientation", sender.locale()), TextColor.color(GREEN))) } @Subcommand("central") @CommandCompletion("@RaceID") fun central(sender: CommandSender, @Single raceID: String) { val player = sender as Player - if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { - player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) - return + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) + return@launch + } } canSetCentral[player.uniqueId] = true centralRaceID[player.uniqueId] = raceID @@ -77,62 +81,68 @@ class PlaceCommands : BaseCommand() { @Subcommand("degree") @CommandCompletion("@RaceID") - fun degree(sender: CommandSender, @Single raceID: String) { - val player = sender as Player - if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { - player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) - return - } - val centralXPoint = getCentralPoint(raceID, true) ?: return sender.sendMessage( - text( - Lang.getText("no-exist-central-point", sender.locale()), TextColor.color - (RED) + fun degree(player: Player, @Single raceID: String) { + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage(text(Lang.getText("only-race-creator-can-setting", player.locale()), TextColor.color(RED))) + return@launch + } + val centralXPoint = getCentralPoint(raceID, true) ?: return@launch player.sendMessage( + text( + Lang.getText("no-exist-central-point", player.locale()), TextColor.color + (RED) + ) ) - ) - val centralYPoint = getCentralPoint(raceID, false) ?: return sender.sendMessage( - text( - Lang.getText("no-exist-central-point", sender.locale()), TextColor.color - (RED) + val centralYPoint = getCentralPoint(raceID, false) ?: return@launch player.sendMessage( + text( + Lang.getText("no-exist-central-point", player.locale()), TextColor.color + (RED) + ) ) - ) - val reverse = - getReverse(raceID) ?: return sender.sendMessage(text(Lang.getText("orientation-is-not-set", sender.locale()), TextColor.color(RED))) - var nowX = player.location.blockX - centralXPoint - val nowY = player.location.blockZ - centralYPoint - if (reverse) { - nowX = -nowX - } - val currentDegree = if (Math.toDegrees(atan2(nowY.toDouble(), nowX.toDouble())).toInt() < 0) { - 360 + Math.toDegrees(atan2(nowY.toDouble(), nowX.toDouble())).toInt() - } else { - Math.toDegrees(atan2(nowY.toDouble(), nowX.toDouble())).toInt() - } - var degree = 0 - when (currentDegree) { - in 0..45 -> { - player.sendMessage(text(Lang.getText("to-set-0-degree", sender.locale()), TextColor.color(GREEN))) - degree = 0 - } - in 46..135 -> { - player.sendMessage(text(Lang.getText("to-set-90-degree", sender.locale()), TextColor.color(GREEN))) - degree = 90 + val reverse = + getReverse(raceID) ?: return@launch player.sendMessage( + text( + Lang.getText("orientation-is-not-set", player.locale()), + TextColor.color(RED) + ) + ) + var nowX = player.location.blockX - centralXPoint + val nowY = player.location.blockZ - centralYPoint + if (reverse) { + nowX = -nowX } - in 136..225 -> { - player.sendMessage(text(Lang.getText("to-set-180-degree", sender.locale()), TextColor.color(GREEN))) - degree = 180 + val currentDegree = if (Math.toDegrees(atan2(nowY.toDouble(), nowX.toDouble())).toInt() < 0) { + 360 + Math.toDegrees(atan2(nowY.toDouble(), nowX.toDouble())).toInt() + } else { + Math.toDegrees(atan2(nowY.toDouble(), nowX.toDouble())).toInt() } - in 226..315 -> { - player.sendMessage(text(Lang.getText("to-set-270-degree", sender.locale()), TextColor.color(GREEN))) - degree = 270 + var degree = 0 + when (currentDegree) { + in 0..45 -> { + player.sendMessage(text(Lang.getText("to-set-0-degree", player.locale()), TextColor.color(GREEN))) + degree = 0 + } + in 46..135 -> { + player.sendMessage(text(Lang.getText("to-set-90-degree", player.locale()), TextColor.color(GREEN))) + degree = 90 + } + in 136..225 -> { + player.sendMessage(text(Lang.getText("to-set-180-degree", player.locale()), TextColor.color(GREEN))) + degree = 180 + } + in 226..315 -> { + player.sendMessage(text(Lang.getText("to-set-270-degree", player.locale()), TextColor.color(GREEN))) + degree = 270 + } + in 316..360 -> { + player.sendMessage(text(Lang.getText("to-set-0-degree", player.locale()), TextColor.color(GREEN))) + degree = 0 + } } - in 316..360 -> { - player.sendMessage(text(Lang.getText("to-set-0-degree", sender.locale()), TextColor.color(GREEN))) - degree = 0 - } - } - transaction { - RaceList.update({ RaceList.raceID eq raceID }) { - it[goalDegree] = degree + newSuspendedTransaction(Dispatchers.IO) { + RaceList.update({ RaceList.raceID eq raceID }) { + it[goalDegree] = degree + } } } } @@ -142,53 +152,58 @@ class PlaceCommands : BaseCommand() { @Syntax("[RaceID] ") fun setLap(sender: CommandSender, @Single raceID: String, @Single lap: Int) { val player = sender as Player - if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { - player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) - return - } - if (lap < 1) { - player.sendMessage(text(Lang.getText("to-need-enter-over-1", sender.locale()), TextColor.color(RED))) - return - } - transaction { - RaceList.update({ RaceList.raceID eq raceID }) { - it[this.lap] = lap + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) + return@launch + } + + if (lap < 1) { + player.sendMessage(text(Lang.getText("to-need-enter-over-1", sender.locale()), TextColor.color(RED))) + return@launch } + newSuspendedTransaction(Dispatchers.IO) { + RaceList.update({ RaceList.raceID eq raceID }) { + it[this.lap] = lap + } + } + player.sendMessage(text(Lang.getText("to-set-lap", sender.locale()), TextColor.color(GREEN))) } - player.sendMessage(text(Lang.getText("to-set-lap", sender.locale()), TextColor.color(GREEN))) } @Subcommand("set") @CommandCompletion("@RaceID in|out") - fun set(sender: CommandSender, @Single raceID: String, @Single type: String) { - val player = sender as Player + fun set(player: Player, @Single raceID: String, @Single type: String) { + plugin!!.launch { - if (RaceCommand.getRaceCreator(raceID) == null) { - player.sendMessage(text(Lang.getText("no-exist-race", sender.locale()), TextColor.color(RED))) - return - } else if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { - player.sendMessage(text(Lang.getText("only-race-creator-can-setting", sender.locale()), TextColor.color(RED))) - return - } - if (canSetOutsideCircuit[player.uniqueId] != null || canSetInsideCircuit[player.uniqueId] != null) { - player.sendMessage(Lang.getText("already-setting-mode", sender.locale())) - return - } - if (type == "in") { - canSetInsideCircuit[player.uniqueId] = true - player.sendMessage(text(Lang.getText("to-be-inside-set-mode", sender.locale()), TextColor.color(GREEN))) - } else if (type == "out") { + if (RaceCommand.getRaceCreator(raceID) == null) { + player.sendMessage(text(Lang.getText("no-exist-race", player.locale()), TextColor.color(RED))) + return@launch + } else if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage(text(Lang.getText("only-race-creator-can-setting", player.locale()), TextColor.color(RED))) + return@launch + } - if (!getInsideRaceExist(raceID)) { - player.sendMessage(Lang.getText("no-inside-course-set", sender.locale())) - return + if (canSetOutsideCircuit[player.uniqueId] != null || canSetInsideCircuit[player.uniqueId] != null) { + player.sendMessage(Lang.getText("already-setting-mode", player.locale())) + return@launch } - canSetOutsideCircuit[player.uniqueId] = true - player.sendMessage(text(Lang.getText("to-be-outside-set-mode", sender.locale()), TextColor.color(GREEN))) + if (type == "in") { + canSetInsideCircuit[player.uniqueId] = true + player.sendMessage(text(Lang.getText("to-be-inside-set-mode", player.locale()), TextColor.color(GREEN))) + } else if (type == "out") { + + if (!getInsideRaceExist(raceID)) { + player.sendMessage(Lang.getText("no-inside-course-set", player.locale())) + return@launch + } + canSetOutsideCircuit[player.uniqueId] = true + player.sendMessage(text(Lang.getText("to-be-outside-set-mode", player.locale()), TextColor.color(GREEN))) + } + circuitRaceID[player.uniqueId] = raceID + player.sendMessage(text(Lang.getText("to-click-left-start-right-finish", player.locale()), TextColor.color(GREEN))) + player.sendMessage(Lang.getText("to-enter-finish-message", player.locale())) } - circuitRaceID[player.uniqueId] = raceID - player.sendMessage(text(Lang.getText("to-click-left-start-right-finish", sender.locale()), TextColor.color(GREEN))) - player.sendMessage(Lang.getText("to-enter-finish-message", sender.locale())) } @Subcommand("finish") @@ -212,20 +227,12 @@ class PlaceCommands : BaseCommand() { } } - private fun getDirection(raceID: String): Boolean { - var direction = false - transaction { - direction = RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.reverse) == true - } - return direction + private suspend fun getDirection(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.reverse) == true } - private fun getInsideRaceExist(raceID: String): Boolean { - var existRaceInside = false - transaction { - existRaceInside = CircuitPoint.select { (CircuitPoint.raceID eq raceID) and (CircuitPoint.inside eq true) }.count() > 0 - } - return existRaceInside + private suspend fun getInsideRaceExist(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + CircuitPoint.select { (CircuitPoint.raceID eq raceID) and (CircuitPoint.inside eq true) }.count() > 0 } companion object { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlayerCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlayerCommand.kt index 34de09d..cf0c8d2 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlayerCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlayerCommand.kt @@ -19,8 +19,11 @@ package dev.nikomaru.raceassist.race.commands import co.aikar.commands.BaseCommand import co.aikar.commands.annotation.* import co.aikar.commands.bukkit.contexts.OnlinePlayer +import com.github.shynixn.mccoroutine.launch +import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.database.PlayerList import dev.nikomaru.raceassist.utils.Lang +import kotlinx.coroutines.Dispatchers import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.NamedTextColor import net.kyori.adventure.text.format.TextColor @@ -30,7 +33,7 @@ import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.text.MessageFormat import java.util.* @@ -41,31 +44,38 @@ class PlayerCommand : BaseCommand() { @CommandPermission("RaceAssist.commands.player") @Subcommand("add") @CommandCompletion("@RaceID @players") - private fun addPlayer(sender: Player, raceID: String, @Single onlinePlayer: OnlinePlayer) { + private fun addPlayer(player: Player, raceID: String, @Single onlinePlayer: OnlinePlayer) { - val player: Player = onlinePlayer.player - if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { - sender.sendMessage(Component.text(Lang.getText("only-race-creator-can-setting", player.locale()), TextColor.color(NamedTextColor.RED))) - return - } - if (getRacePlayerExist(raceID, player.uniqueId)) { - sender.sendMessage(Component.text(Lang.getText("already-exist-this-user", player.locale()), TextColor.color(NamedTextColor.YELLOW))) - return - } - if (getRacePlayerAmount() >= 8) { - sender.sendMessage(Component.text(Lang.getText("max-player-is-eight", player.locale()), TextColor.color(NamedTextColor.RED))) - return - } - transaction { - PlayerList.insert { - it[this.raceID] = raceID - it[playerUUID] = player.uniqueId.toString() + plugin!!.launch { + val jockey = onlinePlayer.player + if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage( + Component.text( + Lang.getText("only-race-creator-can-setting", player.locale()), + TextColor.color(NamedTextColor.RED) + ) + ) + return@launch } + if (getRacePlayerExist(raceID, jockey.uniqueId)) { + player.sendMessage(Component.text(Lang.getText("already-exist-this-user", player.locale()), TextColor.color(NamedTextColor.YELLOW))) + return@launch + } + if (getRacePlayerAmount() >= 8) { + player.sendMessage(Component.text(Lang.getText("max-player-is-eight", player.locale()), TextColor.color(NamedTextColor.RED))) + return@launch + } + newSuspendedTransaction(Dispatchers.IO) { + PlayerList.insert { + it[this.raceID] = raceID + it[playerUUID] = jockey.uniqueId.toString() + } + } + player.sendMessage(MessageFormat.format(Lang.getText("player-add-to-race-group", player.locale()), player.name, raceID)) } - sender.sendMessage(MessageFormat.format(Lang.getText("player-add-to-race-group", player.locale()), player.name, raceID)) } - private fun getRacePlayerAmount(): Long = transaction { + private suspend fun getRacePlayerAmount(): Long = newSuspendedTransaction { PlayerList.select { PlayerList.raceID eq "raceID" }.count() @@ -75,62 +85,68 @@ class PlayerCommand : BaseCommand() { @Subcommand("remove") @CommandCompletion("@RaceID") private fun removePlayer(sender: Player, @Single raceID: String, @Single onlinePlayer: OnlinePlayer) { - if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { - sender.sendMessage(Component.text(Lang.getText("only-race-creator-can-delete", sender.locale()), TextColor.color(NamedTextColor.RED))) - return - } + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { + sender.sendMessage(Component.text(Lang.getText("only-race-creator-can-delete", sender.locale()), TextColor.color(NamedTextColor.RED))) + return@launch + } - transaction { - PlayerList.deleteWhere { (PlayerList.raceID eq raceID) and (PlayerList.playerUUID eq onlinePlayer.player.uniqueId.toString()) } + newSuspendedTransaction(Dispatchers.IO) { + PlayerList.deleteWhere { (PlayerList.raceID eq raceID) and (PlayerList.playerUUID eq onlinePlayer.player.uniqueId.toString()) } + } + sender.sendMessage(MessageFormat.format(Lang.getText("to-delete-player-from-race-group", sender.locale()), raceID)) } - sender.sendMessage(MessageFormat.format(Lang.getText("to-delete-player-from-race-group", sender.locale()), raceID)) } @CommandPermission("RaceAssist.commands.player") @Subcommand("delete") @CommandCompletion("@RaceID") private fun deletePlayer(sender: Player, @Single raceID: String) { - if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { - sender.sendMessage(Component.text(Lang.getText("only-race-creator-can-delete", sender.locale()), TextColor.color(NamedTextColor.RED))) - return - } + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { + sender.sendMessage(Component.text(Lang.getText("only-race-creator-can-delete", sender.locale()), TextColor.color(NamedTextColor.RED))) + return@launch + } - transaction { - PlayerList.deleteWhere { PlayerList.raceID eq raceID } + newSuspendedTransaction(Dispatchers.IO) { + PlayerList.deleteWhere { PlayerList.raceID eq raceID } + } + sender.sendMessage(MessageFormat.format(Lang.getText("to-delete-all-player-from-race-group", sender.locale()), raceID)) } - sender.sendMessage(MessageFormat.format(Lang.getText("to-delete-all-player-from-race-group", sender.locale()), raceID)) } @CommandPermission("RaceAssist.commands.player") @Subcommand("list") @CommandCompletion("@RaceID") private fun displayPlayerList(sender: Player, @Single raceID: String) { - if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { - sender.sendMessage(Component.text(Lang.getText("only-race-creator-can-display", sender.locale()), TextColor.color(NamedTextColor.RED))) - return - } - - transaction { - PlayerList.select { PlayerList.raceID eq raceID }.forEach { + plugin!!.launch { + if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { sender.sendMessage( Component.text( - Bukkit.getOfflinePlayer(UUID.fromString(it[PlayerList.playerUUID])).name.toString(), TextColor.color - ( - NamedTextColor - .YELLOW - ) + Lang.getText("only-race-creator-can-display", sender.locale()), + TextColor.color(NamedTextColor.RED) ) ) + return@launch + } + + newSuspendedTransaction(Dispatchers.IO) { + PlayerList.select { PlayerList.raceID eq raceID }.forEach { + sender.sendMessage( + Component.text( + Bukkit.getOfflinePlayer( + UUID.fromString( + it[PlayerList.playerUUID] + ) + ).name.toString(), TextColor.color(NamedTextColor.YELLOW) + ) + ) + } } } } - private fun getRacePlayerExist(RaceID: String, playerUUID: UUID): Boolean { - var playerExist = false - - transaction { - playerExist = PlayerList.select { (PlayerList.raceID eq RaceID) and (PlayerList.playerUUID eq playerUUID.toString()) }.count() > 0 - } - return playerExist + private suspend fun getRacePlayerExist(RaceID: String, playerUUID: UUID) = newSuspendedTransaction(Dispatchers.IO) { + PlayerList.select { (PlayerList.raceID eq RaceID) and (PlayerList.playerUUID eq playerUUID.toString()) }.count() > 0 } } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/RaceCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/RaceCommand.kt index 6224a0c..dcfdd9a 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/RaceCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/RaceCommand.kt @@ -49,7 +49,6 @@ import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.transactions.transaction import org.json.simple.JSONArray import org.json.simple.JSONObject import java.awt.Polygon @@ -366,49 +365,55 @@ class RaceCommand : BaseCommand() { @Subcommand("debug") @CommandCompletion("@RaceID") fun debug(sender: CommandSender, @Single raceID: String) { - if (starting) { - sender.sendMessage(text(Lang.getText("now-starting-other-race", (sender as Player).locale()), TextColor.color(RED))) - return - } - starting = true - if (getRaceCreator(raceID) != (sender as Player).uniqueId) { - sender.sendMessage(text(Lang.getText("only-race-creator-can-start", sender.locale()), TextColor.color(RED))) - } - if (!getCircuitExist(raceID, true) || !getCircuitExist(raceID, false)) { - sender.sendMessage(text(Lang.getText("no-exist-race", sender.locale()), TextColor.color(YELLOW))) - return - } + plugin?.launch { + if (starting) { + sender.sendMessage(text(Lang.getText("now-starting-other-race", (sender as Player).locale()), TextColor.color(RED))) + return@launch + } + starting = true + if (getRaceCreator(raceID) != (sender as Player).uniqueId) { + sender.sendMessage(text(Lang.getText("only-race-creator-can-start", sender.locale()), TextColor.color(RED))) + } + if (!getCircuitExist(raceID, true) || !getCircuitExist(raceID, false)) { + sender.sendMessage(text(Lang.getText("no-exist-race", sender.locale()), TextColor.color(YELLOW))) + return@launch + } - val insidePolygon = getPolygon(raceID, true) - val outsidePolygon = getPolygon(raceID, false) - if (insidePolygon.npoints < 3 || outsidePolygon.npoints < 3) { - sender.sendMessage(text(Lang.getText("no-exist-race", sender.locale()), TextColor.color(YELLOW))) - return - } - val reverse = getReverse(raceID) ?: false - val lap: Int = getLapCount(raceID) - val threshold = 40 - val centralXPoint: Int = - getCentralPoint(raceID, true) ?: return sender.sendMessage( - text( - Lang.getText("no-exist-central-point", sender.locale()), TextColor.color - (YELLOW) + val insidePolygon = getPolygon(raceID, true) + val outsidePolygon = getPolygon(raceID, false) + if (insidePolygon.npoints < 3 || outsidePolygon.npoints < 3) { + sender.sendMessage(text(Lang.getText("no-exist-race", sender.locale()), TextColor.color(YELLOW))) + return@launch + } + val reverse = getReverse(raceID) ?: false + val lap: Int = getLapCount(raceID) + val threshold = 40 + val centralXPoint: Int = + getCentralPoint(raceID, true) ?: return@launch sender.sendMessage( + text( + Lang.getText("no-exist-central-point", sender.locale()), TextColor.color + (YELLOW) + ) ) - ) - val centralYPoint: Int = - getCentralPoint(raceID, false) ?: return sender.sendMessage( - text( - Lang.getText("no-exist-central-point", sender.locale()), - TextColor.color(YELLOW) + val centralYPoint: Int = + getCentralPoint(raceID, false) ?: return@launch sender.sendMessage( + text( + Lang.getText("no-exist-central-point", sender.locale()), + TextColor.color(YELLOW) + ) ) - ) - val goalDegree: Int = - getGoalDegree(raceID) ?: return sender.sendMessage(text(Lang.getText("no-exist-goal-degree", sender.locale()), TextColor.color(YELLOW))) - var beforeDegree = 0 - var currentLap = 0 - var counter = 0 + val goalDegree: Int = + getGoalDegree(raceID) ?: return@launch sender.sendMessage( + text( + Lang.getText("no-exist-goal-degree", sender.locale()), + TextColor.color(YELLOW) + ) + ) + var beforeDegree = 0 + var currentLap = 0 + var counter = 0 + - plugin?.launch { withContext(Dispatchers.minecraft) { for (timer in 0..4) { @@ -515,12 +520,13 @@ class RaceCommand : BaseCommand() { @Subcommand("stop") @CommandCompletion("@RaceID") fun stop(sender: CommandSender, @Single raceID: String) { - if (getRaceCreator(raceID) != (sender as Player).uniqueId) { - sender.sendMessage(text(Lang.getText("only-race-creator-can-stop", sender.locale()), TextColor.color(RED))) - } - stop[raceID] = true - plugin?.launch { + if (getRaceCreator(raceID) != (sender as Player).uniqueId) { + sender.sendMessage(text(Lang.getText("only-race-creator-can-stop", sender.locale()), TextColor.color(RED))) + } + stop[raceID] = true + + delay(1000) stop[raceID] = false } @@ -529,48 +535,47 @@ class RaceCommand : BaseCommand() { @CommandPermission("RaceAssist.commands.race") @Subcommand("create") fun create(sender: CommandSender, @Single raceID: String) { - if (getRaceCreator(raceID) != null) { - sender.sendMessage(Lang.getText("already-used-the-name-race", (sender as Player).locale())) - return - } - transaction { - RaceList.insert { - it[this.raceID] = raceID - it[this.creator] = (sender as Player).uniqueId.toString() - it[this.reverse] = false - it[this.lap] = 1 - it[this.centralXPoint] = null - it[this.centralYPoint] = null - it[this.goalDegree] = null - } - BetSetting.insert { - it[this.raceID] = raceID - it[this.canBet] = false - it[this.returnPercent] = 75 - it[this.creator] = (sender as Player).uniqueId.toString() - it[this.spreadsheetId] = null + plugin!!.launch { + if (getRaceCreator(raceID) != null) { + sender.sendMessage(Lang.getText("already-used-the-name-race", (sender as Player).locale())) + return@launch + } + newSuspendedTransaction(Dispatchers.IO) { + RaceList.insert { + it[this.raceID] = raceID + it[this.creator] = (sender as Player).uniqueId.toString() + it[this.reverse] = false + it[this.lap] = 1 + it[this.centralXPoint] = null + it[this.centralYPoint] = null + it[this.goalDegree] = null + } + BetSetting.insert { + it[this.raceID] = raceID + it[this.canBet] = false + it[this.returnPercent] = 75 + it[this.creator] = (sender as Player).uniqueId.toString() + it[this.spreadsheetId] = null + } } + setRaceID() + sender.sendMessage(Lang.getText("to-create-race", (sender as Player).locale())) } - setRaceID() - sender.sendMessage(Lang.getText("to-create-race", (sender as Player).locale())) } @CommandPermission("RaceAssist.commands.race") @Subcommand("delete") @CommandCompletion("@RaceID") fun delete(sender: CommandSender, @Single raceID: String) { - - if (getRaceCreator(raceID) == null) { - sender.sendMessage(Lang.getText("no-racetrack-is-set", (sender as Player).locale())) - return - } - if (getRaceCreator(raceID) != (sender as Player).uniqueId) { - sender.sendMessage(Lang.getText("only-race-creator-can-setting", sender.locale())) - return - } - plugin!!.launch { - + if (getRaceCreator(raceID) == null) { + sender.sendMessage(Lang.getText("no-racetrack-is-set", (sender as Player).locale())) + return@launch + } + if (getRaceCreator(raceID) != (sender as Player).uniqueId) { + sender.sendMessage(Lang.getText("only-race-creator-can-setting", sender.locale())) + return@launch + } newSuspendedTransaction(Dispatchers.IO) { RaceList.deleteWhere { RaceList.raceID eq raceID } CircuitPoint.deleteWhere { CircuitPoint.raceID eq raceID } @@ -645,37 +650,45 @@ class RaceCommand : BaseCommand() { startDegree: Int, lap: Int ) { - val manager: ScoreboardManager = Bukkit.getScoreboardManager() - val scoreboard = manager.newScoreboard - val objective: Objective = scoreboard.registerNewObjective( - Lang.getText("scoreboard-ranking", Locale.getDefault()), "dummy", text( - Lang.getText("scoreboard-now-ranking", Locale.getDefault()), TextColor.color - (YELLOW) - ) - ) - objective.displaySlot = DisplaySlot.SIDEBAR - - for (i in nowRankings.indices) { - val playerName = Bukkit.getPlayer(nowRankings[i])?.name - val score = - objective.getScore(MessageFormat.format(Lang.getText("scoreboard-now-ranking-and-name", Locale.getDefault()), i + 1, playerName)) - score.score = nowRankings.size * 2 - 2 * i - 1 - val degree: String = if (currentLap[Bukkit.getPlayer(nowRankings[i])?.uniqueId] == null) { - Lang.getText("finished-the-race", Locale.getDefault()) - } else { - MessageFormat.format( - Lang.getText("now-lap-and-now-length", Locale.getDefault()), - currentLap[Bukkit.getPlayer(nowRankings[i])?.uniqueId]?.toInt(), - lap, - (currentDegree[Bukkit.getPlayer(nowRankings[i])?.uniqueId]?.minus(startDegree))?.times(innerCircumference)?.div(360) - ) - } - val displayDegree = objective.getScore(degree) - displayDegree.score = nowRankings.size * 2 - 2 * i - 2 - } + raceAudience.forEach { + if (Bukkit.getOfflinePlayer(it).isOnline) { - Bukkit.getPlayer(it)?.scoreboard = scoreboard + val player = Bukkit.getPlayer(it)!! + val manager: ScoreboardManager = Bukkit.getScoreboardManager() + val scoreboard = manager.newScoreboard + val objective: Objective = scoreboard.registerNewObjective( + Lang.getText("scoreboard-ranking", player.locale()), "dummy", text( + Lang.getText("scoreboard-now-ranking", player.locale()), TextColor.color + (YELLOW) + ) + ) + objective.displaySlot = DisplaySlot.SIDEBAR + + for (i in nowRankings.indices) { + val playerName = Bukkit.getPlayer(nowRankings[i])?.name + val score = objective.getScore( + MessageFormat.format( + Lang.getText( + "scoreboard-now-ranking-and-name", player.locale() + ), i + 1, playerName + ) + ) + score.score = nowRankings.size * 2 - 2 * i - 1 + val degree: String = if (currentLap[Bukkit.getPlayer(nowRankings[i])?.uniqueId] == null) { + Lang.getText("finished-the-race", player.locale()) + } else { + MessageFormat.format( + Lang.getText("now-lap-and-now-length", player.locale()), + currentLap[Bukkit.getPlayer(nowRankings[i])?.uniqueId]?.toInt(), + lap, + (currentDegree[Bukkit.getPlayer(nowRankings[i])?.uniqueId]?.minus(startDegree))?.times(innerCircumference)?.div(360) + ) + } + val displayDegree = objective.getScore(degree) + displayDegree.score = nowRankings.size * 2 - 2 * i - 2 + } + player.scoreboard = scoreboard } } } @@ -690,62 +703,32 @@ class RaceCommand : BaseCommand() { return ranking } - private fun getRaceCreator(raceID: String): UUID? { - var creatorUUID: UUID? = null - transaction { - - RaceList.select { RaceList.raceID eq raceID }.forEach { - creatorUUID = UUID.fromString(it[RaceList.creator]) - } - } - return creatorUUID - } - - private fun getLapCount(raceID: String): Int { - - var lapCount = 1 - transaction { - lapCount = RaceList.select { RaceList.raceID eq raceID }.first()[RaceList.lap] - } - return lapCount + private suspend fun getLapCount(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + RaceList.select { RaceList.raceID eq raceID }.first()[RaceList.lap] } - private fun getAllJockeys(raceID: String): ArrayList { + private suspend fun getAllJockeys(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { val jockeys: ArrayList = ArrayList() - transaction { - PlayerList.select { PlayerList.raceID eq raceID }.forEach { - jockeys.add(Bukkit.getOfflinePlayer(UUID.fromString(it[PlayerList.playerUUID]))) - } + PlayerList.select { PlayerList.raceID eq raceID }.forEach { + jockeys.add(Bukkit.getOfflinePlayer(UUID.fromString(it[PlayerList.playerUUID]))) } - return jockeys + jockeys } - private fun getGoalDegree(raceID: String): Int? { - var goalDegree: Int? = null - transaction { - goalDegree = RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.goalDegree) - } - return goalDegree + private suspend fun getGoalDegree(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.goalDegree) } - private fun getCircuitExist(raceID: String, inside: Boolean): Boolean { - var raceExist = false - - transaction { - raceExist = CircuitPoint.select { (CircuitPoint.raceID eq raceID) and (CircuitPoint.inside eq inside) }.count() > 0 - - } - return raceExist + private suspend fun getCircuitExist(raceID: String, inside: Boolean) = newSuspendedTransaction(Dispatchers.IO) { + CircuitPoint.select { (CircuitPoint.raceID eq raceID) and (CircuitPoint.inside eq inside) }.count() > 0 } - private fun getPolygon(raceID: String, inside: Boolean): Polygon { + private suspend fun getPolygon(raceID: String, inside: Boolean) = newSuspendedTransaction(Dispatchers.IO) { val polygon = Polygon() - transaction { - CircuitPoint.select { (CircuitPoint.raceID eq raceID) and (CircuitPoint.inside eq inside) }.forEach { - polygon.addPoint(it[CircuitPoint.XPoint], it[CircuitPoint.YPoint]) - } + CircuitPoint.select { (CircuitPoint.raceID eq raceID) and (CircuitPoint.inside eq inside) }.forEach { + polygon.addPoint(it[CircuitPoint.XPoint], it[CircuitPoint.YPoint]) } - return polygon + polygon } companion object { @@ -753,39 +736,26 @@ class RaceCommand : BaseCommand() { var starting = false var stop = HashMap() - fun getRaceCreator(raceID: String): UUID? { - var uuid: UUID? = null - transaction { - RaceList.select { RaceList.raceID eq raceID }.forEach { - uuid = UUID.fromString(it[RaceList.creator]) - } - } - return uuid + suspend fun getRaceCreator(raceID: String): UUID? = newSuspendedTransaction(Dispatchers.IO) { + UUID.fromString(RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.creator)) } - fun getCentralPoint(raceID: String, xPoint: Boolean): Int? { - var centralPoint: Int? = null - transaction { - RaceList.select { RaceList.raceID eq raceID }.forEach { - centralPoint = if (xPoint) { - it.getOrNull(RaceList.centralXPoint) - } else { - it.getOrNull(RaceList.centralYPoint) - } + suspend fun getCentralPoint(raceID: String, xPoint: Boolean): Int? = newSuspendedTransaction(Dispatchers.IO) { + var point: Int? = null + RaceList.select { RaceList.raceID eq raceID }.forEach { + point = if (xPoint) { + it.getOrNull(RaceList.centralXPoint) + } else { + it.getOrNull(RaceList.centralYPoint) } } - return centralPoint + point } - fun getReverse(raceID: String): Boolean? { - var reverse: Boolean? = null - - transaction { - reverse = RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.reverse) - - } - return reverse + suspend fun getReverse(raceID: String) = newSuspendedTransaction(Dispatchers.IO) { + RaceList.select { RaceList.raceID eq raceID }.firstOrNull()?.get(RaceList.reverse) } + } } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetCentralPointEvent.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetCentralPointEvent.kt index 9aca529..e2cb654 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetCentralPointEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetCentralPointEvent.kt @@ -19,23 +19,24 @@ package dev.nikomaru.raceassist.race.event import dev.nikomaru.raceassist.database.RaceList import dev.nikomaru.raceassist.race.commands.PlaceCommands import dev.nikomaru.raceassist.utils.Lang +import kotlinx.coroutines.Dispatchers import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.Action import org.bukkit.event.player.PlayerInteractEvent -import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import org.jetbrains.exposed.sql.update class SetCentralPointEvent : Listener { @EventHandler - fun setCentralPoint(event: PlayerInteractEvent) { + suspend fun setCentralPoint(event: PlayerInteractEvent) { if (PlaceCommands.getCanSetCentral()[event.player.uniqueId] == null || PlaceCommands.getCanSetCentral()[event.player.uniqueId] != true) { return } if (event.action != Action.LEFT_CLICK_BLOCK) { return } - transaction { + newSuspendedTransaction(Dispatchers.IO) { RaceList.update({ RaceList.raceID eq (PlaceCommands.getCentralRaceID()[event.player.uniqueId]!!) }) { it[centralXPoint] = event.clickedBlock?.location?.blockX ?: 0 it[centralYPoint] = event.clickedBlock?.location?.blockZ ?: 0 diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetOutsideCircuitEvent.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetOutsideCircuitEvent.kt index a69ba6e..7036060 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetOutsideCircuitEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetOutsideCircuitEvent.kt @@ -29,7 +29,7 @@ import java.util.* class SetOutsideCircuitEvent : Listener { @EventHandler - fun onSetOutsideCircuitEvent(event: PlayerInteractEvent) { + suspend fun onSetOutsideCircuitEvent(event: PlayerInteractEvent) { if (PlaceCommands.getCanSetOutsideCircuit()[event.player.uniqueId] == null) { return } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt index ed33b5f..f0ca03f 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt @@ -30,18 +30,17 @@ import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.transactions.transaction import java.awt.Polygon import java.text.MessageFormat object OutsideCircuit { private var outsidePolygonMap = HashMap() private var insidePolygonMap = HashMap() - fun outsideCircuit(player: Player, raceId: String, x: Int, z: Int) { + suspend fun outsideCircuit(player: Player, raceId: String, x: Int, z: Int) { outsidePolygonMap.putIfAbsent(raceId, Polygon()) insidePolygonMap.putIfAbsent(raceId, Polygon()) if (insidePolygonMap[raceId]!!.npoints == 0) { - transaction { + newSuspendedTransaction(Dispatchers.IO) { CircuitPoint.select { (CircuitPoint.raceID eq raceId) and (CircuitPoint.inside eq true) }.forEach { insidePolygonMap[raceId]!!.addPoint(it[CircuitPoint.XPoint], it[CircuitPoint.YPoint]) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt index 147bffd..4bc41b0 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt @@ -32,7 +32,7 @@ class RaceAudience { val offlinePlayer = Bukkit.getOfflinePlayer(it) if (offlinePlayer.isOnline) { val player = offlinePlayer.player!! - player.showTitle(title(text(MessageFormat.format(Lang.getText(key, player.locale()), args)), text(" "))) + player.showTitle(title(text(MessageFormat.format(Lang.getText(key, player.locale()), *args)), text(" "))) } } } @@ -42,7 +42,7 @@ class RaceAudience { val offlinePlayer = Bukkit.getOfflinePlayer(it) if (offlinePlayer.isOnline) { val player = offlinePlayer.player!! - player.sendMessage(text(MessageFormat.format(Lang.getText(key, player.locale()), args))) + player.sendMessage(text(MessageFormat.format(Lang.getText(key, player.locale()), *args))) } } } diff --git a/src/main/resources/lang/zh_CN.properties b/src/main/resources/lang/zh_CN.properties index bb7dd81..4544921 100644 --- a/src/main/resources/lang/zh_CN.properties +++ b/src/main/resources/lang/zh_CN.properties @@ -116,7 +116,7 @@ set-rate-message-in1-100=请输入1到100之间的数字 change-bet-rate-message={0}已被设置为{1}。 bet-remove-race=删除了{0}个种族 bet-remove-race-confirm-message=需要删除{0}场比赛,请在5秒内重新运行同一类型的比赛。 -bet-revert-return-message-owner=我将分配{1}天人民币给你。 +bet-revert-return-message-owner=我把{1}日元还给了{0}。 bet-revert-return-message-player=你已经在{2}中为{1}分配了{0}! bet-revert-complete-message=你已经完成了你的{0}赌注回报。 bet-revert-race-confirm-message=如果您想退出比赛,请在5秒内退出!