Skip to content

Examples

Let’s start with the first example file and after that get into some PKL language features you could need. For the full docs just go to the PKL language reference.

Your first recipe

Create the recipe

Create config/pkl4mc/data/minecraft/recipes/stick_frenzy.pkl and put the following code in it:

config/pkl4mc/data/minecraft/recipes/shaped.pkl
amends "@pkl4mc/recipes.pkl"

shaped {
  new {
    name = "chest_alternative"
    category = "misc"
    key {
      ["L"] { tag = "minecraft:logs" }
    }
    pattern { "LLL"; "L L"; "LLL" }
    output { item = "minecraft:chest"; count = 8 }
  }
}

Load

Run /pkl reload in game and try out your recipe at the crafting table.

Break it on purpose

Change pattern to patern and reload the game. After that run the /pkl errors command. You will get the file, line, column and error message. With IDE setup done, the same error shows as a red squiggle before you ever can do /pkl reload.

Amending a schema

amends means “this file fills in the fields of that schema”:

amends "@pkl4mc/recipes.pkl"

The schema defines the fields, types and JSON rendering. The schema catalog lists everything you can amend.

Objects and collections

// object: braces, fields separated by newlines or ;
output { item = "minecraft:stick"; count = 4 }

// listing: elements one per line
pattern {
  "P P"
  " P "
  "P P"
}


// listing of objects: each element wrapped in new { }
shaped {
  new { name = "recipe_one" }
  new { name = "recipe_two" }
}

// mapping: string keys in brackets
key {
  ["S"] { item = "minecraft:stick" }
}

Variables and reuse

local declares a file-private value (like a var kinda):

amends "@pkl4mc/recipes.pkl"

local steel = "gtceu:steel_ingot"

smelting {
  new {
    name = "steel_from_iron"
    input { item = "minecraft:iron_ingot" }
    output { item = steel }
    experience = 0.7
    cookingTime = 200
  }
}

String interpolation uses \(expr):

local mod = "mypack"
name = "\(mod)_steel_recipe"

Mod conditions

The loaded Forge mod list is available to every file. Branch on mod presence:

local loadedMods = read("prop:pkl4mc.loadedMods").split(",")
local hasProjectE = loadedMods.contains("projecte")

shapeless {
  new {
    name = "conditional_example"
    inputs {
      new { item = if (hasProjectE) "minecraft:diamond" else "minecraft:coal" }
    }
    output { item = "minecraft:emerald" }
  }
}

when inserts elements conditionally inside collections:

inputs {
  new { item = "minecraft:coal" }
  when (hasProjectE) {
    new { item = "minecraft:diamond" }
  }
}

Types catch your typos

Schema fields are typed. A misspelled field or count = "four" is an evaluation error and will show up in the editor. After running /pkl dump the item id strings validate against your modpack’s real registries too.

Raw modules

The schemas already declare this for you, but when you write a module from scratch, without amending a schema you should declare the JSON output yourself:

output { renderer = new JsonRenderer {} }