Skip to content

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.

A final local is val, and any other local is var. The type is worked out for you, and there are no semicolons.

Java
final String realm = "Lumbridge";
int online = 0;
online += 1;
Riftlang
val realm = "Lumbridge"
var online = 0
online += 1

A type holds null only when it ends in ?. ?. calls a method only when the value isn’t null, and ?: gives a fallback.

Java
String clan = null;
String shout = clan != null ? clan.toUpperCase() : "No clan";
Riftlang
var clan: String? = null
val shout = clan?.uppercase() ?: "No clan"

A function is fun, outside any class, with the return type after the brackets. A default argument replaces an overload.

Java
static int reward(int level) {
return reward(level, 0);
}
static int reward(int level, int bonus) {
return level * 10 + bonus;
}
Riftlang
fun reward(level: Int, bonus: Int = 0): Int {
return level * 10 + bonus
}

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.

Java
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);
Riftlang
class Shop(val name: String, var stock: Int) {
fun sell() {
stock -= 1
}
}
val store = Shop("General store", 40)

A record is a data class. copy makes a changed copy.

Java
record Drop(String item, int amount) {}
Drop drop = new Drop("coins", 50);
Drop more = new Drop(drop.item(), 100);
Riftlang
data class Drop(val item: String, val amount: Int)
val drop = Drop("coins", 50)
val more = drop.copy(amount = 100)

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.

Java
List<Integer> prices = List.of(4, 50, 20);
List<Integer> doubled = prices.stream()
.filter(p -> p < 30)
.map(p -> p * 2)
.toList();
Riftlang
val prices = listOf(4, 50, 20)
val doubled = prices.filter { it < 30 }.map { it * 2 }

Every string is a template. $name puts in a value, and ${} an expression.

Java
String item = "lobster";
int price = 25;
String line = String.format("A %s costs %d coins.", item, price * 2);
Riftlang
val item = "lobster"
val price = 25
val line = "A $item costs ${price * 2} coins."

switch is when, which gives back a value and never falls through.

Java
String option = "Trade";
String reply = switch (option) {
case "Talk-to" -> "Hello.";
case "Trade" -> "Take a look.";
default -> "No.";
};
Riftlang
val option = "Trade"
val reply = when (option) {
"Talk-to" -> "Hello."
"Trade" -> "Take a look."
else -> "No."
}

== calls equals, so it compares by value. === checks that both are the same object, as Java’s == does.

Java
String a = "coins";
String b = "coins";
boolean same = a.equals(b);
Riftlang
val a = "coins"
val b = "coins"
val same = a == b

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.

Java
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() + ".");
}
}
scripts/lumbridge/welcome.rift
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.") }

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.

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().

Java
import java.util.concurrent.ConcurrentHashMap;
ConcurrentHashMap<String, Long> lastSeen = new ConcurrentHashMap<>();
lastSeen.put("Hans", System.currentTimeMillis());
Riftlang
import java.util.concurrent.ConcurrentHashMap
val lastSeen = ConcurrentHashMap<String, Long>()
on_login { player ->
lastSeen["$player"] = System.currentTimeMillis()
}