diff --git a/src/main/java/at/petrak/hexcasting/common/items/ItemSpellbook.java b/src/main/java/at/petrak/hexcasting/common/items/ItemSpellbook.java index 74847894..6f122762 100644 --- a/src/main/java/at/petrak/hexcasting/common/items/ItemSpellbook.java +++ b/src/main/java/at/petrak/hexcasting/common/items/ItemSpellbook.java @@ -21,6 +21,10 @@ public class ItemSpellbook extends ItemDataHolder { // it is 1-indexed, so that 0/0 can be the special case of "it is empty" public static String TAG_PAGES = "pages"; + // this stores the names of pages, to be restored when you scroll + // it is 1-indexed, and the 0-case for TAG_PAGES will be treated as 1 + public static String TAG_PAGE_NAMES = "page_names"; + public ItemSpellbook(Properties properties) { super(properties); } @@ -51,16 +55,26 @@ public class ItemSpellbook extends ItemDataHolder { @Override public void inventoryTick(ItemStack stack, Level pLevel, Entity pEntity, int pSlotId, boolean pIsSelected) { var tag = stack.getOrCreateTag(); + int index; if (ArePagesEmpty(tag)) { - tag.putInt(TAG_SELECTED_PAGE, 0); + index = 0; } else if (!tag.contains(TAG_SELECTED_PAGE)) { - tag.putInt(TAG_SELECTED_PAGE, 1); + index = 1; } else { - var pageIdx = tag.getInt(TAG_SELECTED_PAGE); - if (pageIdx == 0) { - tag.putInt(TAG_SELECTED_PAGE, 1); - } + index = tag.getInt(TAG_SELECTED_PAGE); + if (index == 0) index = 1; } + tag.putInt(TAG_SELECTED_PAGE, index); + + int shiftedIdx = Math.max(1, index); + String nameKey = String.valueOf(shiftedIdx); + CompoundTag names = tag.getCompound(TAG_PAGE_NAMES); + if (stack.hasCustomHoverName()) { + names.putString(nameKey, Component.Serializer.toJson(stack.getHoverName())); + } else { + names.remove(nameKey); + } + tag.put(TAG_PAGE_NAMES, names); } public static boolean ArePagesEmpty(CompoundTag tag) { @@ -131,7 +145,7 @@ public class ItemSpellbook extends ItemDataHolder { return highestKey.orElse(0); } - public static void RotatePageIdx(CompoundTag tag, boolean increase) { + public static void RotatePageIdx(ItemStack stack, CompoundTag tag, boolean increase) { int newIdx; if (ArePagesEmpty(tag)) { newIdx = 0; @@ -142,5 +156,14 @@ public class ItemSpellbook extends ItemDataHolder { newIdx = 1; } tag.putInt(TAG_SELECTED_PAGE, newIdx); + + CompoundTag names = tag.getCompound(TAG_PAGE_NAMES); + int shiftedIdx = Math.max(1, newIdx); + String nameKey = String.valueOf(shiftedIdx); + if (names.contains(nameKey, Tag.TAG_STRING)) { + stack.setHoverName(Component.Serializer.fromJson(names.getString(nameKey))); + } else { + stack.resetHoverName(); + } } } diff --git a/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java b/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java index 14c8001f..5c31caf3 100644 --- a/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java +++ b/src/main/java/at/petrak/hexcasting/common/network/MsgShiftScrollSyn.java @@ -55,7 +55,7 @@ public record MsgShiftScrollSyn(InteractionHand hand, double scrollDelta, boolea private void spellbook(ServerPlayer sender, ItemStack stack) { var tag = stack.getOrCreateTag(); - ItemSpellbook.RotatePageIdx(tag, this.scrollDelta < 0.0); + ItemSpellbook.RotatePageIdx(stack, tag, this.scrollDelta < 0.0); var newIdx = tag.getInt(ItemSpellbook.TAG_SELECTED_PAGE); var len = ItemSpellbook.HighestPage(tag.getCompound(ItemSpellbook.TAG_PAGES));