Lexicon Bookmarks now save between sessions.

This commit is contained in:
Lizbeth Rika committed 2015-06-24 19:27:51 +01:00
1 parent 704e35512a
commit 418c0d0bf7
6 files changed
+287 -8

No files matched your search

@@ -0,0 +1,116 @@
/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Jun 24, 2015, 5:54:45 PM (GMT)]
*/
package vazkii.botania.client.core.handler;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import vazkii.botania.client.gui.lexicon.GuiLexicon;
public final class PresistantVariableHelper {
private static final String TAG_BOOKMARK_COUNT = "bookmarkCount";
private static final String TAG_BOOKMARK_PREFIX = "bookmark";
private static final String TAG_BOOKMARKS = "bookmarks";
private static final String TAG_FIRST_LOAD = "firstLoad";
private static File cacheFile;
public static boolean firstLoad = true;
public static void save() throws IOException {
NBTTagCompound cmp = new NBTTagCompound();
List<GuiLexicon> bookmarks = GuiLexicon.bookmarks;
int count = bookmarks.size();
cmp.setInteger(TAG_BOOKMARK_COUNT, count);
NBTTagCompound bookmarksCmp = new NBTTagCompound();
for(int i = 0; i < count; i++) {
GuiLexicon lex = bookmarks.get(i);
NBTTagCompound bookmarkCmp = new NBTTagCompound();
lex.serialize(bookmarkCmp);
bookmarksCmp.setTag(TAG_BOOKMARK_PREFIX + i, bookmarkCmp);
}
cmp.setTag(TAG_BOOKMARKS, bookmarksCmp);
cmp.setBoolean(TAG_FIRST_LOAD, firstLoad);
injectNBTToFile(cmp, getCacheFile());
}
public static void load() throws IOException {
NBTTagCompound cmp = getCacheCompound();
int count = cmp.getInteger(TAG_BOOKMARK_COUNT);
GuiLexicon.bookmarks.clear();
if(count > 0) {
NBTTagCompound bookmarksCmp = cmp.getCompoundTag(TAG_BOOKMARKS);
for(int i = 0; i < count; i++) {
NBTTagCompound bookmarkCmp = bookmarksCmp.getCompoundTag(TAG_BOOKMARK_PREFIX + i);
GuiLexicon gui = GuiLexicon.create(bookmarkCmp);
if(gui != null)
GuiLexicon.bookmarks.add(gui);
}
}
firstLoad = cmp.hasKey(TAG_FIRST_LOAD) ? cmp.getBoolean(TAG_FIRST_LOAD) : firstLoad;
}
public static void saveSafe() {
try {
save();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setCacheFile(File f) {
cacheFile = f;
}
public static File getCacheFile() throws IOException {
if(!cacheFile.exists())
cacheFile.createNewFile();
return cacheFile;
}
public static NBTTagCompound getCacheCompound() throws IOException {
return getCacheCompound(getCacheFile());
}
public static NBTTagCompound getCacheCompound(File cache) throws IOException {
if(cache == null)
throw new RuntimeException("No cache file!");
try {
NBTTagCompound cmp = CompressedStreamTools.readCompressed(new FileInputStream(cache));
return cmp;
} catch(IOException e) {
NBTTagCompound cmp = new NBTTagCompound();
CompressedStreamTools.writeCompressed(cmp, new FileOutputStream(cache));
return getCacheCompound(cache);
}
}
public static void injectNBTToFile(NBTTagCompound cmp, File f) {
try {
CompressedStreamTools.writeCompressed(cmp, new FileOutputStream(f));
} catch(IOException e) {
e.printStackTrace();
}
}
}
@@ -11,6 +11,8 @@
package vazkii.botania.client.core.proxy;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Calendar;
@@ -42,6 +44,7 @@ import vazkii.botania.client.core.handler.ClientTickHandler;
import vazkii.botania.client.core.handler.DebugHandler;
import vazkii.botania.client.core.handler.HUDHandler;
import vazkii.botania.client.core.handler.LightningHandler;
import vazkii.botania.client.core.handler.PresistantVariableHelper;
import vazkii.botania.client.core.handler.SubTileRadiusRenderHandler;
import vazkii.botania.client.core.handler.TooltipHandler;
import vazkii.botania.client.core.helper.ShaderHelper;
@@ -141,14 +144,23 @@ import baubles.common.lib.PlayerHandler;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Optional;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.relauncher.ReflectionHelper;
public class ClientProxy extends CommonProxy {
public static boolean singAnnoyingChristmasSongsTillVazkiisHeadExplodesFromAllTheDamnJingle = false;
@Override
public void preInit(FMLPreInitializationEvent event) {
super.preInit(event);
PresistantVariableHelper.setCacheFile(new File(event.getModConfigurationDirectory().getParent(), "BotaniaVars.dat"));
}
@Override
public void init(FMLInitializationEvent event) {
super.init(event);
@@ -173,6 +185,13 @@ public class ClientProxy extends CommonProxy {
singAnnoyingChristmasSongsTillVazkiisHeadExplodesFromAllTheDamnJingle = true;
initRenderers();
try {
PresistantVariableHelper.load();
} catch (IOException e) {
FMLLog.severe("Botania's Presistant Variables couldn't load!");
e.printStackTrace();
}
}
private void initRenderers() {
@@ -23,6 +23,7 @@ import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
@@ -33,6 +34,7 @@ import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.lexicon.LexiconCategory;
import vazkii.botania.api.lexicon.LexiconEntry;
import vazkii.botania.client.core.handler.ClientTickHandler;
import vazkii.botania.client.core.handler.PresistantVariableHelper;
import vazkii.botania.client.core.helper.RenderHelper;
import vazkii.botania.client.core.proxy.ClientProxy;
import vazkii.botania.client.gui.GuiAchievementsHacky;
@@ -53,6 +55,8 @@ public class GuiLexicon extends GuiScreen {
public static GuiLexicon currentOpenLexicon = new GuiLexicon();
public static ItemStack stackUsed;
private static final String TAG_TYPE = "type";
private static final int[] KONAMI_CODE = { 200, 200, 208, 208, 203, 205, 203, 205, 48, 30 };
public static final int BOOKMARK_START = 1337;
@@ -260,15 +264,19 @@ public class GuiLexicon extends GuiScreen {
}
public void handleBookmark(GuiButton par1GuiButton) {
boolean modified = false;
int i = par1GuiButton.id - BOOKMARK_START;
if(i == bookmarks.size()) {
if(!bookmarks.contains(this))
if(!bookmarks.contains(this)) {
bookmarks.add(this);
modified = true;
}
} else {
if(isShiftKeyDown())
if(isShiftKeyDown()) {
bookmarks.remove(i);
else {
GuiLexicon bookmark = bookmarks.get(i);
modified = true;
} else {
GuiLexicon bookmark = bookmarks.get(i).copy();
Minecraft.getMinecraft().displayGuiScreen(bookmark);
if(!bookmark.getTitle().equals(getTitle())) {
if(bookmark instanceof IParented)
@@ -279,6 +287,8 @@ public class GuiLexicon extends GuiScreen {
}
bookmarksNeedPopulation = true;
if(modified)
PresistantVariableHelper.saveSafe();
}
@Override
@@ -287,6 +297,9 @@ public class GuiLexicon extends GuiScreen {
}
public int bookmarkWidth(String b) {
if(fontRendererObj == null)
fontRendererObj = Minecraft.getMinecraft().fontRenderer;
boolean unicode = fontRendererObj.getUnicodeFlag();
fontRendererObj.setUnicodeFlag(true);
int width = fontRendererObj.getStringWidth(b) + 15;
@@ -426,12 +439,41 @@ public class GuiLexicon extends GuiScreen {
super.keyTyped(par1, par2);
}
public static GuiLexicon create(NBTTagCompound cmp) {
String type = cmp.getString(TAG_TYPE);
try {
GuiLexicon lex = (GuiLexicon) Class.forName(type).newInstance();
if(lex != null)
lex.load(cmp);
if(isValidLexiconGui(lex))
return lex;
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void serialize(NBTTagCompound cmp) {
cmp.setString(TAG_TYPE, getClass().getName());
}
public void load(NBTTagCompound cmp) {
// NO-OP
}
public GuiLexicon copy() {
return new GuiLexicon();
}
public static boolean isValidLexiconGui(GuiLexicon gui) {
if(gui == null)
return false;
if(gui.isCategoryIndex())
return true;
if(gui.isIndex()) {
GuiLexiconIndex indexGui=(GuiLexiconIndex)gui;
GuiLexiconIndex indexGui = (GuiLexiconIndex) gui;
if(indexGui.category == null)
return true;
return BotaniaAPI.getAllCategories().contains(indexGui.category);
@@ -17,6 +17,7 @@ import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
@@ -24,6 +25,7 @@ import net.minecraft.util.StatCollector;
import org.lwjgl.input.Mouse;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.internal.IGuiLexiconEntry;
import vazkii.botania.api.lexicon.IAddonEntry;
import vazkii.botania.api.lexicon.LexiconEntry;
@@ -36,6 +38,9 @@ import vazkii.botania.client.gui.lexicon.button.GuiButtonViewOnline;
public class GuiLexiconEntry extends GuiLexicon implements IGuiLexiconEntry, IParented {
private static final String TAG_ENTRY = "entry";
private static final String TAG_PAGE = "page";
public int page = 0;
LexiconEntry entry;
GuiScreen parent;
@@ -44,10 +49,23 @@ public class GuiLexiconEntry extends GuiLexicon implements IGuiLexiconEntry, IPa
GuiButton leftButton, rightButton, backButton;
public GuiLexiconEntry() {
parent = new GuiLexicon();
setTitle();
}
public GuiLexiconEntry(LexiconEntry entry, GuiScreen parent) {
this.entry = entry;
this.parent = parent;
setTitle();
}
public void setTitle() {
if(entry == null) {
title = "(null)";
return;
}
title = StatCollector.translateToLocal(entry.getUnlocalizedName());
if(entry instanceof IAddonEntry)
subtitle = StatCollector.translateToLocal(((IAddonEntry) entry).getSubtitle());
@@ -341,4 +359,35 @@ public class GuiLexiconEntry extends GuiLexicon implements IGuiLexiconEntry, IPa
public float getTickDelta() {
return timeDelta;
}
@Override
public void serialize(NBTTagCompound cmp) {
super.serialize(cmp);
cmp.setString(TAG_ENTRY, entry.getUnlocalizedName());
cmp.setInteger(TAG_PAGE, page);
}
@Override
public void load(NBTTagCompound cmp) {
super.load(cmp);
String entryStr = cmp.getString(TAG_ENTRY);
for(LexiconEntry entry : BotaniaAPI.getAllEntries())
if(entry.getUnlocalizedName().equals(entryStr)) {
this.entry = entry;
break;
}
page = cmp.getInteger(TAG_PAGE);
setTitle();
}
@Override
public GuiLexicon copy() {
GuiLexiconEntry gui = new GuiLexiconEntry(entry, new GuiScreen());
gui.page = page;
gui.setTitle();
return gui;
}
}
@@ -13,6 +13,7 @@ package vazkii.botania.client.gui.lexicon;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import vazkii.botania.api.lexicon.ILexicon;
import vazkii.botania.api.lexicon.LexiconEntry;
@@ -43,4 +44,14 @@ public class GuiLexiconHistory extends GuiLexiconIndex {
history.add(entry);
}
@Override
public GuiLexicon copy() {
return new GuiLexiconHistory();
}
@Override
public void load(NBTTagCompound cmp) {
// NO-OP
}
}
@@ -16,6 +16,7 @@ import java.util.List;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.StatCollector;
@@ -32,7 +33,10 @@ import vazkii.botania.client.gui.lexicon.button.GuiButtonInvisible;
import vazkii.botania.client.gui.lexicon.button.GuiButtonPage;
public class GuiLexiconIndex extends GuiLexicon implements IParented {
private static final String TAG_CATEGORY = "category";
private static final String TAG_PAGE = "page";
LexiconCategory category;
String title;
int page = 0;
@@ -45,10 +49,18 @@ public class GuiLexiconIndex extends GuiLexicon implements IParented {
List<LexiconEntry> entriesToDisplay = new ArrayList();
public GuiLexiconIndex() {
parent = new GuiLexicon();
}
public GuiLexiconIndex(LexiconCategory category) {
this.category = category;
title = StatCollector.translateToLocal(category == null ? "botaniamisc.lexiconIndex" : category.getUnlocalizedName());
parent = new GuiLexicon();
setTitle();
}
public void setTitle() {
title = StatCollector.translateToLocal(category == null ? "botaniamisc.lexiconIndex" : category.getUnlocalizedName());
}
@Override
@@ -317,4 +329,34 @@ public class GuiLexiconIndex extends GuiLexicon implements IParented {
leftButton.func_146113_a(mc.getSoundHandler());
}
}
@Override
public void serialize(NBTTagCompound cmp) {
super.serialize(cmp);
cmp.setString(TAG_CATEGORY, category == null ? "" : category.getUnlocalizedName());
cmp.setInteger(TAG_PAGE, page);
}
@Override
public void load(NBTTagCompound cmp) {
super.load(cmp);
String categoryStr = cmp.getString(TAG_CATEGORY);
if(categoryStr.isEmpty())
category = null;
else for(LexiconCategory cat : BotaniaAPI.getAllCategories())
if(cat.getUnlocalizedName().equals(categoryStr)) {
category = cat;
break;
}
page = cmp.getInteger(TAG_PAGE);
setTitle();
}
@Override
public GuiLexicon copy() {
GuiLexiconIndex gui = new GuiLexiconIndex(category);
gui.page = page;
gui.setTitle();
return gui;
}
}