Skip to content

Commit 70a1378

Browse files
committed
Fix #497 - crash on loading item group that has more than 1 item in single stack
1 parent 524aef4 commit 70a1378

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/main/java/net/earthcomputer/clientcommands/command/ItemGroupCommand.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,15 @@ private static void loadFile() throws IOException {
293293
if (fileVersion >= currentVersion) {
294294
compoundTag.getKeys().forEach(key -> {
295295
NbtCompound group = compoundTag.getCompound(key);
296-
ItemStack icon = ItemStack.fromNbt(group.getCompound("icon"));
296+
ItemStack icon = singleItemFromNbt(group.getCompound("icon"));
297297
NbtList items = group.getList("items", NbtElement.COMPOUND_TYPE);
298298
ItemGroup itemGroup = FabricItemGroup.builder(
299299
new Identifier("clientcommands", key))
300300
.displayName(Text.literal(key))
301301
.icon(() -> icon)
302302
.entries((displayContext, entries) -> {
303303
for (int i = 0; i < items.size(); i++) {
304-
entries.add(ItemStack.fromNbt(items.getCompound(i)));
304+
entries.add(singleItemFromNbt(items.getCompound(i)));
305305
}
306306
})
307307
.build();
@@ -312,7 +312,7 @@ private static void loadFile() throws IOException {
312312
NbtCompound group = compoundTag.getCompound(key);
313313
Dynamic<NbtElement> oldStackDynamic = new Dynamic<>(NbtOps.INSTANCE, group.getCompound("icon"));
314314
Dynamic<NbtElement> newStackDynamic = dataFixer.update(TypeReferences.ITEM_STACK, oldStackDynamic, fileVersion, currentVersion);
315-
ItemStack icon = ItemStack.fromNbt((NbtCompound) newStackDynamic.getValue());
315+
ItemStack icon = singleItemFromNbt((NbtCompound) newStackDynamic.getValue());
316316

317317
NbtList updatedListTag = new NbtList();
318318
group.getList("items", NbtElement.COMPOUND_TYPE).forEach(tag -> {
@@ -326,7 +326,7 @@ private static void loadFile() throws IOException {
326326
.icon(() -> icon)
327327
.entries((displayContext, entries) -> {
328328
for (int i = 0; i < updatedListTag.size(); i++) {
329-
entries.add(ItemStack.fromNbt(updatedListTag.getCompound(i)));
329+
entries.add(singleItemFromNbt(updatedListTag.getCompound(i)));
330330
}
331331
})
332332
.build();
@@ -335,6 +335,12 @@ private static void loadFile() throws IOException {
335335
}
336336
}
337337

338+
private static ItemStack singleItemFromNbt(NbtCompound nbt) {
339+
ItemStack stack = ItemStack.fromNbt(nbt);
340+
stack.setCount(1);
341+
return stack;
342+
}
343+
338344
private static void reloadGroups() {
339345
ItemGroupsAccessor.setGroups(
340346
ItemGroups.getGroups().stream()

0 commit comments

Comments
 (0)