-
Notifications
You must be signed in to change notification settings - Fork 32
First Purugin
Every first program we write is a hello world program. Let's do the same thing with Purugin. You can either type this in from this page or copy the source into your plugins directory.
So create a file in your plugins directory called hello.rb and start to fill it in
class HelloWorldPlugin
include Purugin::Plugin
description 'HelloWorld', 0.1
The first line represents you are making a plain-old Ruby class. This is really just an normal class until line two where we include Purugin::Plugin. This line will let Purugin know this is going to be a plugin. The third line is the metadata for this plugin. 'HelloWorld' and 0.1 will be printed to the console when the plugin is enabled of disabled.
All plugins in both Purugin and Bukkit (<-- API Purugin builds on top of) have the following life-cycle:
- on_load: Plugin has been loaded but not enabled yet. All plugins get loaded before enabled so plugin dependencies can be resolved when on_enable happens.
- on_enable: Plugin is starting. This is where you put most of your magic: register events, commands, background tasks, etc...
- on_disable: Plugin is being stopped. This is where you close out any open resources you might have (e.g. open database connections, stale data which should be saved to disk, etc...)
Each of these life-cycle states are represented by methods of the same name. Let's make on_enable and on_disable:
def on_enable
puts "hello world"
end
def on_disable
puts "goodbye world"
end
Ok based on what was described above we should see 'hello world' printed to the console when the plugin is first started and we should see 'goodbye world' when we shutdown the game.
(if you are typing this in as you follow along remember to close out your class with an 'end')
end
Let's try it:
% run.sh
174 recipes
27 achievements
10:05:33 [INFO] Starting minecraft server version 1.0.0
10:05:33 [INFO] Loading properties
10:05:33 [INFO] Starting Minecraft server on *:25565
10:05:33 [INFO] This server is running Craftbukkit version git-Bukkit-1.8.1-R4-40-gb85b9b2-b1519jnks (MC: 1.0.0) (Implementing API version 1.0.0-R1-SNAPSHOT)
10:05:35 [INFO] Preparing level "world"
10:05:35 [INFO] Default game type: 0
10:05:35 [INFO] Preparing start region for level 0 (Seed: 6705142625061559411)
10:05:35 [INFO] Preparing start region for level 1 (Seed: 6705142625061559411)
10:05:36 [INFO] Preparing start region for level 2 (Seed: 6705142625061559411)
10:05:36 [INFO] Preparing spawn area: 69%
10:05:36 [INFO] hello world
10:05:36 [INFO] [HelloWorld] version 0.4 ENABLED
Notice the 'hello world above! Let's 'stop' the server:
>stop
10:05:42 [INFO] CONSOLE: Stopping the server..
10:05:42 [INFO] goodbye world
10:05:42 [INFO] [HelloWorld] version 0.4 DISABLED
Fun...We did not do a lot; but with this little we know the basics of how to make a plugin. Adding Commands, registering Events, and executing background Tasks will round out the major features. What we do in each of those feature will be where we start to have real fun.