From 81458beb4134bae222bc5fcba1e0c31bee248180 Mon Sep 17 00:00:00 2001 From: Nlkomaru Date: Wed, 17 Jul 2024 23:27:12 +0900 Subject: [PATCH] :bug: Fix betting gui method(WIP) --- gradle/libs.versions.toml | 6 +- .../dev/nikomaru/raceassist/RaceAssist.kt | 2 - .../dev/nikomaru/raceassist/bet/BetUtils.kt | 7 +- .../nikomaru/raceassist/bet/GuiComponent.kt | 56 --- .../raceassist/bet/commands/BetOpenCommand.kt | 5 +- .../raceassist/bet/event/BetGuiClickEvent.kt | 335 ------------------ .../bet/gui/{BetChestGui.kt => BetGui.kt} | 73 ++-- .../raceassist/bet/gui/components/GuiItems.kt | 212 +++++++++++ .../dev/nikomaru/raceassist/files/Config.kt | 6 +- .../raceassist/race/PlainRaceJudgement.kt | 2 + 10 files changed, 265 insertions(+), 439 deletions(-) delete mode 100644 src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt rename src/main/kotlin/dev/nikomaru/raceassist/bet/gui/{BetChestGui.kt => BetGui.kt} (53%) create mode 100644 src/main/kotlin/dev/nikomaru/raceassist/bet/gui/components/GuiItems.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9e91ad8..8a80e07 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -63,11 +63,7 @@ kotlinx-serialization-hocon = { group = "org.jetbrains.kotlinx", name = "kotlinx kotlin-result = { group = "com.michael-bull.kotlin-result", name = "kotlin-result", version.ref = "kotlin-result" } gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" } koin-core = { group = "io.insert-koin", name = "koin-core", version.ref = "koin-core" } -mockBukkit = { module = "com.github.seeseemelk:MockBukkit-v1.20", version.ref = "mockBukkit" } -mockk = { module = "io.mockk:mockk", version.ref = "mockk" } -junitJupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junitJupiter" } -koinTest = { module = "io.insert-koin:koin-test", version.ref = "koinTest" } -koinTestJunit5 = { module = "io.insert-koin:koin-test-junit5", version.ref = "koinTestJunit5" } + [plugins] run-paper = { id = "xyz.jpenilla.run-paper", version = "2.3.0" } diff --git a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt index 0047265..ceea827 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/RaceAssist.kt @@ -32,7 +32,6 @@ import dev.nikomaru.raceassist.api.core.PlaceType import dev.nikomaru.raceassist.api.core.RaceAssistAPI import dev.nikomaru.raceassist.api.core.manager.* import dev.nikomaru.raceassist.bet.commands.* -import dev.nikomaru.raceassist.bet.event.BetGuiClickEvent import dev.nikomaru.raceassist.data.database.BetList import dev.nikomaru.raceassist.data.database.UserAuthData import dev.nikomaru.raceassist.data.files.RaceUtils @@ -269,7 +268,6 @@ open class RaceAssist : SuspendingJavaPlugin(), RaceAssistAPI, KoinComponent { injectServer.pluginManager.registerSuspendingEvents(SetInsideCircuitEvent(), plugin) injectServer.pluginManager.registerSuspendingEvents(SetOutsideCircuitEvent(), plugin) injectServer.pluginManager.registerSuspendingEvents(SetCentralPointEvent(), plugin) - injectServer.pluginManager.registerSuspendingEvents(BetGuiClickEvent(), plugin) injectServer.pluginManager.registerSuspendingEvents(HorseBreedEvent(), plugin) injectServer.pluginManager.registerSuspendingEvents(HorseKillEvent(), plugin) injectServer.pluginManager.registerSuspendingEvents(HorseTamedEvent(), plugin) diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt index eda4697..05a5009 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/BetUtils.kt @@ -19,7 +19,7 @@ package dev.nikomaru.raceassist.bet import dev.nikomaru.raceassist.RaceAssist import dev.nikomaru.raceassist.bet.data.TempBetData -import dev.nikomaru.raceassist.bet.gui.BetChestGui +import dev.nikomaru.raceassist.bet.gui.components.GuiItems.AllPlayers import dev.nikomaru.raceassist.data.database.BetList import dev.nikomaru.raceassist.data.database.BetListData import dev.nikomaru.raceassist.utils.Lang @@ -73,9 +73,12 @@ object BetUtils { } fun initializePlayerTempBetData(raceId: String, sender: Player) { - BetChestGui.AllPlayers[raceId]?.forEach { jockey -> + println(AllPlayers[raceId]) + AllPlayers[raceId]?.forEach { jockey -> tempBetDataList.add(TempBetData(raceId, sender, jockey, 0)) } + println(tempBetDataList) + } suspend fun listBetData(raceId: String): ArrayList { diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt index 9ade156..20dbeee 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/GuiComponent.kt @@ -18,7 +18,6 @@ package dev.nikomaru.raceassist.bet import com.github.stefvanschie.inventoryframework.gui.GuiItem -import dev.nikomaru.raceassist.RaceAssist import dev.nikomaru.raceassist.utils.Lang import net.kyori.adventure.text.Component import org.bukkit.Material @@ -32,61 +31,6 @@ object GuiComponent { return Component.text("レース賭け自販機") } - 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, - RaceAssist.api.getBetManager(raceId)!!.getBetUnit() - ) - ) - onceUp.itemMeta = onceUpMeta - return onceUp - } - - 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, - RaceAssist.api.getBetManager(raceId)!!.getBetUnit() - ) - ) - onceDown.itemMeta = onceDownMeta - return onceDown - } - - 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, - RaceAssist.api.getBetManager(raceId)!!.getBetUnit() * 10 - ) - ) - tenTimesUp.itemMeta = tenTimesUpMeta - return tenTimesUp - } - - 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, - RaceAssist.api.getBetManager(raceId)!!.getBetUnit() * 10 - ) - ) - tenTimesDown.itemMeta = tenTimesDownMeta - return tenTimesDown - } fun accept(locale: Locale): ItemStack { val accept = ItemStack(Material.GREEN_WOOL) 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 1018f31..539c7a6 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetOpenCommand.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/commands/BetOpenCommand.kt @@ -23,7 +23,7 @@ import cloud.commandframework.annotations.CommandMethod import cloud.commandframework.annotations.CommandPermission import dev.nikomaru.raceassist.RaceAssist import dev.nikomaru.raceassist.bet.BetUtils -import dev.nikomaru.raceassist.bet.gui.BetChestGui +import dev.nikomaru.raceassist.bet.gui.BetGui import dev.nikomaru.raceassist.utils.Lang import dev.nikomaru.raceassist.utils.SuggestionId import dev.nikomaru.raceassist.utils.coroutines.minecraft @@ -53,9 +53,8 @@ class BetOpenCommand { } BetUtils.removePlayerTempBetData(sender) withContext(Dispatchers.minecraft) { - sender.openInventory(BetChestGui().getGUI(sender, raceId)) + BetGui().openGui(sender, raceId) } - BetUtils.initializePlayerTempBetData(raceId, sender) } } \ 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 deleted file mode 100644 index 110fad7..0000000 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/event/BetGuiClickEvent.kt +++ /dev/null @@ -1,335 +0,0 @@ -/* - * Copyright © 2021-2024 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.event - -import dev.nikomaru.raceassist.RaceAssist -import dev.nikomaru.raceassist.bet.BetUtils -import dev.nikomaru.raceassist.bet.GuiComponent -import dev.nikomaru.raceassist.bet.gui.BetChestGui.Companion.AllPlayers -import dev.nikomaru.raceassist.utils.Lang -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import net.kyori.adventure.text.Component -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer -import org.bukkit.Material -import org.bukkit.Sound -import org.bukkit.entity.Player -import org.bukkit.event.EventHandler -import org.bukkit.event.Listener -import org.bukkit.event.inventory.InventoryClickEvent -import org.bukkit.inventory.ItemStack -import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction -import java.util.* - -class BetGuiClickEvent : Listener { - - @EventHandler - suspend fun vendingClickEvent(event: InventoryClickEvent) { - - if (event.view.title() != GuiComponent.guiComponent()) { - return - } - var limit = 0 - for (i in 0 until 8) { - if (event.inventory.getItem(18 + i) != ItemStack(Material.GRAY_STAINED_GLASS_PANE)) { - limit = i - } - } - val player = event.whoClicked as Player - event.isCancelled = true - if (clicked[player.uniqueId] == true) { - return - } - val raceId: String = - PlainTextComponentSerializer.plainText().serialize(event.inventory.getItem(8)?.itemMeta?.displayName()!!) - if (event.slot == 35) { - player.closeInventory() - } - val betManager = RaceAssist.api.getBetManager(raceId)!! - val betUnit = betManager.getBetUnit() - when (val slot = event.slot) { - - in 0..7 -> { - //10 ↑ - if (slot > limit) { - return - } - - val selectedNowBet: Int = getNowBet(raceId, player, slot) - player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 1.0f) - - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot)) { - it.betPerUnit = selectedNowBet + 10 - } - } - - val selectedAfterBet: Int = getNowBet(raceId, player, (slot)) - val item = event.inventory.getItem(slot + 18)!! - val itemMeta = item.itemMeta - itemMeta.displayName( - Lang.getComponent( - "now-betting-price", - player.locale(), - betUnit, - selectedAfterBet * betUnit - ) - ) - item.itemMeta = itemMeta - - clicked[player.uniqueId] = true - delay(50) - clicked.remove(player.uniqueId) - } - - in 9..16 -> { - // 1 ↑ - if (slot > (limit + 9)) { - return - } - val selectedNowBet: Int = getNowBet(raceId, player, (slot - 9)) - player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f) - - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 9)) { - it.betPerUnit = selectedNowBet + 1 - } - } - - val selectedAfterBet: Int = getNowBet(raceId, player, (slot - 9)) - val item = event.inventory.getItem(slot + 9)!! - val itemMeta = item.itemMeta - itemMeta.displayName( - Lang.getComponent( - "now-betting-price", - player.locale(), - betUnit, - selectedAfterBet * betUnit - ) - ) - item.itemMeta = itemMeta - - clicked[player.uniqueId] = true - //anti chattering - delay(50) - clicked.remove(player.uniqueId) - } - - in 27..34 -> { - //1 ↓ - if (slot > (limit + 27)) { - return - } - val selectedNowBet: Int = getNowBet(raceId, player, (slot - 27)) - - if (selectedNowBet <= 0) { - 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(), raceId)) - return - } - player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 0.7f) - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 27)) { - it.betPerUnit = selectedNowBet - 1 - } - } - - val selectedAfterBet: Int = getNowBet(raceId, player, (slot - 27)) - val item = event.inventory.getItem(slot - 9)!! - val itemMeta = item.itemMeta - itemMeta.displayName( - Lang.getComponent( - "now-betting-price", - player.locale(), - betUnit, - selectedAfterBet * betUnit - ) - ) - item.itemMeta = itemMeta - - clicked[player.uniqueId] = true - delay(50) - clicked.remove(player.uniqueId) - } - - in 36..43 -> { - // 10 ↓ - if (slot > (limit + 36)) { - return - } - val selectedNowBet: Int = getNowBet(raceId, player, (slot - 36)) - - - if (selectedNowBet <= 9) { - 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(), raceId)) - return - } - player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_CHIME, 1f, 0.7f) - - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 36)) { - it.betPerUnit = selectedNowBet - 10 - } - } - - val selectedAfterBet: Int = getNowBet(raceId, player, (slot - 36)) - val item = event.inventory.getItem(slot - 18)!! - val itemMeta = item.itemMeta - itemMeta.displayName( - Lang.getComponent( - "now-betting-price", - player.locale(), - betUnit, - selectedAfterBet * betUnit - ) - ) - item.itemMeta = itemMeta - - clicked[player.uniqueId] = true - delay(50) - clicked.remove(player.uniqueId) - } - - 17 -> { - //clear - player.playSound(player.location, Sound.UI_BUTTON_CLICK, 1f, 1f) - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player) { - it.betPerUnit = 0 - } - } - - for (i in 0 until limit + 1) { - val item = event.inventory.getItem(i + 18)!! - val itemMeta = item.itemMeta - itemMeta.displayName(Lang.getComponent("betting-zero-money", player.locale(), betUnit)) - item.itemMeta = itemMeta - } - } - - 35 -> { - //deny - player.closeInventory() - player.playSound(player.location, Sound.UI_BUTTON_CLICK, 0.5f, 1f) - - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player) { - iterator.remove() - } - } - - } - - 44 -> { - //accept - - if (betManager.getBalance() < (getAllBet(raceId, player) * betUnit)) { - noticeNoMoney(event, slot, player) - return - } - if (getAllBet(raceId, player) == 0) { - noticeNoBet(event, slot, player) - return - } - - player.playSound(player.location, Sound.ENTITY_PLAYER_LEVELUP, 0.5f, 1f) - player.closeInventory() - - newSuspendedTransaction(Dispatchers.Default) { - BetUtils.tempBetDataList.stream().filter { - it.raceId == raceId && it.player == player && it.betPerUnit != 0 - }.forEach { - betManager.pushBet(it.player, it.jockey, it.betPerUnit) - } - BetUtils.tempBetDataList.removeIf { it.raceId == raceId && it.player == player } - } - } - } - - val accept = event.inventory.getItem(44)!! - val acceptMeta = accept.itemMeta - val acceptLore: ArrayList = ArrayList() - acceptLore.add(Lang.getComponent("gui-need-money", player.locale(), getAllBet(raceId, player) * betUnit)) - acceptMeta.lore(acceptLore) - accept.itemMeta = acceptMeta - event.inventory.setItem(44, accept) - } - - private suspend fun noticeNoBet(event: InventoryClickEvent, slot: Int, player: Player) { - event.inventory.setItem(slot, GuiComponent.noBet(player.locale())) - player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1f, 0.7f) - delay(1000) - event.inventory.setItem(slot, GuiComponent.accept(player.locale())) - } - - private suspend fun noticeNoMoney(event: InventoryClickEvent, slot: Int, player: Player) { - event.inventory.setItem(slot, GuiComponent.noHaveMoney(player.locale())) - player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, 1f, 0.7f) - delay(1000) - event.inventory.setItem(slot, GuiComponent.accept(player.locale())) - } - - private fun getNowBet(raceId: String, player: Player, slot: Int): Int { - var bet = 0 - val iterator = BetUtils.tempBetDataList.iterator() - while (iterator.hasNext()) { - val it = iterator.next() - if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot)) { - bet = it.betPerUnit - } - } - return bet - } - - private fun getAllBet(raceId: String, player: Player): Int { - var sum = 0 - BetUtils.tempBetDataList.forEach { - if (it.raceId == raceId && it.player == player) { - sum += it.betPerUnit - } - } - return sum - } - - - private suspend fun getBetPercent(raceId: String): Int = newSuspendedTransaction(Dispatchers.IO) { - RaceAssist.api.getBetManager(raceId)!!.getReturnPercent() - } - - companion object { - //Prevention of chattering-like phenomena - private val clicked = HashMap() - } -} \ 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/BetGui.kt similarity index 53% rename from src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt rename to src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetGui.kt index 2dff85d..6677921 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetChestGui.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/BetGui.kt @@ -17,43 +17,55 @@ package dev.nikomaru.raceassist.bet.gui +import com.github.stefvanschie.inventoryframework.gui.GuiItem +import com.github.stefvanschie.inventoryframework.gui.type.ChestGui +import com.github.stefvanschie.inventoryframework.pane.StaticPane import dev.nikomaru.raceassist.RaceAssist -import dev.nikomaru.raceassist.bet.BetUtils.getOdds -import dev.nikomaru.raceassist.bet.GuiComponent +import dev.nikomaru.raceassist.bet.BetUtils +import dev.nikomaru.raceassist.bet.gui.components.GuiItems.AllPlayers +import dev.nikomaru.raceassist.bet.gui.components.GuiItems.onceDown +import dev.nikomaru.raceassist.bet.gui.components.GuiItems.onceUp +import dev.nikomaru.raceassist.bet.gui.components.GuiItems.tenTimesDown +import dev.nikomaru.raceassist.bet.gui.components.GuiItems.tenTimesUp import dev.nikomaru.raceassist.utils.Lang 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.OfflinePlayer import org.bukkit.entity.Player -import org.bukkit.inventory.Inventory import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.SkullMeta -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() +class BetGui { + private var players: ArrayList = ArrayList() + private var odds: HashMap = HashMap() + + suspend fun openGui(player: Player, raceId: String) { + val gui = ChestGui(6, "Shop") + val pane = StaticPane(0, 0, 9, 5) AllPlayers[raceId] = arrayListOf() + RaceAssist.api.getRaceManager(raceId)!!.getJockeys().forEach { players.add(it) AllPlayers[raceId]?.add(it) } + BetUtils.initializePlayerTempBetData(raceId, player) + players.forEach { - odds[it] = getOdds(raceId, it) + odds[it] = BetUtils.getOdds(raceId, it) } - for (i in 0 until 45) { - gui.setItem(i, ItemStack(Material.GRAY_STAINED_GLASS_PANE)) + for (x in 0..8) { + for (y in 0..4) { + val none = + GuiItem(ItemStack(Material.LIGHT_GRAY_STAINED_GLASS_PANE)) { event -> event.isCancelled = true } + pane.addItem(none, x, y) + } } - //TODO IFに置き換えるよてい https://github.com/stefvanschie/IF + for (i in 0 until players.size) { val item = ItemStack(Material.PLAYER_HEAD, 1) val meta: SkullMeta = item.itemMeta as SkullMeta @@ -71,28 +83,21 @@ class BetChestGui { meta.lore(lore) item.itemMeta = meta - 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(), raceId)) - gui.setItem(i + 36, GuiComponent.tenTimesDown(player.locale(), raceId)) - } - - val raceIdItem = ItemStack(Material.GRAY_STAINED_GLASS_PANE) - val raceIdMeta = raceIdItem.itemMeta - raceIdMeta.displayName(text(raceId, TextColor.fromHexString("#00ff7f"))) - raceIdItem.itemMeta = raceIdMeta + val guiItem = GuiItem(item) { event -> event.isCancelled = true } - gui.setItem(8, raceIdItem) - gui.setItem(17, GuiComponent.reset(player.locale())) - gui.setItem(35, GuiComponent.deny(player.locale())) - gui.setItem(44, GuiComponent.accept(player.locale())) + pane.addItem(tenTimesUp(player.locale(), raceId), i, 0) + pane.addItem(onceUp(player.locale(), raceId), i, 1) + pane.addItem(guiItem, i, 2) + pane.addItem(onceDown(player.locale(), raceId), i, 3) + pane.addItem(tenTimesDown(player.locale(), raceId), i, 4) - return gui + //TODO Add reset button + //TODO Add accept button + //TODO Add stop button + } - } + gui.addPane(pane) - companion object { - val AllPlayers: HashMap> = HashMap() + gui.show(player) } } \ No newline at end of file diff --git a/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/components/GuiItems.kt b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/components/GuiItems.kt new file mode 100644 index 0000000..eaf8745 --- /dev/null +++ b/src/main/kotlin/dev/nikomaru/raceassist/bet/gui/components/GuiItems.kt @@ -0,0 +1,212 @@ +/* + * Copyright © 2021-2024 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.gui.components + +import com.github.stefvanschie.inventoryframework.gui.GuiItem +import dev.nikomaru.raceassist.RaceAssist +import dev.nikomaru.raceassist.bet.BetUtils +import dev.nikomaru.raceassist.utils.Lang +import org.bukkit.Material +import org.bukkit.OfflinePlayer +import org.bukkit.Sound +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import org.bukkit.inventory.meta.ItemMeta +import java.util.* + +object GuiItems { + + private fun getNowBet(raceId: String, player: Player, slot: Int): Int { + var bet = 0 + val iterator = BetUtils.tempBetDataList.iterator() + while (iterator.hasNext()) { + val it = iterator.next() + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot)) { + bet = it.betPerUnit + } + } + return bet + } + + fun onceUp(locale: Locale, raceId: String): GuiItem { + // once up slot: 9-16 + val onceUp = ItemStack(Material.PINK_STAINED_GLASS_PANE) + val onceUpMeta: ItemMeta = onceUp.itemMeta + onceUpMeta.displayName( + Lang.getComponent( + "to-bet-one-unit", locale, RaceAssist.api.getBetManager(raceId)!!.getBetUnit() + ) + ) + onceUp.itemMeta = onceUpMeta + + val guiItem = GuiItem(onceUp) { event -> + val betManager = RaceAssist.api.getBetManager(raceId)!! + val betUnit = betManager.getBetUnit() + val player = event.whoClicked as Player + val slot = event.slot + val selectedNowBet: Int = getNowBet(raceId, player, (slot - 9)) + player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f) + val iterator = BetUtils.tempBetDataList.iterator() + while (iterator.hasNext()) { + val it = iterator.next() + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 9)) { + it.betPerUnit = selectedNowBet + 1 + } + } + + val selectedAfterBet: Int = getNowBet(raceId, player, (slot - 9)) + val item = event.inventory.getItem(slot + 9)!! + val itemMeta = item.itemMeta + itemMeta.displayName( + Lang.getComponent( + "now-betting-price", player.locale(), betUnit, selectedAfterBet * betUnit + ) + ) + item.itemMeta = itemMeta + + event.isCancelled = true + } + return guiItem + } + + fun onceDown(locale: Locale, raceId: String): GuiItem { + // once down slot: 27-34 + val onceDown = ItemStack(Material.LIGHT_BLUE_STAINED_GLASS_PANE) + val onceDownMeta: ItemMeta = onceDown.itemMeta + onceDownMeta.displayName( + Lang.getComponent( + "to-cancel-bet-one-unit", locale, RaceAssist.api.getBetManager(raceId)!!.getBetUnit() + ) + ) + onceDown.itemMeta = onceDownMeta + + val guiItem = GuiItem(onceDown) { event -> + val betManager = RaceAssist.api.getBetManager(raceId)!! + val betUnit = betManager.getBetUnit() + val player = event.whoClicked as Player + val slot = event.slot + val selectedNowBet: Int = getNowBet(raceId, player, (slot - 27)) + player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f) + val iterator = BetUtils.tempBetDataList.iterator() + while (iterator.hasNext()) { + val it = iterator.next() + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 27)) { + it.betPerUnit = if (selectedNowBet - 1 < 0) 0 else (selectedNowBet - 1) + } + } + + val selectedAfterBet: Int = getNowBet(raceId, player, (slot - 27)) + val item = event.inventory.getItem(slot - 9)!! + val itemMeta = item.itemMeta + itemMeta.displayName( + Lang.getComponent( + "now-betting-price", player.locale(), betUnit, selectedAfterBet * betUnit + ) + ) + item.itemMeta = itemMeta + + event.isCancelled = true + } + return guiItem + } + + fun tenTimesUp(locale: Locale, raceId: String): GuiItem { + //ten times up slot: 0-8 + val tenTimesUp = ItemStack(Material.RED_STAINED_GLASS_PANE) + val tenTimesUpMeta: ItemMeta = tenTimesUp.itemMeta + tenTimesUpMeta.displayName( + Lang.getComponent( + "to-bet-ten-unit", locale, RaceAssist.api.getBetManager(raceId)!!.getBetUnit() * 10 + ) + ) + tenTimesUp.itemMeta = tenTimesUpMeta + + val guiItem = GuiItem(tenTimesUp) { event -> + val betManager = RaceAssist.api.getBetManager(raceId)!! + val betUnit = betManager.getBetUnit() + val player = event.whoClicked as Player + val slot = event.slot + val selectedNowBet: Int = getNowBet(raceId, player, slot) + player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f) + val iterator = BetUtils.tempBetDataList.iterator() + while (iterator.hasNext()) { + val it = iterator.next() + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot)) { + it.betPerUnit = selectedNowBet + 10 + } + } + + val selectedAfterBet: Int = getNowBet(raceId, player, slot) + val item = event.inventory.getItem(slot + 18)!! + val itemMeta = item.itemMeta + itemMeta.displayName( + Lang.getComponent( + "now-betting-price", player.locale(), betUnit, selectedAfterBet * betUnit + ) + ) + item.itemMeta = itemMeta + + event.isCancelled = true + } + return guiItem + } + + fun tenTimesDown(locale: Locale, raceId: String): GuiItem { + // ten times down slot: 36-44 + val tenTimesDown = ItemStack(Material.BLUE_STAINED_GLASS_PANE) + val tenTimesDownMeta: ItemMeta = tenTimesDown.itemMeta + tenTimesDownMeta.displayName( + Lang.getComponent( + "to-cancel-bet-ten-unit", locale, RaceAssist.api.getBetManager(raceId)!!.getBetUnit() * 10 + ) + ) + tenTimesDown.itemMeta = tenTimesDownMeta + + val guiItem = GuiItem(tenTimesDown) { event -> + val betManager = RaceAssist.api.getBetManager(raceId)!! + val betUnit = betManager.getBetUnit() + val player = event.whoClicked as Player + val slot = event.slot + val selectedNowBet: Int = getNowBet(raceId, player, (slot - 36)) + + player.playSound(player.location, Sound.BLOCK_NOTE_BLOCK_BELL, 1f, 1.0f) + val iterator = BetUtils.tempBetDataList.iterator() + while (iterator.hasNext()) { + val it = iterator.next() + if (it.raceId == raceId && it.player == player && it.jockey == AllPlayers[raceId]?.get(slot - 36)) { + it.betPerUnit = if (selectedNowBet - 10 < 0) 0 else (selectedNowBet - 10) + } + } + + val selectedAfterBet: Int = getNowBet(raceId, player, (slot - 36)) + val item = event.inventory.getItem(slot - 18)!! + val itemMeta = item.itemMeta + itemMeta.displayName( + Lang.getComponent( + "now-betting-price", player.locale(), betUnit, selectedAfterBet * betUnit + ) + ) + item.itemMeta = itemMeta + + event.isCancelled = true + } + return guiItem + } + + val AllPlayers: HashMap> = HashMap() +} \ 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 3190a0b..1992a8c 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/files/Config.kt @@ -42,8 +42,10 @@ object Config : KoinComponent { @OptIn(ExperimentalSerializationApi::class) fun reload() { load() - WebAPI.stopServer() - plugin.settingWebAPI() + if (config.webAPI != null) { + WebAPI.stopServer() + plugin.settingWebAPI() + } } @ExperimentalSerializationApi diff --git a/src/main/kotlin/dev/nikomaru/raceassist/race/PlainRaceJudgement.kt b/src/main/kotlin/dev/nikomaru/raceassist/race/PlainRaceJudgement.kt index a8ba1e5..01cf575 100644 --- a/src/main/kotlin/dev/nikomaru/raceassist/race/PlainRaceJudgement.kt +++ b/src/main/kotlin/dev/nikomaru/raceassist/race/PlainRaceJudgement.kt @@ -40,6 +40,7 @@ import dev.nikomaru.raceassist.utils.coroutines.async import dev.nikomaru.raceassist.utils.coroutines.minecraft import io.ktor.client.request.* import io.ktor.http.* +import io.papermc.paper.scoreboard.numbers.NumberFormat import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.delay @@ -365,6 +366,7 @@ class PlainRaceJudgement(override val raceId: String, override val executor: Com Lang.getComponent("scoreboard-now-ranking", player.locale()) ) objective.displaySlot = DisplaySlot.SIDEBAR + objective.numberFormat(NumberFormat.blank()) val goalDistance = getGoalDistance(lap, goalDegree, startDegree, innerCircumference.toDouble())