[tutorial] Write plugins in Scala (the better Java)!

Discussion in 'Resources' started by jtjj222, Nov 25, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    jtjj222

    I recently picked up a copy of "Seven languages in seven weeks" and can't put it down! This lead me to try making a bukkit plugin in scala. For those of you who don't know, Scala is a jvm based programming language that provides plenty of innovative features. One can call java code from scala and vice-versa. This tutorial will describe how to use the scala eclipse plugin, how to make a bukkit plugin in scala, and how to export it so that it works with all of the required libraries.

    Part one: Setting up the IDE plugin:
    Scala provides plugins for many ide's, but seeing as I use eclipse, I can only provide instructions for that :D
    Installing the Scala eclipse plugin is pretty straightforward. Go to the scala eclipse ide website, and find the page for your version of eclipse. Depending on your version of eclipse, either click "Get it now" (3.7 and below) or "Walk on the wild side" (3.8+).
    Capture.PNG
    From eclipse, click 'Help' -> 'Install new software', and add the update site from the scala page to your list of update sites. Check off 'Scala ide for Eclipse' and click next. Follow the on-screen instructions to install the plugin.
    Capture.PNG

    Restart your ide and you should be finished!

    Part two: Set up a project for working with bukkit
    Open a new workspace (optional) and click new->Scala project
    Capture.PNG
    Follow the onscreen wizard. Create a new package, and inside that a new scala class. In the root of the project, create your plugin.yml as normal. Add bukkit.jar to your build path as usual. Add an import to org.bukkit.plugin.java.JavaPlugin in the main class file, and add "extends JavaPlugin" your class declaration, just like in java. Everything is pretty much the same from here on in. Your onEnable method looks like this:
    Code:scala
    1.  
    2. override def onEnable() {...code...}
    3.  

    and your onCommand would look like this:
    Code:scala
    1.  
    2. override def onCommand(sender : CommandSender, cmd : Command, label : String, args : Array[String]) : Boolean = {...code...}
    3.  

    You can call java/bukkit methods just like you normally would with scala. You can see my example here (it opens a villager trading screen), and the equivalent in java.

    Part 3: Export it!
    This is the hardest part. Bukkit doesn't have the Scala libraries, so you will need to load them some how. To solve this, I wrote a plugin called ScalaLoader. You can download it here. Simply add a hard dependency to it in your plugin.yml, and make sure that your users install it. Then, click file->export->java->jar file, and export it as you usually would. The plugin I wrote will load all of the libraries you need!

    All I did was build a fat jar containing a bare-bones plugin class and a plugin.yml, along with the entire scala library. While you could do this yourself, only one copy of the scala runtime can be loaded at a time without conflicts, so you will have to use ScalaLoader to stop your plugin from conflicting with other plugins written in scala. You can include a copy of ScalaLoader with your plugin, but I would prefer you direct your users to the bukkit dev page.

    Let me know below if there are any errors in this tutorial, or if I overlooked anything. Pm me or post below if there is another jvm-based language that you want a tutorial for!
    Happy coding! :D
     
    bobacadodl, JazzaG and Uniclaw like this.
  2. Offline

    Uniclaw

    Wow, awesome Tutorial :D ! But what is better in Scala than in Java?
     
  3. Offline

    LaxWasHere

    Word of the day "assimilation"
     
    jtjj222 likes this.
  4. Offline

    jtjj222

    I was too lazy to write it myself :D
     
  5. Offline

    Icyene

    The only backdraw with Scala is that it requires its own (massive) runtime. That will put off alot of potential utilisers, which in my opinion is a shame, because Scala truly is the future of Java. In the terms of runtime size, AspectJ would still have to win. It weaves the source files with AspectJ ASM-based code, meaning that all a plugin needs to run is the AspectJ runtime (which is only ~150kb in size).

    That said, imagine this:
    • The Java language dies (its doomed to do so anyway; the bad design decisions are killing it)
    • The JVM (arguably the most advanced VM) is maintained to be used by other programming languages built on it (JRuby, Scala etc).
    • Oracle sells Java out of bankruptcy to a better home (I mean seriously, James Gosling, the creator of Java, quit Oracle because of their behavior).
    • That 'better home' gets the developers of Scala, Groovy, and AspectJ together to design a programming language forged of the three.
    • The JVM adds support for that language.
    • The developers of this better language add an AOT (ahead-of-time) compilation.

    What could be better? Oracle dies, Java is replaced by a better programming language. It seems very merry, and truth to be told, doesn't seem too unlikely: 55% of people said they hate Java.

    I personally do not like Java amply, its just that for me its the best choice. I dislike Python's whitespace greatly, C++'s platform dependence, etc. Scala in itself is wonderful and the programming language I would use it for anything I can (the Python-style console code exectuting (scala -e "somecode") is wonderful for prototyping). And of course, operator overloading is a pretty big plus (literally). I mean, who wants to add BigIntegers et al manually?

    tl;dr
    Scala = arguably the most multi-purpose programming language to date; hasn't been adopted by the programming community enough; has a massive runtime.
     
    jtjj222 likes this.
  6. Offline

    jtjj222

    How would one go about using that? :D
     
  7. Offline

    Icyene

Thread Status:
Not open for further replies.

Share This Page