diff --git a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/KotlinPlugin.kt b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/KotlinPlugin.kt new file mode 100644 index 0000000..fbe9eff --- /dev/null +++ b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/KotlinPlugin.kt @@ -0,0 +1,49 @@ +package br.com.gamemods.kotlinfun.bukkit + +import com.avaje.ebean.EbeanServer +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.command.PluginCommand +import org.bukkit.configuration.file.FileConfiguration +import org.bukkit.generator.ChunkGenerator +import org.bukkit.plugin.java.JavaPlugin +import java.io.File +import java.io.InputStream + +open class KotlinPlugin : JavaPlugin() { + override fun getResource(filename: String): InputStream { + return super.getResource(filename) + } + + override fun onTabComplete(sender: CommandSender, command: Command, alias: String, vararg args: String): MutableList { + return super.onTabComplete(sender, command, alias, args) + } + + override fun getDatabase(): EbeanServer { + return super.getDatabase() ?: throw IllegalStateException("Plugin has changed database to true at runtime") + } + + override fun getDatabaseClasses(): MutableList> { + return super.getDatabaseClasses() + } + + override fun getFile(): File { + return super.getFile() + } + + override fun onCommand(sender: CommandSender, command: Command, label: String, vararg args: String): Boolean { + return false + } + + override fun getConfig(): FileConfiguration { + return super.getConfig() + } + + override fun getDefaultWorldGenerator(worldName: String, id: String): ChunkGenerator { + return super.getDefaultWorldGenerator(worldName, id) + } + + override fun getCommand(name: String?): PluginCommand { + return super.getCommand(name) + } +} diff --git a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/bukkit.kt b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/bukkit.kt index ece096c..7b5fdcd 100644 --- a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/bukkit.kt +++ b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/bukkit.kt @@ -5,7 +5,7 @@ import org.bukkit.event.Listener import org.bukkit.plugin.Plugin import org.bukkit.plugin.java.JavaPlugin -class KotlinFun : JavaPlugin() { +class KotlinFun : KotlinPlugin() { companion object { lateinit var instance : KotlinFun } diff --git a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/metadata.kt b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/metadata.kt index 92b656d..48cace7 100644 --- a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/metadata.kt +++ b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/metadata.kt @@ -8,6 +8,8 @@ import org.bukkit.util.NumberConversions import java.lang.ref.WeakReference import java.util.* +inline fun MetadataValue.value() = value() as? T + fun MetadataValue.value(type: Class) : T? { val value = value() return if(type.isInstance(value)) type.cast(value) else null @@ -16,12 +18,18 @@ fun MetadataValue.value(type: Class) : T? { fun Metadatable.getMetadata(metadataKey: String, plugin: Plugin) = getMetadata(metadataKey).find { it.owningPlugin == plugin } fun Metadatable.hasMetadata(metadataKey: String, plugin: Plugin) = getMetadata(metadataKey, plugin) != null +inline fun Metadatable.getMetadata(metadataKey: String, plugin: Plugin) + = getMetadata(metadataKey, plugin)?.value() as? T + fun Metadatable.getMetadata(metadataKey: String, plugin: Plugin, type: Class) = getMetadata(metadataKey, plugin)?.value(type) fun Metadatable.hasMetadata(metadataKey: String, plugin: Plugin, type: Class<*>) = type.isInstance(getMetadata(metadataKey, plugin)) +inline fun Metadatable.getMetadata(metadataKey: String) : List + = getMetadata(metadataKey, T::class.java) + fun Metadatable.getMetadata(metadataKey: String, type: Class) : List = getMetadata(metadataKey).map { it.value() }.filterIsInstance(type) @@ -31,6 +39,9 @@ fun Metadatable.hasMetadata(metadataKey: String, type: Class<*>) fun Metadatable.setMetadata(metadataKey: String, plugin: Plugin, value: Any?) = setMetadata(metadataKey, ConstantMetadataValue(plugin, value)) +inline fun Metadatable.removeMetadata(metadataKey: String, plugin: Plugin) + = removeMetadata(metadataKey, plugin, T::class.java) + fun Metadatable.removeMetadata(metadataKey: String, plugin: Plugin, type: Class) : T? { val data = getMetadata(metadataKey, plugin, type) ?: return null removeMetadata(metadataKey, plugin) diff --git a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/player.kt b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/player.kt index 3271ca3..52dfd5d 100644 --- a/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/player.kt +++ b/BukkitPlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bukkit/player.kt @@ -3,6 +3,7 @@ package br.com.gamemods.kotlinfun.bukkit import org.bukkit.Bukkit import org.bukkit.OfflinePlayer +import org.bukkit.command.CommandSender import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.EventPriority @@ -34,6 +35,10 @@ open class DataCompanion(val plugin: Plugin, val key: String, va return data } + operator fun get(sender: CommandSender?): D? { + return get(sender as? Player ?: return null) + } + operator fun set(player: Player, data: D?) { if(data != null) player.setMetadata(key, plugin, data) @@ -49,6 +54,8 @@ open class DataCompanion(val plugin: Plugin, val key: String, va operator fun contains(player: Player) = player.hasMetadata(key, plugin, type) + operator fun iterator() : Iterator = Bukkit.getOnlinePlayers().map { get(it) }.filterNotNull().iterator() + protected open fun createMissing(player: Player) = factory.invoke(player) } diff --git a/BukkitPlugin/src/test/kotlin/br/com/gamemods/kotlinfun/bukkit/PlayerDataTest.kt b/BukkitPlugin/src/test/kotlin/br/com/gamemods/kotlinfun/bukkit/PlayerDataTest.kt index c00a594..6e4aab6 100644 --- a/BukkitPlugin/src/test/kotlin/br/com/gamemods/kotlinfun/bukkit/PlayerDataTest.kt +++ b/BukkitPlugin/src/test/kotlin/br/com/gamemods/kotlinfun/bukkit/PlayerDataTest.kt @@ -1,7 +1,11 @@ package br.com.gamemods.kotlinfun.bukkit +import br.com.gamemods.kotlinfun.str import org.bukkit.Bukkit import org.bukkit.OfflinePlayer +import org.bukkit.command.Command +import org.bukkit.command.CommandSender +import org.bukkit.entity.Player import org.bukkit.plugin.java.JavaPlugin import java.util.* @@ -13,6 +17,39 @@ class TestData(player: OfflinePlayer) : PlayerData(player) { companion object : AutoDataCompanion(testPlugin, "Testing", TestData::class.java, ::TestData) } +class MyNicePlugin : JavaPlugin() { + companion object { + lateinit var instance : MyNicePlugin + } + + override fun onEnable() { + instance = this + } + + override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array): Boolean { + sender as? Player ?: return false + if(command.name == "silly") { + val data = SillyPlayerData[sender] ?: return false + data.silly = !data.silly + data.tellHim() + return true + } + else { + return false + } + } +} + +class SillyPlayerData(player: OfflinePlayer) : PlayerData(player) { + companion object : AutoDataCompanion(MyNicePlugin.instance, "SillyPlayer", + SillyPlayerData::class.java, ::SillyPlayerData + ) + + var silly = false + + fun tellHim() = player?.sendMessage((if(silly) "You are silly!".red() else "You are not silly :)".green()).str()) +} + fun test() { val data : TestData? = TestData[UUID.randomUUID()] val player = Bukkit.getOfflinePlayer(UUID.randomUUID())?.player ?: return diff --git a/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/KotlinCommand.kt b/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/KotlinCommand.kt new file mode 100644 index 0000000..ce446a2 --- /dev/null +++ b/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/KotlinCommand.kt @@ -0,0 +1,13 @@ +package br.com.gamemods.kotlinfun.bungee + +import net.md_5.bungee.api.CommandSender +import net.md_5.bungee.api.plugin.Command +import net.md_5.bungee.api.plugin.TabExecutor + +abstract class KotlinCommand(name: String, permission: String? = null, vararg aliases: String) : Command(name, permission, *aliases), TabExecutor { + abstract override fun execute(sender: CommandSender, vararg args: String) + + override fun onTabComplete(sender: CommandSender, vararg args: String): MutableIterable { + return mutableSetOf() + } +} diff --git a/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/bungee.kt b/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/bungee.kt index 5d4548f..127a764 100644 --- a/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/bungee.kt +++ b/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/bungee.kt @@ -1,6 +1,8 @@ @file:JvmName(name = "BungeeUtil") package br.com.gamemods.kotlinfun.bungee +import net.md_5.bungee.api.ProxyServer +import net.md_5.bungee.api.plugin.Command import net.md_5.bungee.api.plugin.Listener import net.md_5.bungee.api.plugin.Plugin @@ -8,6 +10,7 @@ class KotlinFun : Plugin() fun Plugin.register(listener: Listener) = proxy.pluginManager.registerListener(this, listener) fun Plugin.unregister(listener: Listener) = proxy.pluginManager.unregisterListener(listener) +fun Listener.unregister() = ProxyServer.getInstance().pluginManager.unregisterListener(this) fun Plugin.unregisterListeners() = proxy.pluginManager.unregisterListeners(this) fun Plugin.register(listener: Listener, vararg listeners: Listener) { val manager = proxy.pluginManager @@ -21,3 +24,13 @@ fun Plugin.unregister(listener: Listener, vararg listeners: Listener) { for(other in listeners) manager.unregisterListener(other) } +fun Plugin.register(command: Command) = proxy.pluginManager.registerCommand(this, command) +fun Plugin.register(command: Command, vararg commands: Command) { + val manager = proxy.pluginManager + manager.registerCommand(this, command) + for(other in commands) + manager.registerCommand(this, command) +} +fun Plugin.unregister(command: Command) = proxy.pluginManager.unregisterCommand(command) +fun Command.unregister() = ProxyServer.getInstance().pluginManager.unregisterCommand(this) +fun Plugin.unregisterCommands() = proxy.pluginManager.unregisterCommands(this) diff --git a/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/player.kt b/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/player.kt index b425fc3..b6790f6 100644 --- a/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/player.kt +++ b/BungeePlugin/src/main/kotlin/br/com/gamemods/kotlinfun/bungee/player.kt @@ -82,4 +82,23 @@ open class PlayerData(val uniqueId: UUID, val name : String) { constructor(player: ProxiedPlayer) : this(player.uniqueId, player.name) { this.player = player } + + open val companion : DataCompanion? = null + + fun set() : Boolean { + companion?.set(player ?: return false, this) ?: throw UnsupportedOperationException("The 'companion' property is null") + return true + } + + fun current() = companion?.get(uniqueId) ?: throw UnsupportedOperationException("The 'companion' property is null") + + fun unset() : Boolean { + val companion = companion ?: throw UnsupportedOperationException("The 'companion' property is null") + val player = player ?: return false + if(companion[player] != this) { + return false + } + companion[player] = null + return true + } } diff --git a/build.gradle b/build.gradle index 78677d6..1076df9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,9 @@ allprojects { + if(project.name.contains('Example')) + return + group 'br.com.gamemods.kotlinfun' - version '0.2' + version '0.3-SNAPSHOT' apply plugin: 'kotlin' @@ -17,7 +20,7 @@ allprojects { } buildscript { - ext.kotlin_version = '1.0.3' + ext.kotlin_version = '1.0.4' repositories { mavenCentral() @@ -31,6 +34,9 @@ buildscript { } subprojects { + if(project.name.contains('Example')) + return + apply plugin: 'kotlin' apply plugin: 'com.github.johnrengelman.shadow'