|
1 | 1 | package com.github.franckyi.ibeeditor.client;
|
2 | 2 |
|
3 | 3 | import com.github.franckyi.ibeeditor.PlatformUtil;
|
| 4 | +import io.netty.buffer.ByteBufInputStream; |
4 | 5 | import io.netty.buffer.Unpooled;
|
5 |
| -import net.minecraft.nbt.CompoundTag; |
| 6 | +import net.minecraft.nbt.*; |
6 | 7 | import net.minecraft.network.FriendlyByteBuf;
|
7 | 8 | import org.apache.logging.log4j.LogManager;
|
8 | 9 | import org.apache.logging.log4j.Logger;
|
@@ -88,14 +89,36 @@ private static void loadFromFile(Path path) {
|
88 | 89 | try (var is = Files.newInputStream(path)) {
|
89 | 90 | buffer.writeBytes(is, is.available());
|
90 | 91 | INSTANCE.version = buffer.readInt();
|
91 |
| - IntStream.range(0, buffer.readInt()).forEach(i -> INSTANCE.items.add(buffer.readNbt())); |
92 |
| - IntStream.range(0, buffer.readInt()).forEach(i -> INSTANCE.entities.add(buffer.readNbt())); |
| 92 | + IntStream.range(0, buffer.readInt()).forEach(i -> INSTANCE.items.add(safeReadNbt())); |
| 93 | + IntStream.range(0, buffer.readInt()).forEach(i -> INSTANCE.entities.add(safeReadNbt())); |
93 | 94 | LOGGER.info("Vault loaded");
|
94 | 95 | } catch (IOException e) {
|
95 | 96 | LOGGER.error("Error while loading vault", e);
|
96 | 97 | }
|
97 | 98 | }
|
98 | 99 |
|
| 100 | + private static CompoundTag safeReadNbt() { |
| 101 | + var i = buffer.readerIndex(); |
| 102 | + try { |
| 103 | + var result = buffer.readNbt(); |
| 104 | + if (result == null || result.isEmpty()) throw new RuntimeException("Tag is empty, this must be an error"); |
| 105 | + return result; |
| 106 | + } catch (Exception e) { |
| 107 | + buffer.readerIndex(i); |
| 108 | + byte b0 = buffer.readByte(); |
| 109 | + if (b0 != Tag.TAG_COMPOUND) { |
| 110 | + throw e; |
| 111 | + } else { |
| 112 | + buffer.readerIndex(i); |
| 113 | + try { |
| 114 | + return NbtIo.read(new ByteBufInputStream(buffer)); |
| 115 | + } catch (IOException e0) { |
| 116 | + throw new RuntimeException(e0); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
99 | 122 | public static void save() {
|
100 | 123 | try (var os = Files.newOutputStream(VAULT_FILE)) {
|
101 | 124 | buffer.writeInt(INSTANCE.version);
|
|
0 commit comments