Interface SkillStateCodec<T extends SkillState>

Type Parameters:
T - the state record this codec reads and writes

public interface SkillStateCodec<T extends SkillState>
Bundles the write and read halves of a skill's persisted state into one object. Both directions share one set of NBT key/type literals — a typo in one cannot drift from the other.

With a codec, both directions share one set of key/type literals. The codec is registered once per skill via SkillManager.registerCodec(Class, Class, SkillStateCodec); MyPet then drives all NBT round-trips through it:

  • Persist — MyPet calls Skill.getState(), narrows the result to the codec's state type, and calls write(SkillState) to produce the per-skill compound stored under skillInfo.
  • Activate — MyPet calls read(CompoundBinaryTag) and hands the resulting state to Skill.applyState(SkillState) so the live skill can rebuild its mutable fields.
  • Query a stored pet — StoredPet#skillState consults the codec to materialize the typed state from the persisted compound.

read(CompoundBinaryTag) should return Optional.empty() when the compound is empty or missing required fields rather than throwing — callers treat both as "no persisted state for this skill".

  • Method Details

    • write

      CompoundBinaryTag write(T state)
      Writes a skill's state out for persistence.
      Parameters:
      state - the state to write
      Returns:
      the state as NBT
    • read

      Optional<T> read(CompoundBinaryTag compound)
      Reads a skill's state back.
      Parameters:
      compound - the persisted NBT
      Returns:
      the state, or empty when the compound holds nothing for this skill