Skip to content

Introduction

Cliven Mitchell edited this page Feb 3, 2019 · 14 revisions

How does it work?

The plugin model design is based on the publish-subscribe pattern. In other words, it works by sending objects filled with data (an Event) to listeners that are declared in scripts.

import api.predef.*

on(ServerLaunchEvent::class) { // Listens for ServerLaunchEvent
    ...
}
on(ItemOnItemEvent::class) { // Listens for ItemOnItemEvent
    ...
}
on(PositionChangeEvent::class) { // Listens for PositionChangeEvent
    ...
}

button(...) { // Matcher that listens for ButtonEvent
    ... 
}
npc1(...) { // Matcher that listens for NpcFirstClickEvent
    ... 
}
cmd(...) { // Matcher that listens for CommandEvent
    ...
}

These listeners can either choose to terminate the event's traversal or pass it along to the next listener.

import api.predef.*

on(ItemOnItemEvent::class) { // Will continue on to other listeners
    ...
}

on(ItemOnItemEvent::class) { // Terminated, will NOT continue on to other listeners
    ...
    terminate()
}

You will learn more about event interception functions in a later section.

Learning Kotlin

Kotlin is a programming language within the Java ecosystem. It should be easy to pick up, especially if you're familiar with Java or Scala. Here are a few great resources to get you started

A lot of developers are turned off by the idea of having to learn a new language. Kotlin is such an expressive language that someone with very little programming knowledge can write simple scripts. For example...

// A command that sends player count!
import predef.api.*

cmd("players", RIGHTS_PLAYER) {
    val count = world.players.size
    plr.sendMessage("There are $count players online!")
}

One of the best ways to get started is to simply look at existing plugins. Give it a try!

Why Kotlin?

A secondary language for plugins should be expressive, compact, and easy to understand. The language should also be compatible with and conceptually similar to Java (the primary language).

Kotlin fits that bill more than any other popular JVM language at the moment.


Continue to next section.