2015-08-30 19:10:10 +02:00
|
|
|
/*
|
2015-10-06 16:36:25 +02:00
|
|
|
* This file ("PersistentClientData.java") is part of the Actually Additions Mod for Minecraft.
|
2015-08-30 19:10:10 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
2015-11-02 20:55:19 +01:00
|
|
|
* under the Actually Additions License to be found at
|
2015-08-30 19:10:10 +02:00
|
|
|
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2015-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-08-30 19:10:10 +02:00
|
|
|
*/
|
|
|
|
|
2015-09-27 15:31:43 +02:00
|
|
|
package ellpeck.actuallyadditions.util.playerdata;
|
2015-08-30 19:10:10 +02:00
|
|
|
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2015-11-11 18:51:20 +01:00
|
|
|
import ellpeck.actuallyadditions.booklet.BookletUtils;
|
2015-08-30 19:10:10 +02:00
|
|
|
import ellpeck.actuallyadditions.booklet.GuiBooklet;
|
|
|
|
import ellpeck.actuallyadditions.booklet.InitBooklet;
|
2015-12-04 18:14:03 +01:00
|
|
|
import ellpeck.actuallyadditions.booklet.button.BookmarkButton;
|
2015-11-11 18:51:20 +01:00
|
|
|
import ellpeck.actuallyadditions.booklet.chapter.BookletChapter;
|
|
|
|
import ellpeck.actuallyadditions.booklet.entry.BookletEntry;
|
2015-09-10 21:25:34 +02:00
|
|
|
import ellpeck.actuallyadditions.booklet.page.BookletPage;
|
2015-09-27 15:31:43 +02:00
|
|
|
import ellpeck.actuallyadditions.util.ModUtil;
|
2015-08-30 19:10:10 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.nbt.CompressedStreamTools;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2015-11-16 20:21:12 +01:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2015-08-30 19:10:10 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2015-09-30 06:56:33 +02:00
|
|
|
public class PersistentClientData{
|
2015-08-30 19:10:10 +02:00
|
|
|
|
|
|
|
private static File theFile;
|
|
|
|
|
2015-11-16 20:21:12 +01:00
|
|
|
public static void saveBookPage(GuiBooklet gui){
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagCompound baseCompound = getBaseCompound();
|
|
|
|
NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
|
2015-11-16 20:21:12 +01:00
|
|
|
//Save Entry etc.
|
2015-10-29 19:51:00 +01:00
|
|
|
if(worldCompound != null){
|
2015-11-16 20:21:12 +01:00
|
|
|
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++){
|
2015-12-04 18:14:03 +01:00
|
|
|
BookmarkButton button = (BookmarkButton)gui.bookmarkButtons[i];
|
2015-11-16 20:21:12 +01:00
|
|
|
|
|
|
|
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);
|
2015-08-30 19:10:10 +02:00
|
|
|
}
|
2015-11-16 20:21:12 +01:00
|
|
|
worldCompound.setTag("Bookmarks", list);
|
|
|
|
|
|
|
|
writeCompound(baseCompound, worldCompound);
|
2015-08-30 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
2015-10-29 19:51:00 +01:00
|
|
|
private static NBTTagCompound getBaseCompound(){
|
2015-10-23 16:54:33 +02:00
|
|
|
try{
|
|
|
|
return CompressedStreamTools.readCompressed(new FileInputStream(getTheFile()));
|
|
|
|
}
|
|
|
|
catch(Exception e){
|
|
|
|
return new NBTTagCompound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:51:00 +01:00
|
|
|
private static NBTTagCompound getCompoundForWorld(NBTTagCompound mainCompound){
|
|
|
|
return mainCompound.getCompoundTag(getName());
|
|
|
|
}
|
2015-10-23 16:54:33 +02:00
|
|
|
|
2015-10-29 19:51:00 +01:00
|
|
|
private static void writeCompound(NBTTagCompound baseCompound, NBTTagCompound worldCompound){
|
|
|
|
baseCompound.setTag(getName(), worldCompound);
|
|
|
|
try{
|
|
|
|
CompressedStreamTools.writeCompressed(baseCompound, new FileOutputStream(getTheFile()));
|
2015-10-23 16:54:33 +02:00
|
|
|
}
|
|
|
|
catch(Exception e){
|
|
|
|
ModUtil.LOGGER.fatal("Couldn't write Persistent Variable!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static File getTheFile(){
|
|
|
|
try{
|
|
|
|
if(!theFile.exists()){
|
|
|
|
theFile.createNewFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception e){
|
|
|
|
ModUtil.LOGGER.fatal("Couldn't create Persistent Variables file!", e);
|
|
|
|
}
|
|
|
|
return theFile;
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
private static String getName(){
|
|
|
|
if(Minecraft.getMinecraft().theWorld != null){
|
|
|
|
return Minecraft.getMinecraft().isIntegratedServerRunning() ? Minecraft.getMinecraft().getIntegratedServer().getFolderName() : Minecraft.getMinecraft().func_147104_D().serverIP;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return "Invalid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 16:54:33 +02:00
|
|
|
public static void setTheFile(File file){
|
|
|
|
theFile = file;
|
|
|
|
}
|
|
|
|
|
2015-08-30 19:10:10 +02:00
|
|
|
public static void openLastBookPage(GuiBooklet gui){
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound());
|
2015-11-16 20:21:12 +01:00
|
|
|
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");
|
|
|
|
|
|
|
|
BookletEntry currentIndexEntry = entry == -1 ? null : InitBooklet.entries.get(entry);
|
|
|
|
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];
|
|
|
|
int pageInIndex = worldCompound.getInteger("PageInIndex");
|
|
|
|
|
|
|
|
BookletUtils.openIndexEntry(gui, currentIndexEntry, pageInIndex, true);
|
|
|
|
if(currentChapter != null){
|
|
|
|
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);
|
2015-10-30 23:37:20 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 20:21:12 +01:00
|
|
|
//Load Bookmarks
|
|
|
|
NBTTagList list = worldCompound.getTagList("Bookmarks", 10);
|
|
|
|
if(list != null){
|
|
|
|
for(int i = 0; i < list.tagCount(); i++){
|
2015-12-04 18:14:03 +01:00
|
|
|
BookmarkButton button = (BookmarkButton)gui.bookmarkButtons[i];
|
2015-11-16 20:21:12 +01:00
|
|
|
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");
|
|
|
|
}
|
2015-08-30 19:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setBoolean(String name, boolean bool){
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagCompound baseCompound = getBaseCompound();
|
|
|
|
NBTTagCompound worldCompound = getCompoundForWorld(baseCompound);
|
|
|
|
if(worldCompound != null){
|
|
|
|
worldCompound.setBoolean(name, bool);
|
|
|
|
writeCompound(baseCompound, worldCompound);
|
2015-08-30 19:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean getBoolean(String name){
|
2015-10-29 19:51:00 +01:00
|
|
|
NBTTagCompound worldCompound = getCompoundForWorld(getBaseCompound());
|
|
|
|
return worldCompound != null && worldCompound.getBoolean(name);
|
2015-08-30 19:10:10 +02:00
|
|
|
}
|
|
|
|
}
|