Riftlang for Java programmers
Coming from Java, the big differences are: types go after names, null is allowed only where a type says so, and a script has no class around it. Riftlang uses the JDK and any Java library.
final and var
Section titled “final and var”A final local is val, and any other local is var. The type is worked out for you, and there are no semicolons.
final String realm = "Lumbridge";int online = 0;online += 1;val realm = "Lumbridge"var online = 0online += 1A type holds null only when it ends in ?. ?. calls a method only when the value isn’t null, and ?: gives a fallback.
String clan = null;String shout = clan != null ? clan.toUpperCase() : "No clan";var clan: String? = nullval shout = clan?.uppercase() ?: "No clan"Methods and default arguments
Section titled “Methods and default arguments”A function is fun, outside any class, with the return type after the brackets. A default argument replaces an overload.
static int reward(int level) { return reward(level, 0);}
static int reward(int level, int bonus) { return level * 10 + bonus;}fun reward(level: Int, bonus: Int = 0): Int { return level * 10 + bonus}Classes
Section titled “Classes”A class’s constructor parameters are in its header, and val or var there makes them properties. There is no new, and no getters or setters to write.
class Shop { private final String name; private int stock;
Shop(String name, int stock) { this.name = name; this.stock = stock; }
String getName() { return name; }
void sell() { stock -= 1; }}
Shop store = new Shop("General store", 40);class Shop(val name: String, var stock: Int) { fun sell() { stock -= 1 }}
val store = Shop("General store", 40)Records and data classes
Section titled “Records and data classes”A record is a data class. copy makes a changed copy.
record Drop(String item, int amount) {}
Drop drop = new Drop("coins", 50);Drop more = new Drop(drop.item(), 100);data class Drop(val item: String, val amount: Int)
val drop = Drop("coins", 50)val more = drop.copy(amount = 100)Lambdas and streams
Section titled “Lambdas and streams”A lambda is written in { }, and one with a single parameter can call it it. Lists have filter, map and the rest without a stream.
List<Integer> prices = List.of(4, 50, 20);List<Integer> doubled = prices.stream() .filter(p -> p < 30) .map(p -> p * 2) .toList();val prices = listOf(4, 50, 20)val doubled = prices.filter { it < 30 }.map { it * 2 }String templates
Section titled “String templates”Every string is a template. $name puts in a value, and ${} an expression.
String item = "lobster";int price = 25;String line = String.format("A %s costs %d coins.", item, price * 2);val item = "lobster"val price = 25val line = "A $item costs ${price * 2} coins."switch
Section titled “switch”switch is when, which gives back a value and never falls through.
String option = "Trade";String reply = switch (option) { case "Talk-to" -> "Hello."; case "Trade" -> "Take a look."; default -> "No.";};val option = "Trade"val reply = when (option) { "Talk-to" -> "Hello." "Trade" -> "Take a look." else -> "No."}Equality
Section titled “Equality”== calls equals, so it compares by value. === checks that both are the same object, as Java’s == does.
String a = "coins";String b = "coins";boolean same = a.equals(b);val a = "coins"val b = "coins"val same = a == bStatements at the top level
Section titled “Statements at the top level”Java needs a class with a main, under a package line with its own imports. A script has none of that: no class, no main and no package line, and common names are imported for you. Its statements run top to bottom when the script loads, and hooks bind what runs later.
package lumbridge;
import java.time.LocalTime;
public class Welcome { public static void main(String[] args) { String town = "Lumbridge"; System.out.println("The " + town + " welcome is ready at " + LocalTime.now() + "."); }}import java.time.LocalTime
val town = "Lumbridge"
fun greeting() = if (LocalTime.now().hour < 12) "Good morning" else "Good evening"
Logger.info("The $town welcome is ready.")
on_login { player -> player.message("${greeting()}, welcome to $town.") }The folder is the package
Section titled “The folder is the package”scripts/lumbridge/welcome.rift is in package lumbridge. Java names the package in a package line, and Riftlang takes it from the folder. See Packages and imports.
Java libraries and the JDK
Section titled “Java libraries and the JDK”A script imports a Java class as Java does, and calls it with no new. A getter reads as a property: LocalTime.now().hour calls getHour().
import java.util.concurrent.ConcurrentHashMap;
ConcurrentHashMap<String, Long> lastSeen = new ConcurrentHashMap<>();lastSeen.put("Hans", System.currentTimeMillis());import java.util.concurrent.ConcurrentHashMap
val lastSeen = ConcurrentHashMap<String, Long>()
on_login { player -> lastSeen["$player"] = System.currentTimeMillis()}