tutorial:codec
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| tutorial:codec [2024/06/30 13:24] – [Packet codec] solidblock | tutorial:codec [2024/06/30 14:16] (current) – solidblock | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== |
| ===== What is a codec ===== | ===== What is a codec ===== | ||
| - | A **codec**, introduced in Java Edition 1.16, is a specification of conversion between any type of object (such as '' | + | A **codec**, introduced in Java Edition 1.16, is a specification of conversion between any type of object (such as '' |
| - | For example, loot tables are written in json forms in data packs, and are loaded as '' | + | For example, loot tables are written in json forms in data packs, and are loaded as '' |
| - | Codec was introduced in 1.16, but has mainly been increasingly widely used in Minecraft since 1.20. For example, '' | + | Codec was introduced in 1.16, but has mainly been increasingly widely used in Minecraft since 1.20. For example, '' |
| ===== Using a codec ===== | ===== Using a codec ===== | ||
| Line 25: | Line 25: | ||
| </ | </ | ||
| - | As seen in the code, the result is not directly '' | + | As seen in the code, the result is not directly '' |
| <code java> | <code java> | ||
| - | // get the result when succeed | + | // get the result when succeeded |
| final NbtList nbtList = new NbtList(); | final NbtList nbtList = new NbtList(); | ||
| nbtList.add(NbtInt.of(1)); | nbtList.add(NbtInt.of(1)); | ||
| Line 41: | Line 41: | ||
| </ | </ | ||
| - | There is a special type of '' | + | There is a special type of '' |
| ===== Writing a codec ===== | ===== Writing a codec ===== | ||
| Line 57: | Line 57: | ||
| * '' | * '' | ||
| - | Besides, '' | + | Besides, '' |
| * '' | * '' | ||
| * '' | * '' | ||
| Line 63: | Line 63: | ||
| > **Tips:** You can learn more about how to write codecs by seeing how vanilla codecs are written. | > **Tips:** You can learn more about how to write codecs by seeing how vanilla codecs are written. | ||
| + | |||
| ==== Mapping existing codec ==== | ==== Mapping existing codec ==== | ||
| Line 101: | Line 102: | ||
| ==== Record codec ==== | ==== Record codec ==== | ||
| === Required fields === | === Required fields === | ||
| - | Most objects are complicated, | + | Most objects are complicated, |
| <code java> | <code java> | ||
| public record Student(String name, int id, Vec3d pos) { | public record Student(String name, int id, Vec3d pos) { | ||
| Line 167: | Line 168: | ||
| } | } | ||
| </ | </ | ||
| - | Writing like this may be simpler. You need to specify the method to convert fields to an objects at first ('' | + | Writing like this may be simpler. You need to specify the method to convert fields to an objects at first ('' |
| ==== Dispatching codec ==== | ==== Dispatching codec ==== | ||
| Some objects may not be in fixed structures, but have variant types, each of which, is in a unique structure. Therefore, dispatching codecs are used. | Some objects may not be in fixed structures, but have variant types, each of which, is in a unique structure. Therefore, dispatching codecs are used. | ||
tutorial/codec.1719753863.txt.gz · Last modified: 2024/06/30 13:24 by solidblock