Fabric Carpet Documentation

repository·master·Indexed 24 days ago

https://github.com/gnembon/fabric-carpet

A technical utility mod for Minecraft (Fabric loader) providing deep control over game mechanics, performance monitoring, and time manipulation. Includes documentation on core features like /tick warp and /log, the scarpet scripting language API for rendering particles, shapes, and markers, NBT data handling, and programmatic datapack creation.

Tokens
66.5K
Snippets
81
Records
335
Agent score
82%

What's inside Fabric Carpet

  1. Overview of Fabric Carpet features

    master

    Fabric Carpet is a technical mod for vanilla Minecraft that provides fine-grained control over game mechanics. Key capabilities include:

    • Time Manipulation: Use /tick warp to accelerate game time (e.g., testing farms) as fast as your hardware allows.
    • Technical Monitoring:
      • Use /log to see live updates for server mobcap, TPS, and other metrics.
      • Use hopperCounters to get a detailed breakdown of items produced by hoppers.
    • Mechanic Tweaks:
      • movableBlockEntities: Allows pistons to push block entities like chests.
      • Various bug fixes (e.g., leadfix, portalsuffocationfix, unloadedentityfix).
  2. Scarpet Language Documentation Overview

    master
    Scarpet is a scripting language designed for the Carpet mod ecosystem. Documentation is organized into two main sections: the Language Specification, which covers core programming constructs (variables, operators, math, loops, functions, and containers), and the Minecraft API, which provides the interface for interacting with the Minecraft world (blocks, entities, inventories, events, and scoreboards).
  3. What is Scarpet?

    master
    Scarpet (also known as Carpet Script) is a programming language designed to run within Minecraft. It allows developers to write custom programs that interact with the Minecraft world. The language is expression-based, meaning programs are treated similarly to mathematical expressions. It consists of constants, operators, variables, and functions.
  4. Query and modify entities in Scarpet

    master

    In Scarpet, entities are interacted with using two primary functions:

    • query(e, 'property'): Retrieves information about an entity e.
    • modify(e, 'property', value): Alters a property of entity e.

    Syntax Alias: You can use the ~ operator as a shorthand for query. This is particularly useful for cleaner code when a statement has no extra arguments.

    // Shorthand examples
    query(p, 'name') <=> p ~ 'name'
    query(p, 'holds', 'offhand') <=> p ~ ['holds', 'offhand']
    query(p, 'name') <=> p ~ 'name'
  5. Explore the Carpet mod ecosystem

    master

    The Carpet ecosystem consists of the core mod and several extensions:

    • Core Carpet: The primary mod for technical control. Available on releases.
    • carpet-extra: An add-on mod providing additional features like autocrafting, block-placing dispensers, and chicken-shearing.
    • scarpet app store: A collection of apps written in the scarpet language. You can browse or contribute at https://github.com/gnembon/scarpet.
    • quick-carpet: A minimal build for when you need basic functionality quickly (e.g., during Minecraft snapshots) but cannot wait for a full release. Note: quick-carpet does not support extensions or scarpet apps.
    • Community Extensions: A list of other community-created extensions can be found on the Carpet Wiki.
  6. Functions, Scoping, and Outer Variables

    master

    Scoping

    • Pass-by-value: All function parameters are passed by value, not by reference. Modifying a parameter inside a function does not change the original variable in the caller's scope.
    • Global vs Local: Variables starting with global_ are global; all others are local to the function.

    Capturing Outer Variables

    To allow a function to 'borrow' or capture variables from the outer scope (similar to capturing lambdas in Java), use the outer() built-in function in the function signature.

    When using outer(var), Scarpet attaches the value of the variable at the time of the function definition. For mutable types like maps or lists, the function can maintain state and act like an object because the reference to the mutable structure is preserved.

  7. Assignment and Unpacking Operators

    master

    Assignment

    • =: Standard assignment.
    • +=: On a list, this extends the list with the new element(s).
    • <>: Requires bounded arguments (variables) on both sides.
    • List Destructuring: You can assign list elements to multiple variables: [a, b, c] = [1, 2, 3] results in a=1, b=2, c=3.

    Unpacking (...)

    The ... operator unpacks an iterator (list or map) into a sequence of arguments.

    • In Functions: Identifies a vararg parameter.
    • In Lists/Maps: Allows spreading elements into a new collection.

    Warning: Unpacking is ignored in certain contexts like map(list, expr) or if(...) where expressions are expected instead of argument lists.

  8. Scarpet Variables and Scoping

    master

    Scarpet uses two types of variables:

    • Local Variables: Any variable name that does not start with global_. These are only visible within the function where they are defined. Parameters passed to functions are passed by value, not by reference.
    • Global Variables: Any variable name starting with global_. These are shared across the entire code scope.

    Capturing Outer Variables

    To allow a function to 'borrow' or capture a variable from an outer scope (similar to capturing lambdas in Java), use the outer(var) built-in function in the function signature.

    Important Behavior: Scarpet attaches the value of the outer variable at the time of the function definition. If the outer variable changes later, the function still uses the value it captured during definition. However, mutable values like maps or lists allow the function to maintain and modify state.

  9. Specify inventory types in scarpet

    master

    Most inventory-related functions require an inventory argument. You can specify the inventory using an entity, a block, or coordinates. You can also prefix the argument with an inventory type:

    • null (default): The standard inventory (e.g., player's main inventory, or an entity's default inventory).
    • 'enderchest': Accesses a player's enderchest storage.
    • 'equipment': Accesses an entity's hand and armor pieces.

    Examples:

    • inventory_size('enderchest', player()) accesses the player's enderchest.
    • inventory_size('equipment', player()) accesses the player's armor and held items.
    • inventory_size(x, y, z) accesses a block inventory at those coordinates.
    • inventory_size(block(pos)) accesses a block inventory at a specific position.

    If the entity or block does not have an inventory, functions typically return null and do nothing.

  10. How custom and built-in events differ

    master

    Custom Events

    • Programmers define and trigger these.
    • They pass a single value as an argument (though this value can be a complex list, map, or NBT tag).
    • They can be targeted to specific players or globally.

    Built-in Events

    • Provided by the system (e.g., tick, player_breaks_block).
    • They have predefined argument structures.
    • They target global apps by default because their first argument is clearly defined.

    Event Scoping Summary

    Target Typetarget_player valueWho is notified?
    Player Scopedplayer_instanceOnly player scoped apps for that specific player.
    Global/AllnullAll global scoped apps AND all player scoped app instances.
  11. How the Scarpet event system works

    master

    Scarpet uses an event-driven system where functions can be bound to specific in-game events.

    Automatic Binding

    Functions starting with __on_<event> are automatically bound to the corresponding built-in event. For example, __on_tick() would be called every tick. This is equivalent to calling handle_event('<event>', _(... args) -> expr).

    Event Scoping

    • Player-scoped apps: Player action events are directed to the appropriate player hosts.
    • Global events: Events like 'tick' are executed once for each player app instance.
    • Recommendation: For controlled application of handlers, define them in the __on_start() function for each player.

    Event Execution and Cancellation

    • Timing: Most built-in events trigger before they take effect in Minecraft. This allows you to handle the event immediately or schedule a call for the end of the tick.
    • Cancellation: Some events allow you to return 'cancel' to prevent Minecraft from processing the event.
      • Warning: Cancelling an event stops subsequent apps from triggering that event.
      • Warning: Cancelling can cause client/server desync (e.g., ghost items). To fix this, manually update the state using inventory_set or set.
    • Priority: The execution order of apps for a single event can be changed via 'event_priority' in the app configuration (higher values execute first).

    Custom Events

    Programmers can define their own custom events and signal them (or built-in events) across all loaded apps.

  12. How to specify inventories for Scarpet functions

    master

    Most inventory-related functions require an inventory argument. You can specify an inventory using several methods:

    1. Entity/Block/Position: Pass an entity, a block, or three coordinates (x, y, z) representing a block position.
    2. Inventory Types: You can prefix the target with an inventory type:
      • null (default): The regular inventory of the entity or the block at the position.
      • 'enderchest': The player's enderchest storage. Requires the player entity as the second argument (e.g., 'enderchest', player()).
      • 'equipment': The entity's hand and armor pieces. For some entities (like creepers), the default inventory is actually their equipment.

    Note on Item Formats: Items returned by the API are triples: [item_name, count, nbt_data]. When using inventory_set, if nbt is provided, it overrides the item type provided in the name.

    If the target does not have an inventory, functions typically return null and do nothing.