Skip to content

TemplateString

youyihj edited this page Aug 30, 2024 · 6 revisions

Template String

@since 1.18.0

Template string is an enhanced string. It can be embedded with several expressions.

Usage

Wrap backquote (`) to define a template string, and use ${} to embed a expression

val name as string = "world";
print(`hello, ${name}!`); // hello, world!

Most expressions are allowed, including variables, getters, functions, but nested template string is not supported currently.

import crafttweaker.event.EntityLivingAttackedEvent;

events.onEntityLivingAttacked(function(event as EntityLivingAttackedEvent) {
    val entity = event.entity;
    print(`entity ${typeof(entity)} ${entity.x} ${entity.y} ${entity.z}`);
});

Template Bracket Handler

Similar ability is also implemented in bracket handlers. You don't need itemUtils or IGame functions anymore.

  • Only CraftTweaker and ContentTweaker standard bracket handlers are supported.
  • About item bracket handler, item: prefix can not be omitted.
import crafttweaker.item.IItemStack;

function getIngot(type as string) as IItemStack {
    return <ore:ingot${type}>.firstItem;
}

print(getIngot("Iron").commandString); // <item:minecraft:iron_ingot>
print(getIngot("Gold").commandString); // <item:minecraft:gold_ingot>

// prints display name of all wools
for i in 0 .. 16 {
    // item: prefix can't be removed
    print(<item:minecraft:wool:${i}>.displayName);
}
Clone this wiki locally