|
| 1 | +# Upgrading guide from Hephaistos v1 to v2 |
| 2 | + |
| 3 | +This guide was made during all changes made to [Minestom](https://github.com/Minestom/Minestom), while I updated its Hephaistos to version v2.1.2. I hope this guide will be clear enough to provide an easy migration. |
| 4 | + |
| 5 | +The guide will give hints with how to apply the changes via IntelliJ IDEA, but any IDE would work. |
| 6 | + |
| 7 | +* `org.jglrxavpok.hephaistos.nbt.SNBTParser` becomes `org.jglrxavpok.hephaistos.parser.SNBTParser`. 'Replace in Files' (Control+Shift+R) is enough. |
| 8 | +* `org.jglrxavpok.hephaistos.nbt.NBTTypes` become `org.jglrxavpok.hephaistos.nbt.NBTType`. 'Replace in Files' (Control+Shift+R) is enough. |
| 9 | +* `NBTEnd` is now a Kotlin `object`. This means `NBTEnd` is now a singleton, creating new instances is not allowed. Use `NBTEnd.INSTANCE` instead. |
| 10 | +* `NBTWriter` and `NBTReader` constructors no longer take a `boolean` as a second parameter to determine whether the contents are compressed or not. |
| 11 | + If you used no compression (`false` second argument), you can simply drop the second argument and only provide the output. |
| 12 | + If you did use compression, select a `CompressedProcessor`. Three are available by default: `CompressedProcessor.NONE`, `CompressedProcessor.GZIP` and `CompressedProcessor.ZLIB`. |
| 13 | + |
| 14 | +* `NBT#deepClone` no longer exists, because tags are now immutable. |
| 15 | + |
| 16 | +* `NBTCompound` is no longer mutable. `NBT.Compound` is used to build a NBTCompound now. |
| 17 | + If you are writing a compound from scratch, use this example as a base: |
| 18 | + |
| 19 | +Old version: |
| 20 | +```java |
| 21 | +@Override |
| 22 | +public @NotNull Component serializeShowEntity(HoverEvent.@NotNull ShowEntity input, Codec.Encoder<Component, String, ? extends RuntimeException> componentEncoder) throws IOException { |
| 23 | + final NBTCompound tag = new NBTCompound(); |
| 24 | + tag.setString(ENTITY_ID, input.id().toString()); |
| 25 | + tag.setString(ENTITY_TYPE, input.type().asString()); |
| 26 | + |
| 27 | + final Component name = input.name(); |
| 28 | + if (name != null) { |
| 29 | + tag.setString(ENTITY_NAME, componentEncoder.encode(name)); |
| 30 | + } |
| 31 | + |
| 32 | + return Component.text(MinestomAdventure.NBT_CODEC.encode(tag)); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +New version: |
| 37 | +```java |
| 38 | +@Override |
| 39 | +public @NotNull Component serializeShowEntity(HoverEvent.@NotNull ShowEntity input, Codec.Encoder<Component, String, ? extends RuntimeException> componentEncoder) throws IOException { |
| 40 | + final NBTCompound tag = NBT.Compound(t -> { |
| 41 | + t.setString(ENTITY_ID, input.id().toString()); |
| 42 | + t.setString(ENTITY_TYPE, input.type().asString()); |
| 43 | + |
| 44 | + final Component name = input.name(); |
| 45 | + if (name != null) { |
| 46 | + t.setString(ENTITY_NAME, componentEncoder.encode(name)); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + return Component.text(MinestomAdventure.NBT_CODEC.encode(tag)); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +* `NBTList` is no longer mutable. Use `NBT.List` to create a list: |
| 55 | + |
| 56 | +Old: |
| 57 | +```java |
| 58 | +List<@NotNull Component> lore; // your list |
| 59 | +// ... |
| 60 | +final NBTList<NBTString> loreNBT = NBT.List<>(NBTType.TAG_String); |
| 61 | +for (Component line : lore) { |
| 62 | + loreNBT.add(new NBTString(GsonComponentSerializer.gson().serialize(line))); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +New: |
| 67 | +```java |
| 68 | +List<@NotNull Component> lore; // your list |
| 69 | +// ... |
| 70 | +final NBTList<NBTString> loreNBT = NBT.List(NBTType.TAG_String, |
| 71 | + lore.stream() |
| 72 | + .map(line -> new NBTString(GsonComponentSerializer.gson().serialize(line))) |
| 73 | + .collect(Collectors.toList()) |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +* `NBTCompound#getByteArray`, `NBTCompound#getIntArray`, `NBTCompound#getLongArray`, now respectively return an `ImmutableByteArray`, `ImmutableIntArray`, or an `ImmutableLongArray`. |
| 78 | + |
| 79 | +* `ChunkColumn#getSections` no longer returns an array, but a map due to negative Y now being allowed (1.17+). |
| 80 | +This kind of code no longer compiles: |
| 81 | +```java |
| 82 | +ChunkColumn chunk; |
| 83 | +for (var section : chunk.getSections()) { |
| 84 | + // do something |
| 85 | +} |
| 86 | +``` |
| 87 | +You will need to use `.values()` to make it work: |
| 88 | +```java |
| 89 | +ChunkColumn chunk; |
| 90 | +for (var section : chunk.getSections().values()) { |
| 91 | + // do something |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +## Additionnal notes |
| 97 | + |
| 98 | +* `NBTCompound#removeTag` no longer exists (NBTCompound is now immutable). `MutableNBTCompound` exposes `remove(String)` instead. |
0 commit comments