Skip to content

Commit fa75be6

Browse files
committed
fix: crash on close book
Closes #312
1 parent 0a842fd commit fa75be6

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

common/src/main/java/com/klikli_dev/modonomicon/client/gui/BookGuiManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,12 @@ public void closeParentScreen(BookParentScreen screen) {
499499
protected void sendBookClosedMessage(BookParentScreen screen) {
500500
var player = Minecraft.getInstance().player;
501501
if (player != null) {
502-
//If the book in the main hand is this book, we send MAIN_HAND, otherwise OFF_HAND
502+
//If there is a book in the main hand and it is the closed screen's book, we send MAIN_HAND, otherwise OFF_HAND
503503
//That means if the book is in neither hand, we send OFF_HAND
504504
//that is fine, the server will then just not update the closed state nbt on any item.
505505
//this is for the case of a custom button opening the book gui while the book is not in hand
506-
if (ModonomiconItem.getBook(player.getMainHandItem()).getId().equals(screen.getBook().getId()))
506+
var bookInMainhand = ModonomiconItem.getBook(player.getMainHandItem());
507+
if (bookInMainhand != null && screen.getBook().getId().equals(bookInMainhand.getId()))
507508
Services.NETWORK.sendToServer(new BookClosedMessage(InteractionHand.MAIN_HAND));
508509
else
509510
Services.NETWORK.sendToServer(new BookClosedMessage(InteractionHand.OFF_HAND));

common/src/main/java/com/klikli_dev/modonomicon/networking/BookClosedMessage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.klikli_dev.modonomicon.networking;
66

77
import com.klikli_dev.modonomicon.Modonomicon;
8+
import com.klikli_dev.modonomicon.item.ModonomiconItem;
89
import com.klikli_dev.modonomicon.util.StreamCodecs;
910
import net.minecraft.network.RegistryFriendlyByteBuf;
1011
import net.minecraft.network.codec.StreamCodec;
@@ -43,8 +44,8 @@ public void onServerReceived(MinecraftServer server, ServerPlayer player) {
4344

4445
//Only set to closed if it is a modonomicon with a valid book.
4546
//The item in hand could be anything if the book screen was opened e.g. via a custom UI button.
46-
if (com.klikli_dev.modonomicon.item.ModonomiconItem.getBook(stack) != null) {
47-
com.klikli_dev.modonomicon.item.ModonomiconItem.setBookClosed(stack);
47+
if (ModonomiconItem.getBook(stack) != null) {
48+
ModonomiconItem.setBookClosed(stack);
4849
}
4950
}
5051
}

0 commit comments

Comments
 (0)