Skip to content

Core - Config Module

anvil.lib.config

CONFIG_FILE = Path('anvilconfig.json') module-attribute

The project's config file, in the current folder.

NAMESPACE_PATTERN = re.compile('(?!minecraft$)[a-z][a-z0-9_]{0,7}') module-attribute

A project namespace: 1 to 8 lowercase letters, digits or _, starting with a letter, and not minecraft.

PROJECT_NAME_PATTERN = re.compile('[a-z][a-z0-9_]{0,15}') module-attribute

A project name: 1 to 16 lowercase letters, digits or _, starting with a letter.

AnvilConfig dataclass

The settings of an Anvil project, read from its anvilconfig.json.

It only holds values: building one never prompts, prints, writes a file or uses the network. load_project does that work for the project in the current folder.

CONFIG is the shared instance. It loads the project in the current folder the first time one of its settings is read.

BP_PATH property

The development behavior pack folder.

RP_PATH property

The development resource pack folder.

from_dict(data) classmethod

Reads the settings of a project from the content of its anvilconfig.json.

Parameters:

Name Type Description Default
data dict

The package, manifest, features, build and anvil sections. Missing options other than the package ones get their default.

required

Raises:

Type Description
ValueError

A package option is missing, the release is not X.X.X, or the target is unknown.

from_file(path='anvilconfig.json') classmethod

Reads the settings of the project whose anvilconfig.json is at path.

Parameters:

Name Type Description Default
path str | Path

The config file. Defaults to the one in the current folder.

'anvilconfig.json'

Config

A class used to read and write to the config.ini file.

__init__(allow_missing=False)

Initializes a Config object.

add_option(section, option, value)

Sets a value in the anvilconfig.json file.

Parameters:

Name Type Description Default
section str

The section to set the value in.

required
option str

The option to set the value in.

required
value Any

The value to set.

required

add_section(section)

Adds a section to the anvilconfig.json file.

Parameters:

Name Type Description Default
section str

The section to add.

required

get_option(section, option)

Gets a value from the anvilconfig.json file.

Parameters:

Name Type Description Default
section ConfigSection

The section to get the value from.

required
option ConfigOption

The option to get the value from.

required

Returns:

Name Type Description
Any Any

The value of the option.

has_option(section, option)

Checks if an option exists in the anvilconfig.json file.

Parameters:

Name Type Description Default
section str

The section to check the option in.

required
option str

The option to check.

required

Returns:

Name Type Description
bool bool

True if the option exists, False otherwise.

has_section(section)

Checks if a section exists in the anvilconfig.json file.

Parameters:

Name Type Description Default
section str

The section to check.

required

Returns:

Name Type Description
bool bool

True if the section exists, False otherwise.

save()

Saves the anvilconfig.json file.

ConfigOption

Bases: StrEnum

Configuration options available in the anvil config file.

Defines all the configuration keys that can be set in various sections of the configuration file.

ConfigPackageTarget

Bases: StrEnum

Package target types for anvil projects.

Defines the types of packages that can be created.

ConfigSection

Bases: StrEnum

The sections of anvilconfig.json.

  • package: what the project is: its namespace, names, descriptions and target.
  • manifest: what the pack manifests carry: the release version and the UUIDs.
  • features: what the project's content uses: Script API, Script UI, PBR, experimental features and the world's random seed.
  • build: how the project is built and exported.
  • anvil: what Anvil records on its own: the last version check and the latest vanilla version it found.

Pack

Bases: StrEnum

The packs an Anvil project writes files to.

load_project()

Loads the project in the current folder.

Asks for the missing package options, writes them and the defaults of other missing options to anvilconfig.json, shows the project banner, checks for new versions once a day and, when run with --clean, deletes the development packs.

Returns:

Name Type Description
AnvilConfig AnvilConfig

The project's settings.

override(config=None, **values)

Changes the settings in CONFIG for the length of a with block.

Parameters:

Name Type Description Default
config AnvilConfig

The settings to use. Defaults to the current ones.

None
**values Any

Settings to change, by attribute name, such as PBR=True.

{}

Yields:

Name Type Description
AnvilConfig AnvilConfig

CONFIG.

use(config)

Makes CONFIG hold the settings of config.

Parameters:

Name Type Description Default
config AnvilConfig | None

The settings. None empties CONFIG, so it loads the project in the current folder on the next read.

required

validate_namespace_project_name(namespace, project_name, is_addon=False)

Validates the namespace and project name against NAMESPACE_PATTERN and PROJECT_NAME_PATTERN. An addon's namespace must also end with _ and the project name's initials, stark_mp for my_project.

Parameters:

Name Type Description Default
namespace str

The namespace to validate.

required
project_name str

The project name to validate.

required
is_addon bool

Whether this is for an addon. Defaults to False.

False

Raises:

Type Description
ValueError

If validation fails for any requirement.