# Lists, maps, text methods and the prelude
How `x[k]` and `.length()` / `.sort()` / `.get()` pick the list, map or text action; every list / map / text / number name; `l.filter` / `l.map` / `l.each`...; nested items; cells. Open when the code keeps lists or maps.

## Lists and maps: `l[i]`, `m[k]`
- Lists `[1, 2, 3]`; read `l[0]`, write `l[0] = 5`, `l[i] += 1`. Maps: create first
  `m = variable::create_map_from_values([], [])` (save/game: `m ??= ...`), then `m["k"] = 5`, `var v = m["k"]`
  (missing key / index out of range → 0, also for `== 0`), `m[k] += 1`. Map literal: `{ "k": 1 }`. `m[k]` / `m[k] = v` compile to the cheap single-key actions
  (`get_map_value_single` ≈1.2, `put_map_value` ≈1.8 — a new key is added; on a non-map both are a server error).
  `x[k]` is a MAP when code above made or used x as a map (create_map*, sort_any_map, `x["k"] = ...`, x passed
  as the `map` of any variable:: action) or k is a text (`"k"`, `"%player%"`, a var set to a text, `value::uuid`);
  or x is a parameter typed `m: map`;
  otherwise a LIST (k a number; nothing known about x and k: a list plus a warning). A text index on a known list,
  or a key that is neither number nor text on an unknown x, is a compile error — create the map first
  (or use `m.get(k)` / `m.set(k, v)` on a map created above).
- Nested: `var c = grid[y][x]`, `grid[y][x] = 1`, `m["row"][0] += 1`, `l[2]["name"]`, `m.keys()[0]`. Lists
  and maps are values (copied on assignment): the inner one is read into a temp, changed, and written back.
  An in-place method on an item as its own statement is written back too: `grid[y].append(5)`,
  `l[0].increment(1)`, `l[i].sort_any_list()`; used as a value (`var s = l[0].sort_any_list()`) it is a copy.
  The result of an action or function is a copy: `m.keys()[0] = 5` / `f()[0] = 5` is an error —
  `var keys = m.keys(); keys[0] = 5`. For game/save the "code above" is file order: create the map in
  an event placed ABOVE the handlers that index it (otherwise a warning).
## JMVCC names (data/aliases.json; same JSON as the server name)
`.sort/.reverse/.shuffle/.length/.contains/.index_of/.chunk/.slice/.get/.set/.remove` pick the list, map or text
action by what the variable holds (known like for `x[k]`: created / used above in the code, also as a result:
`var s = parts.join("-")` is text, `var t = value::display_name` is text; literals and `value::` by type).
`x.get("k")` / `x.set("k", v)` with a TEXT key are the map actions, like `x["k"]`. A function parameter gets its
kind from the type `function f(x: map)` or from ALL calls of the function (→ topic functions-editions); a function result
(`var r = mk(); r.length()`) from its `return`s. Still unknown (value from an event, no calls) → the list action
plus a `kind-guessed` warning naming the exact map / text action; a known wrong kind (`m.contains(k)`,
`t.sort()`, `l.clear()`, `l.has_key(0)`, `m.append(5)`, `t.keys()`) is a `kind-mismatch` error with the
replacement. `variable::sort(m)` = `m.sort()` (the receiver is the first argument, changed in place);
`variable::sort(map = m)` (receiver named) picks by the name.
- List helpers (prelude: compiled to the loop you would write, nothing else in the module; your own function with the
  same name wins): `l.filter(x -> x > 5)`, `l.map(x -> x * 2)`, `l.each { x -> ... }`, `if (l.any(x -> x > 5))`,
  `l.all(...)`, `l.count(...)`, `l.find(...)` (first match, 0 if none), `l.find_index(...)` (-1 if none), `l.sum()`,
  `alive = alive.keep_in(value::all_players_names)` (keep those also in the other list — players still online).
  New lists grow by `insert(length)` (≈2.4, not append ≈6.8); `keep_in` checks with `index_of` (≈2.6).
- Lists: `l.append(x)` (also `push`; a list value goes in as ONE element: [1,2].append([3,4]) → [1,2,[3,4]]),
  `l.extend(other)`, `l.insert(i, x)`, `l.remove(x)` (by value), `l.remove_at(i)`, `l.get(i, default)`,
  `l.set(i, x)`, `l.index_of(x)` (-1 if none), `if (l.contains(x))`, `if (l.is_empty())`, `l.length()` (also
  `size`), `l.sort()`, `l.reverse()`, `l.shuffle()`, `l.unique()`, `l.flatten()`, `l.chunk(n)`, `l.join(", ")`,
  `l.random()` (a random ELEMENT), `l.slice(a, b)` (from 0, end excluded like JS; outside the list, a > b or
  negative is a server error `index_out_of_bounds`).
- Maps: `m.get(k, default)`, `m.set(k, v)` (= `m[k] = v`), `m.remove(k)`, `if (m.has_key(k))`, `m.keys()`,
  `m.values()`, `m.length()` (also `size`), `m.sort(...)`, `m.merge(other)` (same key: `other` wins), `m.clear()`.
- Text: `t.length()`, `if (t.contains("a"))`, `if (t.starts_with("a"))` / `ends_with`, `t.index_of("a")` (from 0,
  -1 if none), `t.split(",")`, `t.replace("a", "b")`, `t.regex_replace(...)`, `t.strip()`, `t.repeat(3)`,
  `t.slice(a, b)` (from 0, end excluded; out of range is clamped: "abcdef".slice(2, 9) → "cdef"), `t.chunk(n)`,
  `t.word_wrap(n)`, `t.parse_int()` (integers only), `t.get_char_at(i)` (from 0).
- `variable::text_matches(..., regular_expressions = true)`: the flag does nothing on the server (server-check-38, 2026-11-09: of 12 patterns only the literal text matched). For a regex check use `t.regex_replace("[0-9.]", "") == ""` (regex_replace works).
- Numbers / vectors: `x.abs()`, `x.wrap(lo, hi)`, `a.lerp(b, t)`, `variable::rgb(r, g, b)` / `hsl` / `hsb`, `v.length()`
  (needs v known as a vector), `v.dot(w)`, `v.cross(w)`, `v.reflect(w)`, `a.distance(b)` (locations).
- Players / entities / world: `player::give_item`, `remove_item`, `give_potion_effect` (also `add_potion_effect`,
  entity too), `has_permission`, `set_hunger`, `stop_using_item`, `finish_using_item`, `set_view_distance`,
  `set_keep_inventory`, `menu_slot_equals`, `set_menu_id`; `entity::set_health`, `is_on_ground`, `is_near`,
  `set_equipment`, `get_equipment`, `set_armor`; `world::spawn_entity`, `set_time`, `set_weather`, `set_gamerule`,
  `set_difficulty`, `set_simulation_distance`, `set_redstone_power`, `set_block_growth`, `set_container_items`.
- Code / loops / selection: `code::end_thread()`, `measure_time(t) { }`, `catch_error(e) { }` (e needs no `var`; easier: try / catch), `throw_error(id, msg)`,
  `label("x") { }` (body runs), `do_not_run() { }` (body skipped), `isolated_selection(mode) { }` (selection
  restored after), `request_transaction(...)`; `repeat::times(n) { i -> }`, `repeat::for_each_in_map(m) { k, v -> }`;
  `select::player_by_condition(...)` / `entity_by_condition` / `add_*_by_condition` / `filter_by_condition`.
Tempted to write X → write Y: `l.add(x)` → `l.append(x)` (`add` is a number sum); `t.trim()` → `t.strip()` or
`t.slice(a, b)`; `t.to_number()` → `t.parse_int()`; `x.count()` → `x.length()`; `repeat::range(0, n)` →
`repeat::on_range(0, n - 1)` (end INCLUDED); `variable::random([1, 10])` is "1 or 10" — a number in range is
`variable::random_in_range(1, 10)`.

## Map loops
- In `for (k, v in m)` (and `repeat::for_each_map_entry(m) { k, v -> }`) k is text when m was built with text keys
  (`{ "a": 1 }`, `create_map_from_values(["a"], ...)`, `m["b"] = v`): `k.length()` is the text length, no guess warning.

## Newer server actions and cells
Newer server actions (data 2026-11-27): dialogs (→ topic text-menus) (`world::create_dialog(id, title)`, `world::add_dialog_button`,
`world::add_text_box_dialog_input`, ..., `player::show_dialog_menu(id)`, `var t = variable::get_dialog_text(input_id)`),
several list elements in one action (`variable::get_list_value_multiple(list, [a, b], [1, 3], default)`),
`variable::raycast_result(...)` (hit point, block, face, entity uuid at once), `var v = variable::get_random_vector(true)`.
Cells keep a group of variables across world restarts and load only on demand:
```js
var st = variable::cell_load(cell("records"), [best])   // st: "success" | "active_lock" | "throttled" | "internal"
best = best + 1
st = variable::cell_save(cell("records"), [best])       // cell must be loaded by this world
st = variable::cell_unload(cell("records"), [best])     // save and release the lock
if (variable::cell_exists(cell("records"))) { }         // also cell_loaded; cell("x", global = true, storage = "name")
```
A cell argument takes `cell("name")`, not text. The JSON shape of a cell is not verified on the server yet
(warning `cell-format-unverified`); a cell from an exported module decompiles to `cell_raw("{...}")`, written as is.

## Verified list / map facts (also in topic server-facts)
- `var b = a` for a list COPIES it: `b[0] = "z"` does not change `a`.
- `sort_any_map` keeps the sorted order: `get_map_key_by_index` and `for_each` follow it.
- A missing key / index out of range in an explicit `get_map_value` / `get_map_value_single` / `get_list_value`
  call without `default_value` gives null: it prints "0" and math reads 0, but `== 0` and `in 0..0` are FALSE
  (verified). `m[k]` / `l[i]` pass `default_value = 0` for you; in explicit calls pass `default_value = 0`.
- `repeat::on_range(1, n)` reads `n` once; modifying the list you `for_each_in_list` over STOPS the loop after the
  current step (no error) — collect changes in another list (the compiler warns: `loop-list-modified`). `stop_repeat` / `skip_iteration` act immediately.
- `variable::equals(x, [5, 6], compare_type = "ANY")` = x is one of; `variable::not_equals(x, [a, b])` = x equals none;
  `number_in_range` NOT_INCLUDE (min,max) / INCLUDE_FIRST [min,max) / INCLUDE_LAST (min,max] / INCLUDE_ALL [min,max]; `multiple_equals` compares
  pairwise; `list_value_equals(l, i, [v])` — index from 0, out of range = false; `list_is_empty([l])` works.
- `l[9]` past the end gives 0 (no error); `variable::list_is_empty([x])` is true for an unset variable and an empty
  map, false for a text.
- `get_list_length` / `l[i]` on a non-list is a server error. `%length(x)`: list size, -1 for an unset variable, the
  text LENGTH for a text, the size of a map.
- `l[-1]` and `l[9]` past the end give 0; `list_value_equals(l, -1, [v])` is false.
- `list.append(x)` on a never-set variable does NOTHING (no error, still unset) — create the list first
  (`l ??= []` or `var l = []`); `l.remove(x)` on it is a server error.
- An empty list literal `[]` fits any list parameter (= argument left empty).
