From 976bf7a7946c8e8a5c7bd7828dcca7cf134315c0 Mon Sep 17 00:00:00 2001 From: Nikomaru Date: Mon, 31 Jan 2022 19:41:42 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 6 +- gradle.properties | 4 +- .../dev/nikomaru/raceassist/RaceAssist.kt | 7 +- .../api/sheet/GoogleAuthorizeUtil.kt | 4 +- .../bet/commands/OpenBetGuiCommand.kt | 2 +- .../raceassist/bet/commands/SetBetCommand.kt | 13 +-- .../raceassist/bet/gui/BetChestGui.kt | 2 +- .../dev/nikomaru/raceassist/files/Config.kt | 12 +-- .../race/commands/AudiencesCommand.kt | 4 +- .../raceassist/race/commands/PlaceCommands.kt | 12 +-- .../raceassist/race/commands/PlayerCommand.kt | 8 +- .../raceassist/race/commands/RaceCommand.kt | 94 ++++++++----------- .../raceassist/race/utils/InsideCircuit.kt | 6 +- .../raceassist/race/utils/OutsideCircuit.kt | 2 +- .../dev/nikomaru/raceassist/utils/Lang.kt | 4 +- .../nikomaru/raceassist/utils/RaceAudience.kt | 20 +++- src/main/resources/config.yml | 6 -- src/main/resources/lang/en_US.properties | 6 +- src/main/resources/lang/ja_JP.properties | 2 +- src/main/resources/lang/zh_CN.properties | 3 +- 20 files changed, 102 insertions(+), 115 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index accb6f7..577affc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -38,7 +38,7 @@ repositories { val exposedVersion: String by project dependencies { compileOnly("io.papermc.paper:paper-api:1.18.1-R0.1-SNAPSHOT") - implementation("com.github.MilkBowl:VaultAPI:1.7") + compileOnly("com.github.MilkBowl:VaultAPI:1.7") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("co.aikar:acf-paper:0.5.0-SNAPSHOT") implementation("net.kyori:adventure-platform-bukkit:4.0.1") @@ -50,8 +50,8 @@ dependencies { implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion") implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion") implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion") - implementation("com.google.api-client:google-api-client:1.33.0") - implementation("com.google.oauth-client:google-oauth-client-jetty:1.32.1") + implementation("com.google.api-client:google-api-client:1.33.1") + implementation("com.google.oauth-client:google-oauth-client-jetty:1.33.0") implementation("com.google.apis:google-api-services-sheets:v4-rev20210629-1.32.1") } diff --git a/gradle.properties b/gradle.properties index 4e66b7e..d892a47 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # -# Copyright 2022 Nikomaru +# Copyright © 2022 Nikomaru # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -14,4 +14,4 @@ # along with this program. If not, see . # kotlin.code.style=official -exposedVersion=0.37.2 +exposedVersion=0.37.3 diff --git a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt index 83cbd98..60b3512 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt @@ -54,7 +54,7 @@ class RaceAssist : SuspendingJavaPlugin() { registerEvents() if (!VaultAPI.setupEconomy()) { - plugin!!.logger.info(Lang.getText("no-economy-plugin-found-disabling-vault", Locale.getDefault())) + plugin.logger.info(Lang.getText("no-economy-plugin-found-disabling-vault", Locale.getDefault())) server.pluginManager.disablePlugin(this) return } @@ -62,7 +62,7 @@ class RaceAssist : SuspendingJavaPlugin() { private fun settingDatabase() { org.jetbrains.exposed.sql.Database.connect( - url = "jdbc:sqlite:${plugin!!.dataFolder}${File.separator}RaceAssist.db", + url = "jdbc:sqlite:${plugin.dataFolder}${File.separator}RaceAssist.db", driver = "org.sqlite.JDBC" ) transaction { @@ -97,8 +97,7 @@ class RaceAssist : SuspendingJavaPlugin() { } companion object { - var plugin: RaceAssist? = null - + lateinit var plugin: RaceAssist val raceID = mutableListOf() fun setRaceID() { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/GoogleAuthorizeUtil.kt b/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/GoogleAuthorizeUtil.kt index 183d389..f125b4c 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/GoogleAuthorizeUtil.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/GoogleAuthorizeUtil.kt @@ -31,11 +31,11 @@ import java.security.GeneralSecurityException object GoogleAuthorizeUtil { - private val credentialsFilePath = File(plugin!!.dataFolder, "credentials.json") + private val credentialsFilePath = File(plugin.dataFolder, "credentials.json") @Throws(IOException::class, GeneralSecurityException::class) fun authorize(spreadsheetId: String): Credential { - val tokensDirectoryPath = File(File(plugin!!.dataFolder, "tokens"), "${spreadsheetId}_tokens") + val tokensDirectoryPath = File(File(plugin.dataFolder, "tokens"), "${spreadsheetId}_tokens") if (!tokensDirectoryPath.exists()) { tokensDirectoryPath.mkdirs() } 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 c478d4d..1a1cf63 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/OpenBetGuiCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/OpenBetGuiCommand.kt @@ -42,7 +42,7 @@ class OpenBetGuiCommand : BaseCommand() { @Subcommand("bet open") @CommandCompletion("@RaceID") fun openVending(player: Player, @Single raceID: String) { - plugin!!.launch { + plugin.launch { if (!raceExist(raceID)) { player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) return@launch 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 49c7ff0..06ad9ba 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/SetBetCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/SetBetCommand.kt @@ -47,7 +47,7 @@ class SetBetCommand : BaseCommand() { @Subcommand("can") @CommandCompletion("@RaceID on|off") fun setCanBet(player: Player, @Single raceID: String, @Single type: String) { - plugin!!.launch { + plugin.launch { if (!raceExist(raceID)) { player.sendMessage(MessageFormat.format(Lang.getText("no-exist-this-raceid-race", player.locale()), raceID)) return@launch @@ -77,7 +77,7 @@ class SetBetCommand : BaseCommand() { @Subcommand("rate") @CommandCompletion("@RaceID") fun setRate(player: Player, @Single raceID: String, @Single rate: Int) { - plugin!!.launch { + plugin.launch { if (!raceExist(raceID)) { player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) return@launch @@ -97,12 +97,13 @@ class SetBetCommand : BaseCommand() { } } player.sendMessage(MessageFormat.format(Lang.getText("change-bet-rate-message", player.locale()), raceID, rate)) + } @Subcommand("delete") @CommandCompletion("@RaceID") fun delete(player: Player, @Single raceID: String) { - plugin!!.launch { + plugin.launch { withContext(Dispatchers.IO) { if (!raceExist(raceID)) { player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) @@ -132,7 +133,7 @@ class SetBetCommand : BaseCommand() { @CommandCompletion("@RaceID") fun revert(player: Player, @Single raceID: String) { val eco: Economy = VaultAPI.getEconomy()!! - plugin!!.launch { + plugin.launch { withContext(Dispatchers.IO) { if (!raceExist(raceID)) { player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) @@ -199,7 +200,7 @@ class SetBetCommand : BaseCommand() { @Subcommand("sheet") @CommandCompletion("@RaceID") fun sheet(player: Player, @Single raceID: String, @Single sheetId: String) { - plugin!!.launch { + plugin.launch { withContext(Dispatchers.IO) { if (!raceExist(raceID)) { player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) @@ -221,7 +222,7 @@ class SetBetCommand : BaseCommand() { @Subcommand("list") @CommandCompletion("@RaceID") fun list(player: Player, @Single raceID: String) { - plugin!!.launch { + plugin.launch { withContext(Dispatchers.IO) { if (!raceExist(raceID)) { player.sendMessage(Lang.getText("no-exist-this-raceid-race", player.locale())) 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 310c798..b0a8c28 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt @@ -57,7 +57,7 @@ class BetChestGui { val players: ArrayList = ArrayList() val odds: HashMap = HashMap() var sum = 0 - plugin!!.launch { + plugin.launch { val rate: Int = newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceID eq raceID }.first()[BetSetting.returnPercent] } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt b/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt index 217dc2f..8083f12 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt @@ -21,25 +21,15 @@ import org.bukkit.configuration.file.FileConfiguration object Config { var config: FileConfiguration? = null - var host: String? = null - var port = 0 - var database: String? = null - var username: String? = null - var password: String? = null var threshold: Int? = null var discordWebHook: String? = null var betUnit: Int = 0 fun load() { if (config != null) { - plugin!!.reloadConfig() + plugin.reloadConfig() } - host = config!!.getString("SQLSettings.host") - port = config!!.getInt("SQLSetting.port") - database = config!!.getString("SQLSetting.database") - username = config!!.getString("SQLSetting.username") - password = config!!.getString("SQLSetting.password") threshold = config!!.getInt("RaceSetting.threshold") betUnit = config!!.getInt("RaceSetting.bet") discordWebHook = config!!.getString("NetworkSettings.discord") 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 dcced9f..128e64c 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/AudiencesCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/AudiencesCommand.kt @@ -46,7 +46,7 @@ class AudiencesCommand : BaseCommand() { @Subcommand("join") @CommandCompletion("@RaceID") private fun join(sender: Player, @Single raceID: String) { - plugin!!.launch { + plugin.launch { if (!getRaceExist(raceID)) { sender.sendMessage(text(Lang.getText("not-found-this-race", sender.locale()), TextColor.color(RED))) return@launch @@ -77,7 +77,7 @@ class AudiencesCommand : BaseCommand() { @Subcommand("list") @CommandCompletion("@RaceID") private fun list(sender: CommandSender, @Single raceID: String) { - plugin!!.launch { + 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))) 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 59edb18..6e4af8c 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlaceCommands.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlaceCommands.kt @@ -48,7 +48,7 @@ class PlaceCommands : BaseCommand() { @Subcommand("reverse") @CommandCompletion("@RaceID") fun reverse(sender: Player, @Single raceID: String) { - plugin!!.launch { + 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 @@ -68,7 +68,7 @@ class PlaceCommands : BaseCommand() { @CommandCompletion("@RaceID") fun central(sender: CommandSender, @Single raceID: String) { val player = sender as Player - plugin!!.launch { + 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 @@ -82,7 +82,7 @@ class PlaceCommands : BaseCommand() { @Subcommand("degree") @CommandCompletion("@RaceID") fun degree(player: Player, @Single raceID: String) { - plugin!!.launch { + 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 @@ -152,7 +152,7 @@ class PlaceCommands : BaseCommand() { @Syntax("[RaceID] ") fun setLap(sender: CommandSender, @Single raceID: String, @Single lap: Int) { val player = sender as Player - plugin!!.launch { + 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 @@ -174,7 +174,7 @@ class PlaceCommands : BaseCommand() { @Subcommand("set") @CommandCompletion("@RaceID in|out") fun set(player: Player, @Single raceID: String, @Single type: String) { - plugin!!.launch { + plugin.launch { if (RaceCommand.getRaceCreator(raceID) == null) { player.sendMessage(text(Lang.getText("no-exist-race", player.locale()), TextColor.color(RED))) @@ -208,7 +208,7 @@ class PlaceCommands : BaseCommand() { @Subcommand("finish") fun finish(sender: CommandSender) { - plugin!!.launch { + plugin.launch { val player = sender as Player if (Objects.isNull(canSetOutsideCircuit[player.uniqueId]) && Objects.isNull(canSetInsideCircuit[player.uniqueId])) { player.sendMessage(Lang.getText("now-you-not-setting-mode", sender.locale())) 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 cf0c8d2..5f7f262 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlayerCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/PlayerCommand.kt @@ -46,7 +46,7 @@ class PlayerCommand : BaseCommand() { @CommandCompletion("@RaceID @players") private fun addPlayer(player: Player, raceID: String, @Single onlinePlayer: OnlinePlayer) { - plugin!!.launch { + plugin.launch { val jockey = onlinePlayer.player if (RaceCommand.getRaceCreator(raceID) != player.uniqueId) { player.sendMessage( @@ -85,7 +85,7 @@ class PlayerCommand : BaseCommand() { @Subcommand("remove") @CommandCompletion("@RaceID") private fun removePlayer(sender: Player, @Single raceID: String, @Single onlinePlayer: OnlinePlayer) { - plugin!!.launch { + 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 @@ -102,7 +102,7 @@ class PlayerCommand : BaseCommand() { @Subcommand("delete") @CommandCompletion("@RaceID") private fun deletePlayer(sender: Player, @Single raceID: String) { - plugin!!.launch { + 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 @@ -119,7 +119,7 @@ class PlayerCommand : BaseCommand() { @Subcommand("list") @CommandCompletion("@RaceID") private fun displayPlayerList(sender: Player, @Single raceID: String) { - plugin!!.launch { + plugin.launch { if (RaceCommand.getRaceCreator(raceID) != sender.uniqueId) { sender.sendMessage( Component.text( 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 dcfdd9a..7a4c7a3 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/RaceCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/RaceCommand.kt @@ -67,7 +67,7 @@ class RaceCommand : BaseCommand() { @Subcommand("start") @CommandCompletion("@RaceID") fun start(sender: CommandSender, @Single raceID: String) { - plugin!!.launch { + plugin.launch { if (starting) { sender.sendMessage(text(Lang.getText("now-starting-other-race", (sender as Player).locale()), TextColor.color(RED))) @@ -186,16 +186,8 @@ class RaceCommand : BaseCommand() { //5.4.3...1 のカウント var timer1 = 0 while (timer1 <= 4) { - val sendTimer = async(Dispatchers.minecraft) { - audiences.getUUID().forEach { - if (Bukkit.getOfflinePlayer(it).isOnline) { - Bukkit.getPlayer(it)?.showTitle(title(text("${5 - timer1}", TextColor.color(GREEN)), text(" "))) - } - } - - } + audiences.showTitle(title(text("${5 - timer1}", TextColor.color(GREEN)), text(" "))) delay(1000) - sendTimer.await() timer1++ } @@ -294,18 +286,10 @@ class RaceCommand : BaseCommand() { delay(2000) for (i in 0 until finishJockey.size) { - audiences.getUUID().forEach { - if (Bukkit.getOfflinePlayer(it).isOnline) { - Bukkit.getPlayer(it)?.sendMessage( - MessageFormat.format( - Lang.getText("to-notice-ranking-message", Bukkit.getPlayer(it)!!.locale()), - i + 1, Bukkit.getPlayer(finishJockey[i])?.name!! - ) - ) - } - } + audiences.sendMessageI18n("to-notice-ranking-message", i + 1, Bukkit.getPlayer(finishJockey[i])?.name!!) } + withContext(Dispatchers.IO) { if (Config.discordWebHook != null) { sendWebHook(finishJockey, time, sender.uniqueId, raceID) @@ -348,7 +332,7 @@ class RaceCommand : BaseCommand() { embeds.add(embedsObject) json["embeds"] = embeds - plugin!!.logger.info(json.toString()) + plugin.logger.info(json.toString()) val discordWebHook = DiscordWebhook() discordWebHook.sendWebHook(json.toString()) } @@ -364,48 +348,48 @@ class RaceCommand : BaseCommand() { @CommandPermission("RaceAssist.commands.race") @Subcommand("debug") @CommandCompletion("@RaceID") - fun debug(sender: CommandSender, @Single raceID: String) { - plugin?.launch { + fun debug(player: Player, @Single raceID: String) { + plugin.launch { if (starting) { - sender.sendMessage(text(Lang.getText("now-starting-other-race", (sender as Player).locale()), TextColor.color(RED))) + player.sendMessage(text(Lang.getText("now-starting-other-race", 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 (getRaceCreator(raceID) != player.uniqueId) { + player.sendMessage(text(Lang.getText("only-race-creator-can-start", player.locale()), TextColor.color(RED))) } if (!getCircuitExist(raceID, true) || !getCircuitExist(raceID, false)) { - sender.sendMessage(text(Lang.getText("no-exist-race", sender.locale()), TextColor.color(YELLOW))) + player.sendMessage(text(Lang.getText("no-exist-race", player.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))) + player.sendMessage(text(Lang.getText("no-exist-race", player.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( + getCentralPoint(raceID, true) ?: return@launch player.sendMessage( text( - Lang.getText("no-exist-central-point", sender.locale()), TextColor.color + Lang.getText("no-exist-central-point", player.locale()), TextColor.color (YELLOW) ) ) val centralYPoint: Int = - getCentralPoint(raceID, false) ?: return@launch sender.sendMessage( + getCentralPoint(raceID, false) ?: return@launch player.sendMessage( text( - Lang.getText("no-exist-central-point", sender.locale()), + Lang.getText("no-exist-central-point", player.locale()), TextColor.color(YELLOW) ) ) val goalDegree: Int = - getGoalDegree(raceID) ?: return@launch sender.sendMessage( + getGoalDegree(raceID) ?: return@launch player.sendMessage( text( - Lang.getText("no-exist-goal-degree", sender.locale()), + Lang.getText("no-exist-goal-degree", player.locale()), TextColor.color(YELLOW) ) ) @@ -415,21 +399,21 @@ class RaceCommand : BaseCommand() { - withContext(Dispatchers.minecraft) { for (timer in 0..4) { - sender.showTitle(title(text("${5 - timer}", TextColor.color(GREEN)), text(" "))) + val showTimer = async(Dispatchers.minecraft) { + player.showTitle(title(text("${5 - timer}", TextColor.color(GREEN)), text(" "))) + } delay(1000) + showTimer.await() } - } - sender.showTitle(title(text(Lang.getText("to-notice-start-message", sender.locale()), TextColor.color(GREEN)), text(" "))) - val it: Player = sender + player.showTitle(title(text(Lang.getText("to-notice-start-message", player.locale()), TextColor.color(GREEN)), text(" "))) while (counter < 180 && stop[raceID] != true) { - val nowX: Int = it.location.blockX - val nowY: Int = it.location.blockZ + val nowX: Int = player.location.blockX + val nowY: Int = player.location.blockZ var relativeNowX = nowX - centralXPoint val relativeNowY = nowY - centralYPoint if (reverse) { @@ -440,37 +424,37 @@ class RaceCommand : BaseCommand() { currentLap += judgeLap(goalDegree, reverse, currentDegree, beforeDegree, threshold) - displayLap(currentLap, beforeLap, it, lap) + displayLap(currentLap, beforeLap, player, lap) if (insidePolygon.contains(nowX, nowY) || !outsidePolygon.contains(nowX, nowY)) { - it.sendActionBar(text(Lang.getText("outside-the-racetrack", sender.locale()), TextColor.color(RED))) + player.sendActionBar(text(Lang.getText("outside-the-racetrack", player.locale()), TextColor.color(RED))) } beforeDegree = currentDegree val manager: ScoreboardManager = Bukkit.getScoreboardManager() val scoreboard = manager.newScoreboard val objective: Objective = scoreboard.registerNewObjective( - Lang.getText("scoreboard-ranking", sender.locale()), + Lang.getText("scoreboard-ranking", player.locale()), "dummy", - text(Lang.getText("scoreboard-context", sender.locale()), TextColor.color(YELLOW)) + text(Lang.getText("scoreboard-context", player.locale()), TextColor.color(YELLOW)) ) objective.displaySlot = DisplaySlot.SIDEBAR - val score = objective.getScore(Lang.getText("first-ranking", sender.locale()) + " " + "§b${sender.name}") + val score = objective.getScore(Lang.getText("first-ranking", player.locale()) + " " + "§b${player.name}") score.score = 4 - val degree = MessageFormat.format(Lang.getText("scoreboard-now-lap-and-now-degree", sender.locale()), currentLap, currentDegree) + val degree = MessageFormat.format(Lang.getText("scoreboard-now-lap-and-now-degree", player.locale()), currentLap, currentDegree) val displayDegree = objective.getScore(degree) displayDegree.score = 2 - val residue = objective.getScore(MessageFormat.format(Lang.getText("time-remaining", sender.locale()), 180 - counter)) + val residue = objective.getScore(MessageFormat.format(Lang.getText("time-remaining", player.locale()), 180 - counter)) residue.score = 1 - sender.scoreboard = scoreboard + player.scoreboard = scoreboard counter++ delay(1000) } delay(2000) - sender.scoreboard.clearSlot(DisplaySlot.SIDEBAR) + player.scoreboard.clearSlot(DisplaySlot.SIDEBAR) starting = false } @@ -520,7 +504,7 @@ class RaceCommand : BaseCommand() { @Subcommand("stop") @CommandCompletion("@RaceID") fun stop(sender: CommandSender, @Single raceID: String) { - plugin?.launch { + plugin.launch { if (getRaceCreator(raceID) != (sender as Player).uniqueId) { sender.sendMessage(text(Lang.getText("only-race-creator-can-stop", sender.locale()), TextColor.color(RED))) } @@ -535,7 +519,7 @@ class RaceCommand : BaseCommand() { @CommandPermission("RaceAssist.commands.race") @Subcommand("create") fun create(sender: CommandSender, @Single raceID: String) { - plugin!!.launch { + plugin.launch { if (getRaceCreator(raceID) != null) { sender.sendMessage(Lang.getText("already-used-the-name-race", (sender as Player).locale())) return@launch @@ -567,7 +551,7 @@ class RaceCommand : BaseCommand() { @Subcommand("delete") @CommandCompletion("@RaceID") fun delete(sender: CommandSender, @Single raceID: String) { - plugin!!.launch { + plugin.launch { if (getRaceCreator(raceID) == null) { sender.sendMessage(Lang.getText("no-racetrack-is-set", (sender as Player).locale())) return@launch @@ -625,7 +609,7 @@ class RaceCommand : BaseCommand() { ) ) } - Bukkit.getScheduler().runTaskLater(plugin!!, Runnable { + Bukkit.getScheduler().runTaskLater(plugin, Runnable { player.clearTitle() }, 40) } else if (currentLap < beforeLap) { @@ -635,7 +619,7 @@ class RaceCommand : BaseCommand() { text(Lang.getText("one-step-backwards-lap", player.locale()), TextColor.color(RED)) ) ) - Bukkit.getScheduler().runTaskLater(plugin!!, Runnable { + Bukkit.getScheduler().runTaskLater(plugin, Runnable { player.clearTitle() }, 40) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/InsideCircuit.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/InsideCircuit.kt index 9ffff81..d36298b 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/InsideCircuit.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/InsideCircuit.kt @@ -38,10 +38,10 @@ object InsideCircuit { fun insideCircuit(player: Player, raceID: String, x: Int, z: Int) { insidePolygonMap.putIfAbsent(raceID, Polygon()) insidePolygonMap[raceID]!!.addPoint(x, z) - plugin?.logger?.info("${insidePolygonMap[raceID]?.npoints}") + plugin.logger.info("${insidePolygonMap[raceID]?.npoints}") player.sendActionBar(text(MessageFormat.format(Lang.getText("to-click-next-point", player.locale()), x, z))) PlaceCommands.removeCanSetInsideCircuit(player.uniqueId) - Bukkit.getScheduler().runTaskLater(plugin!!, Runnable { + Bukkit.getScheduler().runTaskLater(plugin, Runnable { PlaceCommands.putCanSetInsideCircuit(player.uniqueId, true) }, 5) } @@ -52,7 +52,7 @@ object InsideCircuit { CircuitPoint.deleteWhere { (CircuitPoint.raceID eq PlaceCommands.getCircuitRaceID()[player.uniqueId]!!) and (CircuitPoint.inside eq true) } } - plugin?.logger?.info("${insidePolygonMap[PlaceCommands.getCircuitRaceID()[player.uniqueId]]?.npoints}") + plugin.logger.info("${insidePolygonMap[PlaceCommands.getCircuitRaceID()[player.uniqueId]]?.npoints}") val x = insidePolygonMap[PlaceCommands.getCircuitRaceID()[player.uniqueId]]!!.xpoints val y = insidePolygonMap[PlaceCommands.getCircuitRaceID()[player.uniqueId]]!!.ypoints val n = insidePolygonMap[PlaceCommands.getCircuitRaceID()[player.uniqueId]]!!.npoints 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 f0ca03f..0de4d19 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt @@ -54,7 +54,7 @@ object OutsideCircuit { outsidePolygonMap[raceId]!!.addPoint(x, z) player.sendActionBar(text(MessageFormat.format(Lang.getText("to-click-next-point", player.locale()), x, z))) PlaceCommands.removeCanSetOutsideCircuit(player.uniqueId) - Bukkit.getScheduler().runTaskLater(plugin!!, Runnable { + Bukkit.getScheduler().runTaskLater(plugin, Runnable { PlaceCommands.putCanSetOutsideCircuit(player.uniqueId, true) }, 5) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt index 838f2b0..494cde3 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt @@ -31,14 +31,14 @@ object Lang { suspend fun load() { withContext(Dispatchers.IO) { val lang = listOf("en_US", "ja_JP", "zh_CN") - val pluginDir = File(plugin?.dataFolder ?: return@withContext, "lang") + val pluginDir = File(plugin.dataFolder, "lang") if (!pluginDir.exists()) { pluginDir.mkdir() } lang.forEach { locale -> val input: InputStream = this.javaClass.classLoader.getResourceAsStream("lang/$locale.properties") ?: return@forEach - plugin!!.logger.info("Loading lang file for $locale") + plugin.logger.info("Loading lang file for $locale") val file = File(pluginDir, "$locale.properties") if (!file.exists()) { Files.copy(input, file.toPath()) diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt index 4bc41b0..0041efa 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt @@ -17,6 +17,7 @@ package dev.nikomaru.raceassist.utils import net.kyori.adventure.text.Component.text +import net.kyori.adventure.title.Title import net.kyori.adventure.title.Title.title import org.bukkit.Bukkit import org.bukkit.OfflinePlayer @@ -37,7 +38,17 @@ class RaceAudience { } } - fun sendMessageI18n(key: String, vararg args: String) { + fun showTitle(title: Title) { + audience.forEach { + val offlinePlayer = Bukkit.getOfflinePlayer(it) + if (offlinePlayer.isOnline) { + val player = offlinePlayer.player!! + player.showTitle(title) + } + } + } + + fun sendMessageI18n(key: String, vararg args: Any) { audience.forEach { val offlinePlayer = Bukkit.getOfflinePlayer(it) if (offlinePlayer.isOnline) { @@ -51,6 +62,13 @@ class RaceAudience { audience.add(player.uniqueId) } + fun set(players: Collection) { + audience.clear() + players.forEach { + audience.add(it.uniqueId) + } + } + fun getUUID(): Set { return audience } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1668c93..9255bce 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,9 +1,3 @@ -SQLSettings: - host: 'localhost' - port: 3306 - database: 'raceassist' - username: 'root' - password: '' RaceSetting: threshold: 40 bet: 1000 diff --git a/src/main/resources/lang/en_US.properties b/src/main/resources/lang/en_US.properties index df433a6..7e69dc7 100644 --- a/src/main/resources/lang/en_US.properties +++ b/src/main/resources/lang/en_US.properties @@ -13,7 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # -finish-race=The race is over. +finish-race=The race is finished. goal-degree-must-multiple-90=Goal angle must be a multiple of 90 no-economy-plugin-found-disabling-vault=Economy plugin not found, Vault disabled no-exist-central-point=central point does not exist @@ -31,7 +31,7 @@ race-start=The race has started! to-click-block=Click on the block to-click-inside-point=Points must be set outside the inside point to-click-next-point=Current position: X = {0}, Z ={1} Click on the next point -to-notice-ranking-message={0} position {1} +to-notice-ranking-message={0}st {1} to-set-this-point-central=§a Set center to-suspend-process=Suspended process discord-webhook-name=Started by:{0} Race ID:{1} @@ -53,7 +53,7 @@ now-lap=Current {0} / {1} lap one-step-forward-lap=One lap forward one-step-backwards-lap=One step backwards scoreboard-now-ranking=Current ranking -scoreboard-now-ranking-and-name=§6{0}1st §b{1} +scoreboard-now-ranking-and-name=§6{0}st §b{1} finished-the-race=Finished the race now-lap-and-now-length=Current lap {0}/ {1} Lap Current distance {2}m already-exist-this-user=The player already exists diff --git a/src/main/resources/lang/ja_JP.properties b/src/main/resources/lang/ja_JP.properties index 003b662..216b7a2 100644 --- a/src/main/resources/lang/ja_JP.properties +++ b/src/main/resources/lang/ja_JP.properties @@ -54,7 +54,7 @@ now-lap=現在{0} / {1}ラップです one-step-forward-lap=ラップが一つ進みました one-step-backwards-lap=ラップが一つ戻りました scoreboard-now-ranking=現在のランキング -scoreboard-now-ranking-and-name=§6{0}1位 §b{1} +scoreboard-now-ranking-and-name=§6{0}位 §b{1} finished-the-race=完走しました now-lap-and-now-length=現在のラップ{0}/ {1} Lap 現在の距離{2}m already-exist-this-user=すでにそのプレイヤーは既に存在します diff --git a/src/main/resources/lang/zh_CN.properties b/src/main/resources/lang/zh_CN.properties index 4544921..b2a806f 100644 --- a/src/main/resources/lang/zh_CN.properties +++ b/src/main/resources/lang/zh_CN.properties @@ -13,6 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # + finish-race=比赛已经结束 goal-degree-must-multiple-90=目标角度必须是90的倍数 no-economy-plugin-found-disabling-vault=未找到经济插件,禁用保险库 @@ -53,7 +54,7 @@ now-lap=当前{0}/{1}圈数 one-step-forward-lap=你已经前进了一圈 one-step-backwards-lap=倒退一步 scoreboard-now-ranking=当前排名 -scoreboard-now-ranking-and-name=§6{0}1st §b{1} +scoreboard-now-ranking-and-name=§6{0}st §b{1} finished-the-race=你已经完成了比赛! now-lap-and-now-length=当前圈数 {0}/ {1} 圈数 当前距离 {2}m already-exist-this-user=该玩家已经存在。