spellbook names are now per-page

This commit is contained in:
yrsegal@gmail.com 2022-04-08 11:38:21 -04:00
parent bccca64e19
commit f2e33c6c6c
2 changed files with 31 additions and 8 deletions

View file

@ -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();
}
}
}

View file

@ -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));