🎉 Initial Commit
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package dev.solsynth.snConnect
|
||||
|
||||
import dev.solsynth.snConnect.commands.SnCommand
|
||||
import dev.solsynth.snConnect.commands.SnCommandCompleter
|
||||
import dev.solsynth.snConnect.services.SnService
|
||||
import net.milkbowl.vault.economy.Economy
|
||||
import org.bukkit.Bukkit
|
||||
import org.bukkit.plugin.java.JavaPlugin
|
||||
|
||||
|
||||
class SolarNetworkConnect : JavaPlugin() {
|
||||
private var economy: Economy? = null
|
||||
private var sn: SnService? = null
|
||||
|
||||
override fun onEnable() {
|
||||
logger.info(String.format("Enabling Version %s", description.version));
|
||||
|
||||
this.saveDefaultConfig()
|
||||
|
||||
if (!setupNetwork()) {
|
||||
logger.severe("Failed to setup Solar Network Network, check your configuration.")
|
||||
}
|
||||
if (!setupEconomy()) {
|
||||
logger.warning(
|
||||
"Failed to load vault, economy related features will not available."
|
||||
)
|
||||
}
|
||||
|
||||
Bukkit.getPluginCommand("solar")!!.setExecutor(SnCommand(this.sn!!))
|
||||
Bukkit.getPluginCommand("solar")!!.tabCompleter = SnCommandCompleter()
|
||||
|
||||
logger.info(
|
||||
String.format(
|
||||
"Successfully loaded Solar Network Connect connected with %s",
|
||||
config.getString("sn.endpoint")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
override fun onDisable() {
|
||||
logger.info(String.format("Disabled Version %s", description.version));
|
||||
}
|
||||
|
||||
private fun setupNetwork(): Boolean {
|
||||
val baseUrl = config.getString("sn.endpoint") ?: return false;
|
||||
val clientId = config.getString("sn.client_id") ?: return false;
|
||||
val clientSecret = config.getString("sn.client_secret") ?: return false;
|
||||
sn = SnService(baseUrl, clientId, clientSecret);
|
||||
return true;
|
||||
}
|
||||
|
||||
private fun setupEconomy(): Boolean {
|
||||
if (server.pluginManager.getPlugin("Vault") == null) {
|
||||
logger.warning(
|
||||
"No Vault dependency found!"
|
||||
);
|
||||
return false
|
||||
}
|
||||
val rsp = server.servicesManager.getRegistration(Economy::class.java) ?: return false
|
||||
economy = rsp.provider
|
||||
@Suppress("KotlinConstantConditions")
|
||||
return economy != null
|
||||
}
|
||||
}
|
63
src/main/kotlin/dev/solsynth/snConnect/commands/SnCommand.kt
Normal file
63
src/main/kotlin/dev/solsynth/snConnect/commands/SnCommand.kt
Normal file
@@ -0,0 +1,63 @@
|
||||
package dev.solsynth.snConnect.commands
|
||||
|
||||
import dev.solsynth.snConnect.services.SnOrderService
|
||||
import dev.solsynth.snConnect.services.SnService
|
||||
import net.md_5.bungee.api.chat.ClickEvent
|
||||
import net.md_5.bungee.api.chat.TextComponent
|
||||
import org.bukkit.ChatColor
|
||||
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) : 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() + "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;
|
||||
}
|
||||
|
||||
val orderSrv = SnOrderService(sn);
|
||||
val order = orderSrv.createOrder("Deposit to Highland MC", amount / 100);
|
||||
|
||||
val linkComponent = TextComponent(ChatColor.GOLD.toString() + "Click here to payment page")
|
||||
linkComponent.clickEvent =
|
||||
ClickEvent(ClickEvent.Action.OPEN_URL, "https://solsynth.dev/orders/${order.id}");
|
||||
p0.spigot().sendMessage(linkComponent);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package dev.solsynth.snConnect.services
|
||||
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.io.IOException
|
||||
|
||||
@Serializable
|
||||
class SnOrderRequest(
|
||||
@SerialName("client_id")
|
||||
val clientId: String,
|
||||
@SerialName("client_secret")
|
||||
val clientSecret: String,
|
||||
val remark: String,
|
||||
val amount: Double
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class SnOrder(
|
||||
val id: Long,
|
||||
@SerialName("created_at")
|
||||
val createdAt: Instant,
|
||||
@SerialName("updated_at")
|
||||
val updatedAt: Instant,
|
||||
@SerialName("deleted_at")
|
||||
val deletedAt: Instant? = null,
|
||||
val status: Long,
|
||||
val remark: String,
|
||||
val amount: String,
|
||||
@SerialName("payer_id")
|
||||
val payerID: Int? = null,
|
||||
@SerialName("payee_id")
|
||||
val payeeID: Int? = null,
|
||||
@SerialName("transaction_id")
|
||||
val transactionID: Int? = null,
|
||||
@SerialName("client_id")
|
||||
val clientID: Long
|
||||
)
|
||||
|
||||
class SnOrderService(private val sn: SnService) {
|
||||
private val json = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
|
||||
fun createOrder(remark: String, amount: Double): SnOrder {
|
||||
val body = SnOrderRequest(
|
||||
sn.clientId,
|
||||
sn.clientSecret,
|
||||
remark,
|
||||
amount,
|
||||
);
|
||||
val request = Request.Builder()
|
||||
.url(sn.getUrl("wa", "/orders"))
|
||||
.post(Json.encodeToString(body).toRequestBody("application/json".toMediaTypeOrNull()))
|
||||
.build()
|
||||
|
||||
sn.client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) throw IOException("Unexpected code $response")
|
||||
val responseBody = response.body!!.string()
|
||||
val out = json.decodeFromString<SnOrder>(responseBody)
|
||||
|
||||
return out
|
||||
}
|
||||
}
|
||||
}
|
11
src/main/kotlin/dev/solsynth/snConnect/services/SnService.kt
Normal file
11
src/main/kotlin/dev/solsynth/snConnect/services/SnService.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package dev.solsynth.snConnect.services
|
||||
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
class SnService(private val baseUrl: String, val clientId: String, val clientSecret: String) {
|
||||
val client = OkHttpClient.Builder().build();
|
||||
|
||||
fun getUrl(service: String, segment: String): String {
|
||||
return "$baseUrl/cgi/$service$segment"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user