diff --git a/bootstrap.sh b/bootstrap.sh index 7e3516b..46a1e94 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,4 +1,4 @@ #!/usr/bin/bash # Fetch all submodule content. -git submodule update --force --recursive --init --remote +git submodule update --force --recursive --init diff --git a/build.gradle.kts b/build.gradle.kts index 918e74d..899a293 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -44,6 +44,8 @@ dependencies { compileOnly(project(":libs:Utilities-OG")) compileOnly(project(":libs:GxUI-OG")) compileOnly(project(":libs:DiamondBank-OG")) + implementation("org.jetbrains.kotlin:kotlin-stdlib") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2") } tasks.withType().configureEach { // Ensure reproducible .jars diff --git a/src/main/kotlin/plugin/Config.kt b/src/main/kotlin/plugin/Config.kt index e514950..634d9c7 100644 --- a/src/main/kotlin/plugin/Config.kt +++ b/src/main/kotlin/plugin/Config.kt @@ -11,41 +11,28 @@ object Config { private lateinit var file: File fun load() { - file = File(KotlinTemplateOG.plugin.dataFolder, "config.yml") - if (! file.exists()) { - + if (!file.exists()) { KotlinTemplateOG.plugin.saveDefaultConfig() - } - config = YamlConfiguration.loadConfiguration(file) - this.save() - + } private fun save() { - config.save(file) - } fun getTemplateBool(): Boolean { - return config.get("templateBool") as Boolean - } fun getTemplateDouble(): Double { - return config.get("templateDouble") as Double - } fun getTemplateInt(): Int { - return config.get("templateInt") as Int - } } \ No newline at end of file diff --git a/src/main/kotlin/plugin/KotlinTemplateOG.kt b/src/main/kotlin/plugin/KotlinTemplateOG.kt index 654fbca..a47e3c3 100644 --- a/src/main/kotlin/plugin/KotlinTemplateOG.kt +++ b/src/main/kotlin/plugin/KotlinTemplateOG.kt @@ -1,22 +1,44 @@ // This is free and unencumbered software released into the public domain. // Author: Sekalol15 package plugin + +import kotlinx.coroutines.* +import net.trueog.diamondbankog.DiamondBankAPI +import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin + // Extending this class is standard bukkit boilerplate for any plugin, or else the server software won't load the classes. class KotlinTemplateOG : JavaPlugin() { - companion object { - - lateinit var plugin: JavaPlugin - + val scope = CoroutineScope(Dispatchers.Default + SupervisorJob()) + + lateinit var plugin: KotlinTemplateOG + lateinit var diamondBankAPI: DiamondBankAPI } override fun onEnable() { - plugin = this Config.load() + val diamondBankAPIProvider = server.servicesManager.getRegistration(DiamondBankAPI::class.java) + if (diamondBankAPIProvider == null) { + this.logger.severe("DiamondBank-OG API is null") + Bukkit.getPluginManager().disablePlugin(this) + return + } + diamondBankAPI = diamondBankAPIProvider.getProvider() + + + this.server.pluginManager.registerEvents(Listeners(), this) + } + + override fun onDisable() { + scope.cancel() + + runBlocking { + scope.coroutineContext[Job]?.join() + } } } diff --git a/src/main/kotlin/plugin/Listeners.kt b/src/main/kotlin/plugin/Listeners.kt index 479d373..0975677 100644 --- a/src/main/kotlin/plugin/Listeners.kt +++ b/src/main/kotlin/plugin/Listeners.kt @@ -1,13 +1,48 @@ -package nl.skbotnl.apitest +package plugin -import net.trueog.diamondbankog.PostgreSQL +import kotlinx.coroutines.future.await +import kotlinx.coroutines.launch +import net.trueog.diamondbankog.DiamondBankAPI.DiamondBankException.* +import net.trueog.diamondbankog.PostgreSQL.ShardType +import net.trueog.utilitiesog.UtilitiesOG import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.BlockBreakEvent -import java.util.concurrent.CompletableFuture +import java.util.concurrent.ExecutionException + class Listeners : Listener { -// @EventHandler -// fun onBlockBreak(event: BlockBreakEvent) { -// } + @EventHandler + fun onBlockBreak(event: BlockBreakEvent) { + KotlinTemplateOG.scope.launch { + val playerShards = try { + KotlinTemplateOG.diamondBankAPI.getPlayerShards(event.player.uniqueId, ShardType.ALL).await() + } catch (e: EconomyDisabledException) { + UtilitiesOG.trueogMessage(event.player, "The economy is disabled.") + return@launch + } catch (e: TransactionsLockedException) { + UtilitiesOG.trueogMessage(event.player, "Your transactions are locked.") + return@launch + } catch (e: OtherException) { + UtilitiesOG.trueogMessage(event.player, "Something went wrong.") + return@launch + } + + val shardsInBank = playerShards.shardsInBank + val shardsInInventory = playerShards.shardsInInventory + val shardsInEnderChest = playerShards.shardsInEnderChest + if (shardsInBank == null || shardsInInventory == null || shardsInEnderChest == null) { + UtilitiesOG.trueogMessage(event.player, "An error has occurred.") + return@launch + } + val totalBalance = shardsInBank + shardsInInventory + shardsInEnderChest + + // Send a message to the player with their balance. + UtilitiesOG.trueogMessage(event.player, "&BYour balance is: &e$totalBalance&B Diamonds.") + UtilitiesOG.logToConsole( + "[Template-OG]", + "The player: " + event.player + "'s balance is: " + totalBalance + "&B Diamonds" + ) + } + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 35fd1bd..7b5b0c7 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ #Template Config Options -templateBool : true -templateInt : 1 +templateBool: true +templateInt: 1 templateDouble: 0.5 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2d97fc3..d0ec5d6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,5 +3,5 @@ author: Sekalol15 description: Kotlin plugin template for the OG Suite website: https://github.com/Sekalol15/KotlinTemplate-OG version: '${version}' -api-version: '${apiVersion}' +api-version: '${apiVersion}' main: plugin.KotlinTemplateOG