Skip to content

Commit

Permalink
Added some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed Oct 22, 2016
1 parent 351accd commit 0a2e4af
Show file tree
Hide file tree
Showing 17 changed files with 605 additions and 1 deletion.
40 changes: 40 additions & 0 deletions Examples/Bukkit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
buildscript {
repositories {
mavenCentral()
jcenter()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4"
}
}

group 'br.com.gamemods.kotlinfun.examples'
version '0.2'

apply plugin: 'kotlin'

repositories {
mavenCentral()
maven { url = 'https://hub.spigotmc.org/nexus/content/groups/public/' }
maven { url "https://jitpack.io" }
}

dependencies {
compile 'org.bukkit:bukkit:1.10.2-R0.1-SNAPSHOT'
compile('com.github.GameModsBR.KotlinFun:BukkitPlugin:0.2')
}

processResources {
inputs.property "version", project.version

from(sourceSets.main.resources.srcDirs) {
include 'plugin.yml'

expand 'version':project.version
}

from(sourceSets.main.resources.srcDirs) {
exclude 'plugin.yml'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package example.kotlinfun.bukkit

import br.com.gamemods.kotlinfun.bukkit.*
import br.com.gamemods.kotlinfun.str
import org.bukkit.OfflinePlayer

/**
* This holds temporary player data, it's not persisted to the disk but can be quickly accessed from the player object,
*/
class CustomPlayerData(player: OfflinePlayer) : PlayerData(player) {
/**
* The super class of this companion object will setup everything automatically, that's why we need to provide
* all that stuff. Please note that this constructor will be improved in the next version.
*/
companion object : AutoDataCompanion<CustomPlayerData>(SamplePlugin.instance, "session", CustomPlayerData::class.java, ::CustomPlayerData)

// We can do whatever we want here
var observingSpawns = false
var observingDeaths = false
var observingDamages = false
set(value) {
field = value
player?.sendMessage("Damage>".gold() + if(value) "You'll now be notified about entity damages".red() else " You'll no longer see messages about damage".green())
}

var silly = false
fun tellHim() {
/**
* Notice that we have constructed this class with an OfflinePlayer.
* If the player is online than it will be returned below as a Player object, if it's not then it will be null
*/
player?.sendMessage(
/**
* Notice that we have encapsulated the if..else with parentheses to call str() in the end.
* That's because String.red() and String.green() will return a formatter object called Colorized,
* that would cause an issue here because Colorized is not an string, all we have to do is to call
* .str() or .toString() to transform it, but, we had two calls here, so to avoid repetition I
* wrapped the if statement and casted the result.
*
* An other way to workaround this would be to add an empty string, for example:
* if(silly) "You are silly!".red()+"" else "You are cool :)".green()+""
*/
( if(silly) "You are silly!".red() else "You are cool :)".green() ).str()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package example.kotlinfun.bukkit

import br.com.gamemods.kotlinfun.bukkit.*
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.block.Action
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.event.player.PlayerInteractEvent

class PlayerListener(val plugin: SamplePlugin) : Listener {
@EventHandler
fun placeMine(event: BlockPlaceEvent) {
if(event.block.type != Material.WOOD_PLATE)
return

/**
* You can set metadata to Metadatable directly, no need to create a FixedMetadataValue
*/
event.block.setMetadata("mine", plugin, event.player.name)

/**
* Notice how the formatted message was created, you can swap the bold() and darkRed() calls if you want,
* the invocation order is not important.
*
* If the red message wasn't red it would inherit the bold and dark red style
*/
event.player.sendMessage("Mine> ".bold().darkRed() + "The bomb has been placed!".red())
}

@EventHandler
fun activateMine(event: PlayerInteractEvent) {
if(event.action != Action.PHYSICAL)
return

val block = event.clickedBlock ?: return

/**
* It's super easy to get or remove a metadata now
*/
val owner = block.removeMetadata("mine", plugin, String::class.java) ?: return
if(block.world.createExplosion(block.location, 4f))
event.player.sendMessage("Mine> ".darkRed().bold() + "BOOOM!".underline().red() + " You've stepped on $owner's mine!".red())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package example.kotlinfun.bukkit

import br.com.gamemods.kotlinfun.bukkit.DataCompanion
import br.com.gamemods.kotlinfun.bukkit.PlayerData
import br.com.gamemods.kotlinfun.bukkit.register
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin

/**
* Just a simple example
*/
class SamplePlugin : JavaPlugin() {
companion object {
lateinit var instance : SamplePlugin
}

override fun onEnable() {
instance = this

/**
* You can register your listeners with a simple call
*/
register(PlayerListener(this), WorldListener())

getCommand("silly")?.setExecutor { sender, command, label, strings ->
/**
* We can get and work the player data easily
*/
CustomPlayerData[sender]?.run { silly = !silly ; tellHim() ; true } ?: false
}

getCommand("ts")?.setExecutor { sender, command, label, strings ->
CustomPlayerData[sender]?.run { observingSpawns = !observingSpawns; true } ?: false
}

getCommand("tdt")?.setExecutor { sender, command, label, strings ->
CustomPlayerData[sender]?.run { observingDeaths = !observingDeaths; true } ?: false
}

getCommand("tda")?.setExecutor { sender, command, label, strings ->
CustomPlayerData[sender]?.run { observingDamages = !observingDamages; true } ?: false
}
}
}

/**
* This will be added in the next version
*/
operator fun <D:PlayerData> DataCompanion<D>.get(sender: CommandSender?): D? {
return get(sender as? Player ?: return null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package example.kotlinfun.bukkit

import br.com.gamemods.kotlinfun.bukkit.DataCompanion
import br.com.gamemods.kotlinfun.bukkit.PlayerData
import br.com.gamemods.kotlinfun.bukkit.darkGray
import br.com.gamemods.kotlinfun.bukkit.gray
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.CreatureSpawnEvent
import org.bukkit.event.entity.EntityDamageEvent
import org.bukkit.event.entity.EntityDeathEvent

class WorldListener : Listener {
@EventHandler
fun spawn(event: CreatureSpawnEvent) {
val msg = "Spawn> ".darkGray() + "${event.entity.name} has spawned by ${event.spawnReason} in ${event.location}".gray()

/**
* We can easily iterate through all the session data from the players online
*/
for(data in CustomPlayerData) {
if(data.observingSpawns) {
data.player?.sendMessage(msg)
}
}
}

@EventHandler
fun death(event: EntityDeathEvent) {
/**
* Notice that we have a string "nothing" inside a string, Kotlin is fun
*/
val msg = "Death> ".darkGray() + "${event.entity.name} has died by ${event.entity.lastDamageCause?.cause?.name ?: "nothing"} in ${event.entity.location}".gray()
for(data in CustomPlayerData) {
if(data.observingDeaths) {
data.player?.sendMessage(msg)
}
}
}

@EventHandler
fun damage(event: EntityDamageEvent) {
val msg = "Damage> ".darkGray() + "${event.entity.name} has been damaged by ${event.cause} in ${event.entity.location}".gray()
for(data in CustomPlayerData) {
if(data.observingDeaths) {
data.player?.sendMessage(msg)
}
}
}
}

/**
* This will be included in the next version
*/
operator fun <D: PlayerData> DataCompanion<D>.iterator() : Iterator<D> =
Bukkit.getOnlinePlayers().map { get(it) }.filterNotNull().iterator()
13 changes: 13 additions & 0 deletions Examples/Bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: KotlinFunExampleBukkit
version: ${version}
main: example.kotlinfun.bukkit.SamplePlugin

commands:
silly:
description: Tells you if you are silly
ts:
description: Show/Hide where the entities are spawning
tdt:
description: Show/Hide where the entities are dying
tda:
description: Show/Hide where the entities are taking damage
40 changes: 40 additions & 0 deletions Examples/Bungee/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
buildscript {
repositories {
mavenCentral()
jcenter()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4"
}
}

group 'br.com.gamemods.kotlinfun.examples'
version '0.2'

apply plugin: 'kotlin'

repositories {
mavenCentral()
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url "https://jitpack.io" }
}

dependencies {
compile 'net.md-5:bungeecord-api:1.10-SNAPSHOT'
compile('com.github.GameModsBR.KotlinFun:BungeePlugin:0.2')
}

processResources {
inputs.property "version", project.version

from(sourceSets.main.resources.srcDirs) {
include 'bungee.yml'

expand 'version':project.version
}

from(sourceSets.main.resources.srcDirs) {
exclude 'bungee.yml'
}
}
3 changes: 3 additions & 0 deletions Examples/Bungee/src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: KotlinFunExampleBungee
version: ${version}
main:
40 changes: 40 additions & 0 deletions Examples/Spigot/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
buildscript {
repositories {
mavenCentral()
jcenter()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.4"
}
}

group 'br.com.gamemods.kotlinfun.examples'
version '0.2'

apply plugin: 'kotlin'

repositories {
mavenCentral()
maven { url = 'https://hub.spigotmc.org/nexus/content/groups/public/' }
maven { url "https://jitpack.io" }
}

dependencies {
compile 'org.spigotmc:spigot-api:1.10.2-R0.1-SNAPSHOT'
compile('com.github.GameModsBR.KotlinFun:SpigotPlugin:0.2')
}

processResources {
inputs.property "version", project.version

from(sourceSets.main.resources.srcDirs) {
include 'plugin.yml'

expand 'version':project.version
}

from(sourceSets.main.resources.srcDirs) {
exclude 'plugin.yml'
}
}
3 changes: 3 additions & 0 deletions Examples/Spigot/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: KotlinFunExampleSpigot
version: ${version}
main:
2 changes: 2 additions & 0 deletions Examples/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group 'br.com.gamemods.kotlinfun.examples'
version '0.2'
1 change: 1 addition & 0 deletions Examples/gradle/wrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gradle-wrapper.jar
6 changes: 6 additions & 0 deletions Examples/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Wed Oct 19 20:45:31 BRT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
Loading

0 comments on commit 0a2e4af

Please sign in to comment.