Skip to content

Commit 7f535e6

Browse files
committed
Refactor PayCommand dialogs and simplify logic
- Introduced reusable dialog functions for success, error, and insufficient funds scenarios. - Added a confirmation dialog for payment processing. - Removed concurrency lock (`payLock`) to streamline command handling. - Simplified transaction result handling with updated dialog interactions.
1 parent 4abe0b3 commit 7f535e6

File tree

3 files changed

+157
-67
lines changed

3 files changed

+157
-67
lines changed

surf-transaction-paper/src/main/kotlin/dev/slne/surf/transaction/paper/commands/pay/PayCommand.kt

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ import dev.slne.surf.transaction.core.component.Components
1818
import dev.slne.surf.transaction.core.redis.RedisService
1919
import dev.slne.surf.transaction.paper.commands.CommandPermission
2020
import dev.slne.surf.transaction.paper.redis.events.pay.PaymentReceivedEvent
21+
import net.kyori.adventure.text.Component
2122
import org.bukkit.entity.Player
2223
import java.util.*
23-
import java.util.concurrent.ConcurrentHashMap
2424

2525
private val log = logger()
26-
private val payLock = ConcurrentHashMap.newKeySet<UUID>()
2726

2827
fun payCommand() = commandTree("pay") {
2928
withPermission(CommandPermission.PAY)
@@ -51,100 +50,77 @@ private suspend fun pay(
5150
throw CommandAPI.failWithString("Du kannst dir kein Geld selbst überweisen!")
5251
}
5352

54-
if (!payLock.add(sender.uniqueId)) {
55-
throw CommandAPI.failWithString("Du führst bereits eine Überweisung durch. Bitte warte einen Moment...")
56-
}
57-
5853
sender.sendText {
5954
appendPrefix()
6055
info("Überweisung wird ausgeführt...")
6156
}
6257

63-
try {
64-
val currency = Currency.default()
65-
val result = sender.transactionUser().transfer(
66-
amount = amount.toBigDecimal(),
67-
currency = currency,
68-
receiver = TransactionUser.byUuid(receiverUuid).getDefaultAccount()
58+
val currency = Currency.default()
59+
60+
val receiverName = Components.usernameOrUuid(receiverUuid)
61+
val formattedAmount = currency.format(amount)
62+
val confirmed = payConfirmationDialog(sender, receiverName, formattedAmount)
63+
if (!confirmed) return
64+
65+
val result = sender.transactionUser().transfer(
66+
amount = amount.toBigDecimal(),
67+
currency = currency,
68+
receiver = TransactionUser.byUuid(receiverUuid).getDefaultAccount()
69+
)
70+
71+
when (result) {
72+
is TransactionResult.Success, is TransactionResult.TransferSuccess -> handleSuccess(
73+
sender,
74+
receiverUuid,
75+
receiverName,
76+
amount,
77+
currency,
78+
formattedAmount
6979
)
7080

71-
when (result) {
72-
is TransactionResult.Success, is TransactionResult.TransferSuccess -> handleSuccess(
73-
sender,
74-
receiverUuid,
75-
amount,
76-
currency
77-
)
78-
79-
TransactionResult.ReceiverInsufficientFunds -> handleReceiverInsufficientFunds(
80-
sender,
81-
receiverUuid,
82-
currency
83-
)
84-
85-
TransactionResult.SenderInsufficientFunds -> handleSenderInsufficientFunds(
86-
sender,
87-
currency
88-
)
89-
90-
is TransactionResult.DatabaseError -> handleError(sender, result)
91-
}
92-
} finally {
93-
payLock.remove(sender.uniqueId)
81+
TransactionResult.ReceiverInsufficientFunds -> handleReceiverInsufficientFunds(
82+
sender,
83+
receiverName,
84+
currency
85+
)
86+
87+
TransactionResult.SenderInsufficientFunds -> handleSenderInsufficientFunds(
88+
sender,
89+
currency
90+
)
91+
92+
is TransactionResult.DatabaseError -> handleError(sender, result)
9493
}
9594
}
9695

9796
private suspend fun handleSuccess(
9897
sender: Player,
9998
receiver: UUID,
99+
receiverName: String,
100100
amount: Double,
101-
currency: Currency
101+
currency: Currency,
102+
amountComponent: Component
102103
) {
103-
sender.sendText {
104-
appendPrefix()
105-
106-
info("Du hast ")
107-
append(currency.format(amount))
108-
info(" an ")
109-
append(Components.usernameOrUuidComponent(receiver))
110-
info(" überwiesen.")
111-
}
112-
104+
sender.showDialog(paySuccessDialog(receiverName, amountComponent))
113105
RedisService.publish(PaymentReceivedEvent(receiver, sender.name, currency, amount)).await()
114106
}
115107

116108
private fun handleError(sender: Player, error: TransactionResult.DatabaseError) {
117-
sender.sendText {
118-
appendPrefix()
119-
error("Es ist ein Fehler aufgetreten!")
120-
}
109+
sender.showDialog(payErrorDialog())
121110

122111
log.atSevere()
123112
.withCause(error.cause)
124113
.log("Database error during transaction for player ${sender.name} (UUID: ${sender.uniqueId})")
125114
}
126115

127116
private fun handleSenderInsufficientFunds(sender: Player, currency: Currency) {
128-
sender.sendText {
129-
appendPrefix()
130-
131-
error("Du hast nicht genügend ")
132-
append(currency.displayName)
133-
error(" um diese Transaktion durchzuführen!")
134-
}
117+
sender.showDialog(paySenderInsufficientFundsDialog(currency.displayName))
135118
}
136119

137-
private suspend fun handleReceiverInsufficientFunds(
120+
private fun handleReceiverInsufficientFunds(
138121
sender: Player,
139-
receiver: UUID,
122+
receiverName: String,
140123
currency: Currency
141124
) {
142-
sender.sendText {
143-
appendPrefix()
144-
145-
append(Components.usernameOrUuidComponent(receiver))
146-
error(" hat nicht genügend ")
147-
append(currency.displayName)
148-
error(" um diese Transaktion durchzuführen!")
149-
}
125+
sender.showDialog(payReceiverInsufficientFundsDialog(currency.displayName, receiverName))
150126
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
package dev.slne.surf.transaction.paper.commands.pay
4+
5+
import dev.slne.surf.surfapi.bukkit.api.dialog.base
6+
import dev.slne.surf.surfapi.bukkit.api.dialog.dialog
7+
import dev.slne.surf.surfapi.bukkit.api.dialog.type
8+
import io.papermc.paper.registry.data.dialog.DialogBase
9+
import net.kyori.adventure.text.Component
10+
import org.bukkit.entity.Player
11+
import kotlin.coroutines.resume
12+
import kotlin.coroutines.suspendCoroutine
13+
14+
@Suppress("SuspendCoroutineLacksCancellationGuarantees")
15+
suspend fun payConfirmationDialog(
16+
sender: Player,
17+
receiverName: String,
18+
amount: Component
19+
) = suspendCoroutine { continuation ->
20+
sender.showDialog(dialog {
21+
base {
22+
title {
23+
primary("Überweisung überprüfen")
24+
}
25+
preventClosingWithEscape()
26+
afterAction(DialogBase.DialogAfterAction.WAIT_FOR_RESPONSE)
27+
body {
28+
plainMessage {
29+
append {
30+
variableKey("Empfänger: ")
31+
variableValue(receiverName)
32+
}
33+
appendNewline {
34+
variableKey("Betrag: ")
35+
append(amount)
36+
}
37+
}
38+
}
39+
}
40+
41+
type {
42+
confirmation {
43+
no {
44+
label { error("Abbrechen") }
45+
action {
46+
playerCallback {
47+
it.showDialog(payErrorDialog())
48+
it.closeDialog()
49+
continuation.resume(false)
50+
}
51+
}
52+
}
53+
yes {
54+
label { success("Bestätigen") }
55+
action {
56+
callback {
57+
continuation.resume(true)
58+
}
59+
}
60+
}
61+
}
62+
}
63+
})
64+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
package dev.slne.surf.transaction.paper.commands.pay
4+
5+
import dev.slne.surf.surfapi.bukkit.api.dialog.noticeDialog
6+
import dev.slne.surf.surfapi.bukkit.api.dialog.noticeDialogWithBuilder
7+
import dev.slne.surf.surfapi.core.api.messages.Colors
8+
import dev.slne.surf.surfapi.core.api.messages.adventure.text
9+
import net.kyori.adventure.text.Component
10+
11+
fun paySuccessDialog(
12+
receiverName: String,
13+
amount: Component,
14+
) = noticeDialogWithBuilder(
15+
text("Überweisung erfolgreich", Colors.SUCCESS)
16+
) {
17+
info("Du hast ")
18+
append(amount)
19+
info(" an ")
20+
variableValue(receiverName)
21+
info(" überwiesen.")
22+
}
23+
24+
25+
fun payErrorDialog() = noticeDialog(
26+
text("Überweisung fehlgeschlagen", Colors.ERROR),
27+
text("Bei der Überweisung ist ein Fehler aufgetreten.", Colors.ERROR)
28+
)
29+
30+
fun paySenderInsufficientFundsDialog(
31+
currencyName: Component
32+
) = noticeDialogWithBuilder(
33+
text("Kontostand zu niedrig", Colors.WARNING),
34+
) {
35+
warning("Du hast nicht genügend ")
36+
append(currencyName)
37+
warning(".")
38+
}
39+
40+
fun payReceiverInsufficientFundsDialog(
41+
currencyName: Component,
42+
receiverName: String
43+
) = noticeDialogWithBuilder(
44+
text("Kontostand zu niedrig", Colors.WARNING),
45+
) {
46+
variableValue(receiverName)
47+
warning(" hat nicht genügend ")
48+
append(currencyName)
49+
warning(".")
50+
}

0 commit comments

Comments
 (0)