Word in Search Bar gets saved between sessions

This commit is contained in:
Ellpeck 2015-10-30 23:37:20 +01:00
parent e404bbf783
commit c6d14a25a4
2 changed files with 56 additions and 47 deletions

View file

@ -52,17 +52,17 @@ public class GuiBooklet extends GuiScreen{
public BookletIndexEntry currentIndexEntry; public BookletIndexEntry currentIndexEntry;
public int pageOpenInIndex; public int pageOpenInIndex;
public int indexPageAmount; public int indexPageAmount;
public GuiButton buttonForward; private GuiButton buttonForward;
public GuiButton buttonBackward; private GuiButton buttonBackward;
public GuiButton buttonPreviousScreen; private GuiButton buttonPreviousScreen;
public GuiButton buttonPreviouslyOpenedGui; private GuiButton buttonPreviouslyOpenedGui;
public GuiButton buttonUpdate; private GuiButton buttonUpdate;
public GuiButton buttonTwitter; private GuiButton buttonTwitter;
public GuiButton buttonForum; private GuiButton buttonForum;
public GuiButton buttonAchievements; private GuiButton buttonAchievements;
public GuiButton buttonConfig; private GuiButton buttonConfig;
public GuiButton[] chapterButtons = new GuiButton[CHAPTER_BUTTONS_AMOUNT]; private GuiButton[] chapterButtons = new GuiButton[CHAPTER_BUTTONS_AMOUNT];
private GuiTextField searchField; public GuiTextField searchField;
private int ticksElapsed; private int ticksElapsed;
private boolean mousePressed; private boolean mousePressed;
@ -166,34 +166,37 @@ public class GuiBooklet extends GuiScreen{
} }
} }
@SuppressWarnings("unchecked")
@Override @Override
public void keyTyped(char theChar, int key){ public void keyTyped(char theChar, int key){
if(key != 1 && this.searchField.isFocused()){ if(key != 1 && this.searchField.isFocused()){
this.searchField.textboxKeyTyped(theChar, key); this.searchField.textboxKeyTyped(theChar, key);
this.updateSearchBar();
if(this.currentIndexEntry instanceof BookletEntryAllSearch){
BookletEntryAllSearch currentEntry = (BookletEntryAllSearch)this.currentIndexEntry;
if(this.searchField.getText() != null && !this.searchField.getText().isEmpty()){
currentEntry.chapters.clear();
for(BookletChapter chapter : currentEntry.allChapters){
if(chapter.getLocalizedName().toLowerCase().contains(this.searchField.getText().toLowerCase())){
currentEntry.chapters.add(chapter);
}
}
}
else{
currentEntry.chapters = (ArrayList<BookletChapter>)currentEntry.allChapters.clone();
}
this.openIndexEntry(this.currentIndexEntry, this.pageOpenInIndex, false);
}
} }
else{ else{
super.keyTyped(theChar, key); super.keyTyped(theChar, key);
} }
} }
@SuppressWarnings("unchecked")
public void updateSearchBar(){
if(this.currentIndexEntry instanceof BookletEntryAllSearch){
BookletEntryAllSearch currentEntry = (BookletEntryAllSearch)this.currentIndexEntry;
if(this.searchField.getText() != null && !this.searchField.getText().isEmpty()){
currentEntry.chapters.clear();
for(BookletChapter chapter : currentEntry.allChapters){
if(chapter.getLocalizedName().toLowerCase().contains(this.searchField.getText().toLowerCase())){
currentEntry.chapters.add(chapter);
}
}
}
else{
currentEntry.chapters = (ArrayList<BookletChapter>)currentEntry.allChapters.clone();
}
this.openIndexEntry(this.currentIndexEntry, this.pageOpenInIndex, false);
}
}
@Override @Override
protected void mouseClicked(int par1, int par2, int par3){ protected void mouseClicked(int par1, int par2, int par3){
this.searchField.mouseClicked(par1, par2, par3); this.searchField.mouseClicked(par1, par2, par3);
@ -398,7 +401,7 @@ public class GuiBooklet extends GuiScreen{
@Override @Override
public void onGuiClosed(){ public void onGuiClosed(){
PersistentClientData.saveBookPage(this.currentIndexEntry, this.currentChapter, this.currentPage, this.pageOpenInIndex); PersistentClientData.saveBookPage(this.currentIndexEntry, this.currentChapter, this.currentPage, this.pageOpenInIndex, this.searchField.getText());
} }
@Override @Override

View file

@ -31,7 +31,7 @@ public class PersistentClientData{
private static File theFile; private static File theFile;
public static void saveBookPage(BookletIndexEntry entry, BookletChapter chapter, BookletPage page, int pageInIndex){ public static void saveBookPage(BookletIndexEntry entry, BookletChapter chapter, BookletPage page, int pageInIndex, String searchWord){
NBTTagCompound baseCompound = getBaseCompound(); NBTTagCompound baseCompound = getBaseCompound();
NBTTagCompound worldCompound = getCompoundForWorld(baseCompound); NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
if(worldCompound != null){ if(worldCompound != null){
@ -39,6 +39,7 @@ public class PersistentClientData{
worldCompound.setInteger("Chapter", entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter)); worldCompound.setInteger("Chapter", entry == null || chapter == null ? -1 : entry.chapters.indexOf(chapter));
worldCompound.setInteger("Page", page == null ? -1 : page.getID()); worldCompound.setInteger("Page", page == null ? -1 : page.getID());
worldCompound.setInteger("PageInIndex", pageInIndex); worldCompound.setInteger("PageInIndex", pageInIndex);
worldCompound.setString("SearchWord", searchWord);
writeCompound(baseCompound, worldCompound); writeCompound(baseCompound, worldCompound);
} }
} }
@ -88,26 +89,31 @@ 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){ if(worldCompound != null && worldCompound.hasKey("Entry")){
if(worldCompound.hasKey("Entry")){ int entry = worldCompound.getInteger("Entry");
int entry = worldCompound.getInteger("Entry"); int chapter = worldCompound.getInteger("Chapter");
int chapter = worldCompound.getInteger("Chapter"); int page = worldCompound.getInteger("Page");
int page = worldCompound.getInteger("Page");
BookletIndexEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry); BookletIndexEntry 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");
gui.openIndexEntry(currentIndexEntry, pageInIndex, true); gui.openIndexEntry(currentIndexEntry, pageInIndex, true);
if(currentChapter != null){ if(currentChapter != null){
gui.openChapter(currentChapter, currentPage); gui.openChapter(currentChapter, currentPage);
} }
return;
String searchText = worldCompound.getString("SearchWord");
if(!searchText.isEmpty()){
gui.searchField.setText(searchText);
gui.updateSearchBar();
} }
} }
//If everything fails, initialize the front page else{
gui.openIndexEntry(null, 1, true); //If everything fails, initialize the front page
gui.openIndexEntry(null, 1, true);
}
} }
public static void setBoolean(String name, boolean bool){ public static void setBoolean(String name, boolean bool){