Skip to content

Commit

Permalink
Close #113 and document naming pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gamma-delta committed Jun 12, 2022
1 parent c4a5f97 commit 8ef43af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -34,6 +35,8 @@ public class ItemSpellbook extends Item implements DataHolderItem {
// it is 1-indexed, and the 0-case for TAG_PAGES will be treated as 1
public static String TAG_SEALED = "sealed_pages";

public static final int MAX_PAGES = 64;

public ItemSpellbook(Properties properties) {
super(properties);
}
Expand All @@ -47,17 +50,18 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
var pageIdx = NBTHelper.getInt(stack, TAG_SELECTED_PAGE);
int highest = HighestPage(stack);
if (highest != 0) {
if (sealed)
if (sealed) {
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.page.sealed",
new TextComponent(String.valueOf(pageIdx)).withStyle(ChatFormatting.WHITE),
new TextComponent(String.valueOf(highest)).withStyle(ChatFormatting.WHITE),
new TranslatableComponent("hexcasting.tooltip.spellbook.sealed").withStyle(ChatFormatting.GOLD))
.withStyle(ChatFormatting.GRAY));
else
} else {
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.page",
new TextComponent(String.valueOf(pageIdx)).withStyle(ChatFormatting.WHITE),
new TextComponent(String.valueOf(highest)).withStyle(ChatFormatting.WHITE))
.withStyle(ChatFormatting.GRAY));
}
} else {
empty = true;
}
Expand All @@ -69,14 +73,17 @@ public void appendHoverText(ItemStack stack, @Nullable Level level, List<Compone
boolean overridden = NBTHelper.hasString(stack, TAG_OVERRIDE_VISUALLY);
if (sealed) {
if (overridden) {
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.sealed").withStyle(ChatFormatting.GOLD));
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.sealed").withStyle(
ChatFormatting.GOLD));
} else {
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.empty.sealed",
new TranslatableComponent("hexcasting.tooltip.spellbook.sealed").withStyle(ChatFormatting.GOLD))
.withStyle(ChatFormatting.GRAY));
}
} else if (!overridden)
tooltip.add(new TranslatableComponent("hexcasting.tooltip.spellbook.empty").withStyle(ChatFormatting.GRAY));
} else if (!overridden) {
tooltip.add(
new TranslatableComponent("hexcasting.tooltip.spellbook.empty").withStyle(ChatFormatting.GRAY));
}
}

DataHolderItem.appendHoverText(this, stack, tooltip, isAdvanced);
Expand Down Expand Up @@ -128,8 +135,9 @@ public boolean canWrite(ItemStack stack, SpellDatum<?> datum) {

@Override
public void writeDatum(ItemStack stack, SpellDatum<?> datum) {
if (datum != null && IsSealed(stack))
if (datum != null && IsSealed(stack)) {
return;
}

int idx = GetPage(stack, 1);
var key = String.valueOf(idx);
Expand All @@ -138,11 +146,13 @@ public void writeDatum(ItemStack stack, SpellDatum<?> datum) {
if (datum == null) {
pages.remove(key);
NBTHelper.remove(NBTHelper.getCompound(stack, TAG_SEALED), key);
} else
} else {
pages.put(key, datum.serializeToNBT());
}

if (pages.isEmpty())
if (pages.isEmpty()) {
NBTHelper.remove(stack, TAG_PAGES);
}
} else if (datum != null) {
NBTHelper.getOrCreateCompound(stack, TAG_PAGES).put(key, datum.serializeToNBT());
} else {
Expand All @@ -151,11 +161,13 @@ public void writeDatum(ItemStack stack, SpellDatum<?> datum) {
}

public static int GetPage(ItemStack stack, int ifEmpty) {
if (ArePagesEmpty(stack))
if (ArePagesEmpty(stack)) {
return ifEmpty;
else if (NBTHelper.hasNumber(stack, TAG_SELECTED_PAGE)) {
} else if (NBTHelper.hasNumber(stack, TAG_SELECTED_PAGE)) {
int index = NBTHelper.getInt(stack, TAG_SELECTED_PAGE);
if (index == 0) index = 1;
if (index == 0) {
index = 1;
}
return index;
} else {
return 1;
Expand All @@ -168,15 +180,17 @@ public static void SetSealed(ItemStack stack, boolean sealed) {
String nameKey = String.valueOf(index);
CompoundTag names = NBTHelper.getOrCreateCompound(stack, TAG_SEALED);

if (!sealed)
if (!sealed) {
names.remove(nameKey);
else
} else {
names.putBoolean(nameKey, true);
}

if (names.isEmpty())
if (names.isEmpty()) {
NBTHelper.remove(stack, TAG_SEALED);
else
} else {
NBTHelper.putCompound(stack, TAG_SEALED, names);
}

}

Expand All @@ -190,8 +204,9 @@ public static boolean IsSealed(ItemStack stack) {

public static int HighestPage(ItemStack stack) {
CompoundTag tag = NBTHelper.getCompound(stack, TAG_PAGES);
if (tag == null)
if (tag == null) {
return 0;
}
return tag.getAllKeys().stream().flatMap(s -> {
try {
return Stream.of(Integer.parseInt(s));
Expand All @@ -207,6 +222,7 @@ public static int RotatePageIdx(ItemStack stack, boolean increase) {
idx += increase ? 1 : -1;
idx = Math.max(1, idx);
}
idx = Mth.clamp(idx, 0, MAX_PAGES);
NBTHelper.putInt(stack, TAG_SELECTED_PAGE, idx);

CompoundTag names = NBTHelper.getCompound(stack, TAG_PAGE_NAMES);
Expand Down
4 changes: 2 additions & 2 deletions Common/src/main/resources/assets/hexcasting/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@
"hexcasting.page.abacus.crafting.desc": "$(italic)Mathematics? That's for eggheads!/$",

"hexcasting.entry.spellbook": "Spellbook",
"hexcasting.page.spellbook.1": "A $(l:items/spellbook)$(item)Spellbook/$ is the culmination of my art-- it acts like an entire library of $(l:items/focus)$(item)Foci/$.$(br2)Each page can hold a single iota, and I can select the active page (the page that iotas are saved to and copied from) by sneak-scrolling while holding it, or simply holding it in my off-hand and scrolling while casting a _Hex.",
"hexcasting.page.spellbook.2": "Like a $(l:items/focus)$(item)Focus/$, there exists a simple method to prevent accidental overwriting. Crafting it with a $(item)Honeycomb/$ will lacquer the current page, preventing $(l:patterns/readwrite#hexcasting:write)$(action)Scribe's Gambit/$ from modifying its contents. Also like a $(l:items/focus)$(item)Focus/$, using $(l:patterns/spells/hexcasting#hexcasting:erase)$(action)Erase Item/$ will remove the lacquer along with the page's contents.",
"hexcasting.page.spellbook.1": "A $(l:items/spellbook)$(item)Spellbook/$ is the culmination of my art-- it acts like an entire library of $(l:items/focus)$(item)Foci/$. Up to $(thing)sixty-four/$ of them, to be exact.$(br2)Each page can hold a single iota, and I can select the active page (the page that iotas are saved to and copied from) by sneak-scrolling while holding it, or simply holding it in my off-hand and scrolling while casting a _Hex.",
"hexcasting.page.spellbook.2": "Like a $(l:items/focus)$(item)Focus/$, there exists a simple method to prevent accidental overwriting. Crafting it with a $(item)Honeycomb/$ will lacquer the current page, preventing $(l:patterns/readwrite#hexcasting:write)$(action)Scribe's Gambit/$ from modifying its contents. Also like a $(l:items/focus)$(item)Focus/$, using $(l:patterns/spells/hexcasting#hexcasting:erase)$(action)Erase Item/$ will remove the lacquer along with the page's contents.$(br2)I can also name each page individually in an anvil. Naming it will change only the name of the currently selected page, for easy browsing.",
"hexcasting.page.spellbook.crafting.desc": "$(italic)Wizards love words. Most of them read a great deal, and indeed one strong sign of a potential wizard is the inability to get to sleep without reading something first.",

"hexcasting.entry.scroll": "Scrolls",
Expand Down

0 comments on commit 8ef43af

Please sign in to comment.