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){ 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()); ArrayList list = new ArrayList();
this.booklet.drawHoveringText(Collections.singletonList(strg), mouseX, mouseY); 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 @Override
public void onGuiClosed(){ public void onGuiClosed(){
PersistentClientData.saveBookPage(this.currentIndexEntry, this.currentChapter, this.currentPage, this.pageOpenInIndex, this.searchField.getText()); PersistentClientData.saveBookPage(this);
} }
@Override @Override

View file

@ -22,6 +22,7 @@ import ellpeck.actuallyadditions.util.ModUtil;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -32,17 +33,33 @@ public class PersistentClientData{
private static File theFile; 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 baseCompound = getBaseCompound();
NBTTagCompound worldCompound = getCompoundForWorld(baseCompound); NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
//Save Entry etc.
if(worldCompound != null){ if(worldCompound != null){
worldCompound.setInteger("Entry", entry == null ? -1 : InitBooklet.entries.indexOf(entry)); worldCompound.setInteger("Entry", gui.currentIndexEntry == null ? -1 : InitBooklet.entries.indexOf(gui.currentIndexEntry));
worldCompound.setInteger("Chapter", entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter)); worldCompound.setInteger("Chapter", gui.currentIndexEntry == null || gui.currentChapter == null ? -1 : gui.currentIndexEntry.chapters.indexOf(gui.currentChapter));
worldCompound.setInteger("Page", page == null ? -1 : page.getID()); worldCompound.setInteger("Page", gui.currentPage == null ? -1 : gui.currentPage.getID());
worldCompound.setInteger("PageInIndex", pageInIndex); worldCompound.setInteger("PageInIndex", gui.pageOpenInIndex);
worldCompound.setString("SearchWord", searchWord); worldCompound.setString("SearchWord", gui.searchField.getText());
writeCompound(baseCompound, worldCompound);
} }
//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(){ private static NBTTagCompound getBaseCompound(){
@ -93,31 +110,52 @@ public class PersistentClientData{
public static void openLastBookPage(GuiBooklet gui){ public static void openLastBookPage(GuiBooklet gui){
NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound()); NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound());
if(worldCompound != null && worldCompound.hasKey("Entry")){ if(worldCompound != null){
int entry = worldCompound.getInteger("Entry"); //Open Entry etc.
int chapter = worldCompound.getInteger("Chapter"); if(worldCompound.hasKey("Entry")){
int page = worldCompound.getInteger("Page"); int entry = worldCompound.getInteger("Entry");
int chapter = worldCompound.getInteger("Chapter");
int page = worldCompound.getInteger("Page");
BookletEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry); BookletEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry);
BookletChapter currentChapter = chapter == -1 || entry == -1 || currentIndexEntry.chapters.size() <= chapter ? null : currentIndexEntry.chapters.get(chapter); BookletChapter currentChapter = chapter == -1 || entry == -1 || currentIndexEntry.chapters.size() <= chapter ? null : currentIndexEntry.chapters.get(chapter);
BookletPage currentPage = chapter == -1 || currentChapter == null || currentChapter.pages.length <= page-1 ? null : currentChapter.pages[page-1]; BookletPage currentPage = chapter == -1 || currentChapter == null || currentChapter.pages.length <= page-1 ? null : currentChapter.pages[page-1];
int pageInIndex = worldCompound.getInteger("PageInIndex"); int pageInIndex = worldCompound.getInteger("PageInIndex");
BookletUtils.openIndexEntry(gui, currentIndexEntry, pageInIndex, true); BookletUtils.openIndexEntry(gui, currentIndexEntry, pageInIndex, true);
if(currentChapter != null){ if(currentChapter != null){
BookletUtils.openChapter(gui, currentChapter, currentPage); BookletUtils.openChapter(gui, currentChapter, currentPage);
}
String searchText = worldCompound.getString("SearchWord");
if(!searchText.isEmpty()){
gui.searchField.setText(searchText);
BookletUtils.updateSearchBar(gui);
}
}
else{
//If everything fails, initialize the front page
BookletUtils.openIndexEntry(gui, null, 1, true);
} }
String searchText = worldCompound.getString("SearchWord"); //Load Bookmarks
if(!searchText.isEmpty()){ NBTTagList list = worldCompound.getTagList("Bookmarks", 10);
gui.searchField.setText(searchText); if(list != null){
BookletUtils.updateSearchBar(gui); 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");
}
} }
} }
else{
//If everything fails, initialize the front page
BookletUtils.openIndexEntry(gui, null, 1, true);
}
} }
public static void setBoolean(String name, boolean bool){ public static void setBoolean(String name, boolean bool){