Skip to content

Simple Plugin Example

Cliven Mitchell edited this page Feb 4, 2019 · 10 revisions

Let's use what we've just learned to write a simple plugin. The plugin we're going to write will spawn NPC 1 at (3200, 3200) and make them give the player gold coins after some dialogue.

All of this can be done nicely in one small script, so let's create one called niceMan.kts in package world.npcs.niceMan. The first thing that all scripts require is the API import, so let's do that

import api.predef.*

We want the item they give us to be easily modifiable, so lets store it in a property

import api.predef.*
import io.luna.game.model.item.Item

val giftItem = Item(995, 2_000_000) // 2M coins

Now lets spawn the NPC. We can do this by intercepting ServerLaunchEvent and using a World extension function to spawn it.

import api.predef.*
import io.luna.game.event.impl.ServerLaunchEvent
import io.luna.game.model.item.Item

val giftItem = Item(995, 2_000_000)

// Spawn NPC on launch
on(ServerLaunchEvent::class) { world.spawnNpc(1, 3200, 3200) }

Now let's invoke the second NPC click (for "Talk-to") matcher function so we can talk.

import api.predef.*
import io.luna.game.event.impl.ServerLaunchEvent
import io.luna.game.model.item.Item

val giftItem = Item(995, 2_000_000)

// Add second click event for NPC(1)
npc2(1) { ... }

on(ServerLaunchEvent::class) { world.spawnNpc(1, 3200, 3200) }

... and add in the conversation. We can do that through the DialogueQueueBuilder and its functions

import api.predef.*
import io.luna.game.event.impl.ServerLaunchEvent
import io.luna.game.model.item.Item

val giftItem = Item(995, 2_000_000)

npc2(1) {
    // And add in the dialogue.
    plr.newDialogue()
        .npc(id, "Hello! How're you doing?")
        .player("Not too bad, just learning some scripting.")
        .npc(id, "And how's that going, pretty well?")
        .player("Yeah! I'm learning a lot.")
        .npc(id, "Well here's a little gift for all your hard work.")
        .give(giftItem)
        .player("Thanks! Guess I'll be going now.")
        .npc(id, "Okay, take care!")
        .open()
}

on(ServerLaunchEvent::class) { world.spawnNpc(1, 3200, 3200) }

Which completes the scripting portion of writing a new plugin. Now we need to make a plugin.toml file in the same directory as niceMan.kts containing this

[metadata]
name = "Nice man"
description = "A plugin that enables 'Man' NPCs to give free gifts of 2M coins."
version = "your.luna.version"
authors = [
  "your_name"
]

And finally, replace your_name and your.luna.version with your own values. Build and run the server to test it. Congratulations, you've just developed and installed your very first plugin!


Go back to home.