|
1 | 1 | package io.icker.factions.database; |
2 | 2 |
|
| 3 | +import com.mojang.serialization.Codec; |
| 4 | +import com.mojang.serialization.codecs.RecordCodecBuilder; |
| 5 | + |
| 6 | +import io.icker.factions.FactionsMod; |
3 | 7 | import io.icker.factions.api.persistents.Relationship.Permissions; |
4 | 8 | import io.icker.factions.api.persistents.Relationship.Status; |
5 | 9 | import io.icker.factions.api.persistents.User.ChatMode; |
|
22 | 26 | import net.minecraft.nbt.NbtLongArray; |
23 | 27 | import net.minecraft.nbt.NbtShort; |
24 | 28 | import net.minecraft.nbt.NbtString; |
| 29 | +import net.minecraft.storage.NbtReadView; |
| 30 | +import net.minecraft.storage.NbtWriteView; |
| 31 | +import net.minecraft.storage.ReadView; |
| 32 | +import net.minecraft.storage.ReadView.TypedListReadView; |
| 33 | +import net.minecraft.storage.WriteView.ListAppender; |
| 34 | +import net.minecraft.util.ErrorReporter; |
25 | 35 | import net.minecraft.util.Uuids; |
| 36 | +import net.minecraft.util.dynamic.Codecs; |
26 | 37 |
|
27 | 38 | import org.apache.commons.lang3.ArrayUtils; |
28 | 39 |
|
29 | 40 | import java.util.HashMap; |
30 | | -import java.util.Optional; |
31 | 41 | import java.util.UUID; |
32 | 42 | import java.util.function.Function; |
33 | 43 |
|
@@ -134,49 +144,72 @@ private static <T extends Enum<T>> Serializer<T, NbtString> createEnumSerializer |
134 | 144 | el -> Enum.valueOf(clazz, el.asString().orElse(""))); |
135 | 145 | } |
136 | 146 |
|
| 147 | + public record InventoryItem(int slot, ItemStack stack) { |
| 148 | + public static final Codec<InventoryItem> CODEC = |
| 149 | + RecordCodecBuilder.create( |
| 150 | + (instance) -> { |
| 151 | + return instance.group( |
| 152 | + Codecs.UNSIGNED_BYTE |
| 153 | + .fieldOf("Slot") |
| 154 | + .orElse(0) |
| 155 | + .forGetter(InventoryItem::slot), |
| 156 | + ItemStack.MAP_CODEC |
| 157 | + .fieldOf("Data") |
| 158 | + .forGetter(InventoryItem::stack)) |
| 159 | + .apply(instance, InventoryItem::new); |
| 160 | + }); |
| 161 | + } |
| 162 | + |
137 | 163 | private static Serializer<SimpleInventory, NbtList> createInventorySerializer(int size) { |
138 | 164 | return new Serializer<SimpleInventory, NbtList>( |
139 | 165 | val -> { |
140 | | - NbtList nbtList = new NbtList(); |
| 166 | + ErrorReporter.Logging reporter = new ErrorReporter.Logging(FactionsMod.LOGGER); |
| 167 | + NbtWriteView view = |
| 168 | + NbtWriteView.create( |
| 169 | + reporter, |
| 170 | + WorldUtils.getWorld("minecraft:overworld") |
| 171 | + .getRegistryManager()); |
| 172 | + ListAppender<InventoryItem> appender = |
| 173 | + view.getListAppender("Data", InventoryItem.CODEC); |
141 | 174 |
|
142 | 175 | for (int i = 0; i < val.size(); ++i) { |
143 | 176 | ItemStack itemStack = val.getStack(i); |
144 | 177 | if (!itemStack.isEmpty()) { |
145 | | - NbtCompound nbtCompound = new NbtCompound(); |
146 | | - nbtCompound.putByte("Slot", (byte) i); |
147 | | - nbtCompound.put( |
148 | | - "Data", |
149 | | - itemStack.toNbt(WorldUtils.server.getRegistryManager())); |
150 | | - nbtList.add(nbtCompound); |
| 178 | + appender.add(new InventoryItem(i, itemStack)); |
151 | 179 | } |
152 | 180 | } |
153 | 181 |
|
154 | | - return nbtList; |
| 182 | + reporter.close(); |
| 183 | + |
| 184 | + return view.getNbt().getList("Data").get(); |
155 | 185 | }, |
156 | 186 | el -> { |
| 187 | + NbtCompound compound = new NbtCompound(); |
| 188 | + compound.put("Data", el); |
| 189 | + |
| 190 | + ErrorReporter.Logging reporter = new ErrorReporter.Logging(FactionsMod.LOGGER); |
| 191 | + |
| 192 | + ReadView view = |
| 193 | + NbtReadView.create( |
| 194 | + reporter, |
| 195 | + WorldUtils.getWorld("minecraft:overworld").getRegistryManager(), |
| 196 | + compound); |
| 197 | + |
157 | 198 | SimpleInventory inventory = new SimpleInventory(size); |
158 | 199 |
|
159 | 200 | for (int i = 0; i < size; ++i) { |
160 | 201 | inventory.setStack(i, ItemStack.EMPTY); |
161 | 202 | } |
162 | 203 |
|
163 | | - for (int i = 0; i < el.size(); ++i) { |
164 | | - Optional<NbtCompound> optionalNbtCompound = el.getCompound(i); |
165 | | - if (optionalNbtCompound.isEmpty()) { |
166 | | - continue; |
167 | | - } |
168 | | - NbtCompound nbtCompound = optionalNbtCompound.get(); |
169 | | - int slot = nbtCompound.getByte("Slot").orElse((byte) size) & 255; |
170 | | - if (slot >= 0 && slot < size) { |
171 | | - inventory.setStack( |
172 | | - slot, |
173 | | - ItemStack.fromNbt( |
174 | | - WorldUtils.server.getRegistryManager(), |
175 | | - nbtCompound.get("Data")) |
176 | | - .get()); |
177 | | - } |
| 204 | + TypedListReadView<InventoryItem> list_view = |
| 205 | + view.getTypedListView("Data", InventoryItem.CODEC); |
| 206 | + |
| 207 | + for (InventoryItem item : list_view) { |
| 208 | + inventory.setStack(item.slot(), item.stack()); |
178 | 209 | } |
179 | 210 |
|
| 211 | + reporter.close(); |
| 212 | + |
180 | 213 | return inventory; |
181 | 214 | }); |
182 | 215 | } |
|
0 commit comments