diff --git a/build.gradle.kts b/build.gradle.kts index 7aaa257..43bce78 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,6 +9,7 @@ plugins { id("xyz.jpenilla.run-paper") version "1.0.6" id("org.sonarqube") version "3.3" id("net.minecrell.plugin-yml.bukkit") version "0.5.1" + kotlin("plugin.serialization") version "1.6.10" } group = "dev.nikomaru" @@ -25,27 +26,38 @@ repositories { } val cloudVersion = "1.6.2" -val exposedVersion = "0.37.3" +val exposedVersion = "0.38.2" dependencies { - compileOnly("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT") - compileOnly("com.github.MilkBowl:VaultAPI:1.7") - implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") - implementation("cloud.commandframework:cloud-core:$cloudVersion") - implementation("cloud.commandframework:cloud-kotlin-extensions:$cloudVersion") - implementation("cloud.commandframework:cloud-paper:$cloudVersion") - implementation("cloud.commandframework:cloud-annotations:$cloudVersion") - implementation("org.jetbrains.kotlin:kotlin-stdlib:1.6.10-RC") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1") - implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.2.0") - implementation("com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.2.0") - implementation("org.jetbrains.exposed:exposed-core:$exposedVersion") - 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.4") - implementation("com.google.oauth-client:google-oauth-client-jetty:1.33.2") - implementation("com.google.apis:google-api-services-sheets:v4-rev20220308-1.32.1") + compileOnly("io.papermc.paper", "paper-api", "1.19-R0.1-SNAPSHOT") + library(kotlin("stdlib")) + + compileOnly("com.github.MilkBowl", "VaultAPI", "1.7") + + implementation("cloud.commandframework", "cloud-core", cloudVersion) + implementation("cloud.commandframework", "cloud-kotlin-extensions", cloudVersion) + implementation("cloud.commandframework", "cloud-paper", cloudVersion) + implementation("cloud.commandframework", "cloud-annotations", cloudVersion) + implementation("cloud.commandframework", "cloud-kotlin-coroutines-annotations", cloudVersion) + implementation("cloud.commandframework", "cloud-kotlin-coroutines", cloudVersion) + + implementation("org.jetbrains.exposed", "exposed-core", exposedVersion) + implementation("org.jetbrains.exposed", "exposed-dao", exposedVersion) + implementation("org.jetbrains.exposed", "exposed-jdbc", exposedVersion) + implementation("org.jetbrains.exposed", "exposed-java-time", exposedVersion) + + implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.6.2") + + implementation("com.github.shynixn.mccoroutine", "mccoroutine-bukkit-api", "2.2.0") + implementation("com.github.shynixn.mccoroutine", "mccoroutine-bukkit-core", "2.2.0") + + implementation("org.jetbrains.kotlinx", "kotlinx-serialization-json", "1.3.3") + implementation("org.jetbrains.kotlinx", "kotlinx-serialization-hocon", "1.3.3") + + implementation("com.google.api-client", "google-api-client", "1.35.1") + implementation("com.google.oauth-client", "google-oauth-client-jetty", "1.34.1") + implementation("com.google.apis", "google-api-services-sheets", "v4-rev20220606-1.32.1") + bukkitLibrary("com.google.code.gson", "gson", "2.8.7") } @@ -68,6 +80,9 @@ tasks { build { dependsOn(shadowJar) } + runServer { + minecraftVersion("1.19") + } } @@ -78,7 +93,7 @@ bukkit { main = "dev.nikomaru.raceassist.RaceAssist" - apiVersion = "1.18" + apiVersion = "1.19" depend = listOf("Vault") libraries = listOf("com.github.shynixn.mccoroutine:mccoroutine-bukkit-api:2.2.0", "com.github.shynixn.mccoroutine:mccoroutine-bukkit-core:2.2.0") diff --git a/gradle.properties b/gradle.properties index f3dcb9e..28b992e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,4 @@ # suppress inspection "UnusedProperty" for whole file -# suppress inspection "UnusedProperty" for whole file kotlin.code.style=official exposedVersion=0.37.3 cloudVersion=1.6.2 diff --git a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt index 3f9b254..9e1a167 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -15,8 +16,10 @@ */ package dev.nikomaru.raceassist +import cloud.commandframework.annotations.AnnotationParser import cloud.commandframework.bukkit.CloudBukkitCapabilities import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator +import cloud.commandframework.kotlin.coroutines.annotations.installCoroutineSupport import cloud.commandframework.meta.SimpleCommandMeta import cloud.commandframework.paper.PaperCommandManager import com.github.shynixn.mccoroutine.bukkit.SuspendingJavaPlugin @@ -24,8 +27,9 @@ import com.github.shynixn.mccoroutine.bukkit.registerSuspendingEvents import dev.nikomaru.raceassist.api.VaultAPI import dev.nikomaru.raceassist.bet.commands.* import dev.nikomaru.raceassist.bet.event.BetGuiClickEvent -import dev.nikomaru.raceassist.database.* +import dev.nikomaru.raceassist.data.database.BetList import dev.nikomaru.raceassist.files.Config +import dev.nikomaru.raceassist.horse.event.HorseBreedEvent import dev.nikomaru.raceassist.race.commands.HelpCommand import dev.nikomaru.raceassist.race.commands.audience.* import dev.nikomaru.raceassist.race.commands.place.* @@ -37,20 +41,19 @@ import dev.nikomaru.raceassist.utils.CommandSuggestions import dev.nikomaru.raceassist.utils.Lang import dev.nikomaru.raceassist.utils.coroutines.minecraft import kotlinx.coroutines.withContext +import kotlinx.serialization.ExperimentalSerializationApi import org.bukkit.command.CommandSender -import org.bukkit.configuration.file.YamlConfiguration import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.transactions.transaction import java.io.File class RaceAssist : SuspendingJavaPlugin() { + @OptIn(ExperimentalSerializationApi::class) override suspend fun onEnableAsync() { // Plugin startup logic plugin = this Lang.load() - saveDefaultConfig() - Config.config = YamlConfiguration.loadConfiguration(File(dataFolder, "config.yml")) Config.load() settingDatabase() setCommand() @@ -63,7 +66,7 @@ class RaceAssist : SuspendingJavaPlugin() { private fun settingDatabase() { org.jetbrains.exposed.sql.Database.connect(url = "jdbc:sqlite:${plugin.dataFolder}${File.separator}RaceAssist.db", driver = "org.sqlite.JDBC") transaction { - SchemaUtils.create(CircuitPoint, PlayerList, RaceList, BetList, BetSetting, RaceStaff) + SchemaUtils.create(BetList) } } @@ -74,7 +77,7 @@ class RaceAssist : SuspendingJavaPlugin() { private fun setCommand() { val commandManager: PaperCommandManager = PaperCommandManager(this, - AsynchronousCommandExecutionCoordinator.simpleCoordinator(), + AsynchronousCommandExecutionCoordinator.newBuilder().build(), java.util.function.Function.identity(), java.util.function.Function.identity()) @@ -82,10 +85,10 @@ class RaceAssist : SuspendingJavaPlugin() { if (commandManager.queryCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) { commandManager.registerAsynchronousCompletions() } - val annotationParser: cloud.commandframework.annotations.AnnotationParser = - cloud.commandframework.annotations.AnnotationParser(commandManager, CommandSender::class.java) { - SimpleCommandMeta.empty() - } + + val annotationParser = AnnotationParser(commandManager, CommandSender::class.java) { + SimpleCommandMeta.empty() + }.installCoroutineSupport() annotationParser.parse(CommandSuggestions()) @@ -118,7 +121,6 @@ class RaceAssist : SuspendingJavaPlugin() { annotationParser.parse(BetSheetCommand()) annotationParser.parse(BetRemoveCommand()) annotationParser.parse(BetReturnCommand()) - annotationParser.parse(BetTransfarCommand()) annotationParser.parse(SettingCreateCommand()) annotationParser.parse(SettingDeleteCommand()) @@ -133,6 +135,7 @@ class RaceAssist : SuspendingJavaPlugin() { server.pluginManager.registerSuspendingEvents(SetOutsideCircuitEvent(), this) server.pluginManager.registerSuspendingEvents(SetCentralPointEvent(), this) server.pluginManager.registerSuspendingEvents(BetGuiClickEvent(), this) + server.pluginManager.registerSuspendingEvents(HorseBreedEvent(), this) } companion object { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/api/VaultAPI.kt b/src/main/kotlin/dev/nikomaru/raceassist/api/VaultAPI.kt index b113264..7738d50 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/api/VaultAPI.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/api/VaultAPI.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. 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 ab5781b..ea2d634 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/GoogleAuthorizeUtil.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/GoogleAuthorizeUtil.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/SheetsServiceUtil.kt b/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/SheetsServiceUtil.kt index ad42be0..d60ded3 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/SheetsServiceUtil.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/api/sheet/SheetsServiceUtil.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/TempBetData.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt similarity index 68% rename from src/main/kotlin/dev/nikomaru/raceassist/utils/TempBetData.kt rename to src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt index aa9168e..5721903 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/TempBetData.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -14,8 +15,6 @@ * along with this program. If not, see . */ -package dev.nikomaru.raceassist.utils - -import java.util.* +package dev.nikomaru.raceassist.bet -data class TempBetData(val raceId: String, val uuid: UUID, val jockey: UUID, var bet: Int) +object BetUtils {} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt index 782170d..03421e8 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -16,7 +17,7 @@ package dev.nikomaru.raceassist.bet -import dev.nikomaru.raceassist.files.Config.betUnit +import dev.nikomaru.raceassist.data.files.BetSettingData import dev.nikomaru.raceassist.utils.Lang import net.kyori.adventure.text.Component import org.bukkit.Material @@ -30,34 +31,34 @@ object GuiComponent { return Component.text("レース賭け自販機") } - fun onceUp(locale: Locale): ItemStack { + suspend fun onceUp(locale: Locale, raceId: String): ItemStack { val onceUp = ItemStack(Material.PINK_STAINED_GLASS_PANE) val onceUpMeta: ItemMeta = onceUp.itemMeta - onceUpMeta.displayName(Lang.getComponent("to-bet-one-unit", locale, betUnit)) + onceUpMeta.displayName(Lang.getComponent("to-bet-one-unit", locale, BetSettingData.getBetUnit(raceId))) onceUp.itemMeta = onceUpMeta return onceUp } - fun onceDown(locale: Locale): ItemStack { + suspend fun onceDown(locale: Locale, raceId: String): ItemStack { val onceDown = ItemStack(Material.LIGHT_BLUE_STAINED_GLASS_PANE) val onceDownMeta: ItemMeta = onceDown.itemMeta - onceDownMeta.displayName(Lang.getComponent("to-cancel-bet-one-unit", locale, betUnit)) + onceDownMeta.displayName(Lang.getComponent("to-cancel-bet-one-unit", locale, BetSettingData.getBetUnit(raceId))) onceDown.itemMeta = onceDownMeta return onceDown } - fun tenTimesUp(locale: Locale): ItemStack { + suspend fun tenTimesUp(locale: Locale, raceId: String): ItemStack { val tenTimesUp = ItemStack(Material.RED_STAINED_GLASS_PANE) val tenTimesUpMeta: ItemMeta = tenTimesUp.itemMeta - tenTimesUpMeta.displayName(Lang.getComponent("to-bet-ten-unit", locale, betUnit * 10)) + tenTimesUpMeta.displayName(Lang.getComponent("to-bet-ten-unit", locale, BetSettingData.getBetUnit(raceId) * 10)) tenTimesUp.itemMeta = tenTimesUpMeta return tenTimesUp } - fun tenTimesDown(locale: Locale): ItemStack { + suspend fun tenTimesDown(locale: Locale, raceId: String): ItemStack { val tenTimesDown = ItemStack(Material.BLUE_STAINED_GLASS_PANE) val tenTimesDownMeta: ItemMeta = tenTimesDown.itemMeta - tenTimesDownMeta.displayName(Lang.getComponent("to-cancel-bet-ten-unit", locale, betUnit * 10)) + tenTimesDownMeta.displayName(Lang.getComponent("to-cancel-bet-ten-unit", locale, BetSettingData.getBetUnit(raceId) * 10)) tenTimesDown.itemMeta = tenTimesDownMeta return tenTimesDown } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetCanCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetCanCommand.kt index 1e88d46..bfd1a67 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetCanCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetCanCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,52 +18,38 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.BetSetting +import dev.nikomaru.raceassist.data.files.BetSettingData import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update import java.util.* @CommandMethod("ra|RaceAssist bet") class BetCanCommand { @CommandPermission("RaceAssist.commands.bet.can") @CommandMethod("can ") - fun setCanBet(sender: CommandSender, + suspend fun setCanBet(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "type", suggestions = "betType") type: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - if (type == "on") { - setCanBet(raceId, sender) - } else if (type == "off") { - setCannotBet(raceId, sender) - } + if (CommandUtils.returnRaceSetting(raceId, sender)) return + if (type == "on") { + setCanBet(raceId, sender) + } else if (type == "off") { + setCannotBet(raceId, sender) } + } private suspend fun setCanBet(raceId: String, sender: CommandSender) { - newSuspendedTransaction(Dispatchers.IO) { - BetSetting.update({ BetSetting.raceId eq raceId }) { - it[canBet] = true - } - } + BetSettingData.setAvailable(raceId, true) val locale = if (sender is Player) sender.locale() else Locale.getDefault() sender.sendMessage(Lang.getComponent("can-bet-this-raceid", locale, raceId)) } companion object { suspend fun setCannotBet(raceId: String, sender: CommandSender) { - newSuspendedTransaction(Dispatchers.IO) { - BetSetting.update({ BetSetting.raceId eq raceId }) { - it[canBet] = false - } - } + BetSettingData.setAvailable(raceId, false) val locale = if (sender is Player) sender.locale() else Locale.getDefault() sender.sendMessage(Lang.getComponent("cannot-bet-this-raceid", locale, raceId)) diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetDeleteCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetDeleteCommand.kt index 6b880e2..d8e38f6 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetDeleteCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetDeleteCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,45 +18,31 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.BetList +import dev.nikomaru.raceassist.data.database.BetList import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.Lang import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.util.* @CommandMethod("ra|RaceAssist bet") class BetDeleteCommand { @CommandPermission("RaceAssist.commands.bet.delete") @CommandMethod("delete ") - fun delete(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + @Confirmation + suspend fun delete(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - if (canDelete[sender.uniqueId] == true) { - newSuspendedTransaction(Dispatchers.IO) { - BetList.deleteWhere { BetList.raceId eq raceId } - } - sender.sendMessage(Lang.getComponent("bet-remove-race", sender.locale(), raceId)) - } else { - canDelete[sender.uniqueId] = true - sender.sendMessage(Lang.getComponent("bet-remove-race-confirm-message", sender.locale(), raceId)) - delay(5000) - canDelete.remove(sender.uniqueId) - } + if (CommandUtils.returnRaceSetting(raceId, sender)) return + newSuspendedTransaction(Dispatchers.IO) { + BetList.deleteWhere { BetList.raceId eq raceId } } - } + sender.sendMessage(Lang.getComponent("bet-remove-race", sender.locale(), raceId)) - companion object { - val canDelete: HashMap = HashMap() } + } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetListCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetListCommand.kt index ae733b0..a549aa6 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetListCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetListCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,9 +18,7 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.BetList +import dev.nikomaru.raceassist.data.database.BetList import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.Lang import kotlinx.coroutines.Dispatchers @@ -33,23 +32,23 @@ import java.util.* class BetListCommand { @CommandPermission("RaceAssist.commands.bet.list") @CommandMethod("list ") - fun list(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun list(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { val locale = if (sender is Player) sender.locale() else Locale.getDefault() - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - newSuspendedTransaction(Dispatchers.IO) { - BetList.select { BetList.raceId eq raceId }.forEach { - sender.sendMessage(Lang.getComponent("bet-list-detail-message", - locale, - it[BetList.rowNum], - it[BetList.timeStamp], - it[BetList.playerName], - it[BetList.jockey], - it[BetList.betting])) - } + + if (CommandUtils.returnRaceSetting(raceId, sender)) return + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { BetList.raceId eq raceId }.forEach { + sender.sendMessage(Lang.getComponent("bet-list-detail-message", + locale, + it[BetList.rowNum], + it[BetList.timeStamp], + it[BetList.playerName], + it[BetList.jockey], + it[BetList.betting])) } } + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetOpenCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetOpenCommand.kt index 15011e8..6d89fd5 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetOpenCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetOpenCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,63 +18,53 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin +import dev.nikomaru.raceassist.bet.data.TempBetData import dev.nikomaru.raceassist.bet.gui.BetChestGui -import dev.nikomaru.raceassist.database.BetSetting +import dev.nikomaru.raceassist.data.files.BetSettingData +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.Lang -import dev.nikomaru.raceassist.utils.TempBetData import dev.nikomaru.raceassist.utils.coroutines.minecraft -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction @CommandMethod("ra|RaceAssist bet") class BetOpenCommand { @CommandPermission("RaceAssist.commands.bet.open") @CommandMethod("open ") - fun openVending(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun openVending(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (!raceExist(raceId)) { - sender.sendMessage(Lang.getComponent("no-exist-this-raceid-race", sender.locale())) - return@launch - } - val vending = BetChestGui() - val canBet = newSuspendedTransaction(Dispatchers.IO) { BetSetting.select { BetSetting.raceId eq raceId }.first()[BetSetting.canBet] } - if (!canBet) { - sender.sendMessage(Lang.getComponent("now-cannot-bet-race", sender.locale())) - return@launch - } - - val iterator = TempBetDatas.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.uuid == sender.uniqueId) { - iterator.remove() - } - } - withContext(minecraft) { - sender.openInventory(vending.getGUI(sender, raceId)) - } + if (!RaceSettingData.existsRace(raceId)) { + sender.sendMessage(Lang.getComponent("no-exist-this-raceid-race", sender.locale())) + return + } + val vending = BetChestGui() + val canBet = BetSettingData.getAvailable(raceId) + if (!canBet) { + sender.sendMessage(Lang.getComponent("now-cannot-bet-race", sender.locale())) + return + } - BetChestGui.AllPlayers[raceId]?.forEach { jockey -> - TempBetDatas.add(TempBetData(raceId, sender.uniqueId, jockey, 0)) + val iterator = TempBetDatas.iterator() + while (iterator.hasNext()) { + val it = iterator.next() + if (it.player == sender) { + iterator.remove() } - + } + withContext(minecraft) { + sender.openInventory(vending.getGUI(sender, raceId)) } - } - private suspend fun raceExist(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.count() > 0 + BetChestGui.AllPlayers[raceId]?.forEach { jockey -> + TempBetDatas.add(TempBetData(raceId, sender, jockey, 0)) + } + } companion object { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRateCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRateCommand.kt index f2d327f..db32d2b 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRateCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRateCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -18,38 +19,33 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* import cloud.commandframework.annotations.specifier.Range -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.BetSetting +import dev.nikomaru.raceassist.data.files.BetSettingData import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update import java.util.* @CommandMethod("ra|RaceAssist bet") class BetRateCommand { @CommandPermission("RaceAssist.commands.bet.rate") @CommandMethod("rate ") - fun setRate(sender: CommandSender, + suspend fun setRate(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "rate") @Range(min = "0", max = "100") rate: Int) { val locale = if (sender is Player) sender.locale() else Locale.getDefault() - plugin.launch { - if (returnRaceSetting(raceId, sender)) return@launch - if (rate !in 1..100) { - sender.sendMessage(Lang.getComponent("set-rate-message-in1-100", locale)) - return@launch - } - newSuspendedTransaction(Dispatchers.IO) { - BetSetting.update({ BetSetting.raceId eq raceId }) { - it[returnPercent] = rate - } - } + + if (returnRaceSetting(raceId, sender)) return + if (rate !in 1..100) { + sender.sendMessage(Lang.getComponent("set-rate-message-in1-100", locale)) + return } + newSuspendedTransaction(Dispatchers.IO) { + BetSettingData.setReturnPercent(raceId, rate) + } + sender.sendMessage(Lang.getComponent("change-bet-rate-message", locale, raceId, rate)) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt index 6d9c979..b486a7d 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRemoveCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,10 +18,8 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.api.VaultAPI -import dev.nikomaru.raceassist.database.BetList +import dev.nikomaru.raceassist.data.database.BetList import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang import dev.nikomaru.raceassist.utils.coroutines.minecraft @@ -38,44 +37,45 @@ import java.util.* class BetRemoveCommand { @CommandPermission("RaceAssist.commands.bet.revert") @CommandMethod("remove ") - fun remove(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "betId") betId: Int) { + suspend fun remove(sender: CommandSender, + @Argument(value = "raceId", suggestions = "raceId") raceId: String, + @Argument(value = "betId") betId: Int) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } val eco: Economy = VaultAPI.getEconomy() - plugin.launch { - withContext(Dispatchers.IO) { - if (returnRaceSetting(raceId, sender)) return@withContext - if (eco.getBalance(sender) < getBetSum(raceId)) { - sender.sendMessage(Lang.getComponent("no-have-money", sender.locale())) - return@withContext - } - } - newSuspendedTransaction(Dispatchers.IO) { - BetList.select { (BetList.rowNum eq betId) and (BetList.raceId eq raceId) }.forEach { - val receiver = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID].toString())) - withContext(minecraft) { - eco.withdrawPlayer(sender, it[BetList.betting].toDouble()) - eco.depositPlayer(receiver, it[BetList.betting].toDouble()) - } - sender.sendMessage(Lang.getComponent("bet-revert-return-message-owner", sender.locale(), receiver.name, it[BetList.betting])) - if (receiver.isOnline) { - (receiver as Player).sendMessage(Lang.getComponent("bet-revert-return-message-player", - receiver.locale(), - sender.name, - it[BetList.raceId], - it[BetList.jockey], - it[BetList.betting])) - } + withContext(Dispatchers.IO) { + if (returnRaceSetting(raceId, sender)) return@withContext + if (eco.getBalance(sender) < getBetSum(raceId)) { + sender.sendMessage(Lang.getComponent("no-have-money", sender.locale())) + return@withContext + } + } + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { (BetList.rowNum eq betId) and (BetList.raceId eq raceId) }.forEach { + val receiver = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID].toString())) + withContext(minecraft) { + eco.withdrawPlayer(sender, it[BetList.betting].toDouble()) + eco.depositPlayer(receiver, it[BetList.betting].toDouble()) + } + sender.sendMessage(Lang.getComponent("bet-revert-return-message-owner", sender.locale(), receiver.name, it[BetList.betting])) + if (receiver.isOnline) { + (receiver as Player).sendMessage(Lang.getComponent("bet-revert-return-message-player", + receiver.locale(), + sender.name, + it[BetList.raceId], + it[BetList.jockey], + it[BetList.betting])) } - BetList.deleteWhere { (BetList.rowNum eq betId) and (BetList.raceId eq raceId) } } + BetList.deleteWhere { (BetList.rowNum eq betId) and (BetList.raceId eq raceId) } } + } private suspend fun getBetSum(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetReturnCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetReturnCommand.kt index 4d020b0..b927977 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetReturnCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetReturnCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,12 +18,11 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.api.VaultAPI -import dev.nikomaru.raceassist.bet.event.BetGuiClickEvent.Companion.getBetOwner -import dev.nikomaru.raceassist.database.* -import dev.nikomaru.raceassist.database.BetList.jockey +import dev.nikomaru.raceassist.data.database.BetList +import dev.nikomaru.raceassist.data.database.BetList.jockey +import dev.nikomaru.raceassist.data.files.BetSettingData +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.Lang import dev.nikomaru.raceassist.utils.coroutines.minecraft import kotlinx.coroutines.* @@ -39,65 +39,58 @@ import kotlin.math.floor class BetReturnCommand { @CommandPermission("RaceAssist.commands.bet.return") @CommandMethod("return ") - fun returnBet(sender: CommandSender, + suspend fun returnBet(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "playerName", suggestions = "playerName") playerName: String) { - plugin.launch { - val player = Bukkit.getOfflinePlayer(playerName) - val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (!player.hasPlayedBefore()) { - sender.sendMessage(Lang.getComponent("player-not-exist", locale, player.name)) - return@launch - } - if (!existPlayer(player)) { - sender.sendMessage(Lang.getComponent("player-not-jockey", locale, player.name)) - return@launch - } - var sum = 0 - newSuspendedTransaction(Dispatchers.IO) { - BetList.select { BetList.raceId eq raceId }.forEach { - sum += it[BetList.betting] - } - } - val rate: Int = newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.first()[BetSetting.returnPercent] + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + val player = Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + + if (!existPlayer(raceId, player)) { + sender.sendMessage(Lang.getComponent("player-not-jockey", locale, player.name)) + return + } + var sum = 0 + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { BetList.raceId eq raceId }.forEach { + sum += it[BetList.betting] } - var jockeySum = 0 - newSuspendedTransaction(Dispatchers.IO) { - BetList.select { (jockey eq player.name.toString()) and (BetList.raceId eq raceId) }.forEach { - jockeySum += it[BetList.betting] - } + } + val rate: Int = BetSettingData.getReturnPercent(raceId) + var jockeySum = 0 + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { (BetList.jockeyUUID eq player.uniqueId.toString()) and (BetList.raceId eq raceId) }.forEach { + jockeySum += it[BetList.betting] } + } - val odds = floor(((sum * (rate.toDouble() / 100)) / jockeySum) * 100) / 100 + val odds = floor(((sum * (rate.toDouble() / 100)) / jockeySum) * 100) / 100 - if (canReturn[raceId] == true) { - payRefund(player, raceId, sender, locale) - } else { - canReturn[raceId] = true - newSuspendedTransaction(Dispatchers.IO) { - BetList.select { (jockey eq player.name.toString()) and (BetList.raceId eq raceId) }.forEach { - val returnAmount = it[BetList.betting] * odds - val retunrPlayer = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID])) - sender.sendMessage(Lang.getComponent("pay-confirm-bet-player", - locale, - retunrPlayer.name, - player.name, - it[BetList.betting], - returnAmount)) - } + if (canReturn[raceId] == true) { + payRefund(player, raceId, sender, locale) + } else { + canReturn[raceId] = true + newSuspendedTransaction(Dispatchers.IO) { + BetList.select { (jockey eq player.name.toString()) and (BetList.raceId eq raceId) }.forEach { + val returnAmount = it[BetList.betting] * odds + val retunrPlayer = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID])) + sender.sendMessage(Lang.getComponent("pay-confirm-bet-player", + locale, + retunrPlayer.name, + player.name, + it[BetList.betting], + returnAmount)) } - sender.sendMessage(Lang.getComponent("return-confirm-message", locale)) - delay(5000) - canReturn.remove(raceId) } + sender.sendMessage(Lang.getComponent("return-confirm-message", locale)) + delay(5000) + canReturn.remove(raceId) } + } - private suspend fun existPlayer(player: OfflinePlayer): Boolean { - return newSuspendedTransaction(Dispatchers.IO) { - PlayerList.select { PlayerList.playerUUID eq player.uniqueId.toString() }.count() > 0 - } + private suspend fun existPlayer(raceId: String, player: OfflinePlayer): Boolean { + return RaceSettingData.getJockeys(raceId).contains(player) } companion object { @@ -111,9 +104,7 @@ class BetReturnCommand { } } - val rate: Int = newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.first()[BetSetting.returnPercent] - } + val rate: Int = BetSettingData.getReturnPercent(raceId) var jockeySum = 0 newSuspendedTransaction(Dispatchers.IO) { BetList.select { (jockey eq player.name.toString()) and (BetList.raceId eq raceId) }.forEach { @@ -130,7 +121,7 @@ class BetReturnCommand { withContext(minecraft) { val eco = VaultAPI.getEconomy() eco.depositPlayer(retunrPlayer, returnAmount) - eco.withdrawPlayer(getBetOwner(raceId), returnAmount) + eco.withdrawPlayer(RaceSettingData.getOwner(raceId), returnAmount) } sender.sendMessage(Lang.getComponent("paid-bet-creator", locale, retunrPlayer.name, returnAmount)) retunrPlayer.player?.sendMessage(Lang.getComponent("paid-bet-player", diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt index ad71dc1..da62afd 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetRevertCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,16 +18,16 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.api.VaultAPI -import dev.nikomaru.raceassist.bet.event.BetGuiClickEvent.Companion.getBetOwner -import dev.nikomaru.raceassist.database.BetList +import dev.nikomaru.raceassist.data.database.BetList +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang import dev.nikomaru.raceassist.utils.coroutines.minecraft -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import net.milkbowl.vault.economy.Economy +import org.bukkit.Bukkit import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.jetbrains.exposed.sql.deleteWhere @@ -38,51 +39,42 @@ import java.util.* class BetRevertCommand { @CommandPermission("RaceAssist.commands.bet.revert") @CommandMethod("revert ") - fun revert(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun revert(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { val eco: Economy = VaultAPI.getEconomy() - if (sender !is Player) { - sender.sendMessage("Only the player can do this.") - return - } - plugin.launch { - withContext(Dispatchers.IO) { - if (returnRaceSetting(raceId, sender)) return@withContext - if (eco.getBalance(sender) < getBetSum(raceId)) { - sender.sendMessage(Lang.getComponent("no-have-money", sender.locale())) - return@withContext - } + val owner = RaceSettingData.getOwner(raceId) + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + withContext(Dispatchers.IO) { + if (returnRaceSetting(raceId, sender)) return@withContext + if (eco.getBalance(owner) < getBetSum(raceId)) { + sender.sendMessage(Lang.getComponent("no-have-money", locale)) + return@withContext } + } + - if (canRevert[sender.uniqueId] == true) { - newSuspendedTransaction(Dispatchers.IO) { + newSuspendedTransaction(Dispatchers.IO) { - BetList.select { BetList.raceId eq raceId }.forEach { - val receiver = getBetOwner(raceId) - withContext(minecraft) { - eco.withdrawPlayer(sender, it[BetList.betting].toDouble()) - eco.depositPlayer(receiver, it[BetList.betting].toDouble()) - } - sender.sendMessage(Lang.getComponent("bet-revert-return-message-owner", sender.locale(), receiver.name, it[BetList.betting])) - if (receiver.isOnline) { - (receiver as Player).sendMessage(Lang.getComponent("bet-revert-return-message-player", - receiver.locale(), - sender.name, - it[BetList.raceId], - it[BetList.jockey], - it[BetList.betting])) - } - } - BetList.deleteWhere { BetList.raceId eq raceId } + BetList.select { BetList.raceId eq raceId }.forEach { + val receiver = Bukkit.getOfflinePlayer(UUID.fromString(it[BetList.playerUUID])) + withContext(minecraft) { + eco.withdrawPlayer(owner, it[BetList.betting].toDouble()) + eco.depositPlayer(receiver, it[BetList.betting].toDouble()) + } + sender.sendMessage(Lang.getComponent("bet-revert-return-message-owner", locale, receiver.name, it[BetList.betting])) + if (receiver.isOnline) { + (receiver as Player).sendMessage(Lang.getComponent("bet-revert-return-message-player", + receiver.locale(), + owner.name, + it[BetList.raceId], + it[BetList.jockey], + it[BetList.betting])) } - sender.sendMessage(Lang.getComponent("bet-revert-complete-message", sender.locale(), raceId)) - } else { - canRevert[sender.uniqueId] = true - sender.sendMessage(Lang.getComponent("bet-revert-race-confirm-message", sender.locale(), raceId)) - delay(5000) - canRevert.remove(sender.uniqueId) } + BetList.deleteWhere { BetList.raceId eq raceId } } + sender.sendMessage(Lang.getComponent("bet-revert-complete-message", locale, raceId)) + } private suspend fun getBetSum(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { @@ -90,8 +82,4 @@ class BetRevertCommand { it[BetList.betting] } } - - companion object { - val canRevert: HashMap = HashMap() - } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetSheetCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetSheetCommand.kt index 2a54e9e..8aa9678 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetSheetCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetSheetCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,34 +18,26 @@ package dev.nikomaru.raceassist.bet.commands import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch import com.google.api.services.sheets.v4.model.* -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.api.sheet.SheetsServiceUtil.getSheetsService -import dev.nikomaru.raceassist.database.BetSetting +import dev.nikomaru.raceassist.data.files.BetSettingData import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.bukkit.command.CommandSender -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update @CommandMethod("ra|RaceAssist bet") class BetSheetCommand { @CommandPermission("RaceAssist.commands.bet.sheet") @CommandMethod("sheet ") - fun sheet(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "sheet") sheetId: String) { - plugin.launch { - withContext(Dispatchers.IO) { - if (returnRaceSetting(raceId, sender)) return@withContext - } - newSuspendedTransaction(Dispatchers.IO) { - BetSetting.update({ BetSetting.raceId eq raceId }) { - it[spreadsheetId] = sheetId - } - } - createNewSheets(sheetId, raceId) + suspend fun sheet(sender: CommandSender, + @Argument(value = "raceId", suggestions = "raceId") raceId: String, + @Argument(value = "sheet") sheetId: String) { + withContext(Dispatchers.IO) { + if (returnRaceSetting(raceId, sender)) return@withContext } + BetSettingData.setSpreadSheetId(raceId, sheetId) + createNewSheets(sheetId, raceId) } private suspend fun createNewSheets(sheetId: String, raceId: String) = withContext(Dispatchers.IO) { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetTransfarCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetTransfarCommand.kt deleted file mode 100644 index ff291a1..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetTransfarCommand.kt +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright © 2021-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 - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package dev.nikomaru.raceassist.bet.commands - -import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.BetSetting -import dev.nikomaru.raceassist.utils.* -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext -import org.bukkit.Bukkit -import org.bukkit.OfflinePlayer -import org.bukkit.command.CommandSender -import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update -import java.util.* - -@CommandMethod("ra|RaceAssist bet") -class BetTransfarCommand { - @CommandPermission("RaceAssist.commands.bet.transfar") - @CommandMethod("transfar ") - fun transfer(sender: CommandSender, - @Argument(value = "raceId", suggestions = "raceId") raceId: String, - @Argument(value = "playerName", suggestions = "playerName") playerName: String) { - plugin.launch { - withContext(Dispatchers.IO) { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@withContext - } - val player: OfflinePlayer? = Bukkit.getOfflinePlayerIfCached(playerName) - - val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (player == null) { - sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) - return@launch - } - - if (!RaceStaffUtils.existStaff(raceId, player.uniqueId)) { - sender.sendMessage(Lang.getComponent("cant-set-not-staff", locale)) - return@launch - } - - newSuspendedTransaction(Dispatchers.IO) { - BetSetting.update({ BetSetting.raceId eq raceId }) { - it[creator] = player.uniqueId.toString() - } - } - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/database/RaceStaff.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/data/TempBetData.kt similarity index 65% rename from src/main/kotlin/dev/nikomaru/raceassist/database/RaceStaff.kt rename to src/main/kotlin/dev/nikomaru/raceassist/bet/data/TempBetData.kt index caadc9a..2f2a477 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/database/RaceStaff.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/data/TempBetData.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -14,11 +15,8 @@ * along with this program. If not, see . */ -package dev.nikomaru.raceassist.database +package dev.nikomaru.raceassist.bet.data -import org.jetbrains.exposed.sql.Table +import org.bukkit.OfflinePlayer -object RaceStaff : Table() { - val raceId = varchar("raceId", 30) - var uuid = varchar("uuid", 36) -} \ No newline at end of file +data class TempBetData(val raceId: String, val player: OfflinePlayer, val jockey: OfflinePlayer, var bet: Int) \ No newline at end of file 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 6f197b3..00b647e 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -23,10 +24,10 @@ import dev.nikomaru.raceassist.api.sheet.SheetsServiceUtil.getSheetsService import dev.nikomaru.raceassist.bet.GuiComponent import dev.nikomaru.raceassist.bet.commands.BetOpenCommand.Companion.TempBetDatas import dev.nikomaru.raceassist.bet.gui.BetChestGui.Companion.AllPlayers -import dev.nikomaru.raceassist.database.BetList -import dev.nikomaru.raceassist.database.BetList.rowNum -import dev.nikomaru.raceassist.database.BetSetting -import dev.nikomaru.raceassist.files.Config.betUnit +import dev.nikomaru.raceassist.data.database.BetList +import dev.nikomaru.raceassist.data.database.BetList.rowNum +import dev.nikomaru.raceassist.data.files.BetSettingData +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.Lang import kotlinx.coroutines.* import net.kyori.adventure.text.Component @@ -72,6 +73,7 @@ class BetGuiClickEvent : Listener { player.closeInventory() } + val betUnit = BetSettingData.getBetUnit(raceId) when (val slot = event.slot) { in 0..7 -> { @@ -86,7 +88,7 @@ class BetGuiClickEvent : Listener { val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId && it.jockey == AllPlayers[raceId]?.get(slot)) { + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot)) { it.bet = selectedNowBet + 10 } } @@ -112,7 +114,7 @@ class BetGuiClickEvent : Listener { val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId && it.jockey == AllPlayers[raceId]?.get(slot - 9)) { + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 9)) { it.bet = selectedNowBet + 1 } } @@ -139,14 +141,14 @@ class BetGuiClickEvent : Listener { event.inventory.setItem(slot, GuiComponent.noUnderNotice(player.locale())) player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1f, 0.7f) delay(1000) - event.inventory.setItem(slot, GuiComponent.onceDown(player.locale())) + event.inventory.setItem(slot, GuiComponent.onceDown(player.locale(), raceId)) return } player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 0.7f) val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId && it.jockey == AllPlayers[raceId]?.get(slot - 27)) { + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 27)) { it.bet = selectedNowBet - 1 } } @@ -173,7 +175,7 @@ class BetGuiClickEvent : Listener { event.inventory.setItem(slot, GuiComponent.noUnderNotice(player.locale())) player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1f, 0.7f) delay(1000) - event.inventory.setItem(slot, GuiComponent.tenTimesDown(player.locale())) + event.inventory.setItem(slot, GuiComponent.tenTimesDown(player.locale(), raceId)) return } player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 0.7f) @@ -181,7 +183,7 @@ class BetGuiClickEvent : Listener { val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId && it.jockey == AllPlayers[raceId]?.get(slot - 36)) { + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 36)) { it.bet = selectedNowBet - 10 } } @@ -202,7 +204,7 @@ class BetGuiClickEvent : Listener { val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId) { + if (it.raceId == raceId && it.player == player) { it.bet = 0 } } @@ -222,7 +224,7 @@ class BetGuiClickEvent : Listener { val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId) { + if (it.raceId == raceId && it.player == player) { iterator.remove() } } @@ -244,30 +246,31 @@ class BetGuiClickEvent : Listener { player.closeInventory() val eco: Economy = VaultAPI.getEconomy() - val owner = getBetOwner(raceId) + val owner = RaceSettingData.getOwner(raceId) newSuspendedTransaction(Dispatchers.Default) { var row = getMaxRow(raceId) val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val temp = iterator.next() - if (temp.raceId == raceId && temp.uuid == player.uniqueId && temp.bet != 0) { + if (temp.raceId == raceId && temp.player == player && temp.bet != 0) { BetList.insert { bet -> bet[BetList.raceId] = raceId bet[playerName] = player.name bet[playerUUID] = player.uniqueId.toString() - bet[jockey] = Bukkit.getOfflinePlayer(temp.jockey).name.toString() + bet[jockey] = temp.jockey.name.toString() + bet[jockeyUUID] = temp.jockey.uniqueId.toString() bet[betting] = temp.bet * betUnit bet[timeStamp] = LocalDateTime.now() bet[rowNum] = row + 1 } - betProcess(player, row, temp.bet, temp.jockey, eco, owner) + betProcess(player, row, temp.bet, temp.jockey, eco, owner, raceId) row++ } } while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId) { + if (it.raceId == raceId && it.player == player) { iterator.remove() } } @@ -295,18 +298,21 @@ class BetGuiClickEvent : Listener { maxRow } - private fun betProcess(player: Player, row: Int, bet: Int, jockey: UUID, eco: Economy, owner: OfflinePlayer) { + private suspend fun betProcess(player: Player, row: Int, bet: Int, jockey: OfflinePlayer, eco: Economy, owner: OfflinePlayer, raceId: String) { player.sendMessage(Lang.getComponent("bet-complete-message-player", player.locale(), row + 1, - Bukkit.getOfflinePlayer(jockey).name.toString(), - bet * betUnit)) - eco.withdrawPlayer(player, bet * betUnit.toDouble()) + jockey.name.toString(), + bet * BetSettingData.getBetUnit(raceId))) + eco.withdrawPlayer(player, bet * BetSettingData.getBetUnit(raceId).toDouble()) if (owner.isOnline) { - (owner as Player).sendMessage(Lang.getComponent("bet-complete-message-owner", player.locale(), player.name, bet * betUnit)) + (owner as Player).sendMessage(Lang.getComponent("bet-complete-message-owner", + player.locale(), + player.name, + bet * BetSettingData.getBetUnit(raceId))) } - eco.depositPlayer(owner, bet * betUnit.toDouble()) + eco.depositPlayer(owner, bet * BetSettingData.getBetUnit(raceId).toDouble()) } private suspend fun noticeNoBet(event: InventoryClickEvent, slot: Int, player: Player) { @@ -328,7 +334,7 @@ class BetGuiClickEvent : Listener { val iterator = TempBetDatas.iterator() while (iterator.hasNext()) { val it = iterator.next() - if (it.raceId == raceId && it.uuid == player.uniqueId && it.jockey == AllPlayers[raceId]?.get(slot)) { + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot)) { bet = it.bet } } @@ -338,7 +344,7 @@ class BetGuiClickEvent : Listener { private fun getAllBet(raceId: String, player: Player): Int { var sum = 0 TempBetDatas.forEach { - if (it.raceId == raceId && it.uuid == player.uniqueId) { + if (it.raceId == raceId && it.player == player) { sum += it.bet } } @@ -346,49 +352,45 @@ class BetGuiClickEvent : Listener { } private suspend fun putSheetsData(raceId: String) = withContext(Dispatchers.Default) { - val spreadsheetId = getSheetID(raceId) ?: return@withContext - val sheetsService = getSheetsService(spreadsheetId) ?: return@withContext - - var i = 1 - val data: ArrayList = ArrayList() - data.add(ValueRange().setRange("${raceId}_RaceAssist_Bet!A${i}").setValues(listOf(listOf(Lang.getText("sheet-timestamp", Locale.getDefault()), - Lang.getText("sheet-minecraft-name", Locale.getDefault()), - Lang.getText("sheet-jockey", Locale.getDefault()), - Lang.getText("sheet-bet-price", Locale.getDefault()), - Lang.getText("sheet-bet-multiplier", Locale.getDefault()), - getBetPercent(raceId))))) - - newSuspendedTransaction(Dispatchers.Default) { - BetList.select { BetList.raceId eq raceId }.forEach { - i++ - val player = it[BetList.playerName] - val jockey = it[BetList.jockey] - val betting = it[BetList.betting] - val timeStamp = it[BetList.timeStamp] - data.add(ValueRange().setRange("${raceId}_RaceAssist_Bet!A${i}") - .setValues(listOf(listOf(timeStamp.toString(), player, jockey, betting)))) - } - val batchBody = BatchUpdateValuesRequest().setValueInputOption("USER_ENTERED").setData(data) + val spreadSheetIdList = BetSettingData.getSpreadSheetId(raceId) + spreadSheetIdList?.let { sheetId -> + + val sheetsService = getSheetsService(sheetId) ?: return@withContext + + var i = 1 + val data: ArrayList = ArrayList() + data.add(ValueRange().setRange("${raceId}_RaceAssist_Bet!A${i}") + .setValues(listOf(listOf(Lang.getText("sheet-timestamp", Locale.getDefault()), + Lang.getText("sheet-minecraft-name", Locale.getDefault()), + Lang.getText("sheet-jockey", Locale.getDefault()), + Lang.getText("sheet-bet-price", Locale.getDefault()), + Lang.getText("sheet-bet-multiplier", Locale.getDefault()), + getBetPercent(raceId))))) + + newSuspendedTransaction(Dispatchers.Default) { + BetList.select { BetList.raceId eq raceId }.forEach { + i++ + val player = it[BetList.playerName] + val jockey = it[BetList.jockey] + val betting = it[BetList.betting] + val timeStamp = it[BetList.timeStamp] + data.add(ValueRange().setRange("${raceId}_RaceAssist_Bet!A${i}") + .setValues(listOf(listOf(timeStamp.toString(), player, jockey, betting)))) + } + val batchBody = BatchUpdateValuesRequest().setValueInputOption("USER_ENTERED").setData(data) - sheetsService.spreadsheets()?.values()?.batchUpdate(spreadsheetId, batchBody)?.execute() + sheetsService.spreadsheets()?.values()?.batchUpdate(sheetId, batchBody)?.execute() + } } } - private suspend fun getSheetID(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.firstOrNull()?.get(BetSetting.spreadsheetId) - } - private suspend fun getBetPercent(raceId: String): Int = newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.first()[BetSetting.returnPercent] + BetSettingData.getReturnPercent(raceId) } companion object { //Prevention of chattering-like phenomena private val clicked = HashMap() - - suspend fun getBetOwner(raceId: String) = Bukkit.getOfflinePlayer(UUID.fromString(newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.first()[BetSetting.creator] - })) } } \ No newline at end of file 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 f838878..0129901 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,15 +18,15 @@ package dev.nikomaru.raceassist.bet.gui import dev.nikomaru.raceassist.bet.GuiComponent -import dev.nikomaru.raceassist.database.* -import dev.nikomaru.raceassist.files.Config.betUnit +import dev.nikomaru.raceassist.data.database.BetList +import dev.nikomaru.raceassist.data.files.BetSettingData +import dev.nikomaru.raceassist.data.files.RaceSettingData 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 -import org.bukkit.Bukkit -import org.bukkit.Material +import org.bukkit.* import org.bukkit.entity.Player import org.bukkit.inventory.Inventory import org.bukkit.inventory.ItemStack @@ -33,28 +34,24 @@ import org.bukkit.inventory.meta.SkullMeta import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.util.* import kotlin.math.floor class BetChestGui { suspend fun getGUI(player: Player, raceId: String): Inventory { val gui = Bukkit.createInventory(player, 45, GuiComponent.guiComponent()) - val players: ArrayList = ArrayList() - val odds: HashMap = HashMap() + val players: ArrayList = ArrayList() + val odds: HashMap = HashMap() var sum = 0 - val rate: Int = newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId }.first()[BetSetting.returnPercent] - } + val rate: Int = BetSettingData.getReturnPercent(raceId) - 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])) - } + AllPlayers[raceId] = arrayListOf() + RaceSettingData.getJockeys(raceId).forEach { + players.add(it) + AllPlayers[raceId]!!.add(it) } + newSuspendedTransaction(Dispatchers.IO) { BetList.select { BetList.raceId eq raceId }.forEach { sum += it[BetList.betting] @@ -63,7 +60,7 @@ class BetChestGui { players.forEach { jockey -> newSuspendedTransaction(Dispatchers.IO) { var jockeySum = 0 - BetList.select { (BetList.raceId eq raceId) and (BetList.jockey eq Bukkit.getOfflinePlayer(jockey).name!!) }.forEach { + BetList.select { (BetList.raceId eq raceId) and (BetList.jockeyUUID eq jockey.uniqueId.toString()) }.forEach { jockeySum += it[BetList.betting] } odds[jockey] = floor(((sum * (rate.toDouble() / 100)) / jockeySum) * 100) / 100 @@ -77,19 +74,19 @@ class BetChestGui { for (i in 0 until players.size) { val item = ItemStack(Material.PLAYER_HEAD, 1) val meta: SkullMeta = item.itemMeta as SkullMeta - meta.owningPlayer = Bukkit.getOfflinePlayer(players[i]) - meta.displayName(Lang.getComponent("betting-zero-money", player.locale(), betUnit)) - val lore: ArrayList = ArrayList() - lore.add(Lang.getComponent("gui-jockey-name", player.locale(), Bukkit.getOfflinePlayer(players[i]).name)) + meta.owningPlayer = players[i] + meta.displayName(Lang.getComponent("betting-zero-money", player.locale(), BetSettingData.getBetUnit(raceId))) + val lore: ArrayList = arrayListOf() + lore.add(Lang.getComponent("gui-jockey-name", player.locale(), players[i].name)) lore.add(Lang.getComponent("gui-jockey-odds", player.locale(), odds[players[i]])) meta.lore(lore) item.itemMeta = meta - gui.setItem(i, GuiComponent.tenTimesUp(player.locale())) - gui.setItem(i + 9, GuiComponent.onceUp(player.locale())) + gui.setItem(i, GuiComponent.tenTimesUp(player.locale(), raceId)) + gui.setItem(i + 9, GuiComponent.onceUp(player.locale(), raceId)) gui.setItem(i + 18, item) - gui.setItem(i + 27, GuiComponent.onceDown(player.locale())) - gui.setItem(i + 36, GuiComponent.tenTimesDown(player.locale())) + gui.setItem(i + 27, GuiComponent.onceDown(player.locale(), raceId)) + gui.setItem(i + 36, GuiComponent.tenTimesDown(player.locale(), raceId)) } val raceIdItem = ItemStack(Material.GRAY_STAINED_GLASS_PANE) @@ -102,13 +99,11 @@ class BetChestGui { gui.setItem(35, GuiComponent.deny(player.locale())) gui.setItem(44, GuiComponent.accept(player.locale())) - - return gui } companion object { - val AllPlayers: HashMap> = HashMap() + val AllPlayers: HashMap> = HashMap() } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/database/BetList.kt b/src/main/kotlin/dev/nikomaru/raceassist/data/database/BetList.kt similarity index 81% rename from src/main/kotlin/dev/nikomaru/raceassist/database/BetList.kt rename to src/main/kotlin/dev/nikomaru/raceassist/data/database/BetList.kt index 18a98c7..8ab8bce 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/database/BetList.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/data/database/BetList.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -14,7 +15,7 @@ * along with this program. If not, see . */ -package dev.nikomaru.raceassist.database +package dev.nikomaru.raceassist.data.database import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.javatime.datetime @@ -26,5 +27,6 @@ object BetList : Table() { val playerName = varchar("playerName", 16) val playerUUID = varchar("playerUUID", 40) val jockey = varchar("jockey", 16) + val jockeyUUID = varchar("jockeyUUID", 40) val betting = integer("betting") } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/data/files/BetSettingData.kt b/src/main/kotlin/dev/nikomaru/raceassist/data/files/BetSettingData.kt new file mode 100644 index 0000000..031c4d1 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/data/files/BetSettingData.kt @@ -0,0 +1,66 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.data.files + +import dev.nikomaru.raceassist.data.files.RaceUtils.getRaceConfig +import dev.nikomaru.raceassist.data.files.RaceUtils.save +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +object BetSettingData { + + suspend fun getAvailable(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).bet.available + } + + suspend fun getReturnPercent(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).bet.returnPercent + } + + suspend fun getSpreadSheetId(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).bet.spreadSheetId + } + + suspend fun getBetUnit(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).bet.betUnit + } + + suspend fun setAvailable(raceId: String, available: Boolean) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.bet.available = available + data.save(raceId) + } + + suspend fun setReturnPercent(raceId: String, returnPercent: Int) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.bet.returnPercent = returnPercent + data.save(raceId) + } + + suspend fun setSpreadSheetId(raceId: String, spreadSheetId: String) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.bet.spreadSheetId = spreadSheetId + data.save(raceId) + } + + suspend fun setBetUnit(raceId: String, betUnit: Int) { + val data = getRaceConfig(raceId) + data.bet.betUnit = betUnit + data.save(raceId) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/data/files/PlaceSettingData.kt b/src/main/kotlin/dev/nikomaru/raceassist/data/files/PlaceSettingData.kt new file mode 100644 index 0000000..0f019d9 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/data/files/PlaceSettingData.kt @@ -0,0 +1,98 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.data.files + +import dev.nikomaru.raceassist.data.files.RaceUtils.getRaceConfig +import dev.nikomaru.raceassist.data.files.RaceUtils.save +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.awt.Polygon + +object PlaceSettingData { + + suspend fun getLap(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.lap + } + + suspend fun getInsidePolygon(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.inside + } + + suspend fun getOutsidePolygon(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.outside + } + + suspend fun getCentralXPoint(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.centralX + } + + suspend fun getCentralYPoint(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.centralY + } + + suspend fun getGoalDegree(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.goalDegree + } + + suspend fun getReverse(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).place.reverse + } + + suspend fun setLap(raceId: String, lap: Int) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.lap = lap + data.save(raceId) + } + + suspend fun setInsidePolygon(raceId: String, polygon: Polygon) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.inside = polygon + data.save(raceId) + } + + suspend fun setOutsidePolygon(raceId: String, polygon: Polygon) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.outside = polygon + data.save(raceId) + } + + suspend fun setCentralXPoint(raceId: String, x: Int) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.centralX = x + data.save(raceId) + } + + suspend fun setCentralYPoint(raceId: String, y: Int) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.centralY = y + data.save(raceId) + } + + suspend fun setGoalDegree(raceId: String, degree: Int) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.goalDegree = degree + data.save(raceId) + } + + suspend fun setReverse(raceId: String, reverse: Boolean) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.place.reverse = reverse + data.save(raceId) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/data/files/RaceSettingData.kt b/src/main/kotlin/dev/nikomaru/raceassist/data/files/RaceSettingData.kt new file mode 100644 index 0000000..43072cd --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/data/files/RaceSettingData.kt @@ -0,0 +1,122 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +@file:Suppress("BooleanMethodIsAlwaysInverted") + +package dev.nikomaru.raceassist.data.files + +import dev.nikomaru.raceassist.RaceAssist +import dev.nikomaru.raceassist.data.files.RaceUtils.getRaceConfig +import dev.nikomaru.raceassist.data.files.RaceUtils.save +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kotlinx.serialization.json.encodeToJsonElement +import org.bukkit.OfflinePlayer +import java.awt.Polygon +import java.io.* +import java.util.* + +object RaceSettingData { + + fun existsRace(raceId: String): Boolean { + val file = File(File(RaceAssist.plugin.dataFolder, "RaceData"), "$raceId.json") + return file.exists() + } + + suspend fun createRace(raceId: String, owner: OfflinePlayer): Boolean = withContext(Dispatchers.IO) { + val file = File(File(RaceAssist.plugin.dataFolder, "RaceData"), "$raceId.json") + if (!file.parentFile.exists()) { + file.parentFile.mkdirs() + } + if (file.exists()) { + return@withContext false + } + val raceName = raceId.split("-")[0] + + val place = Place(1, null, null, 0, false, Polygon(), Polygon()) + val bet = Bet(false, 75, null, 100) + val raceConfig = RaceConfig(raceId, raceName, owner, arrayListOf(owner), arrayListOf(), place, bet, hashMapOf()) + val json = json.encodeToJsonElement(raceConfig) + val string = json.toString() + + file.createNewFile() + val fw = PrintWriter(BufferedWriter(OutputStreamWriter(FileOutputStream(file), "UTF-8"))) + fw.write(string) + fw.close() + + return@withContext true + } + + suspend fun copyRace(raceId_1: String, raceId_2: String, owner: OfflinePlayer) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId_1) + data.raceId = raceId_2 + data.owner = owner + data.staff = arrayListOf(owner) + data.bet.available = false + data.jockeys = arrayListOf() + data.save(raceId_2) + + } + + fun deleteRace(raceId: String) { + val file = File(File(RaceAssist.plugin.dataFolder, "RaceData"), "$raceId.json") + file.delete() + } + + suspend fun getJockeys(raceId: String): ArrayList = withContext(Dispatchers.IO) { + getRaceConfig(raceId).jockeys + } + + suspend fun getOwner(raceId: String): OfflinePlayer = withContext(Dispatchers.IO) { + getRaceConfig(raceId).owner + } + + suspend fun getReplacement(raceId: String): HashMap = withContext(Dispatchers.IO) { + getRaceConfig(raceId).replacement + } + + suspend fun addJockey(raceId: String, jockey: OfflinePlayer) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.jockeys.add(jockey) + data.save(raceId) + } + + suspend fun setReplacement(raceId: String, uuid: UUID, name: String) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.replacement[uuid] = name + data.save(raceId) + } + + suspend fun removeJockey(raceId: String, jockey: OfflinePlayer) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.jockeys.remove(jockey) + data.save(raceId) + } + + suspend fun removeReplacement(raceId: String, uuid: UUID) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.replacement.remove(uuid) + data.save(raceId) + } + + suspend fun deleteReplacement(raceId: String) = withContext(Dispatchers.IO) { + val data = getRaceConfig(raceId) + data.replacement.clear() + data.save(raceId) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/data/files/RaceUtils.kt b/src/main/kotlin/dev/nikomaru/raceassist/data/files/RaceUtils.kt new file mode 100644 index 0000000..a4f0289 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/data/files/RaceUtils.kt @@ -0,0 +1,150 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.data.files + +import dev.nikomaru.raceassist.RaceAssist.Companion.plugin +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import kotlinx.serialization.* +import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* +import org.bukkit.Bukkit +import org.bukkit.OfflinePlayer +import java.awt.Polygon +import java.io.* +import java.nio.file.Files +import java.util.* + +object RaceUtils { + + suspend fun RaceConfig.save(raceId: String) { + val file = File(File(plugin.dataFolder, "RaceData"), "$raceId.json") + val json = json.encodeToJsonElement(this) + val string = json.toString() + raceConfigCache[raceId] = this + withContext(Dispatchers.IO) { + file.createNewFile() + val fw = PrintWriter(BufferedWriter(OutputStreamWriter(FileOutputStream(file), "UTF-8"))) + fw.write(string) + fw.close() + } + } + + suspend fun getRaceConfig(raceId: String) = withContext(Dispatchers.IO) { + if (raceConfigCache.containsKey(raceId)) { + return@withContext raceConfigCache[raceId]!! + } + + val file = File(File(plugin.dataFolder, "RaceData"), "$raceId.json") + val raceConfig = json.decodeFromString(Files.readString(file.toPath())) + raceConfigCache[raceId] = raceConfig + return@withContext raceConfig + } + + private val raceConfigCache: HashMap = HashMap() + +} + +@Serializable +data class RaceConfig(var raceId: String, + var raceName: String, + var owner: @Serializable(with = OfflinePlayerSerializer::class) OfflinePlayer, + var staff: ArrayList<@Serializable(with = OfflinePlayerSerializer::class) OfflinePlayer>, + var jockeys: ArrayList<@Serializable(with = OfflinePlayerSerializer::class) OfflinePlayer>, + val place: Place, + val bet: Bet, + val replacement: HashMap<@Serializable(with = UUIDSerializer::class) UUID, String>) + +@Serializable +data class Place(var lap: Int, + var centralX: Int?, + var centralY: Int?, + var goalDegree: Int, + var reverse: Boolean, + var inside: @Serializable(with = PolygonSerializer::class) Polygon, + var outside: @Serializable(with = PolygonSerializer::class) Polygon) + +@Serializable +data class Bet(var available: Boolean, var returnPercent: Int, var spreadSheetId: String?, var betUnit: Int) + +// UUID <==> String +object UUIDSerializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("UUID", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): UUID { + return UUID.fromString(decoder.decodeString()) + } + + override fun serialize(encoder: Encoder, value: UUID) { + encoder.encodeString(value.toString()) + } +} + +// OfflinePlayer <==> UUID +object OfflinePlayerSerializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("OfflinePlayer", PrimitiveKind.STRING) + + override fun deserialize(decoder: Decoder): OfflinePlayer { + return Bukkit.getOfflinePlayer(UUID.fromString(decoder.decodeString())) + } + + override fun serialize(encoder: Encoder, value: OfflinePlayer) { + encoder.encodeString(value.uniqueId.toString()) + } +} + +// Polygon <==> List,List> +object PolygonSerializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("Polygon", PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: Polygon) { + require(encoder is JsonEncoder) + val points: ArrayList> = ArrayList() + for (i in 0 until value.npoints) { + points.add(Pair(value.xpoints[i], value.ypoints[i])) + } + val encode = PolygonData(points) + encoder.encodeJsonElement(json.encodeToJsonElement(encode)) + } + + override fun deserialize(decoder: Decoder): Polygon { + require(decoder is JsonDecoder) + val element = decoder.decodeJsonElement() + val polygonData = json.decodeFromJsonElement(element) + + val polygon = Polygon() + + polygonData.points.forEach { + polygon.addPoint(it.first, it.second) + } + + return polygon + } +} + +@Serializable +data class PolygonData(val points: ArrayList>) + +val json = Json { + isLenient = true + prettyPrint = true +} diff --git a/src/main/kotlin/dev/nikomaru/raceassist/data/files/StaffSettingData.kt b/src/main/kotlin/dev/nikomaru/raceassist/data/files/StaffSettingData.kt new file mode 100644 index 0000000..ed685a0 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/data/files/StaffSettingData.kt @@ -0,0 +1,53 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.data.files + +import dev.nikomaru.raceassist.data.files.RaceUtils.getRaceConfig +import dev.nikomaru.raceassist.data.files.RaceUtils.save +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.bukkit.OfflinePlayer + +object StaffSettingData { + + suspend fun getStaffs(raceId: String) = withContext(Dispatchers.IO) { + getRaceConfig(raceId).staff + } + + suspend fun addStaff(raceId: String, player: OfflinePlayer) = withContext(Dispatchers.IO) { + if (existStaff(raceId, player)) return@withContext false + val data = getRaceConfig(raceId) + data.staff.add(player) + data.save(raceId) + return@withContext true + } + + suspend fun removeStaff(raceId: String, player: OfflinePlayer) = withContext(Dispatchers.IO) { + if (RaceSettingData.getOwner(raceId) == player || !existStaff(raceId, player)) { + return@withContext false //Owner can't be removed or staff can't be removed if they aren't in the list + } + val data = getRaceConfig(raceId) + data.staff.remove(player) + data.save(raceId) + return@withContext true + } + + suspend fun existStaff(raceId: String, staff: OfflinePlayer) = withContext(Dispatchers.IO) { + return@withContext getRaceConfig(raceId).staff.contains(staff) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/database/BetSetting.kt b/src/main/kotlin/dev/nikomaru/raceassist/database/BetSetting.kt deleted file mode 100644 index fdf2b8a..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/database/BetSetting.kt +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright © 2021-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 - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package dev.nikomaru.raceassist.database - -import org.jetbrains.exposed.sql.Table - -object BetSetting : Table() { - val raceId = varchar("raceId", 30) - val creator = varchar("creator", 40) - val canBet = bool("canBet") - val returnPercent = integer("returnPercent") - val spreadsheetId = varchar("spreadsheetId", 50).nullable() -} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/database/CircuitPoint.kt b/src/main/kotlin/dev/nikomaru/raceassist/database/CircuitPoint.kt deleted file mode 100644 index d1e5c67..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/database/CircuitPoint.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright © 2021-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 - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package dev.nikomaru.raceassist.database - -import org.jetbrains.exposed.sql.Table - -object CircuitPoint : Table() { - val raceId = varchar("raceId", 30) - val inside = bool("Inside") - val XPoint = (integer("XPoint")) - val YPoint = (integer("YPoint")) -} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/database/PlayerList.kt b/src/main/kotlin/dev/nikomaru/raceassist/database/PlayerList.kt deleted file mode 100644 index 774d5c7..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/database/PlayerList.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright © 2021-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 - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package dev.nikomaru.raceassist.database - -import org.jetbrains.exposed.sql.Table - -object PlayerList : Table() { - val raceId = varchar("raceId", 30) - val playerUUID = varchar("playerUUID", 40) -} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/database/RaceList.kt b/src/main/kotlin/dev/nikomaru/raceassist/database/RaceList.kt deleted file mode 100644 index 3abfdf8..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/database/RaceList.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright © 2021-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 - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package dev.nikomaru.raceassist.database - -import org.jetbrains.exposed.sql.Table - -object RaceList : Table() { - val raceId = varchar("raceId", 30) - val creator = varchar("creator", 40) - val reverse = bool("reverse") - val lap = integer("lap") - val centralXPoint = integer("centralXPoint").nullable() - val centralYPoint = integer("centralYPoint").nullable() - val goalDegree = integer("goalDegree").nullable() -} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/dispatch/discord/DiscordWebhook.kt b/src/main/kotlin/dev/nikomaru/raceassist/dispatch/discord/DiscordWebhook.kt index 5671779..bde16d8 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/dispatch/discord/DiscordWebhook.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/dispatch/discord/DiscordWebhook.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -23,33 +24,36 @@ import java.net.HttpURLConnection import java.net.URL import javax.net.ssl.HttpsURLConnection -class DiscordWebhook { +object DiscordWebhook { fun sendWebHook(json: String) { - try { - val webHookUrl = URL(Config.discordWebHook) - val con: HttpsURLConnection = (webHookUrl.openConnection() as HttpsURLConnection) - con.addRequestProperty("Content-Type", "application/JSON; charset=utf-8") - con.addRequestProperty("User-Agent", "DiscordBot") - con.doOutput = true - con.requestMethod = "POST" + Config.config.discordWebHook.result.forEach { + try { + val webHookUrl = URL(it) + val con: HttpsURLConnection = (webHookUrl.openConnection() as HttpsURLConnection) - con.setRequestProperty("Content-Length", json.length.toString()) + con.addRequestProperty("Content-Type", "application/JSON; charset=utf-8") + con.addRequestProperty("User-Agent", "DiscordBot") + con.doOutput = true + con.requestMethod = "POST" - val stream: OutputStream = con.outputStream - stream.write(json.toByteArray(Charsets.UTF_8)) - stream.flush() - stream.close() + con.setRequestProperty("Content-Length", json.length.toString()) - val status: Int = con.responseCode - if (status != HttpURLConnection.HTTP_OK && status != HttpURLConnection.HTTP_NO_CONTENT) { - plugin.logger.warning("error:$status") - } - con.disconnect() + val stream: OutputStream = con.outputStream + stream.write(json.toByteArray(Charsets.UTF_8)) + stream.flush() + stream.close() - } catch (e: Exception) { - e.printStackTrace() + val status: Int = con.responseCode + if (status != HttpURLConnection.HTTP_OK && status != HttpURLConnection.HTTP_NO_CONTENT) { + plugin.logger.warning("error:$status") + } + con.disconnect() + + } catch (e: Exception) { + e.printStackTrace() + } } } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt b/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt index a916c7f..9839bca 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -15,26 +16,58 @@ */ package dev.nikomaru.raceassist.files +import com.typesafe.config.ConfigFactory +import com.typesafe.config.ConfigRenderOptions import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import org.bukkit.configuration.file.FileConfiguration +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.hocon.* +import java.io.* object Config { - var config: FileConfiguration? = null - - var threshold: Int? = null - var discordWebHook: String? = null - var betUnit: Int = 0 + lateinit var config: ConfigData + const val version: String = "1.0.0" + @ExperimentalSerializationApi fun load() { - if (config != null) { - plugin.reloadConfig() + val file = plugin.dataFolder.resolve("config.conf") + + createConfig(file) + + config = hocon.decodeFromConfig(ConfigFactory.parseFile(file)) + } + + @ExperimentalSerializationApi + private fun createConfig(file: File) { + if (!file.parentFile.exists()) { + file.parentFile.mkdirs() } - threshold = config!!.getInt("RaceSetting.threshold") - betUnit = config!!.getInt("RaceSetting.bet") - discordWebHook = config!!.getString("NetworkSettings.discord") + val spreadSheet = SpreadSheet(8888, arrayListOf()) + val discordWebHook = DiscordWebHook(arrayListOf(), arrayListOf()) + val configData = ConfigData(version, 40, 200, discordWebHook, spreadSheet) + + val renderOptions = ConfigRenderOptions.defaults().setOriginComments(false).setComments(false).setFormatted(true).setJson(false) + val string = hocon.encodeToConfig(configData).root().render(renderOptions) + + if (!file.exists()) { + file.createNewFile() + val fw = PrintWriter(BufferedWriter(OutputStreamWriter(FileOutputStream(file), "UTF-8"))) + fw.write(string) + fw.close() + } else { + val verNode = hocon.decodeFromConfig(ConfigFactory.parseFile(file)).version + if (verNode != version) { + file.deleteOnExit() + val fw = PrintWriter(BufferedWriter(OutputStreamWriter(FileOutputStream(file), "UTF-8"))) + fw.write(string) + fw.close() + } + } } } +@ExperimentalSerializationApi +private val hocon = Hocon + diff --git a/src/main/kotlin/dev/nikomaru/raceassist/files/ConfigData.kt b/src/main/kotlin/dev/nikomaru/raceassist/files/ConfigData.kt new file mode 100644 index 0000000..f1c2f7c --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/files/ConfigData.kt @@ -0,0 +1,30 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.files + +import kotlinx.serialization.Serializable + +//TODO(change to hocon to use kotlinx.serialization) +@Serializable +data class ConfigData(val version: String, val threshold: Int, val delay: Long, val discordWebHook: DiscordWebHook, val spreadSheet: SpreadSheet) + +@Serializable +data class DiscordWebHook(val result: ArrayList, val betAll: ArrayList) + +@Serializable +data class SpreadSheet(val port: Int, val sheetName: ArrayList) \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/horse/event/HorseBreedEvent.kt b/src/main/kotlin/dev/nikomaru/raceassist/horse/event/HorseBreedEvent.kt new file mode 100644 index 0000000..9914fc0 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/horse/event/HorseBreedEvent.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.horse.event + +import dev.nikomaru.raceassist.horse.utlis.HorseUtils.getCalcJump +import dev.nikomaru.raceassist.horse.utlis.HorseUtils.getCalcSpeed +import org.bukkit.entity.EntityType +import org.bukkit.entity.Horse +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.entity.EntityBreedEvent + +class HorseBreedEvent : Listener { + @EventHandler + suspend fun onHorseBreed(event: EntityBreedEvent) { + if (event.entity.type != EntityType.HORSE) { + return + } + val horse = event.entity as Horse + + if (horse.getCalcSpeed() < 13.7 && horse.getCalcJump() < 4.0) { + return + } + val mother = event.mother as Horse + val father = event.father as Horse + + } + +} diff --git a/src/main/kotlin/dev/nikomaru/raceassist/horse/utlis/HorseUtils.kt b/src/main/kotlin/dev/nikomaru/raceassist/horse/utlis/HorseUtils.kt new file mode 100644 index 0000000..30371f1 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/horse/utlis/HorseUtils.kt @@ -0,0 +1,37 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.horse.utlis + +import org.bukkit.attribute.Attribute +import org.bukkit.entity.Horse +import kotlin.math.pow + +object HorseUtils { + + fun Horse.getCalcSpeed(): Double { + return this.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED)!!.value * 42.162962963 + } + + fun Horse.getCalcJump(): Double { + return this.jumpStrength.pow(1.7) * 5.293; + } + + fun Horse.getCalcMaxHealth(): Double { + return this.getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value + } +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/HelpCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/HelpCommand.kt index 0e81a40..1a8a4a7 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/HelpCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/HelpCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,7 +18,7 @@ package dev.nikomaru.raceassist.race.commands import cloud.commandframework.annotations.* -import net.kyori.adventure.text.minimessage.MiniMessage +import dev.nikomaru.raceassist.utils.Lang.mm import org.bukkit.command.CommandSender @CommandMethod("ra|raceassist") @@ -42,7 +43,7 @@ class HelpCommand { /ra bet open 賭け画面を開く """.trimIndent() } - sender.sendMessage(MiniMessage.miniMessage().deserialize(message)) + sender.sendMessage(mm.deserialize(message)) } @CommandMethod("help ") @@ -117,7 +118,7 @@ class HelpCommand { "" } } - sender.sendMessage(MiniMessage.miniMessage().deserialize(message)) + sender.sendMessage(mm.deserialize(message)) } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceJoinCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceJoinCommand.kt index af5c965..454b150 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceJoinCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceJoinCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,10 +18,8 @@ package dev.nikomaru.raceassist.race.commands.audience import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.audience -import dev.nikomaru.raceassist.utils.CommandUtils.getRaceExist import dev.nikomaru.raceassist.utils.Lang import org.bukkit.command.CommandSender import org.bukkit.entity.Player @@ -29,25 +28,25 @@ import org.bukkit.entity.Player class AudienceJoinCommand { @CommandPermission("RaceAssist.commands.audience.join") @CommandMethod("join ") - private fun join(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + fun join(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (!getRaceExist(raceId)) { - sender.sendMessage(Lang.getComponent("not-found-this-race", sender.locale())) - return@launch - } - if (audience[raceId]?.contains(sender.uniqueId) == true) { - sender.sendMessage(Lang.getComponent("already-joined", sender.locale())) - return@launch - } - if (!audience.containsKey(raceId)) { - audience[raceId] = ArrayList() - } - audience[raceId]?.add(sender.uniqueId) - sender.sendMessage(Lang.getComponent("joined-group", sender.locale())) + + if (!RaceSettingData.existsRace(raceId)) { + sender.sendMessage(Lang.getComponent("not-found-this-race", sender.locale())) + return } + if (audience[raceId]?.contains(sender.uniqueId) == true) { + sender.sendMessage(Lang.getComponent("already-joined", sender.locale())) + return + } + if (!audience.containsKey(raceId)) { + audience[raceId] = ArrayList() + } + audience[raceId]?.add(sender.uniqueId) + sender.sendMessage(Lang.getComponent("joined-group", sender.locale())) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceLeaveCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceLeaveCommand.kt index 8f12d64..d2c0ea2 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceLeaveCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceLeaveCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -26,7 +27,7 @@ import org.bukkit.entity.Player class AudienceLeaveCommand { @CommandPermission("RaceAssist.commands.audience.leave") @CommandMethod("leave ") - private fun leave(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + fun leave(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceListCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceListCommand.kt index 37f6702..90063ef 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceListCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/audience/AudienceListCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,8 +18,6 @@ package dev.nikomaru.raceassist.race.commands.audience import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang @@ -31,14 +30,14 @@ import java.util.* class AudienceListCommand { @CommandPermission("RaceAssist.commands.audience.list") @CommandMethod("list ") - private fun list(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun list(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { val locale = if (sender is Player) sender.locale() else Locale.getDefault() - plugin.launch { - if (returnRaceSetting(raceId, sender)) return@launch - sender.sendMessage(Lang.getComponent("participants-list", locale)) - CommandUtils.audience[raceId]?.forEach { - sender.sendMessage(Bukkit.getOfflinePlayer(it).name.toString()) - } + + if (returnRaceSetting(raceId, sender)) return + sender.sendMessage(Lang.getComponent("participants-list", locale)) + CommandUtils.audience[raceId]?.forEach { + sender.sendMessage(Bukkit.getOfflinePlayer(it).name.toString()) } + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceCentralCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceCentralCommand.kt index babcb58..99e0163 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceCentralCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceCentralCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,8 +18,6 @@ package dev.nikomaru.raceassist.race.commands.place import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.utils.CommandUtils.canSetCentral import dev.nikomaru.raceassist.utils.CommandUtils.centralRaceId import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting @@ -30,16 +29,16 @@ import org.bukkit.entity.Player class PlaceCentralCommand { @CommandPermission("RaceAssist.commands.place.central") @CommandMethod("central ") - fun central(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun central(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (returnRaceSetting(raceId, sender)) return@launch - canSetCentral[sender.uniqueId] = true - centralRaceId[sender.uniqueId] = raceId - sender.sendMessage(Lang.getComponent("to-set-central-point", sender.locale())) - } + + if (returnRaceSetting(raceId, sender)) return + canSetCentral[sender.uniqueId] = true + centralRaceId[sender.uniqueId] = raceId + sender.sendMessage(Lang.getComponent("to-set-central-point", sender.locale())) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceDegreeCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceDegreeCommand.kt index c410cd3..861bd9a 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceDegreeCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceDegreeCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,67 +18,56 @@ package dev.nikomaru.raceassist.race.commands.place import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.RaceList +import dev.nikomaru.raceassist.data.files.PlaceSettingData +import dev.nikomaru.raceassist.data.files.PlaceSettingData.getReverse import dev.nikomaru.raceassist.utils.CommandUtils.getCentralPoint import dev.nikomaru.raceassist.utils.CommandUtils.getRaceDegree -import dev.nikomaru.raceassist.utils.CommandUtils.getReverse import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update @CommandMethod("ra|RaceAssist place") class PlaceDegreeCommand { @CommandPermission("RaceAssist.commands.place.degree") @CommandMethod("degree ") - fun degree(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun degree(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (returnRaceSetting(raceId, sender)) return@launch - val centralXPoint = - getCentralPoint(raceId, true) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", sender.locale())) - val centralYPoint = - getCentralPoint(raceId, false) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", sender.locale())) - val reverse = getReverse(raceId) ?: return@launch sender.sendMessage(Lang.getComponent("orientation-is-not-set", sender.locale())) - val nowX = sender.location.blockX - val nowY = sender.location.blockZ - val relativeNowX = if (!reverse) nowX - centralXPoint else -1 * (nowX - centralXPoint) - val relativeNowY = nowY - centralYPoint - val degree = when (getRaceDegree(relativeNowY.toDouble(), relativeNowX.toDouble())) { - in 0..45 -> { - 0 - } - in 46..135 -> { - 90 - } - in 136..225 -> { - 180 - } - in 226..315 -> { - 270 - } - in 316..360 -> { - 0 - } - else -> { - 0 - } + if (returnRaceSetting(raceId, sender)) return + val centralXPoint = getCentralPoint(raceId, true) ?: return sender.sendMessage(Lang.getComponent("no-exist-central-point", sender.locale())) + val centralYPoint = getCentralPoint(raceId, false) ?: return sender.sendMessage(Lang.getComponent("no-exist-central-point", sender.locale())) + val reverse = getReverse(raceId) + val nowX = sender.location.blockX + val nowY = sender.location.blockZ + val relativeNowX = if (!reverse) nowX - centralXPoint else -1 * (nowX - centralXPoint) + val relativeNowY = nowY - centralYPoint + + val degree = when (getRaceDegree(relativeNowY.toDouble(), relativeNowX.toDouble())) { + in 0..45 -> { + 0 + } + in 46..135 -> { + 90 + } + in 136..225 -> { + 180 } - sender.sendMessage(Lang.getComponent("to-set-degree", sender.locale(), degree)) - newSuspendedTransaction(Dispatchers.IO) { - RaceList.update({ RaceList.raceId eq raceId }) { - it[goalDegree] = degree - } + in 226..315 -> { + 270 + } + in 316..360 -> { + 0 + } + else -> { + 0 } } + sender.sendMessage(Lang.getComponent("to-set-degree", sender.locale(), degree)) + PlaceSettingData.setGoalDegree(raceId, degree) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceFinishCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceFinishCommand.kt index 8774187..32306cb 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceFinishCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceFinishCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -18,8 +19,6 @@ package dev.nikomaru.raceassist.race.commands.place import cloud.commandframework.annotations.CommandMethod import cloud.commandframework.annotations.CommandPermission -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.race.utils.InsideCircuit import dev.nikomaru.raceassist.race.utils.OutsideCircuit import dev.nikomaru.raceassist.utils.CommandUtils.canSetInsideCircuit @@ -32,26 +31,26 @@ import org.bukkit.entity.Player class PlaceFinishCommand { @CommandPermission("RaceAssist.commands.place.finish") @CommandMethod("finish") - fun finish(sender: CommandSender) { + suspend fun finish(sender: CommandSender) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (canSetOutsideCircuit[sender.uniqueId] == true && canSetInsideCircuit[sender.uniqueId] == true) { - sender.sendMessage(Lang.getComponent("now-you-not-setting-mode", sender.locale())) - return@launch - } - if (canSetInsideCircuit[sender.uniqueId] == true) { - canSetInsideCircuit.remove(sender.uniqueId) - InsideCircuit.finish(sender) - sender.sendMessage(Lang.getComponent("to-finish-inside-course-setting", sender.locale())) - } - if (canSetOutsideCircuit[sender.uniqueId] == true) { - canSetOutsideCircuit.remove(sender.uniqueId) - OutsideCircuit.finish(sender) - sender.sendMessage(Lang.getComponent("to-finish-outside-course-setting", sender.locale())) - } + + if (canSetOutsideCircuit[sender.uniqueId] == true && canSetInsideCircuit[sender.uniqueId] == true) { + sender.sendMessage(Lang.getComponent("now-you-not-setting-mode", sender.locale())) + return + } + if (canSetInsideCircuit[sender.uniqueId] == true) { + canSetInsideCircuit.remove(sender.uniqueId) + InsideCircuit.finish(sender) + sender.sendMessage(Lang.getComponent("to-finish-inside-course-setting", sender.locale())) + } + if (canSetOutsideCircuit[sender.uniqueId] == true) { + canSetOutsideCircuit.remove(sender.uniqueId) + OutsideCircuit.finish(sender) + sender.sendMessage(Lang.getComponent("to-finish-outside-course-setting", sender.locale())) } + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceLapCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceLapCommand.kt index 35f36c3..923da01 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceLapCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceLapCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -18,41 +19,32 @@ package dev.nikomaru.raceassist.race.commands.place import cloud.commandframework.annotations.* import cloud.commandframework.annotations.specifier.Range -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.RaceList +import dev.nikomaru.raceassist.data.files.PlaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update @CommandMethod("ra|RaceAssist place") class PlaceLapCommand { @CommandPermission("RaceAssist.commands.place.lap") @CommandMethod("lap ") - fun setLap(sender: CommandSender, + suspend fun setLap(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "lap") @Range(min = "1", max = "100") lap: Int) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - if (lap < 1) { - sender.sendMessage(Lang.getComponent("to-need-enter-over-1", sender.locale())) - return@launch - } - newSuspendedTransaction(Dispatchers.IO) { - RaceList.update({ RaceList.raceId eq raceId }) { - it[this.lap] = lap - } - } - sender.sendMessage(Lang.getComponent("to-set-lap", sender.locale())) + if (CommandUtils.returnRaceSetting(raceId, sender)) return + + if (lap < 1) { + sender.sendMessage(Lang.getComponent("to-need-enter-over-1", sender.locale())) + return } + PlaceSettingData.setLap(raceId, lap) + sender.sendMessage(Lang.getComponent("to-set-lap", sender.locale())) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceReverseCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceReverseCommand.kt index ea973c2..6fe1e1c 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceReverseCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceReverseCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,38 +18,27 @@ package dev.nikomaru.raceassist.race.commands.place import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.RaceList -import dev.nikomaru.raceassist.utils.CommandUtils.getDirection +import dev.nikomaru.raceassist.data.files.PlaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update @CommandMethod("ra|RaceAssist place") class PlaceReverseCommand { @CommandPermission("RaceAssist.commands.place.reverse") @CommandMethod("reverse ") - fun reverse(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun reverse(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (returnRaceSetting(raceId, sender)) return@launch - val nowDirection = getDirection(raceId) + if (returnRaceSetting(raceId, sender)) return + + PlaceSettingData.setReverse(raceId, !PlaceSettingData.getReverse(raceId)) + + sender.sendMessage(Lang.getComponent("to-change-race-orientation", sender.locale())) - newSuspendedTransaction(Dispatchers.IO) { - RaceList.update({ RaceList.raceId eq raceId }) { - it[reverse] = !nowDirection - } - } - sender.sendMessage(Lang.getComponent("to-change-race-orientation", sender.locale())) - } } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceSetCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceSetCommand.kt index 11f3fd7..0fa43ad 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceSetCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/place/PlaceSetCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,13 +18,11 @@ package dev.nikomaru.raceassist.race.commands.place import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.canSetInsideCircuit import dev.nikomaru.raceassist.utils.CommandUtils.canSetOutsideCircuit import dev.nikomaru.raceassist.utils.CommandUtils.circuitRaceId import dev.nikomaru.raceassist.utils.CommandUtils.getInsideRaceExist -import dev.nikomaru.raceassist.utils.CommandUtils.getRaceExist import dev.nikomaru.raceassist.utils.CommandUtils.returnRaceSetting import dev.nikomaru.raceassist.utils.Lang import org.bukkit.command.CommandSender @@ -33,38 +32,37 @@ import org.bukkit.entity.Player class PlaceSetCommand { @CommandPermission("RaceAssist.commands.place.set") @CommandMethod("set ") - fun set(sender: CommandSender, + suspend fun set(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "type", suggestions = "placeType") type: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (!getRaceExist(raceId)) { - sender.sendMessage(Lang.getComponent("no-exist-race", sender.locale())) - return@launch - } else if (returnRaceSetting(raceId, sender)) return@launch + if (!RaceSettingData.existsRace(raceId)) { + sender.sendMessage(Lang.getComponent("no-exist-race", sender.locale())) + return + } else if (returnRaceSetting(raceId, sender)) return - if (canSetOutsideCircuit[sender.uniqueId] != null || canSetInsideCircuit[sender.uniqueId] != null) { - sender.sendMessage(Lang.getComponent("already-setting-mode", sender.locale())) - return@launch - } - if (type == "in") { - canSetInsideCircuit[sender.uniqueId] = true - sender.sendMessage(Lang.getComponent("to-be-inside-set-mode", sender.locale())) - } else if (type == "out") { - if (!getInsideRaceExist(raceId)) { - sender.sendMessage(Lang.getComponent("no-inside-course-set", sender.locale())) - return@launch - } - canSetOutsideCircuit[sender.uniqueId] = true - sender.sendMessage(Lang.getComponent("to-be-outside-set-mode", sender.locale())) + if (canSetOutsideCircuit[sender.uniqueId] != null || canSetInsideCircuit[sender.uniqueId] != null) { + sender.sendMessage(Lang.getComponent("already-setting-mode", sender.locale())) + return + } + if (type == "in") { + canSetInsideCircuit[sender.uniqueId] = true + sender.sendMessage(Lang.getComponent("to-be-inside-set-mode", sender.locale())) + } else if (type == "out") { + if (!getInsideRaceExist(raceId)) { + sender.sendMessage(Lang.getComponent("no-inside-course-set", sender.locale())) + return } - circuitRaceId[sender.uniqueId] = raceId - sender.sendMessage(Lang.getComponent("to-click-left-start-right-finish", sender.locale())) - sender.sendMessage(Lang.getComponent("to-enter-finish-message", sender.locale())) + canSetOutsideCircuit[sender.uniqueId] = true + sender.sendMessage(Lang.getComponent("to-be-outside-set-mode", sender.locale())) } + circuitRaceId[sender.uniqueId] = raceId + sender.sendMessage(Lang.getComponent("to-click-left-start-right-finish", sender.locale())) + sender.sendMessage(Lang.getComponent("to-enter-finish-message", sender.locale())) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt index 53b3b3e..4c8501d 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerAddCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,20 +18,13 @@ package dev.nikomaru.raceassist.race.commands.player import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.PlayerList +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils -import dev.nikomaru.raceassist.utils.CommandUtils.getRacePlayerAmount -import dev.nikomaru.raceassist.utils.CommandUtils.getRacePlayerExist import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.insert -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.util.* @CommandMethod("ra|RaceAssist player") @@ -38,35 +32,27 @@ class PlayerAddCommand { @CommandPermission("RaceAssist.commands.player.add") @CommandMethod("add ") - private fun addPlayer(sender: CommandSender, + suspend fun addPlayer(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "playerName", suggestions = "playerName") playerName: String) { - - val jockey: OfflinePlayer = Bukkit.getOfflinePlayer(playerName) + if (CommandUtils.returnRaceSetting(raceId, sender)) return val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (!jockey.hasPlayedBefore()) { - sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + val jockey: OfflinePlayer = + Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + + if (RaceSettingData.getJockeys(raceId).contains(jockey)) { + sender.sendMessage(Lang.getComponent("already-exist-this-user", locale)) return } - - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - if (getRacePlayerExist(raceId, jockey.uniqueId)) { - sender.sendMessage(Lang.getComponent("already-exist-this-user", locale)) - return@launch - } - if (getRacePlayerAmount(raceId) > 7) { - sender.sendMessage(Lang.getComponent("max-player-is-eight", locale)) - return@launch - } - newSuspendedTransaction(Dispatchers.IO) { - PlayerList.insert { - it[this.raceId] = raceId - it[playerUUID] = jockey.uniqueId.toString() - } - } - sender.sendMessage(Lang.getComponent("player-add-to-race-group", locale, jockey.name.toString(), raceId)) + if (RaceSettingData.getJockeys(raceId).size > 7) { + sender.sendMessage(Lang.getComponent("max-player-is-eight", locale)) + return } + RaceSettingData.addJockey(raceId, jockey) + sender.sendMessage(Lang.getComponent("player-add-to-race-group", locale, jockey.name.toString(), raceId)) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerDeleteCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerDeleteCommand.kt index cda0a83..a8a5365 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerDeleteCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerDeleteCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,16 +18,11 @@ package dev.nikomaru.raceassist.race.commands.player import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.PlayerList +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.deleteWhere -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.util.* @CommandMethod("ra|RaceAssist player") @@ -34,16 +30,15 @@ class PlayerDeleteCommand { @CommandPermission("RaceAssist.commands.player.delete") @CommandMethod("delete ") - private fun deletePlayer(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun deletePlayer(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch + if (CommandUtils.returnRaceSetting(raceId, sender)) return - newSuspendedTransaction(Dispatchers.IO) { - PlayerList.deleteWhere { PlayerList.raceId eq raceId } - } - val locale = if (sender is Player) sender.locale() else Locale.getDefault() - sender.sendMessage(Lang.getComponent("to-delete-all-player-from-race-group", locale, raceId)) + RaceSettingData.getJockeys(raceId).forEach { + RaceSettingData.removeJockey(raceId, it) } + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + sender.sendMessage(Lang.getComponent("to-delete-all-player-from-race-group", locale, raceId)) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerListCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerListCommand.kt index 1be1c28..a915c40 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerListCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerListCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,31 +18,25 @@ package dev.nikomaru.raceassist.race.commands.player import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.PlayerList +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils -import kotlinx.coroutines.Dispatchers -import org.bukkit.Bukkit import org.bukkit.command.CommandSender -import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.util.* @CommandMethod("ra|RaceAssist player") class PlayerListCommand { @CommandPermission("RaceAssist.commands.player.list") @CommandMethod("list ") - private fun displayPlayerList(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch + suspend fun displayPlayerList(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + + if (CommandUtils.returnRaceSetting(raceId, sender)) return + if (RaceSettingData.getJockeys(raceId).isEmpty()) { + sender.sendMessage("プレイヤーはいません") + } - newSuspendedTransaction(Dispatchers.IO) { - PlayerList.select { PlayerList.raceId eq raceId }.forEach { - sender.sendMessage(Bukkit.getOfflinePlayer(UUID.fromString(it[PlayerList.playerUUID])).name.toString()) - } - } + RaceSettingData.getJockeys(raceId).forEach { + sender.sendMessage(it.name.toString()) } + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerRemoveCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerRemoveCommand.kt index ddd4c01..c8b850f 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerRemoveCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/player/PlayerRemoveCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,40 +18,30 @@ package dev.nikomaru.raceassist.race.commands.player import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.PlayerList +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.Bukkit import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.and -import org.jetbrains.exposed.sql.deleteWhere -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.util.* @CommandMethod("ra|RaceAssist player") class PlayerRemoveCommand { @CommandPermission("RaceAssist.commands.player.remove") @CommandMethod("remove ") - private fun removePlayer(sender: CommandSender, + suspend fun removePlayer(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "playerName", suggestions = "playerName") playerName: String) { - val player = Bukkit.getOfflinePlayer(playerName) val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (!player.hasPlayedBefore()) { - sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) - return - } - - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - newSuspendedTransaction(Dispatchers.IO) { - PlayerList.deleteWhere { (PlayerList.raceId eq raceId) and (PlayerList.playerUUID eq player.uniqueId.toString()) } - } - sender.sendMessage(Lang.getComponent("to-delete-player-from-race-group", locale, raceId)) - } + val player = Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + + + + if (CommandUtils.returnRaceSetting(raceId, sender)) return + RaceSettingData.removeJockey(raceId, player) + sender.sendMessage(Lang.getComponent("to-delete-player-from-race-group", locale, raceId)) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceDebugCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceDebugCommand.kt index dd89c43..6febc5e 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceDebugCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceDebugCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -19,15 +20,12 @@ package dev.nikomaru.raceassist.race.commands.race import cloud.commandframework.annotations.* import com.github.shynixn.mccoroutine.bukkit.launch import dev.nikomaru.raceassist.RaceAssist.Companion.plugin +import dev.nikomaru.raceassist.data.files.PlaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.CommandUtils.displayLap import dev.nikomaru.raceassist.utils.CommandUtils.getCentralPoint -import dev.nikomaru.raceassist.utils.CommandUtils.getCircuitExist -import dev.nikomaru.raceassist.utils.CommandUtils.getGoalDegree -import dev.nikomaru.raceassist.utils.CommandUtils.getLapCount import dev.nikomaru.raceassist.utils.CommandUtils.getPolygon import dev.nikomaru.raceassist.utils.CommandUtils.getRaceDegree -import dev.nikomaru.raceassist.utils.CommandUtils.getReverse import dev.nikomaru.raceassist.utils.CommandUtils.judgeLap import dev.nikomaru.raceassist.utils.CommandUtils.stop import dev.nikomaru.raceassist.utils.Lang @@ -39,6 +37,7 @@ import org.bukkit.Bukkit import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.scoreboard.* +import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.awt.Polygon import kotlin.math.hypot import kotlin.math.roundToInt @@ -48,7 +47,7 @@ class RaceDebugCommand { @CommandPermission("RaceAssist.commands.race.debug") @CommandMethod("debug ") - fun debug(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + suspend fun debug(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return @@ -56,7 +55,7 @@ class RaceDebugCommand { val locale = sender.locale() plugin.launch { if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - if (!getCircuitExist(raceId, true) || !getCircuitExist(raceId, false)) { + if (!getCircuitExist(raceId)) { sender.sendMessage(Lang.getComponent("no-exist-race", locale)) return@launch } @@ -67,14 +66,14 @@ class RaceDebugCommand { sender.sendMessage(Lang.getComponent("no-exist-race", locale)) return@launch } - val reverse = getReverse(raceId) ?: false - val lap: Int = getLapCount(raceId) + val reverse = PlaceSettingData.getReverse(raceId) + val lap: Int = PlaceSettingData.getLap(raceId) val threshold = 40 val centralXPoint: Int = getCentralPoint(raceId, true) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) val centralYPoint: Int = getCentralPoint(raceId, false) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) - val goalDegree: Int = getGoalDegree(raceId) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-goal-degree", locale)) + val goalDegree: Int = PlaceSettingData.getGoalDegree(raceId) var beforeDegree = 0 var currentLap = 0 var counter = 0 @@ -150,7 +149,7 @@ class RaceDebugCommand { sender.scoreboard.clearSlot(DisplaySlot.SIDEBAR) - } + }.join() } private fun calcurateLength(insidePolygon: Polygon): Double { @@ -167,4 +166,8 @@ class RaceDebugCommand { return total } + private suspend fun getCircuitExist(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { + (PlaceSettingData.getInsidePolygon(raceId).npoints > 0 && PlaceSettingData.getInsidePolygon(raceId).npoints > 0) + } + } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStartCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStartCommand.kt index 5719d63..2c2b38a 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStartCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStartCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -20,29 +21,23 @@ import cloud.commandframework.annotations.* import com.github.shynixn.mccoroutine.bukkit.launch import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.bet.commands.BetReturnCommand +import dev.nikomaru.raceassist.data.files.PlaceSettingData +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.dispatch.discord.DiscordWebhook import dev.nikomaru.raceassist.files.Config import dev.nikomaru.raceassist.utils.* -import dev.nikomaru.raceassist.utils.CommandUtils.decideRanking import dev.nikomaru.raceassist.utils.CommandUtils.displayLap -import dev.nikomaru.raceassist.utils.CommandUtils.getAllJockeys -import dev.nikomaru.raceassist.utils.CommandUtils.getCentralPoint -import dev.nikomaru.raceassist.utils.CommandUtils.getCircuitExist -import dev.nikomaru.raceassist.utils.CommandUtils.getGoalDegree -import dev.nikomaru.raceassist.utils.CommandUtils.getLapCount -import dev.nikomaru.raceassist.utils.CommandUtils.getOwner -import dev.nikomaru.raceassist.utils.CommandUtils.getPolygon import dev.nikomaru.raceassist.utils.CommandUtils.getRaceDegree -import dev.nikomaru.raceassist.utils.CommandUtils.getReverse import dev.nikomaru.raceassist.utils.CommandUtils.judgeLap import dev.nikomaru.raceassist.utils.CommandUtils.stop +import dev.nikomaru.raceassist.utils.Lang.mm import dev.nikomaru.raceassist.utils.coroutines.minecraft import kotlinx.coroutines.* import net.kyori.adventure.text.Component.text -import net.kyori.adventure.text.minimessage.MiniMessage import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer import net.kyori.adventure.title.Title import org.bukkit.Bukkit +import org.bukkit.OfflinePlayer import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.scoreboard.* @@ -57,188 +52,157 @@ class RaceStartCommand { @CommandPermission("RaceAssist.commands.race.start") @CommandMethod("start ") - fun start(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { - plugin.launch { - val locale = if (sender is Player) sender.locale() else Locale.getDefault() + suspend fun start(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (!getCircuitExist(raceId, true) || !getCircuitExist(raceId, false)) { - sender.sendMessage(Lang.getComponent("no-exist-race", locale)) - return@launch - } - val jockeys: ArrayList = ArrayList() - getAllJockeys(raceId).forEach { jockey -> - if (jockey.isOnline) { - jockeys.add(jockey as Player) - sender.sendMessage(Lang.getComponent("player-join", locale, jockey.name)) - } else { - sender.sendMessage(Lang.getComponent("player-is-offline", locale, jockey.name)) - } - } - if (jockeys.size < 2) { - sender.sendMessage(Lang.getComponent("over-two-users-need", locale)) - return@launch + if (PlaceSettingData.getInsidePolygon(raceId).npoints == 0 || PlaceSettingData.getOutsidePolygon(raceId).npoints == 0) { + sender.sendMessage(Lang.getComponent("no-exist-race", locale)) + return + } + val jockeys: ArrayList = ArrayList() + RaceSettingData.getJockeys(raceId).forEach { + if (it.isOnline) { + jockeys.add(it as Player) + sender.sendMessage(Lang.getComponent("player-join", locale, it.name)) + } else { + sender.sendMessage(Lang.getComponent("player-is-offline", locale, it.name)) } + } + if (jockeys.size < 2) { + sender.sendMessage(Lang.getComponent("over-two-users-need", locale)) + return + } - val centralXPoint: Int = - getCentralPoint(raceId, true) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) - val centralYPoint: Int = - getCentralPoint(raceId, false) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) - val goalDegree: Int = getGoalDegree(raceId) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-goal-degree", locale)) + val centralXPoint: Int = + PlaceSettingData.getCentralXPoint(raceId) ?: return sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) + val centralYPoint: Int = + PlaceSettingData.getCentralYPoint(raceId) ?: return sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) + val goalDegree: Int = PlaceSettingData.getGoalDegree(raceId) - if (goalDegree % 90 != 0) { - sender.sendMessage(Lang.getComponent("goal-degree-must-multiple-90", locale)) - return@launch - } + val jockeyCount = jockeys.size + val finishJockey: ArrayList = ArrayList() + val totalDegree: HashMap = HashMap() + val beforeDegree: HashMap = HashMap() + val currentLap: HashMap = HashMap() + val threshold = Config.config.threshold + val raceAudience: TreeSet = TreeSet() + var suspend = false - val jockeyCount = jockeys.size - val finishJockey: ArrayList = ArrayList() - val totalDegree: HashMap = HashMap() - val beforeDegree: HashMap = HashMap() - val currentLap: HashMap = HashMap() - val threshold = Config.threshold!! - val raceAudience: TreeSet = TreeSet() - var suspend = false + val passBorders: HashMap = HashMap() + val time: HashMap = HashMap() - val passBorders: HashMap = HashMap() - val time: HashMap = HashMap() + val insidePolygon = PlaceSettingData.getInsidePolygon(raceId) - var insidePolygon = Polygon() - val setInsidePolygon = async(Dispatchers.IO) { - insidePolygon = getPolygon(raceId, true) + //観客(スコアボードを表示する人)の設定 - } + val audiences = RaceAudience() - //観客(スコアボードを表示する人)の設定 - val audiences = RaceAudience() + CommandUtils.audience[raceId]?.forEach { + audiences.add(Bukkit.getOfflinePlayer(it)) + } + jockeys.forEach { + audiences.add(it) + } + if (sender is Player) { + audiences.add(sender) + } + audiences.getUUID().forEach { + raceAudience.add(it) + } + //5.4.3...1 のカウント + var timer1 = 0 + while (timer1 <= 4) { + audiences.showTitle(Title.title(text("${5 - timer1}"), text(""))) + delay(1000) + timer1++ + } - CommandUtils.audience[raceId]?.forEach { - audiences.add(Bukkit.getOfflinePlayer(it)) - } - jockeys.forEach { - audiences.add(it) - } - if (sender is Player) { - audiences.add(sender) - } - audiences.getUUID().forEach { - raceAudience.add(it) - } + val lap = PlaceSettingData.getLap(raceId) - //5.4.3...1 のカウント - var timer1 = 0 - while (timer1 <= 4) { - audiences.showTitle(Title.title(text("${5 - timer1}"), text(""))) - delay(1000) - timer1++ - } + val outsidePolygon = PlaceSettingData.getOutsidePolygon(raceId) - val lap = withContext(Dispatchers.IO) { - getLapCount(raceId) - } + val reverse = PlaceSettingData.getReverse(raceId) - val outsidePolygon = withContext(Dispatchers.IO) { - getPolygon(raceId, false) - } + val innerCircumference = getInnerCircumference(insidePolygon) - val reverse = withContext(Dispatchers.IO) { - getReverse(raceId) ?: false - } + jockeys.forEach { + beforeDegree[it.uniqueId] = getRaceDegree(if (!reverse) (it.location.blockX - centralXPoint).toDouble() + else (-1 * (it.location.blockX - centralXPoint)).toDouble(), (it.location.blockZ - centralYPoint).toDouble()) + currentLap[it.uniqueId] = 0 + passBorders[it.uniqueId] = 0 + } - val innerCircumference = withContext(Dispatchers.Default) { - //内周の距離のカウント - var total = 0.0 - val insideX = insidePolygon.xpoints - val insideY = insidePolygon.ypoints - setInsidePolygon.await() - for (i in 0 until insidePolygon.npoints) { - total += if (i <= insidePolygon.npoints - 2) { - hypot((insideX[i] - insideX[i + 1]).toDouble(), (insideY[i] - insideY[i + 1]).toDouble()) - } else { - hypot((insideX[i] - insideX[0]).toDouble(), (insideY[i] - insideY[0]).toDouble()) - } - } - total + val beforeTime = System.currentTimeMillis() + audiences.showTitleI18n("race-start") + + val randomJockey = jockeys.random() + val startDegree = getRaceDegree((randomJockey.location.blockZ - centralYPoint).toDouble(), if (reverse) { + (-1 * (randomJockey.location.blockX - centralXPoint)).toDouble() + } else { + (randomJockey.location.blockX - centralXPoint).toDouble() + }) + + //レースの処理 + while (true) { + //正常時の終了 + if (jockeys.size < 1) { + BetReturnCommand.canReturn[raceId] = true + BetReturnCommand.payRefund(Bukkit.getOfflinePlayer(finishJockey[0]), raceId, sender, locale) + break } - - jockeys.forEach { - beforeDegree[it.uniqueId] = getRaceDegree(if (!reverse) (it.location.blockX - centralXPoint).toDouble() - else (-1 * (it.location.blockX - centralXPoint)).toDouble(), (it.location.blockZ - centralYPoint).toDouble()) - currentLap[it.uniqueId] = 0 - passBorders[it.uniqueId] = 0 + //stopコマンドによる終了 + if (stop[raceId] == true) { + suspend = true + audiences.sendMessageI18n("suspended-race") + break } - - val beforeTime = System.currentTimeMillis() - audiences.showTitleI18n("race-start") - - val randomJockey = jockeys.random() - val startDegree = getRaceDegree((randomJockey.location.blockZ - centralYPoint).toDouble(), if (reverse) { - (-1 * (randomJockey.location.blockX - centralXPoint)).toDouble() - } else { - (randomJockey.location.blockX - centralXPoint).toDouble() - }) - - //レースの処理 - while (true) { - //正常時の終了 - if (jockeys.size < 1) { - BetReturnCommand.canReturn[raceId] = true - BetReturnCommand.payRefund(Bukkit.getOfflinePlayer(finishJockey[0]), raceId, sender, locale) - break + val iterator = jockeys.iterator() + //各騎手の位置の取得 + while (iterator.hasNext()) { + val player: Player = iterator.next() + if (!player.isOnline) { + iterator.remove() + continue } - //stopコマンドによる終了 - if (stop[raceId] == true) { - suspend = true - audiences.sendMessageI18n("suspended-race") - break - } - val iterator = jockeys.iterator() - //各騎手の位置の取得 - while (iterator.hasNext()) { - val player: Player = iterator.next() - if (!player.isOnline) { - iterator.remove() - continue - } - val nowX: Int = player.location.blockX - val nowY = player.location.blockZ - val relativeNowX = if (!reverse) nowX - centralXPoint else -1 * (nowX - centralXPoint) - val relativeNowY = nowY - centralYPoint - val currentDegree = getRaceDegree(relativeNowY.toDouble(), relativeNowX.toDouble()) - val uuid = player.uniqueId - - val beforeLap = currentLap[uuid] - //ラップの計算 - val calculateLap = async(Dispatchers.Default) { - currentLap[uuid] = currentLap[uuid]!! + judgeLap(goalDegree, beforeDegree[uuid], currentDegree, threshold) - passBorders[uuid] = passBorders[uuid]!! + judgeLap(0, beforeDegree[uuid], currentDegree, threshold) - displayLap(currentLap[uuid], beforeLap, player, lap) - beforeDegree[uuid] = currentDegree - totalDegree[uuid] = currentDegree + (passBorders[uuid]!! * 360) - } + val nowX: Int = player.location.blockX + val nowY = player.location.blockZ + val relativeNowX = if (!reverse) nowX - centralXPoint else -1 * (nowX - centralXPoint) + val relativeNowY = nowY - centralYPoint + val currentDegree = getRaceDegree(relativeNowY.toDouble(), relativeNowX.toDouble()) + val uuid = player.uniqueId + + val beforeLap = currentLap[uuid] + //ラップの計算 + withContext(Dispatchers.Default) { + currentLap[uuid] = currentLap[uuid]!! + judgeLap(goalDegree, beforeDegree[uuid], currentDegree, threshold) + passBorders[uuid] = passBorders[uuid]!! + judgeLap(0, beforeDegree[uuid], currentDegree, threshold) + displayLap(currentLap[uuid], beforeLap, player, lap) + beforeDegree[uuid] = currentDegree + totalDegree[uuid] = currentDegree + (passBorders[uuid]!! * 360) + } - //コース内にいるか判断 - if (insidePolygon.contains(nowX, nowY) || !outsidePolygon.contains(nowX, nowY)) { - player.sendActionBar(Lang.getComponent("outside-the-racetrack", player.locale())) - } + //コース内にいるか判断 + if (insidePolygon.contains(nowX, nowY) || !outsidePolygon.contains(nowX, nowY)) { + player.sendActionBar(Lang.getComponent("outside-the-racetrack", player.locale())) + } - calculateLap.await() - - //ゴールした時の処理 - if (currentLap[uuid]!! >= lap) { - iterator.remove() - finishJockey.add(uuid) - totalDegree.remove(uuid) - currentLap.remove(uuid) - player.showTitle(Title.title(Lang.getComponent("player-ranking", player.locale(), jockeyCount - jockeys.size, jockeyCount), - text(""))) - time[uuid] = ((System.currentTimeMillis() - beforeTime) / 1000).toInt() - continue - } + //ゴールした時の処理 + if (currentLap[uuid]!! >= lap) { + iterator.remove() + finishJockey.add(uuid) + totalDegree.remove(uuid) + currentLap.remove(uuid) + player.showTitle(Title.title(Lang.getComponent("player-ranking", player.locale(), jockeyCount - jockeys.size, jockeyCount), + text(""))) + time[uuid] = ((System.currentTimeMillis() - beforeTime) / 1000).toInt() + continue } - //順位の表示 + } + //順位の表示 + plugin.launch { val displayRanking = async(minecraft) { displayScoreboard(finishJockey.plus(decideRanking(totalDegree)), @@ -247,32 +211,49 @@ class RaceStartCommand { innerCircumference.roundToInt(), startDegree, goalDegree, - lap) + lap, + raceId) } - delay(200) + delay(Config.config.delay) displayRanking.await() - } - //終了時の処理 - audiences.showTitleI18n("finish-race") - delay(2000) + }.join() + } + //終了時の処理 + audiences.showTitleI18n("finish-race") + delay(2000) - for (i in 0 until finishJockey.size) { - audiences.sendMessageI18n("to-notice-ranking-message", i + 1, Bukkit.getPlayer(finishJockey[i])?.name!!) - } + for (i in 0 until finishJockey.size) { + audiences.sendMessageI18n("to-notice-ranking-message", i + 1, Bukkit.getPlayer(finishJockey[i])?.name!!) + } - withContext(Dispatchers.IO) { - if (Config.discordWebHook != null) { - sendWebHook(finishJockey, time, getOwner(raceId)!!, raceId, suspend) - } - } - Bukkit.getOnlinePlayers().forEach { - it.scoreboard.clearSlot(DisplaySlot.SIDEBAR) + withContext(Dispatchers.IO) { + + if (Config.config.discordWebHook.result.isNotEmpty()) { + sendWebHook(finishJockey, time, RaceSettingData.getOwner(raceId), raceId, suspend) } + } + Bukkit.getOnlinePlayers().forEach { + it.scoreboard.clearSlot(DisplaySlot.SIDEBAR) + } + + } + private suspend fun getInnerCircumference(insidePolygon: Polygon) = withContext(Dispatchers.Default) { + //内周の距離のカウント + var total = 0.0 + val insideX = insidePolygon.xpoints + val insideY = insidePolygon.ypoints + for (i in 0 until insidePolygon.npoints) { + total += if (i <= insidePolygon.npoints - 2) { + hypot((insideX[i] - insideX[i + 1]).toDouble(), (insideY[i] - insideY[i + 1]).toDouble()) + } else { + hypot((insideX[i] - insideX[0]).toDouble(), (insideY[i] - insideY[0]).toDouble()) + } } + total } - private fun sendWebHook(finishJockey: ArrayList, time: HashMap, starter: UUID, raceId: String, suspend: Boolean) { + private fun sendWebHook(finishJockey: ArrayList, time: HashMap, starter: OfflinePlayer, raceId: String, suspend: Boolean) { val json = JSONObject() json["username"] = "RaceAssist" json["avatar_url"] = "https://3.bp.blogspot.com/-Y3AVYVjLcPs/UYiNxIliDxI/AAAAAAAARSg/nZLIqBRUta8/s800/animal_uma.png" @@ -281,7 +262,7 @@ class RaceStartCommand { val author = JSONObject() val embedsObject = JSONObject() embedsObject["title"] = if (suspend) "RaceAssist_suspend" else "RaceAssist" - author["name"] = Lang.getText("discord-webhook-name", Locale.getDefault(), Bukkit.getOfflinePlayer(starter).name, raceId) + author["name"] = Lang.getText("discord-webhook-name", Locale.getDefault(), starter.name, raceId) author["icon_url"] = "https://crafthead.net/avatar/$starter" embedsObject["author"] = author for (i in 0 until finishJockey.size) { @@ -298,17 +279,18 @@ class RaceStartCommand { embeds.add(embedsObject) json["embeds"] = embeds - val discordWebHook = DiscordWebhook() - discordWebHook.sendWebHook(json.toString()) + + DiscordWebhook.sendWebHook(json.toString()) } - private fun displayScoreboard(nowRankings: List, + private suspend fun displayScoreboard(nowRankings: List, currentDegree: HashMap, raceAudience: TreeSet, innerCircumference: Int, startDegree: Int, goalDegree: Int, - lap: Int) { + lap: Int, + raceId: String) { raceAudience.forEach { @@ -323,7 +305,7 @@ class RaceStartCommand { for (i in nowRankings.indices) { - val playerName = Bukkit.getPlayer(nowRankings[i])?.name + val playerName = RaceSettingData.getReplacement(raceId)[nowRankings[i]] ?: Bukkit.getPlayer(nowRankings[i])?.name val goalDistance = (((lap - 1).toDouble() + if (goalDegree > startDegree) { ((goalDegree.toDouble() - startDegree.toDouble()) / 360.0) } else { @@ -338,7 +320,7 @@ class RaceStartCommand { ((currentDegree[Bukkit.getPlayer(nowRankings[i])!!.uniqueId]!!.toDouble() - startDegree.toDouble()) / 360.0 * innerCircumference.toDouble()).toInt() Lang.getComponent("scoreboard-now-ranking-and-name", player.locale(), i + 1, playerName) - .append(MiniMessage.miniMessage().deserialize("${currentDistance}m/${goalDistance}m ")) + .append(mm.deserialize("${currentDistance}m/${goalDistance}m ")) } val displayDegree = objective.getScore(LegacyComponentSerializer.legacySection().serialize(component)) @@ -349,4 +331,14 @@ class RaceStartCommand { } } + private fun decideRanking(totalDegree: HashMap): ArrayList { + val ranking = ArrayList() + val sorted = totalDegree.toList().sortedBy { (_, value) -> value } + sorted.forEach { + ranking.add(it.first) + } + ranking.reverse() + return ranking + } } + diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStopCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStopCommand.kt index d996414..60f4f77 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStopCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/race/RaceStopCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,8 +18,6 @@ package dev.nikomaru.raceassist.race.commands.race import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import dev.nikomaru.raceassist.utils.CommandUtils import dev.nikomaru.raceassist.utils.CommandUtils.stop import kotlinx.coroutines.delay @@ -28,12 +27,11 @@ import org.bukkit.command.CommandSender class RaceStopCommand { @CommandPermission("RaceAssist.commands.race.stop") @CommandMethod("stop ") - fun stop(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - stop[raceId] = true - delay(1000) - stop[raceId] = false - } + suspend fun stop(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + if (CommandUtils.returnRaceSetting(raceId, sender)) return + stop[raceId] = true + delay(1000) + stop[raceId] = false + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCopyCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCopyCommand.kt index fab05fe..9cc67ca 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCopyCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCopyCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,76 +18,29 @@ package dev.nikomaru.raceassist.race.commands.setting import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.* -import dev.nikomaru.raceassist.utils.* -import dev.nikomaru.raceassist.utils.CommandUtils.getOwner -import kotlinx.coroutines.Dispatchers +import dev.nikomaru.raceassist.data.files.RaceSettingData +import dev.nikomaru.raceassist.utils.Lang import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.insert -import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction @CommandMethod("ra|RaceAssist setting") class SettingCopyCommand { @CommandPermission("RaceAssist.commands.setting.copy") @CommandMethod("copy ") - fun copy(sender: CommandSender, - @Argument(value = "raceId_1", suggestions = "raceId") raceId_1: String, - @Argument(value = "raceId_2") raceId_2: String) { + suspend fun copy(sender: CommandSender, + @Regex(value = "[a-zA-Z]+-\\d+$") @Argument(value = "raceId_1", suggestions = "raceId") raceId_1: String, + @Regex(value = "[a-zA-Z]+-\\d+$") @Argument(value = "raceId_2") raceId_2: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - val locale = sender.locale() - if (getOwner(raceId_2) != null) { - sender.sendMessage(Lang.getComponent("already-used-the-name-race", locale)) - return@launch - } - if (!CommandUtils.getCircuitExist(raceId_1, true) || !CommandUtils.getCircuitExist(raceId_1, false)) { - sender.sendMessage(Lang.getComponent("no-exist-race", locale)) - return@launch - } - CommandUtils.getCentralPoint(raceId_1, true) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) - CommandUtils.getCentralPoint(raceId_1, false) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-central-point", locale)) - CommandUtils.getGoalDegree(raceId_1) ?: return@launch sender.sendMessage(Lang.getComponent("no-exist-goal-degree", locale)) - - newSuspendedTransaction(Dispatchers.IO) { - BetSetting.select { BetSetting.raceId eq raceId_1 }.forEach { before -> - BetSetting.insert { after -> - after[raceId] = raceId_2 - after[creator] = sender.uniqueId.toString() - after[canBet] = false - after[returnPercent] = before[returnPercent] - after[spreadsheetId] = null - } - } - CircuitPoint.select { CircuitPoint.raceId eq raceId_1 }.forEach { before -> - CircuitPoint.insert { after -> - after[raceId] = raceId_2 - after[inside] = before[inside] - after[XPoint] = before[XPoint] - after[YPoint] = before[YPoint] - } - } - RaceList.select { RaceList.raceId eq raceId_1 }.forEach { before -> - RaceList.insert { after -> - after[raceId] = raceId_2 - after[creator] = sender.uniqueId.toString() - after[reverse] = before[reverse] - after[lap] = before[lap] - after[centralXPoint] = before[centralXPoint] - after[centralYPoint] = before[centralYPoint] - after[goalDegree] = before[goalDegree] - } - - } - } - RaceStaffUtils.addStaff(raceId_2, sender.uniqueId) + val locale = sender.locale() + if (RaceSettingData.existsRace(raceId_2)) { + sender.sendMessage(Lang.getComponent("already-used-the-name-race", locale)) + return } + RaceSettingData.copyRace(raceId_1, raceId_2, sender) + } } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCreateCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCreateCommand.kt index 8e10b79..2913d87 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCreateCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingCreateCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,53 +18,26 @@ package dev.nikomaru.raceassist.race.commands.setting import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.BetSetting -import dev.nikomaru.raceassist.database.RaceList -import dev.nikomaru.raceassist.utils.CommandUtils.getOwner +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.Lang -import dev.nikomaru.raceassist.utils.RaceStaffUtils -import kotlinx.coroutines.Dispatchers import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.insert -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction @CommandMethod("ra|RaceAssist setting") class SettingCreateCommand { @CommandPermission("RaceAssist.commands.setting.create") @CommandMethod("create ") - fun create(sender: CommandSender, @Argument(value = "raceId") raceId: String) { + suspend fun create(sender: CommandSender, @Argument(value = "raceId") @Regex(value = "[a-zA-Z]+-\\d+$") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (getOwner(raceId) != null) { - sender.sendMessage(Lang.getComponent("already-used-the-name-race", sender.locale())) - return@launch - } - newSuspendedTransaction(Dispatchers.IO) { - RaceList.insert { - it[this.raceId] = raceId - it[this.creator] = sender.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.uniqueId.toString() - it[this.spreadsheetId] = null - } - } - RaceStaffUtils.addStaff(raceId, sender.uniqueId) - sender.sendMessage(Lang.getComponent("to-create-race", sender.locale())) + if (RaceSettingData.existsRace(raceId)) { + sender.sendMessage(Lang.getComponent("already-used-the-name-race", sender.locale())) + return } + RaceSettingData.createRace(raceId, sender) + sender.sendMessage(Lang.getComponent("to-create-race", sender.locale())) + } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingDeleteCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingDeleteCommand.kt index 9bc8c87..f699478 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingDeleteCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingDeleteCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,52 +18,23 @@ package dev.nikomaru.raceassist.race.commands.setting import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.* +import dev.nikomaru.raceassist.data.files.RaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils -import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay import org.bukkit.command.CommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.deleteWhere -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.util.* @CommandMethod("ra|RaceAssist setting") class SettingDeleteCommand { @CommandPermission("RaceAssist.commands.setting.delete") @CommandMethod("delete ") - fun delete(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + @Confirmation + suspend fun delete(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { if (sender !is Player) { sender.sendMessage("Only the player can do this.") return } - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - if (canDelete[sender.uniqueId] == true) { - newSuspendedTransaction(Dispatchers.IO) { - RaceList.deleteWhere { RaceList.raceId eq raceId } - CircuitPoint.deleteWhere { CircuitPoint.raceId eq raceId } - PlayerList.deleteWhere { PlayerList.raceId eq raceId } - BetList.deleteWhere { BetList.raceId eq raceId } - BetSetting.deleteWhere { BetSetting.raceId eq raceId } - RaceStaff.deleteWhere { RaceStaff.raceId eq raceId } - sender.sendMessage(Lang.getComponent("to-delete-race-and-so-on", sender.locale())) - } - } else { - canDelete[sender.uniqueId] = true - sender.sendMessage(Lang.getComponent("race-remove-race-confirm-message", sender.locale(), raceId)) - delay(5000) - canDelete.remove(sender.uniqueId) - } + if (CommandUtils.returnRaceSetting(raceId, sender)) return + RaceSettingData.deleteRace(raceId) - } } - - companion object { - val canDelete = HashMap() - } - } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingReplacemcntCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingReplacemcntCommand.kt new file mode 100644 index 0000000..69a3d49 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingReplacemcntCommand.kt @@ -0,0 +1,84 @@ +/* + * Copyright © 2021-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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package dev.nikomaru.raceassist.race.commands.setting + +import cloud.commandframework.annotations.* +import dev.nikomaru.raceassist.data.files.RaceSettingData +import dev.nikomaru.raceassist.utils.CommandUtils +import dev.nikomaru.raceassist.utils.Lang +import org.bukkit.Bukkit +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player +import java.util.* + +@CommandMethod("ra|RaceAssist setting") +@CommandPermission("raceassist.commands.setting.replacement") +class SettingReplacemcntCommand { + + @CommandMethod("replacement set ") + suspend fun add(sender: CommandSender, + @Argument(value = "raceId", suggestions = "raceId") raceId: String, + @Argument(value = "playerName", suggestions = "playerName") playerName: String, + @Argument(value = "replacement") replacement: String) { + if (CommandUtils.returnRaceSetting(raceId, sender)) return + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + val player = Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + RaceSettingData.setReplacement(raceId, player.uniqueId, replacement) + sender.sendMessage(Lang.getComponent("command-result-replacement-set-success", locale, player.name, replacement)) + + } + + @CommandMethod("replacement remove ") + suspend fun remove(sender: CommandSender, + @Argument(value = "raceId", suggestions = "raceId") raceId: String, + @Argument(value = "playerName", suggestions = "playerName") playerName: String) { + if (CommandUtils.returnRaceSetting(raceId, sender)) return + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + val player = Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + RaceSettingData.removeReplacement(raceId, player.uniqueId) + sender.sendMessage(Lang.getComponent("command-result-replacement-remove-success", locale)) + } + + @CommandMethod("replacement delete ") + suspend fun delete(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + if (CommandUtils.returnRaceSetting(raceId, sender)) return + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + + RaceSettingData.deleteReplacement(raceId) + sender.sendMessage(Lang.getComponent("command-result-delete-success", locale)) + + } + + @CommandMethod("replacement list ") + suspend fun list(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + if (CommandUtils.returnRaceSetting(raceId, sender)) return + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + val replacement = RaceSettingData.getReplacement(raceId) + if (replacement.isEmpty()) { + sender.sendMessage(Lang.getComponent("command-result-replacement-list-empty", locale)) + return + } + replacement.forEach { (t, u) -> + sender.sendMessage(Lang.getComponent("command-result-replacement-list-message", locale, Bukkit.getOfflinePlayer(t).name, u)) + } + + } + +} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingStaffCommand.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingStaffCommand.kt index 8ca3fa5..4f100b0 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingStaffCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/commands/setting/SettingStaffCommand.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -17,10 +18,10 @@ package dev.nikomaru.raceassist.race.commands.setting import cloud.commandframework.annotations.* -import com.github.shynixn.mccoroutine.bukkit.launch -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.utils.* -import dev.nikomaru.raceassist.utils.CommandUtils.getOwner +import dev.nikomaru.raceassist.data.files.RaceSettingData +import dev.nikomaru.raceassist.data.files.StaffSettingData +import dev.nikomaru.raceassist.utils.CommandUtils +import dev.nikomaru.raceassist.utils.Lang import org.bukkit.Bukkit import org.bukkit.OfflinePlayer import org.bukkit.command.CommandSender @@ -32,50 +33,50 @@ import java.util.* class SettingStaffCommand { @CommandMethod("staff add ") - fun addStaff(sender: CommandSender, + suspend fun addStaff(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "playerName", suggestions = "playerName") playerName: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - val target = Bukkit.getOfflinePlayer(playerName) - if (returnCanSetPlayer(target, sender, playerName)) return@launch - val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (RaceStaffUtils.addStaff(raceId, target.uniqueId)) { - sender.sendMessage(Lang.getComponent("add-staff", locale)) - } else { - sender.sendMessage(Lang.getComponent("already-added-staff", locale)) - } + + if (CommandUtils.returnRaceSetting(raceId, sender)) return + + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + val target = Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + + if (StaffSettingData.addStaff(raceId, target)) { + sender.sendMessage(Lang.getComponent("add-staff", locale)) + } else { + sender.sendMessage(Lang.getComponent("already-added-staff", locale)) } + } @CommandMethod("staff remove ") - fun removeStaff(sender: CommandSender, + suspend fun removeStaff(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String, @Argument(value = "playerName", suggestions = "playerName") playerName: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - val target = Bukkit.getOfflinePlayer(playerName) - if (returnCanSetPlayer(target, sender, playerName)) return@launch - val locale = if (sender is Player) sender.locale() else Locale.getDefault() - if (getOwner(raceId) == target.uniqueId) { - return@launch sender.sendMessage(Lang.getComponent("cant-remove-yourself-staff", locale)) - } - if (RaceStaffUtils.removeStaff(raceId, target.uniqueId)) { - sender.sendMessage(Lang.getComponent("delete-staff", locale)) - } else { - sender.sendMessage(Lang.getComponent("not-find-staff", locale)) - } + if (CommandUtils.returnRaceSetting(raceId, sender)) return + + val locale = if (sender is Player) sender.locale() else Locale.getDefault() + val target = Bukkit.getOfflinePlayerIfCached(playerName) ?: return sender.sendMessage(Lang.getComponent("player-add-not-exist", locale)) + if (RaceSettingData.getOwner(raceId) == target) { + return sender.sendMessage(Lang.getComponent("cant-remove-yourself-staff", locale)) + } + + if (StaffSettingData.removeStaff(raceId, target)) { + sender.sendMessage(Lang.getComponent("delete-staff", locale)) + } else { + sender.sendMessage(Lang.getComponent("not-find-staff", locale)) } + } @CommandMethod("staff list ") - fun listStaff(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { - plugin.launch { - if (CommandUtils.returnRaceSetting(raceId, sender)) return@launch - RaceStaffUtils.getStaff(raceId).forEach { - sender.sendMessage(Bukkit.getOfflinePlayer(it).name.toString()) - } + suspend fun listStaff(sender: CommandSender, @Argument(value = "raceId", suggestions = "raceId") raceId: String) { + if (CommandUtils.returnRaceSetting(raceId, sender)) return + StaffSettingData.getStaffs(raceId).forEach { + sender.sendMessage(it.name.toString()) } + } private fun returnCanSetPlayer(target: OfflinePlayer, sender: CommandSender, playerName: String): Boolean { 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 fd16a04..7060df7 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetCentralPointEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetCentralPointEvent.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -16,17 +17,14 @@ package dev.nikomaru.raceassist.race.event -import dev.nikomaru.raceassist.database.RaceList +import dev.nikomaru.raceassist.data.files.PlaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.canSetCentral import dev.nikomaru.raceassist.utils.CommandUtils.centralRaceId 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.experimental.newSuspendedTransaction -import org.jetbrains.exposed.sql.update class SetCentralPointEvent : Listener { @EventHandler @@ -37,12 +35,10 @@ class SetCentralPointEvent : Listener { if (event.action != Action.LEFT_CLICK_BLOCK) { return } - newSuspendedTransaction(Dispatchers.IO) { - RaceList.update({ RaceList.raceId eq (centralRaceId[event.player.uniqueId]!!) }) { - it[centralXPoint] = event.clickedBlock?.location?.blockX ?: 0 - it[centralYPoint] = event.clickedBlock?.location?.blockZ ?: 0 - } - } + + PlaceSettingData.setCentralXPoint(centralRaceId[event.player.uniqueId]!!, event.clickedBlock?.location?.blockX ?: 0) + PlaceSettingData.setCentralYPoint(centralRaceId[event.player.uniqueId]!!, event.clickedBlock?.location?.blockZ ?: 0) + event.player.sendMessage(Lang.getComponent("to-set-this-point-central", event.player.locale())) canSetCentral.remove(event.player.uniqueId) } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetInsideCircuitEvent.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetInsideCircuitEvent.kt index 4f13848..8dae791 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetInsideCircuitEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetInsideCircuitEvent.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -23,7 +24,6 @@ import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.Action import org.bukkit.event.player.PlayerInteractEvent -import java.util.* class SetInsideCircuitEvent : Listener { @EventHandler @@ -41,7 +41,7 @@ class SetInsideCircuitEvent : Listener { event.player.sendMessage(Lang.getComponent("to-click-block", player.locale())) return } - InsideCircuit.insideCircuit(player, circuitRaceId[player.uniqueId]!!, Objects.requireNonNull(event.clickedBlock)!!.x, event.clickedBlock!!.z) + InsideCircuit.insideCircuit(player, circuitRaceId[player.uniqueId]!!, event.clickedBlock!!.x, event.clickedBlock!!.z) } } 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 88e0062..c724708 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetOutsideCircuitEvent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/event/SetOutsideCircuitEvent.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. 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 544ef52..2a9d538 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/InsideCircuit.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/InsideCircuit.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -16,15 +17,12 @@ package dev.nikomaru.raceassist.race.utils import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.CircuitPoint +import dev.nikomaru.raceassist.data.files.PlaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.canSetInsideCircuit import dev.nikomaru.raceassist.utils.CommandUtils.circuitRaceId import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.Bukkit import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.awt.Polygon object InsideCircuit { @@ -41,26 +39,7 @@ object InsideCircuit { } suspend fun finish(player: Player) { - - newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.deleteWhere { (CircuitPoint.raceId eq circuitRaceId[player.uniqueId]!!) and (CircuitPoint.inside eq true) } - } - - val x = insidePolygonMap[circuitRaceId[player.uniqueId]]!!.xpoints - val y = insidePolygonMap[circuitRaceId[player.uniqueId]]!!.ypoints - val n = insidePolygonMap[circuitRaceId[player.uniqueId]]!!.npoints - - for (i in 0 until n) { - newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.insert { - it[raceId] = circuitRaceId[player.uniqueId]!! - it[inside] = true - it[XPoint] = x[i] - it[YPoint] = y[i] - } - } - } - + PlaceSettingData.setInsidePolygon(circuitRaceId[player.uniqueId]!!, insidePolygonMap[circuitRaceId[player.uniqueId]]!!) insidePolygonMap.remove(circuitRaceId[player.uniqueId]) } } \ No newline at end of file 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 18883a6..3710713 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/utils/OutsideCircuit.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -16,15 +17,12 @@ package dev.nikomaru.raceassist.race.utils import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.CircuitPoint +import dev.nikomaru.raceassist.data.files.PlaceSettingData import dev.nikomaru.raceassist.utils.CommandUtils.canSetOutsideCircuit import dev.nikomaru.raceassist.utils.CommandUtils.circuitRaceId import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers import org.bukkit.Bukkit import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import java.awt.Polygon object OutsideCircuit { @@ -32,14 +30,9 @@ object OutsideCircuit { private var insidePolygonMap = HashMap() suspend fun outsideCircuit(player: Player, raceId: String, x: Int, z: Int) { outsidePolygonMap.putIfAbsent(raceId, Polygon()) - insidePolygonMap.putIfAbsent(raceId, Polygon()) - if (insidePolygonMap[raceId]!!.npoints == 0) { - newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.select { (CircuitPoint.raceId eq raceId) and (CircuitPoint.inside eq true) }.forEach { - insidePolygonMap[raceId]!!.addPoint(it[CircuitPoint.XPoint], it[CircuitPoint.YPoint]) - } - } - } + insidePolygonMap.putIfAbsent(raceId, PlaceSettingData.getInsidePolygon(raceId)) + + if (insidePolygonMap[raceId]!!.contains(x, z)) { player.sendActionBar(Lang.getComponent("to-click-inside-point", player.locale())) @@ -54,25 +47,7 @@ object OutsideCircuit { } suspend fun finish(player: Player) { - - newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.deleteWhere { - (CircuitPoint.raceId eq circuitRaceId[player.uniqueId]!!) and (CircuitPoint.inside eq false) - } - } - val x = outsidePolygonMap[circuitRaceId[player.uniqueId]]!!.xpoints - val y = outsidePolygonMap[circuitRaceId[player.uniqueId]]!!.ypoints - val n = outsidePolygonMap[circuitRaceId[player.uniqueId]]!!.npoints - for (i in 0 until n) { - newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.insert { - it[raceId] = circuitRaceId[player.uniqueId]!! - it[inside] = false - it[XPoint] = x[i] - it[YPoint] = y[i] - } - } - } + PlaceSettingData.setOutsidePolygon(circuitRaceId[player.uniqueId]!!, outsidePolygonMap[circuitRaceId[player.uniqueId]]!!) outsidePolygonMap.remove(circuitRaceId[player.uniqueId]) } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandSuggestions.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandSuggestions.kt index 3b7428e..eb01667 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandSuggestions.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandSuggestions.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -18,11 +19,10 @@ package dev.nikomaru.raceassist.utils import cloud.commandframework.annotations.suggestions.Suggestions import cloud.commandframework.context.CommandContext -import dev.nikomaru.raceassist.database.RaceList +import dev.nikomaru.raceassist.RaceAssist import org.bukkit.Bukkit import org.bukkit.command.CommandSender -import org.jetbrains.exposed.sql.selectAll -import org.jetbrains.exposed.sql.transactions.transaction +import java.io.File open class CommandSuggestions { @@ -38,10 +38,8 @@ open class CommandSuggestions { @Suggestions("raceId") fun suggestRaceId(sender: CommandContext, input: String?): List { val list = ArrayList() - transaction { - RaceList.selectAll().forEach { - list.add(it[RaceList.raceId]) - } + File(RaceAssist.plugin.dataFolder, "RaceData").listFiles()?.forEach { + list.add(it.nameWithoutExtension) } return list } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandUtils.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandUtils.kt index 869b451..6932e8f 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandUtils.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/CommandUtils.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -16,21 +17,15 @@ package dev.nikomaru.raceassist.utils -import dev.nikomaru.raceassist.RaceAssist.Companion.plugin -import dev.nikomaru.raceassist.database.* -import dev.nikomaru.raceassist.utils.RaceStaffUtils.existStaff -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext +import dev.nikomaru.raceassist.data.files.PlaceSettingData +import dev.nikomaru.raceassist.data.files.RaceSettingData +import dev.nikomaru.raceassist.data.files.StaffSettingData.existStaff +import kotlinx.coroutines.* import net.kyori.adventure.title.Title.title -import org.bukkit.Bukkit -import org.bukkit.OfflinePlayer import org.bukkit.command.CommandSender import org.bukkit.command.ConsoleCommandSender import org.bukkit.entity.Player -import org.jetbrains.exposed.sql.and -import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.awt.Polygon import java.util.* import kotlin.math.atan2 @@ -44,19 +39,12 @@ object CommandUtils { val centralRaceId = HashMap() var stop = HashMap() - suspend fun getRaceExist(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - RaceList.select { RaceList.raceId eq raceId }.count() > 0 - } - - suspend fun getDirection(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - RaceList.select { RaceList.raceId eq raceId }.firstOrNull()?.get(RaceList.reverse) == true - } suspend fun getInsideRaceExist(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.select { (CircuitPoint.raceId eq raceId) and (CircuitPoint.inside eq true) }.count() > 0 + PlaceSettingData.getInsidePolygon(raceId).npoints > 0 } - fun displayLap(currentLap: Int?, beforeLap: Int?, player: Player, lap: Int) { + suspend fun displayLap(currentLap: Int?, beforeLap: Int?, player: Player, lap: Int) { if (currentLap == null || beforeLap == null) { return } @@ -67,15 +55,14 @@ object CommandUtils { player.showTitle(title(Lang.getComponent("now-lap", player.locale(), currentLap, lap), Lang.getComponent("one-step-forward-lap", player.locale()))) } - Bukkit.getScheduler().runTaskLater(plugin, Runnable { - player.clearTitle() - }, 40) + delay(40) + player.clearTitle() } else if (currentLap < beforeLap) { player.showTitle(title(Lang.getComponent("now-lap", player.locale(), currentLap, lap), Lang.getComponent("one-step-backwards-lap", player.locale()))) - Bukkit.getScheduler().runTaskLater(plugin, Runnable { - player.clearTitle() - }, 40) + delay(40) + player.clearTitle() + } } @@ -84,25 +71,17 @@ object CommandUtils { return@withContext true } (player as Player) - if (!raceExist(raceId)) { - player.sendMessage(Lang.getComponent("no-exist-this-raceid-race", player.locale())) + if (!RaceSettingData.existsRace(raceId)) { + player.sendMessage(Lang.getComponent("no-exist-this-raceid-race", player.locale(), raceId)) return@withContext true } - if (!existStaff(raceId, player.uniqueId)) { + if (!existStaff(raceId, player)) { player.sendMessage(Lang.getComponent("only-race-creator-can-setting", player.locale())) return@withContext true } return@withContext false } - private suspend fun raceExist(raceId: String): Boolean { - var exist = false - newSuspendedTransaction(Dispatchers.IO) { - exist = BetSetting.select { BetSetting.raceId eq raceId }.count() > 0 - } - return exist - } - fun judgeLap(goalDegree: Int, beforeDegree: Int?, currentDegree: Int?, threshold: Int): Int { if (currentDegree == null) return 0 when (goalDegree) { @@ -151,78 +130,20 @@ object CommandUtils { } } - - - fun decideRanking(totalDegree: HashMap): ArrayList { - val ranking = ArrayList() - val sorted = totalDegree.toList().sortedBy { (_, value) -> value } - sorted.forEach { - ranking.add(it.first) - } - ranking.reverse() - return ranking - } - - suspend fun getLapCount(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - RaceList.select { RaceList.raceId eq raceId }.first()[RaceList.lap] - } - - suspend fun getAllJockeys(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - val jockeys: ArrayList = ArrayList() - PlayerList.select { PlayerList.raceId eq raceId }.forEach { - jockeys.add(Bukkit.getOfflinePlayer(UUID.fromString(it[PlayerList.playerUUID]))) - } - jockeys - } - - suspend fun getGoalDegree(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - RaceList.select { RaceList.raceId eq raceId }.firstOrNull()?.get(RaceList.goalDegree) - } - - suspend fun getCircuitExist(raceId: String, inside: Boolean) = newSuspendedTransaction(Dispatchers.IO) { - CircuitPoint.select { (CircuitPoint.raceId eq raceId) and (CircuitPoint.inside eq inside) }.count() > 0 - } - suspend fun getPolygon(raceId: String, inside: Boolean) = newSuspendedTransaction(Dispatchers.IO) { - val polygon = Polygon() - CircuitPoint.select { (CircuitPoint.raceId eq raceId) and (CircuitPoint.inside eq inside) }.forEach { - polygon.addPoint(it[CircuitPoint.XPoint], it[CircuitPoint.YPoint]) - } - polygon - } - - suspend fun getOwner(raceId: String): UUID? { - var raceCreator: String? = null - newSuspendedTransaction(Dispatchers.IO) { - raceCreator = RaceList.select { RaceList.raceId eq raceId }.firstOrNull()?.get(RaceList.creator) + if (inside) { + PlaceSettingData.getInsidePolygon(raceId) + } else { + PlaceSettingData.getOutsidePolygon(raceId) } - raceCreator ?: return null - return UUID.fromString(raceCreator) } 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) - } + if (xPoint) { + PlaceSettingData.getCentralXPoint(raceId) + } else { + PlaceSettingData.getCentralYPoint(raceId) } - point - } - - suspend fun getReverse(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - RaceList.select { RaceList.raceId eq raceId }.firstOrNull()?.get(RaceList.reverse) - } - - suspend fun getRacePlayerAmount(raceId: String): Long = newSuspendedTransaction { - PlayerList.select { - PlayerList.raceId eq raceId - }.count() } - suspend fun getRacePlayerExist(RaceId: String, playerUUID: UUID) = newSuspendedTransaction(Dispatchers.IO) { - PlayerList.select { (PlayerList.raceId eq RaceId) and (PlayerList.playerUUID eq playerUUID.toString()) }.count() > 0 - } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt index ed796b8..bd252a5 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/Lang.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. @@ -16,53 +17,61 @@ package dev.nikomaru.raceassist.utils -import com.github.shynixn.mccoroutine.bukkit.launch import dev.nikomaru.raceassist.RaceAssist.Companion.plugin import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import net.kyori.adventure.text.Component import net.kyori.adventure.text.minimessage.MiniMessage -import java.io.File -import java.io.InputStreamReader +import java.io.* +import java.nio.file.Files import java.text.MessageFormat import java.util.* object Lang { private val langList: HashMap = HashMap() - fun load() { - plugin.launch { - withContext(Dispatchers.IO) { - val lang = listOf("ja_JP", "de_DE", "en_US", "fr_FR", "ko_KR", "zh_CN", "zh_TW") + val mm = MiniMessage.miniMessage() - lang.forEach { locale -> - val conf = Properties() - conf.load(InputStreamReader(this.javaClass.classLoader.getResourceAsStream("lang/$locale.properties")!!, "UTF-8")) - langList[locale] = conf - } - if (!plugin.dataFolder.exists()) { - plugin.dataFolder.mkdir() - } - val langDir = File(plugin.dataFolder, "lang") - if (!langDir.exists()) { - langDir.mkdir() + suspend fun load() { + withContext(Dispatchers.IO) { + val lang = listOf("ja_JP", "de_DE", "en_US", "fr_FR", "ko_KR", "zh_CN", "zh_TW") + + lang.forEach { locale -> + val conf = Properties() + conf.load(InputStreamReader(this.javaClass.classLoader.getResourceAsStream("lang/$locale.properties")!!, "UTF-8")) + langList[locale] = conf + } + + if (!plugin.dataFolder.exists()) { + plugin.dataFolder.mkdir() + } + val langDir = File(plugin.dataFolder, "lang") + if (!langDir.exists()) { + langDir.mkdir() + } + lang.forEach { locale -> + val input: InputStream = this.javaClass.classLoader.getResourceAsStream("lang/$locale.properties") ?: return@forEach + plugin.logger.info("Loading resource lang file for $locale") + val file = File(langDir, "$locale.properties") + if (!file.exists()) { + Files.copy(input, file.toPath()) } - withContext(Dispatchers.IO) { - langDir.listFiles()?.forEach { - plugin.logger.info("Loading lang file for ${it.nameWithoutExtension}") - langList[it.nameWithoutExtension] = Properties().apply { - load(InputStreamReader(it.inputStream(), "UTF-8")) - } + } + withContext(Dispatchers.IO) { + langDir.listFiles()?.forEach { + plugin.logger.info("Loading local lang file for ${it.nameWithoutExtension}") + langList[it.nameWithoutExtension] = Properties().apply { + load(InputStreamReader(it.inputStream(), "UTF-8")) } } } } + } fun getComponent(key: String, locale: Locale, vararg args: Any?): Component { val lang = langList[locale.toString()] ?: langList["ja_JP"] - return lang?.getProperty(key)?.let { MiniMessage.miniMessage().deserialize(MessageFormat.format(it, *args)) } ?: MiniMessage.miniMessage() - .deserialize(key) + return lang?.getProperty(key)?.let { mm.deserialize(MessageFormat.format(it, *args)) } ?: mm.deserialize(key) } fun getText(key: String, locale: Locale, vararg args: Any?): String { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt index 213b50f..e520b2a 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceAudience.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceStaffUtils.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceStaffUtils.kt deleted file mode 100644 index 494362d..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/RaceStaffUtils.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright © 2021-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 - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package dev.nikomaru.raceassist.utils - -import dev.nikomaru.raceassist.database.RaceStaff -import kotlinx.coroutines.Dispatchers -import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.util.* - -object RaceStaffUtils { - suspend fun addStaff(raceId: String, staff: UUID) = newSuspendedTransaction(Dispatchers.IO) { - if (existStaff(raceId, staff)) return@newSuspendedTransaction false - - RaceStaff.insert { - it[this.raceId] = raceId - it[this.uuid] = staff.toString() - } - return@newSuspendedTransaction true - } - - suspend fun existStaff(raceId: String, staff: UUID) = newSuspendedTransaction(Dispatchers.IO) { - RaceStaff.select { - RaceStaff.raceId eq raceId and (RaceStaff.uuid eq staff.toString()) - }.count() > 0 - } - - suspend fun removeStaff(raceId: String, staff: UUID) = newSuspendedTransaction(Dispatchers.IO) { - if (!existStaff(raceId, staff)) return@newSuspendedTransaction false - - RaceStaff.deleteWhere { - RaceStaff.raceId eq raceId and (RaceStaff.uuid eq staff.toString()) - } - return@newSuspendedTransaction true - } - - suspend fun getStaff(raceId: String) = newSuspendedTransaction(Dispatchers.IO) { - RaceStaff.select { - RaceStaff.raceId eq raceId - }.map { - UUID.fromString(it[RaceStaff.uuid]) - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/AsyncCoroutineDispatcher.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/AsyncCoroutineDispatcher.kt index d27ecd9..e19347e 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/AsyncCoroutineDispatcher.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/AsyncCoroutineDispatcher.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/DispatcherContainer.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/DispatcherContainer.kt index 0f6b712..3d2f8f7 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/DispatcherContainer.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/DispatcherContainer.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineDispatcher.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineDispatcher.kt index 9aea228..3f175fa 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineDispatcher.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineDispatcher.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineUtil.kt b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineUtil.kt index 2cab5d4..7c10919 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineUtil.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/utils/coroutines/MinecraftCoroutineUtil.kt @@ -1,6 +1,7 @@ /* - * Copyright © 2021-2022 Nikomaru - * This program is free software: you can redistribute it and/or modify + * Copyright © 2021-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 * (at your option) any later version. diff --git a/src/main/resources/lang/ja_JP.properties b/src/main/resources/lang/ja_JP.properties index 0651c7c..92153b5 100644 --- a/src/main/resources/lang/ja_JP.properties +++ b/src/main/resources/lang/ja_JP.properties @@ -7,10 +7,8 @@ already-used-the-name-race=その名前のレース場は既に設 bet-complete-message-owner={0}{1}円 を賭けました bet-complete-message-player=番号{0} : {1} に {2}円 bet-list-detail-message={0} {1} プレイヤー:{2} -> 騎手:{3} {4}円 -bet-remove-race-confirm-message={0}のベットを削除するには、5秒以内にもう一度同じコマンドを実行してください bet-remove-race={0}のレースを削除しました bet-revert-complete-message={0}の賭け金の返還を完了しました -bet-revert-race-confirm-message={0}のレースですべて返金する場合は、5秒以内にもう一度同じコマンドを実行してください bet-revert-return-message-owner={0}に{1}円を返しました bet-revert-return-message-player={0}から{1}で{2}にかけていた{3}円が返金されました betting-zero-money={0}円単位 : 0円かけています @@ -68,7 +66,6 @@ player-join={0}が参加しました player-not-exist={0} は存在しません player-not-jockey={0} は騎手ではありません player-ranking={0}/{1} 位です -race-remove-race-confirm-message={0}のレースを削除するには、5秒以内にもう一度同じコマンドを実行してください race-start=レースが開始しました return-confirm-message=支払う場合は5秒以内にもう一度実行してください scoreboard-context=デバッグモード @@ -110,4 +107,9 @@ to-set-degree={0}度にしました to-set-central-point=中心点を設定してください to-set-lap=ラップ数を設定しました to-set-this-point-central=中心を設定しました -to-suspend-process=処理を中断しました \ No newline at end of file +to-suspend-process=処理を中断しました +command-result-replacement-set-success=置換対象に{0}を{1}設定しました。 +command-replacement-remove-success=置換対象から{0}を削除しました。 +replacement-delete-success=置換対象を{0}に設定しました。 +command-result-replacement-list-empty=置換対象が見つかりません。 +command-result-replacement-list-message={0}