Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed Oct 23, 2016
1 parent 9d9eb20 commit ee28d09
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> {
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<Class<*>> {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import org.bukkit.util.NumberConversions
import java.lang.ref.WeakReference
import java.util.*

inline fun <reified T> MetadataValue.value() = value() as? T

fun <T> MetadataValue.value(type: Class<T>) : T? {
val value = value()
return if(type.isInstance(value)) type.cast(value) else null
Expand All @@ -16,12 +18,18 @@ fun <T> MetadataValue.value(type: Class<T>) : 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 <reified T> Metadatable.getMetadata(metadataKey: String, plugin: Plugin)
= getMetadata(metadataKey, plugin)?.value() as? T

fun <T> Metadatable.getMetadata(metadataKey: String, plugin: Plugin, type: Class<T>)
= getMetadata(metadataKey, plugin)?.value(type)

fun Metadatable.hasMetadata(metadataKey: String, plugin: Plugin, type: Class<*>)
= type.isInstance(getMetadata(metadataKey, plugin))

inline fun <reified T : Any> Metadatable.getMetadata(metadataKey: String) : List<T>
= getMetadata(metadataKey, T::class.java)

fun <T> Metadatable.getMetadata(metadataKey: String, type: Class<T>) : List<T>
= getMetadata(metadataKey).map { it.value() }.filterIsInstance(type)

Expand All @@ -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 <reified T : Any> Metadatable.removeMetadata(metadataKey: String, plugin: Plugin)
= removeMetadata(metadataKey, plugin, T::class.java)

fun <T> Metadatable.removeMetadata(metadataKey: String, plugin: Plugin, type: Class<T>) : T? {
val data = getMetadata(metadataKey, plugin, type) ?: return null
removeMetadata(metadataKey, plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -34,6 +35,10 @@ open class DataCompanion<D : PlayerData>(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)
Expand All @@ -49,6 +54,8 @@ open class DataCompanion<D : PlayerData>(val plugin: Plugin, val key: String, va

operator fun contains(player: Player) = player.hasMetadata(key, plugin, type)

operator fun iterator() : Iterator<D> = Bukkit.getOnlinePlayers().map { get(it) }.filterNotNull().iterator()

protected open fun createMissing(player: Player) = factory.invoke(player)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.*

Expand All @@ -13,6 +17,39 @@ class TestData(player: OfflinePlayer) : PlayerData(player) {
companion object : AutoDataCompanion<TestData>(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<out String>): 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<SillyPlayerData>(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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> {
return mutableSetOf()
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
@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

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
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerData>? = 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
}
}
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -17,7 +20,7 @@ allprojects {
}

buildscript {
ext.kotlin_version = '1.0.3'
ext.kotlin_version = '1.0.4'

repositories {
mavenCentral()
Expand All @@ -31,6 +34,9 @@ buildscript {
}

subprojects {
if(project.name.contains('Example'))
return

apply plugin: 'kotlin'
apply plugin: 'com.github.johnrengelman.shadow'

Expand Down

0 comments on commit ee28d09

Please sign in to comment.