git clone https://github.com/MetuMortis-code/KotlinSpigotPluginTemplate.git
Configure project/src/main/resources/plugin.yml.
Set version, name, main, api-version and do more if you want.
You should run ./gradlew shadowJar to get usable jar.
val array = arrayOf(1, 2, 3, 4, 5)
val sum = array.sum()
val first = array.first()
val last = array.last()
val biggest = array.maxOrNull()
You don't have to create separate getter and setter methods for fields
var name = "John"
get() = field.toUpperCase()
set(value) {
field = value.toLowerCase()
}
You can use any Java library or api written in Java in Kotlin
import java.util.*
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)
print("Enter your name: ")
val name = scanner.nextLine()
println("Name is: $name")
scanner.close()
}
val name = "John"
println("Hello, $name")
val nullableName: String? = null
val nameLength: Int = name?.length ?: -1
println(length)
data class Person(val name: String, val age: Int)
val John = Person("John", 30)
val anotherJohn = Person("John", 30)
println(John.equals(anotherJohn))
fun sayHi(name: String = "No one", age: Integer) = println("Hi, $name, you are $age years old")
sayHi(age = 30)
println(5 in 1..10)
for(i in 1..10) {
println(i)
}
data class Point(val x: Int, val y: Int)
operator fun Point.unaryMinus() = Point(-x, -y)
val point = Point(10, 20)
fun main() {
println(-point) // prints "Point(x=-10, y=-20)"
}
infix fun Int.powerOf(n: Int): Int {
return this.pow(n)
}
println(2 powerOf 3)
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
val list = mutableListOf(1, 2, 3)
list.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'list'
Kotlin has objects that don’t have any nontrivial supertypes.
val person = object {
val name = "John"
val age = 30
}
println(person.name)
data class Person(val name: String, val age: Int)
val (name, age) = Person("John", 30)
data class Person(var name: String, var age: Int = 0, var city: String = "")
val adam = Person("Adam").apply {
age = 32
city = "London"
}
println(adam) // Person(name=Adam, age=32, city=London)
fun getStringLength(obj: Any): Int? {
if (obj is String) {
return obj.length
}
return null
}
I used Gradle because scripting with Gradle using Kotlin is a lot easier than Maven