package dev.solsynth.snConnect.commands import dev.solsynth.snConnect.services.* import net.md_5.bungee.api.chat.ClickEvent import net.md_5.bungee.api.chat.TextComponent import net.milkbowl.vault.economy.Economy import net.milkbowl.vault.economy.EconomyResponse import org.bukkit.ChatColor import org.bukkit.Sound import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender import org.bukkit.command.TabCompleter import org.bukkit.entity.Player class SnCommand(private val sn: SnService, private val eco: Economy?) : CommandExecutor { override fun onCommand(p0: CommandSender, p1: Command, p2: String, p3: Array): Boolean { if (p0 !is Player) { return false; } if (p3.isEmpty()) return false; when (p3[0].lowercase()) { "deposit" -> { if (p3.size < 2) { p0.sendMessage(ChatColor.RED.toString() + "You need to specific an amount to deposit.") return true; } val orderSrv = SnOrderService(sn); if (p3[1].equals("confirm", ignoreCase = true) && p3.size >= 3) { // Confirming order val orderNumber = p3[2].toLongOrNull(); if (orderNumber == null) { p0.sendMessage(ChatColor.RED.toString() + "Invalid order number, it must be a number."); return true; } p0.sendMessage(ChatColor.GRAY.toString() + "Confirming payment, please stand by..."); val order: SnOrder try { order = orderSrv.getOrder(orderNumber); orderSrv.cancelOrder(orderNumber); } catch (_: Exception) { p0.sendMessage(ChatColor.RED.toString() + "An error occurred while pulling transaction. Make sure the order is exists then try again later.") return true; } if (order.status != 1L) { p0.sendMessage(ChatColor.RED.toString() + "Order was not paid yet."); return true; } val bal = order.amount.toDouble() * 100; eco?.depositPlayer(p0.player, bal) val doneComponent = TextComponent(ChatColor.GREEN.toString() + "Done!") val moneyComponent = TextComponent(ChatColor.GOLD.toString() + ChatColor.BOLD + " $bal$") val suffixComponent = TextComponent(ChatColor.GREEN.toString() + " has been added to your balance.") p0.playSound(p0.player!!, Sound.BLOCK_ANVIL_PLACE, 1.0F, 1.0F) p0.spigot().sendMessage(doneComponent, moneyComponent, suffixComponent) return true; } // Creating new order val amount = p3[1].toDoubleOrNull(); if (amount == null) { p0.sendMessage(ChatColor.RED.toString() + "You need to specific an amount of number to deposit.") return true; } p0.sendMessage(ChatColor.GRAY.toString() + "Creating order, please stand by..."); val order: SnOrder try { order = orderSrv.createOrder("Deposit to Highland MC", amount / 100); } catch (_: Exception) { p0.sendMessage(ChatColor.RED.toString() + "An error occurred while creating order. Try again later.") return true; } val linkComponent = TextComponent(ChatColor.GOLD.toString() + ChatColor.UNDERLINE.toString() + ChatColor.BOLD.toString() + "click here") linkComponent.clickEvent = ClickEvent(ClickEvent.Action.OPEN_URL, "https://solsynth.dev/orders/${order.id}"); val orderHintComponent = TextComponent(ChatColor.GRAY.toString() + "Order created, number " + ChatColor.WHITE + ChatColor.BOLD + "#${order.id}") val followingComponent = TextComponent(ChatColor.GRAY.toString() + " to the payment page.") p0.spigot() .sendMessage(orderHintComponent, linkComponent, followingComponent); val afterPaidComponent = TextComponent(ChatColor.UNDERLINE.toString() + ChatColor.YELLOW + "After you paid, click here to confirm payment.") afterPaidComponent.clickEvent = ClickEvent(ClickEvent.Action.RUN_COMMAND, "/sn deposit confirm ${order.id}") p0.spigot().sendMessage(afterPaidComponent); } "withdraw" -> { if (p3.size < 2) { p0.sendMessage(ChatColor.RED.toString() + "You need to specific an amount to deposit.") return true; } val amount = p3[1].toDoubleOrNull(); if (amount == null) { p0.sendMessage(ChatColor.RED.toString() + "You need to specific an amount of number to deposit.") return true; } if (p3.size < 3) { p0.sendMessage(ChatColor.RED.toString() + "You need to specific a wallet account to deposit.") return true; } val walletId = p3[2].toLongOrNull(); if (walletId == null) { p0.sendMessage(ChatColor.RED.toString() + "You need to specific a wallet account to deposit.") return true; } p0.sendMessage(ChatColor.GRAY.toString() + "Making transaction, please stand by..."); val bal = amount / 100; val resp = eco?.withdrawPlayer(p0.player, "Withdraw to Source Point - $bal SRC", amount); if (resp?.type != EconomyResponse.ResponseType.SUCCESS) { p0.sendMessage(ChatColor.RED.toString() + "Your in game account has no enough money for that.") return true; } try { val transactionSrv = SnTransactionService(sn); val transaction = transactionSrv.makeTransaction(bal, "Withdraw from Highland MC", walletId); val transactionHintComponent = TextComponent(ChatColor.GREEN.toString() + "Done! transaction number " + ChatColor.WHITE + ChatColor.BOLD + "#${transaction.id}") p0.playSound(p0.player!!, Sound.BLOCK_ANVIL_PLACE, 1.0F, 1.0F) p0.spigot().sendMessage(transactionHintComponent) } catch (_: Exception) { eco?.depositPlayer(p0.player, "Withdraw to Source Point Failed - Refund", amount) p0.sendMessage(ChatColor.RED.toString() + "An error occurred while making transaction. Make sure your wallet is exists then try again later.") return true } } else -> { return false } } return true; } } class SnCommandCompleter : TabCompleter { override fun onTabComplete( p0: CommandSender, p1: Command, p2: String, p3: Array ): MutableList { return when (p3.size) { 1 -> mutableListOf("deposit", "withdraw"); else -> mutableListOf(); } } }