232 lines
9.5 KiB
Kotlin
232 lines
9.5 KiB
Kotlin
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.Bukkit.getLogger
|
|
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?,
|
|
private val messages: Map<String, String>,
|
|
private val playerSolarpassMap: MutableMap<String, String?>
|
|
) :
|
|
CommandExecutor {
|
|
override fun onCommand(p0: CommandSender, p1: Command, p2: String, p3: Array<out String>): 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() + (messages["command_deposit_no_amount"]
|
|
?: "You need to specify 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];
|
|
p0.sendMessage(
|
|
ChatColor.GRAY.toString() + (messages["command_deposit_confirming"]
|
|
?: "Confirming payment, please stand by...")
|
|
);
|
|
|
|
val order: SnOrder
|
|
try {
|
|
order = orderSrv.getOrder(orderNumber);
|
|
// The update validated the order is created by us
|
|
orderSrv.updateOrderStatus(orderNumber, false);
|
|
} catch (_: Exception) {
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + (messages["command_deposit_pull_error"]
|
|
?: "An error occurred while pulling transaction. Make sure the order exists then try again later.")
|
|
)
|
|
return true;
|
|
}
|
|
|
|
if (order.status != 1L) {
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + (messages["command_deposit_order_not_paid"]
|
|
?: "Order was not paid yet.")
|
|
)
|
|
return true;
|
|
}
|
|
|
|
val bal = order.amount;
|
|
eco?.depositPlayer(p0.player, bal)
|
|
|
|
val doneComponent =
|
|
TextComponent(ChatColor.GREEN.toString() + (messages["command_deposit_done"] ?: "Done!"))
|
|
val moneyComponent = TextComponent(ChatColor.GOLD.toString() + ChatColor.BOLD + " $bal$")
|
|
val suffixComponent =
|
|
TextComponent(
|
|
ChatColor.GREEN.toString() + (messages["command_deposit_added_balance"]
|
|
?: " 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() + (messages["command_deposit_invalid_amount"]
|
|
?: "You need to specify an amount as a number to deposit.")
|
|
)
|
|
return true;
|
|
}
|
|
|
|
p0.sendMessage(
|
|
ChatColor.GRAY.toString() + (messages["command_deposit_creating"]
|
|
?: "Creating order, please stand by...")
|
|
);
|
|
|
|
val order: SnOrder
|
|
try {
|
|
order = orderSrv.createOrder("Deposit to GoatCraft", amount);
|
|
} catch (e: Exception) {
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + (messages["command_deposit_create_error"]
|
|
?: "An error occurred while creating order. Try again later.")
|
|
)
|
|
e.printStackTrace();
|
|
return true;
|
|
}
|
|
|
|
val linkComponent =
|
|
TextComponent(
|
|
ChatColor.GOLD.toString() + ChatColor.UNDERLINE.toString() + ChatColor.BOLD.toString() + (messages["command_deposit_click_here"]
|
|
?: "click here")
|
|
)
|
|
linkComponent.clickEvent =
|
|
ClickEvent(ClickEvent.Action.OPEN_URL, "https://solian.app/orders/${order.id}");
|
|
val orderHintComponent =
|
|
TextComponent(
|
|
ChatColor.GRAY.toString() + (messages["command_deposit_order_created"]
|
|
?: "Order created, number ") + ChatColor.WHITE + ChatColor.BOLD + "#${order.id.take(6)}" + " "
|
|
)
|
|
val followingComponent = TextComponent(
|
|
ChatColor.GRAY.toString() + (messages["command_deposit_to_payment_page"] ?: " to the payment page.")
|
|
)
|
|
p0.spigot()
|
|
.sendMessage(orderHintComponent, linkComponent, followingComponent);
|
|
|
|
val afterPaidComponent =
|
|
TextComponent(
|
|
ChatColor.UNDERLINE.toString() + ChatColor.YELLOW + (messages["command_deposit_after_paid"]
|
|
?: "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() + (messages["command_deposit_no_amount"]
|
|
?: "You need to specify an amount to deposit.")
|
|
)
|
|
return true;
|
|
}
|
|
|
|
val amount = p3[1].toDoubleOrNull();
|
|
if (amount == null) {
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + (messages["command_deposit_invalid_amount"]
|
|
?: "You need to specify an amount as a number to deposit.")
|
|
)
|
|
return true;
|
|
}
|
|
|
|
val playerUuid = p0.uniqueId.toString()
|
|
val accountId = playerSolarpassMap[playerUuid]
|
|
if (accountId == null) {
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + "Bind your Solarpass first!"
|
|
)
|
|
return true;
|
|
}
|
|
|
|
p0.sendMessage(
|
|
ChatColor.GRAY.toString() + (messages["command_withdraw_making"]
|
|
?: "Making transaction, please stand by...")
|
|
);
|
|
|
|
// Takes extra 20% as fee
|
|
val fee = amount * 0.2;
|
|
val resp = eco?.withdrawPlayer(p0.player, amount + fee);
|
|
if (resp?.type != EconomyResponse.ResponseType.SUCCESS) {
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + (messages["command_withdraw_no_money"]
|
|
?: "Your in-game account does not have enough money for that.")
|
|
)
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
val transactionSrv = SnBalanceService(sn);
|
|
transactionSrv.addBalance(amount, "Withdraw from GoatCraft", accountId);
|
|
val transactionHintComponent =
|
|
TextComponent(
|
|
ChatColor.GREEN.toString() + (messages["command_withdraw_done"]
|
|
?: "Done! ")
|
|
)
|
|
|
|
p0.playSound(p0.player!!, Sound.BLOCK_ANVIL_PLACE, 1.0F, 1.0F)
|
|
p0.spigot().sendMessage(transactionHintComponent)
|
|
} catch (e: Exception) {
|
|
eco.depositPlayer(p0.player, amount + fee)
|
|
p0.sendMessage(
|
|
ChatColor.RED.toString() + (messages["command_withdraw_error"]
|
|
?: "An error occurred while making transaction. Make sure your wallet exists then try again later.")
|
|
)
|
|
e.printStackTrace()
|
|
return true
|
|
}
|
|
}
|
|
|
|
else -> {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
class SnCommandCompleter : TabCompleter {
|
|
override fun onTabComplete(
|
|
p0: CommandSender,
|
|
p1: Command,
|
|
p2: String,
|
|
p3: Array<out String>
|
|
): MutableList<String> {
|
|
return when (p3.size) {
|
|
1 -> mutableListOf("deposit", "withdraw");
|
|
else -> mutableListOf();
|
|
}
|
|
}
|
|
}
|