Skip to content

Commit 4abe0b3

Browse files
committed
Add concurrency lock to prevent duplicate payments in PayCommand
- Introduced `payLock` to ensure one payment per sender at a time. - Updated `pay` function to handle concurrency gracefully with appropriate feedback to users. - Cleaned up formatting and added error handling improvements.
1 parent fe81acd commit 4abe0b3

File tree

1 file changed

+38
-24
lines changed
  • surf-transaction-paper/src/main/kotlin/dev/slne/surf/transaction/paper/commands/pay

1 file changed

+38
-24
lines changed

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import dev.slne.surf.transaction.paper.commands.CommandPermission
2020
import dev.slne.surf.transaction.paper.redis.events.pay.PaymentReceivedEvent
2121
import org.bukkit.entity.Player
2222
import java.util.*
23+
import java.util.concurrent.ConcurrentHashMap
2324

2425
private val log = logger()
26+
private val payLock = ConcurrentHashMap.newKeySet<UUID>()
2527

2628
fun payCommand() = commandTree("pay") {
2729
withPermission(CommandPermission.PAY)
@@ -40,7 +42,6 @@ fun payCommand() = commandTree("pay") {
4042
}
4143
}
4244

43-
4445
private suspend fun pay(
4546
sender: Player,
4647
receiverUuid: UUID,
@@ -50,33 +51,46 @@ private suspend fun pay(
5051
throw CommandAPI.failWithString("Du kannst dir kein Geld selbst überweisen!")
5152
}
5253

53-
val currency = Currency.default()
54-
val result = sender.transactionUser().transfer(
55-
amount = amount.toBigDecimal(),
56-
currency = currency,
57-
receiver = TransactionUser.byUuid(receiverUuid).getDefaultAccount()
58-
)
59-
60-
when (result) {
61-
is TransactionResult.Success, is TransactionResult.TransferSuccess -> handleSuccess(
62-
sender,
63-
receiverUuid,
64-
amount,
65-
currency
66-
)
54+
if (!payLock.add(sender.uniqueId)) {
55+
throw CommandAPI.failWithString("Du führst bereits eine Überweisung durch. Bitte warte einen Moment...")
56+
}
6757

68-
TransactionResult.ReceiverInsufficientFunds -> handleReceiverInsufficientFunds(
69-
sender,
70-
receiverUuid,
71-
currency
72-
)
58+
sender.sendText {
59+
appendPrefix()
60+
info("Überweisung wird ausgeführt...")
61+
}
7362

74-
TransactionResult.SenderInsufficientFunds -> handleSenderInsufficientFunds(
75-
sender,
76-
currency
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()
7769
)
7870

79-
is TransactionResult.DatabaseError -> handleError(sender, result)
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)
8094
}
8195
}
8296

0 commit comments

Comments
 (0)