Getting Started with Ruby
Getting Started with Ruby
See Ruby Lang Organization to Ruby Programming Language
Creating a Runnable Script
Runnable Scripts can be execute via command (scripter execute {script}) or by another scripts. Runnable Script Example:
Interop = InteropManager Object arg = Any argument passed to script
def Run(InteropManager interop, Object arg) interop.getServer().sendMessage("Hello World") end
When the Scripter runs this script an message "Hello World" its been showed in server console.
Calling an Runnable Script
You can call a script via another script or via command:
interop.Run("myscript.rb", "ARGUMENT")
Command:
/scripter execute myscript.rb
(In Version 1.2 you cant pass arguments via command)
Creating a Event Handler
Steps to create a event handler:
First define the event to handler (See all events Here) In this example I will use the BlockBreakEvent, to display a message to the player:
Code:
def onBlockBreak(interop, eventArgs) # Show an Message to Player eventArgs.getPlayer().sendMessage("You breaked an block") end
Event Set XML:
<EventSet> <Name>on-blockbreak</Name> <Method>onBlockBreak</Method> <Script>myexemple.rb</Script> </EventSet>
Creating an Event Condition
In this example we will create a conditional event for the example above
Code:
def canDoBlockBreakEvent(interop, conditionalEventArgs) if conditionalEventArgs.getPlayer().getName().equals("Steve") == true conditionalEventArgs.setDoEvent(true) else conditionalEventArgs.setDoEvent(false) end end
Event Set XML
<EventSet> <Name>on-blockbreak</Name> <Method>onBlockBreak</Method> <Script>myexemple.rb</Script> <Condition> <Name>on-blockbreak</Name> <Method>canDoBlockBreakEvent</Method> <Use>true</Use> <Script>myexemple.rb</Script> </Condition> </EventSet>
With this conditional the Script of the BlockBreakEvent will only be triggered if the player's name is Steve.
Changing Event Map in Ruby Script
require "java" import java.io def Run(interop, arg) interop.getEventManager().Load(File.new("myfile.xml")) end
Comments