Bookmark Saving

This commit is contained in:
Ellpeck 2015-11-16 20:21:12 +01:00
parent 3ff1c50454
commit bbb64eadba
3 changed files with 82 additions and 29 deletions

View file

@ -526,9 +526,24 @@ public class BookletUtils{
}
}
@SuppressWarnings("unchecked")
public void drawHover(int mouseX, int mouseY){
String strg = this.assignedChapter == null ? (this.assignedEntry == null ? "None" : (this.assignedEntry.getLocalizedName()+", Page "+this.assignedPageInIndex)) : (this.assignedChapter.getLocalizedName()+", Page "+this.assignedPage.getID());
this.booklet.drawHoveringText(Collections.singletonList(strg), mouseX, mouseY);
ArrayList list = new ArrayList();
if(this.assignedEntry != null){
if(this.assignedChapter != null){
list.add(EnumChatFormatting.GOLD+this.assignedChapter.getLocalizedName()+", Page "+this.assignedPage.getID());
}
else{
list.add(EnumChatFormatting.GOLD+this.assignedEntry.getLocalizedName()+", Page "+this.assignedPageInIndex);
}
list.add("Click to open");
list.add(EnumChatFormatting.ITALIC+"Shift-Click to remove");
}
else{
list.add(EnumChatFormatting.GOLD+"None");
list.add("Click to save current page");
}
this.booklet.drawHoveringText(list, mouseX, mouseY);
}
}
}

View file

@ -301,7 +301,7 @@ public class GuiBooklet extends GuiScreen{
@Override
public void onGuiClosed(){
PersistentClientData.saveBookPage(this.currentIndexEntry, this.currentChapter, this.currentPage, this.pageOpenInIndex, this.searchField.getText());
PersistentClientData.saveBookPage(this);
}
@Override

View file

@ -22,6 +22,7 @@ import ellpeck.actuallyadditions.util.ModUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import java.io.File;
import java.io.FileInputStream;
@ -32,17 +33,33 @@ public class PersistentClientData{
private static File theFile;
public static void saveBookPage(BookletEntry entry, BookletChapter chapter, BookletPage page, int pageInIndex, String searchWord){
public static void saveBookPage(GuiBooklet gui){
NBTTagCompound baseCompound = getBaseCompound();
NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
//Save Entry etc.
if(worldCompound != null){
worldCompound.setInteger("Entry", entry == null ? -1 : InitBooklet.entries.indexOf(entry));
worldCompound.setInteger("Chapter", entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter));
worldCompound.setInteger("Page", page == null ? -1 : page.getID());
worldCompound.setInteger("PageInIndex", pageInIndex);
worldCompound.setString("SearchWord", searchWord);
writeCompound(baseCompound, worldCompound);
worldCompound.setInteger("Entry", gui.currentIndexEntry == null ? -1 : InitBooklet.entries.indexOf(gui.currentIndexEntry));
worldCompound.setInteger("Chapter", gui.currentIndexEntry == null || gui.currentChapter == null ? -1 : gui.currentIndexEntry.chapters.indexOf(gui.currentChapter));
worldCompound.setInteger("Page", gui.currentPage == null ? -1 : gui.currentPage.getID());
worldCompound.setInteger("PageInIndex", gui.pageOpenInIndex);
worldCompound.setString("SearchWord", gui.searchField.getText());
}
//Save Bookmarks
NBTTagList list = new NBTTagList();
for(int i = 0; i < gui.bookmarkButtons.length; i++){
BookletUtils.BookmarkButton button = (BookletUtils.BookmarkButton)gui.bookmarkButtons[i];
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("Entry", button.assignedEntry == null ? -1 : InitBooklet.entries.indexOf(button.assignedEntry));
compound.setInteger("Chapter", button.assignedEntry == null || button.assignedChapter == null ? -1 : button.assignedEntry.chapters.indexOf(button.assignedChapter));
compound.setInteger("Page", button.assignedPage == null ? -1 : button.assignedPage.getID());
compound.setInteger("PageInIndex", button.assignedPageInIndex);
list.appendTag(compound);
}
worldCompound.setTag("Bookmarks", list);
writeCompound(baseCompound, worldCompound);
}
private static NBTTagCompound getBaseCompound(){
@ -93,7 +110,9 @@ public class PersistentClientData{
public static void openLastBookPage(GuiBooklet gui){
NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound());
if(worldCompound != null && worldCompound.hasKey("Entry")){
if(worldCompound != null){
//Open Entry etc.
if(worldCompound.hasKey("Entry")){
int entry = worldCompound.getInteger("Entry");
int chapter = worldCompound.getInteger("Chapter");
int page = worldCompound.getInteger("Page");
@ -118,6 +137,25 @@ public class PersistentClientData{
//If everything fails, initialize the front page
BookletUtils.openIndexEntry(gui, null, 1, true);
}
//Load Bookmarks
NBTTagList list = worldCompound.getTagList("Bookmarks", 10);
if(list != null){
for(int i = 0; i < list.tagCount(); i++){
BookletUtils.BookmarkButton button = (BookletUtils.BookmarkButton)gui.bookmarkButtons[i];
NBTTagCompound compound = list.getCompoundTagAt(i);
int entry = compound.getInteger("Entry");
int chapter = compound.getInteger("Chapter");
int page = compound.getInteger("Page");
button.assignedEntry = entry == -1 ? null : InitBooklet.entries.get(entry);
button.assignedChapter = chapter == -1 || entry == -1 || button.assignedEntry.chapters.size() <= chapter ? null : button.assignedEntry.chapters.get(chapter);
button.assignedPage = chapter == -1 || button.assignedChapter == null || button.assignedChapter.pages.length <= page-1 ? null : button.assignedChapter.pages[page-1];
button.assignedPageInIndex = compound.getInteger("PageInIndex");
}
}
}
}
public static void setBoolean(String name, boolean bool){