Stopped ignoring exceptions

Closing #56
This commit is contained in:
Arthur Uzulin committed 2016-04-10 13:22:45 +10:00
1 parent caca30ad77
commit ebd2ad2675
3 files changed
+140 -93

No files matched your search

@@ -1,18 +1,21 @@
package slimeknights.mantle.client.book.data;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatFileWriter;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatFileWriter;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.BookTransformer;
import slimeknights.mantle.client.book.data.content.ContentError;
@@ -43,15 +46,15 @@ public class BookData implements IDataItem {
appearance = new AppearanceData();
itemLinks.clear();
for (BookRepository repo : repositories) {
for(BookRepository repo : repositories) {
try {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);
for (SectionData section : repoContents) {
for(SectionData section : repoContents) {
section.source = repo;
}
} catch (Exception e) {
} catch(Exception e) {
SectionData error = new SectionData();
error.name = "errorenous";
PageData page = new PageData(true);
@@ -63,33 +66,39 @@ public class BookData implements IDataItem {
ResourceLocation appearanceLocation = repo.getResourceLocation("appearance.json");
if (repo.resourceExists(appearanceLocation))
if(repo.resourceExists(appearanceLocation)) {
try {
appearance = BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(appearanceLocation)), AppearanceData.class);
} catch (Exception ignored) {
appearance = BookLoader.GSON
.fromJson(repo.resourceToString(repo.getResource(appearanceLocation)), AppearanceData.class);
} catch(Exception e) {
e.printStackTrace();
}
}
appearance.load();
ResourceLocation itemLinkLocation = repo.getResourceLocation("items.json");
if (repo.resourceExists(itemLinkLocation)) {
if(repo.resourceExists(itemLinkLocation)) {
try {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
} catch (Exception ignored) {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON
.fromJson(repo.resourceToString(repo.getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
} catch(Exception e) {
e.printStackTrace();
}
}
ResourceLocation languageLocation = repo.getResourceLocation("language.lang");
if (repo.resourceExists(languageLocation)) {
if(repo.resourceExists(languageLocation)) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(repo.getResource(languageLocation).getInputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(repo.getResource(languageLocation)
.getInputStream()));
String next = br.readLine();
while (next != null) {
if (!next.startsWith("//") && next.contains("=")) {
while(next != null) {
if(!next.startsWith("//") && next.contains("=")) {
String key = next.substring(0, next.indexOf('='));
String value = next.substring(next.indexOf('=') + 1);
@@ -98,28 +107,31 @@ public class BookData implements IDataItem {
next = br.readLine();
}
} catch (Exception ignored) {
} catch(Exception ignored) {
}
}
}
for (SectionData section : sections) {
if (section.source == null)
for(SectionData section : sections) {
if(section.source == null) {
section.source = BookRepository.DUMMY;
}
section.parent = this;
section.load();
}
for (BookTransformer transformer : transformers)
for(BookTransformer transformer : transformers) {
transformer.transform(this);
}
// Loads orphaned sections, unless something went wrong, that would only be sections added by a transformer
for (SectionData section : sections) {
if (section.source == null)
for(SectionData section : sections) {
if(section.source == null) {
section.source = BookRepository.DUMMY;
}
if (section.parent == null) {
if(section.parent == null) {
section.parent = this;
section.load();
}
@@ -131,11 +143,12 @@ public class BookData implements IDataItem {
}
public SectionData findSection(String name, @Nullable StatFileWriter writer) {
for (SectionData section : sections) {
for(SectionData section : sections) {
section.update(writer);
if (section.name.equals(name.toLowerCase()))
if(section.name.equals(name.toLowerCase())) {
return section.isUnlocked(writer) ? section : null;
}
}
return null;
@@ -147,14 +160,16 @@ public class BookData implements IDataItem {
public int getFirstPageNumber(SectionData section, @Nullable StatFileWriter writer) {
int pages = 0;
for (SectionData sect : sections) {
for(SectionData sect : sections) {
sect.update(writer);
if (section == sect)
if(section == sect) {
return section.isUnlocked(writer) ? pages + 1 : -1;
}
if (!sect.isUnlocked(writer))
if(!sect.isUnlocked(writer)) {
continue;
}
pages += sect.getPageCount();
}
@@ -167,20 +182,23 @@ public class BookData implements IDataItem {
}
public PageData findPage(int number, @Nullable StatFileWriter writer) {
if (number < 0)
if(number < 0) {
return null;
}
int pages = 0;
for (SectionData section : sections) {
for(SectionData section : sections) {
section.update(writer);
if (!section.isUnlocked(writer))
if(!section.isUnlocked(writer)) {
continue;
}
if (pages + section.getPageCount() > number)
if(pages + section.getPageCount() > number) {
return section.pages.get(number - pages);
else
} else {
pages += section.getPageCount();
}
}
return null;
@@ -203,25 +221,27 @@ public class BookData implements IDataItem {
int pages = 0;
if (!location.contains("."))
if(!location.contains(".")) {
return -1;
}
String sectionName = location.substring(0, location.indexOf('.'));
String pageName = location.substring(location.indexOf('.') + 1);
for (SectionData section : sections) {
for(SectionData section : sections) {
section.update(writer);
if (!section.isUnlocked(writer))
if(!section.isUnlocked(writer)) {
continue;
}
if (!sectionName.equals(section.name)) {
if(!sectionName.equals(section.name)) {
pages += section.getPageCount();
continue;
}
for (PageData page : section.pages) {
if (!pageName.equals(page.name)) {
for(PageData page : section.pages) {
if(!pageName.equals(page.name)) {
pages++;
continue;
}
@@ -239,7 +259,7 @@ public class BookData implements IDataItem {
public int getPageCount(@Nullable StatFileWriter writer) {
int pages = 0;
for (SectionData section : sections) {
for(SectionData section : sections) {
section.update(writer);
pages += section.isUnlocked(writer) ? section.getPageCount() : 0;
@@ -256,9 +276,10 @@ public class BookData implements IDataItem {
}
public String getItemAction(ItemStackData item) {
for (ItemStackData.ItemLink link : itemLinks) {
if (item.id.equals(link.item.id) && (!link.damageSensitive || item.damage == link.item.damage))
for(ItemStackData.ItemLink link : itemLinks) {
if(item.id.equals(link.item.id) && (!link.damageSensitive || item.damage == link.item.damage)) {
return link.action;
}
}
return "";
@@ -267,26 +288,31 @@ public class BookData implements IDataItem {
public List<SectionData> getVisibleSections(StatFileWriter writer) {
List<SectionData> visible = new ArrayList<>();
for (SectionData section : sections) {
if (section.isUnlocked(writer) || !section.hideWhenLocked)
for(SectionData section : sections) {
if(section.isUnlocked(writer) || !section.hideWhenLocked) {
visible.add(section);
}
}
return visible;
}
public void openGui(@Nullable ItemStack item) {
if (Minecraft.getMinecraft().thePlayer != null)
Minecraft.getMinecraft().displayGuiScreen(new GuiBook(this, Minecraft.getMinecraft().thePlayer.getStatFileWriter(), item));
if(Minecraft.getMinecraft().thePlayer != null) {
Minecraft.getMinecraft()
.displayGuiScreen(new GuiBook(this, Minecraft.getMinecraft().thePlayer.getStatFileWriter(), item));
}
}
public void addRepository(BookRepository repository) {
if (repository != null && !this.repositories.contains(repository))
if(repository != null && !this.repositories.contains(repository)) {
this.repositories.add(repository);
}
}
public void addTransformer(BookTransformer transformer) {
if (transformer != null && !transformers.contains(transformer))
if(transformer != null && !transformers.contains(transformer)) {
transformers.add(transformer);
}
}
}
@@ -1,11 +1,13 @@
package slimeknights.mantle.client.book.data;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.content.PageContent;
@@ -32,38 +34,40 @@ public class PageData implements IDataItem {
}
public PageData(boolean custom) {
if (custom)
if(custom) {
data = "no-load";
}
}
@Override
@SuppressWarnings("unchecked")
public void load() {
if (name == null)
if(name == null) {
name = "page" + parent.unnamedPageCounter++;
}
name = name.toLowerCase();
if (!data.equals("no-load")) {
if(!data.equals("no-load")) {
IResource pageInfo = source.getResource(source.getResourceLocation(data));
if (pageInfo != null) {
if(pageInfo != null) {
String data = source.resourceToString(pageInfo);
if (!data.isEmpty()) {
if(!data.isEmpty()) {
Class<? extends PageContent> ctype = BookLoader.getPageType(type);
try {
content = BookLoader.GSON.fromJson(data, ctype);
} catch (Exception e) {
} catch(Exception e) {
content = new ContentError(ctype == null ? "Failed to create a page of type \"" + type + "\", perhaps the type is not registered?" : "Failed to create a page of type \"" + type + "\", perhaps the page file \"" + this.data + "\" is missing or invalid?", e);
}
}
}
}
if (content == null) {
if(content == null) {
try {
content = BookLoader.getPageType(type).newInstance();
} catch (InstantiationException | IllegalAccessException | NullPointerException e) {
} catch(InstantiationException | IllegalAccessException | NullPointerException e) {
content = new ContentError("Failed to create a page of type \"" + type + "\", perhaps the type is not registered?");
}
}
@@ -71,44 +75,48 @@ public class PageData implements IDataItem {
try {
content.parent = this;
content.load();
} catch (Exception e) {
} catch(Exception e) {
content = new ContentError("Failed to load page " + parent.name + "." + name + ".", e);
e.printStackTrace();
}
content.source = source;
for (Field f : content.getClass().getFields()) {
if (Modifier.isTransient(f.getModifiers()) || Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers()))
for(Field f : content.getClass().getFields()) {
if(Modifier.isTransient(f.getModifiers()) || Modifier.isStatic(f.getModifiers()) || Modifier
.isFinal(f.getModifiers())) {
continue;
}
try {
if (f.get(content) == null)
if(f.get(content) == null) {
continue;
} catch (IllegalAccessException e) {
}
} catch(IllegalAccessException e) {
e.printStackTrace();
}
for (ValueHotswap swap : hotswaps) {
for(ValueHotswap swap : hotswaps) {
Class<?> c = f.getType();
if (c.isArray() && c.getComponentType().isAssignableFrom(swap.t))
if(c.isArray() && c.getComponentType().isAssignableFrom(swap.t)) {
try {
f.setAccessible(true);
Object[] o = (Object[]) f.get(content);
for (Object ob : o)
for(Object ob : o) {
swap.swap(source, ob);
} catch (IllegalAccessException e) {
}
} catch(IllegalAccessException e) {
e.printStackTrace();
}
else if (swap.t.isAssignableFrom(c)) {
} else if(swap.t.isAssignableFrom(c)) {
try {
f.setAccessible(true);
Object o = f.get(content);
swap.swap(source, o);
} catch (IllegalAccessException e) {
} catch(IllegalAccessException e) {
e.printStackTrace();
}
}
@@ -122,7 +130,7 @@ public class PageData implements IDataItem {
hotswap.t = t;
hotswaps.add(hotswap);
} catch (InstantiationException | IllegalAccessException e) {
} catch(InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
@@ -145,15 +153,17 @@ public class PageData implements IDataItem {
object.source = source;
object.itemListLocation = source.getResourceLocation(object.itemList);
if (object.itemListLocation != null)
if(object.itemListLocation != null) {
object.id = "->itemList";
}
}
}.getClass());
addSwap(BlockData.class, new ValueHotswap<BlockData>() {
@Override
public void swap(BookRepository source, BlockData object) {
if (object.endPos == null)
if(object.endPos == null) {
object.endPos = object.pos;
}
}
}.getClass());
}
@@ -1,14 +1,17 @@
package slimeknights.mantle.client.book.data;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Nullable;
import net.minecraft.client.resources.IResource;
import net.minecraft.stats.Achievement;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatFileWriter;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.Arrays;
import javax.annotation.Nullable;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.element.ImageData;
@@ -33,35 +36,38 @@ public class SectionData implements IDataItem {
}
public SectionData(boolean custom) {
if (custom)
if(custom) {
data = "no-load";
}
}
@Override
public void load() {
if (name == null)
if(name == null) {
name = "section" + parent.unnamedSectionCounter++;
}
name = name.toLowerCase();
if (!data.equals("no-load")) {
if(!data.equals("no-load")) {
IResource pagesInfo = source.getResource(source.getResourceLocation(data));
if (pagesInfo != null) {
if(pagesInfo != null) {
String data = source.resourceToString(pagesInfo);
if (!data.isEmpty())
if(!data.isEmpty()) {
try {
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
} catch (Exception e) {
} catch(Exception e) {
pages = new ArrayList<>();
PageData pdError = new PageData(true);
pdError.name = "errorrenous";
pdError.content = new ContentError("Failed to load section " + name + ".", e);
pages.add(pdError);
}
}
}
}
for (PageData page : pages) {
for(PageData page : pages) {
page.parent = this;
page.source = source;
page.load();
@@ -83,20 +89,23 @@ public class SectionData implements IDataItem {
}
public boolean isUnlocked(StatFileWriter writer) {
if (writer == null || requirements == null || requirements.length == 0)
if(writer == null || requirements == null || requirements.length == 0) {
return true;
}
for (String achievement : requirements) {
if (!requirementSatisfied(achievement, writer))
for(String achievement : requirements) {
if(!requirementSatisfied(achievement, writer)) {
return false;
}
}
return true;
}
public static boolean requirementSatisfied(String requirement, StatFileWriter writer) {
if (writer == null)
if(writer == null) {
return true;
}
Achievement achievement = findAchievement(requirement);
@@ -105,12 +114,14 @@ public class SectionData implements IDataItem {
}
public static Achievement findAchievement(String name) {
if (name == null || name.isEmpty())
if(name == null || name.isEmpty()) {
return null;
}
for (Achievement achievement : AchievementList.ACHIEVEMENTS) {
if (achievement.statId.equals(name))
for(Achievement achievement : AchievementList.ACHIEVEMENTS) {
if(achievement.statId.equals(name)) {
return achievement;
}
}
return null;