Skip to content

Commit

Permalink
Book crafting pages
Browse files Browse the repository at this point in the history
- Added crafting and smelting page types to the book
- Improved error handling
- Line breaks in ResourceHelper.getStringFromResource output
- 2 layers for pages instead of one
- ElementItem now does item cycling
  • Loading branch information
fuj1n committed Feb 6, 2016
1 parent 413b43d commit 1ac2d95
Show file tree
Hide file tree
Showing 35 changed files with 516 additions and 53 deletions.
21 changes: 20 additions & 1 deletion src/main/java/slimeknights/mantle/client/book/BookLoader.java
Expand Up @@ -14,10 +14,14 @@
import slimeknights.mantle.client.book.action.StringActionProcessor;
import slimeknights.mantle.client.book.action.protocol.ProtocolGoToPage;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.PageData;
import slimeknights.mantle.client.book.data.SectionData;
import slimeknights.mantle.client.book.data.content.ContentBlank;
import slimeknights.mantle.client.book.data.content.ContentCrafting;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.content.ContentImage;
import slimeknights.mantle.client.book.data.content.ContentImageText;
import slimeknights.mantle.client.book.data.content.ContentSmelting;
import slimeknights.mantle.client.book.data.content.ContentText;
import slimeknights.mantle.client.book.data.content.ContentTextImage;
import slimeknights.mantle.client.book.data.content.ContentTextLeftImage;
Expand Down Expand Up @@ -54,6 +58,8 @@ public BookLoader() {
registerPageType("text with image below", ContentTextImage.class);
registerPageType("text with left image etch", ContentTextLeftImage.class);
registerPageType("text with right image etch", ContentTextRightImage.class);
registerPageType("crafting", ContentCrafting.class);
registerPageType("smelting", ContentSmelting.class);

// Register action protocols
StringActionProcessor.registerProtocol(new ProtocolGoToPage());
Expand Down Expand Up @@ -126,7 +132,20 @@ public static void updateSavedPage(EntityPlayer player, ItemStack item, String p
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
for (BookData book : books.values()) {
book.load();
try {
book.load();
} catch (Exception e) {
book.sections.clear();
SectionData section = new SectionData(true);
section.name = "errorenous";
PageData page = new PageData(true);
page.name = "errorenous";
page.content = new ContentError("Failed to load the book due to an unexpected error.", e);
section.pages.add(page);
book.sections.add(section);

e.printStackTrace();
}
}
}
}
Expand Up @@ -86,7 +86,7 @@ public static String resourceToString(IResource resource, boolean skipCommments)
boolean isLongComment = false;

while (iterator.hasNext()) {
String s = ((String) iterator.next()).trim();
String s = ((String) iterator.next()).trim() + "\n";

// Comment skipper
if (skipCommments) {
Expand Down
29 changes: 25 additions & 4 deletions src/main/java/slimeknights/mantle/client/book/data/BookData.java
Expand Up @@ -11,6 +11,7 @@
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;
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.book.repository.BookRepository;
import slimeknights.mantle.client.gui.book.GuiBook;
Expand All @@ -37,15 +38,32 @@ public BookData(BookRepository... repositories) {

@Override
public void load() {
sections.clear();
appearance = new AppearanceData();
itemLinks.clear();

for (BookRepository repo : repositories) {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);
try {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);
} catch (Exception e) {
SectionData error = new SectionData();
error.name = "errorenous";
PageData page = new PageData(true);
page.name = "errorenous";
page.content = new ContentError("Failed to load repository " + repo.toString() + ".", e);
error.pages.add(page);
sections.add(error);
}

if (repo.hasAppearanceData) {
ResourceLocation appearanceLocation = getResourceLocation("appearance.json");

if (resourceExists(appearanceLocation))
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class);
try {
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class);
} catch (Exception ignored) {
}
else
appearance = new AppearanceData();

Expand All @@ -54,7 +72,10 @@ public void load() {
ResourceLocation itemLinkLocation = getResourceLocation("items.json");

if (resourceExists(itemLinkLocation)) {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
try {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
} catch (Exception ignored) {
}
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/slimeknights/mantle/client/book/data/PageData.java
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand Down Expand Up @@ -43,19 +44,24 @@ public void load() {
if (pageInfo != null) {
String data = resourceToString(pageInfo);
if (!data.isEmpty())
content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type));
try {
content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type));
} catch (Exception e) {
content = new ContentError("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) != null ? BookLoader.getPageType(type).newInstance() : new ContentError();
content = BookLoader.getPageType(type).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
content = new ContentError("Failed to create a page of type \"" + type + "\", perhaps the type is not registered?");
}
}

for (Field f : content.getClass().getFields()) {
if (f.getType().isAssignableFrom(ImageData.class))
if (f.getType().isAssignableFrom(ImageData.class) && !Modifier.isTransient(f.getModifiers()))
try {
f.setAccessible(true);
ImageData d = (ImageData) f.get(content);
Expand Down
Expand Up @@ -6,6 +6,7 @@
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.element.CriteriaData;
import slimeknights.mantle.client.book.data.element.ImageData;
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
Expand Down Expand Up @@ -46,7 +47,15 @@ public void load() {
if (pagesInfo != null) {
String data = resourceToString(pagesInfo);
if (!data.isEmpty())
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
try {
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
} 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);
}
}
}

Expand Down
Expand Up @@ -3,13 +3,14 @@
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;

@SideOnly(Side.CLIENT)
public class ContentBlank extends PageContent {

@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {

}
}
@@ -0,0 +1,80 @@
package slimeknights.mantle.client.book.data.content;

import java.util.ArrayList;
import net.minecraft.util.ResourceLocation;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementItem;
import slimeknights.mantle.client.gui.book.element.ElementText;

public class ContentCrafting extends PageContent {

public static final transient ResourceLocation TEX_CRAFTING = new ResourceLocation("mantle:textures/gui/book/crafting.png");
public static final transient int TEX_SIZE = 256;
public static final transient ImageData IMG_CRAFTING_LARGE = new ImageData(TEX_CRAFTING, 0, 0, 183, 114, TEX_SIZE, TEX_SIZE);
public static final transient ImageData IMG_CRAFTING_SMALL = new ImageData(TEX_CRAFTING, 0, 114, 155, 78, TEX_SIZE, TEX_SIZE);

public static final transient int X_RESULT_SMALL = 118;
public static final transient int Y_RESULT_SMALL = 23;
public static final transient int X_RESULT_LARGE = 146;
public static final transient int Y_RESULT_LARGE = 41;

public static final transient float ITEM_SCALE = 2.0F;
public static final transient int SLOT_MARGIN = 5;
public static final transient int SLOT_PADDING = 4;

public String title = "Crafting";
public String grid_size = "large";
public ItemStackData[][] grid;
public ItemStackData result;
public TextData[] description;

@Override
public void build(BookData book, ArrayList<BookElement> list) {
int x = 0;
int y = 16;
int height = 100;
int resultX = 100;
int resultY = 50;

TextData tdTitle = new TextData(title);
tdTitle.underlined = true;
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, 9, new TextData[]{tdTitle}));

if (grid_size.equalsIgnoreCase("small")) {
x = GuiBook.PAGE_WIDTH / 2 - IMG_CRAFTING_SMALL.width / 2;
height = y + IMG_CRAFTING_SMALL.height;
list.add(new ElementImage(x, y, IMG_CRAFTING_SMALL.width, IMG_CRAFTING_SMALL.height, IMG_CRAFTING_SMALL, book.appearance.coverColor));
resultX = x + X_RESULT_SMALL;
resultY = y + Y_RESULT_SMALL;
} else if (grid_size.equalsIgnoreCase("large")) {
x = GuiBook.PAGE_WIDTH / 2 - IMG_CRAFTING_LARGE.width / 2;
height = y + IMG_CRAFTING_LARGE.height;
list.add(new ElementImage(x, y, IMG_CRAFTING_LARGE.width, IMG_CRAFTING_LARGE.height, IMG_CRAFTING_LARGE, book.appearance.coverColor));
resultX = x + X_RESULT_LARGE;
resultY = y + Y_RESULT_LARGE;
}

if (grid != null)
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j].id.equals(""))
continue;
list.add(new ElementItem(x + SLOT_MARGIN + (SLOT_PADDING + Math.round(ElementItem.ITEM_SIZE_HARDCODED * ITEM_SCALE)) * j, y + SLOT_MARGIN + (SLOT_PADDING + Math.round(ElementItem.ITEM_SIZE_HARDCODED * ITEM_SCALE)) * i, ITEM_SCALE, grid[i][j].getItemStack()));
}
}

if (result != null) {
list.add(new ElementItem(resultX, resultY, ITEM_SCALE, result.getItemStack()));
}

if (description != null && description.length > 0) {
list.add(new ElementText(0, height + 5, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - height - 5, description));
}
}
}
Expand Up @@ -3,13 +3,14 @@
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;

@SideOnly(Side.CLIENT)
public class ContentDummy extends PageContent {

@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
//TODO load from JSON
}
}
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
Expand All @@ -11,13 +12,35 @@
@SideOnly(Side.CLIENT)
public class ContentError extends PageContent {

private String errorStage;
private Exception exception;

public ContentError(String errorStage) {
this.errorStage = errorStage;
}

public ContentError(String errorStage, Exception e) {
this(errorStage);
this.exception = e;
}

@Override
public void build(ArrayList<BookElement> list) {
TextData[] text = new TextData[1];
text[0] = new TextData("Could not load page, perhaps the file is missing or the page type does not exist?");
public void build(BookData book, ArrayList<BookElement> list) {
TextData[] text = new TextData[1 + (exception != null ? 2 : 0)];
text[0] = new TextData(errorStage);
text[0].color = "dark_red";
text[0].underlined = true;

if (exception != null) {
text[1] = new TextData("The following error has occured: ");
text[1].color = "dark_red";
text[1].paragraph = true;

text[2] = new TextData(exception.getMessage());
text[2].color = "dark_red";
text[2].paragraph = true;
}

list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, text));
}
}
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
Expand All @@ -14,7 +15,7 @@ public class ContentImage extends PageContent {
public ImageData image;

@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (image != null && image.location != null)
list.add(new ElementImage(0, 0, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, image));
else
Expand Down
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
Expand All @@ -17,7 +18,7 @@ public class ContentImageText extends PageContent {
public TextData[] text;

@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (image != null && image.location != null)
list.add(new ElementImage(0, 0, GuiBook.PAGE_WIDTH, 100, image));
else
Expand Down
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data.content;

import java.util.ArrayList;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.SectionData;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementSection;
Expand All @@ -10,15 +11,11 @@ public class ContentSectionList extends PageContent {
protected ArrayList<SectionData> sections = new ArrayList<>();

public boolean addSection(SectionData data) {
if (sections.size() >= 9) {
return false;
}

return sections.add(data);
return sections.size() < 9 && sections.add(data);
}

@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
int width = (ElementSection.WIDTH + 5) * 3 - 5;
int height = (ElementSection.HEIGHT + 5) * 3 - 5;

Expand Down

0 comments on commit 1ac2d95

Please sign in to comment.