Skip to content

Codec Utilities

MehVahdJukaar edited this page Jul 25, 2026 · 1 revision

Codec Utilities

Codecs for the things pack authors actually write, and for surviving their mistakes.

Vanilla codecs are strict: one bad field and the whole file fails to load, usually with an unhelpful message. Most of CodecUtils exists to make datapack facing formats forgiving, and to accept the shorthand forms people expect ("a single item where a list is declared", "an id string instead of an object").

Lenient codecs

These log a warning and fall back to a default instead of failing the whole file:

CodecUtils.lenientWithLog(SomeCodec, "field_name", defaultValue)
CodecUtils.lenientListCodec(elementCodec)          // bad entries are skipped, not fatal
CodecUtils.lenientUnboundedMap(keyCodec, valueCodec)
CodecUtils.lenientHomogeneousList(Registries.BLOCK) // ids that don't exist are dropped

Use them wherever a pack, rather than your own code, provides the file.

Convenience formats

Helper Accepts
singleOrList(codec) / lenientListOrSingleCodec One element or a list of them
ITEM_OR_STACK An item id or a full stack
ITEMSTACK_OR_LIST_OR_HOLDER_SET A stack, a list, or a tag
optionalRegistryCodec(registry, default) A registry id, falling back if it's missing
alias(codec, "name", "old_name") Two field names for the same thing, for renames
remapNamespaceCodec(reg, old, new) Ids whose namespace moved
alternatives(a, b, c) / bestAlternative(...) Several formats for one value, picking the one that parses
AABB_CODEC, VEC2F The geometry ones vanilla doesn't have

MOD_LOADED_CODEC reads a mod id and gives you whether it's installed, useful for conditional entries.

Also here

PostProcessCodecs runs a validation or fixup step after decoding. UnionCodec merges two decoded objects into one. EitherLeftCodec and AlternativeCodec handle either-shaped values. BiggerCodecs and BiggerStreamCodecs extend the record builder past vanilla's arity limit, which you hit surprisingly fast on config objects.

CodecMapRegistry and StreamCodecMapRegistry are small registries keyed by id, for dispatch codecs. RegistryAccessJsonReloadListener is the base for a reload listener that needs registry access, which is how the trades and map markers files are loaded.

For codecs that also describe an editable form (used by the config screen), see codecui's SchemaCodec, which Moonlight bundles.

Clone this wiki locally