diff --git a/src/main/java/slimeknights/mantle/client/book/BookLoader.java b/src/main/java/slimeknights/mantle/client/book/BookLoader.java index 951f1ca..fb58950 100644 --- a/src/main/java/slimeknights/mantle/client/book/BookLoader.java +++ b/src/main/java/slimeknights/mantle/client/book/BookLoader.java @@ -26,6 +26,7 @@ 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.ContentSmithing; +import slimeknights.mantle.client.book.data.content.ContentStructure; import slimeknights.mantle.client.book.data.content.ContentText; import slimeknights.mantle.client.book.data.content.ContentTextImage; import slimeknights.mantle.client.book.data.content.ContentTextLeftImage; @@ -67,6 +68,7 @@ public class BookLoader implements IResourceManagerReloadListener { registerPageType("smelting", ContentSmelting.class); registerPageType("smithing", ContentSmithing.class); registerPageType("block interaction", ContentBlockInteraction.class); + registerPageType("structure", ContentStructure.class); // Register action protocols StringActionProcessor.registerProtocol(new ProtocolGoToPage()); @@ -94,7 +96,7 @@ public class BookLoader implements IResourceManagerReloadListener { * @return The class of the page type, ContentError.class if page type not registered */ public static Class getPageType(String name) { - return typeToContentMap.getOrDefault(name, ContentError.class); + return typeToContentMap.get(name); } /** @@ -106,11 +108,28 @@ public class BookLoader implements IResourceManagerReloadListener { * @return The book object, not immediately populated */ public static BookData registerBook(String name, BookRepository... repositories) { + return registerBook(name, true, true, repositories); + } + + /** + * Adds a book to the loader, and returns a reference object + * Be warned that the returned BookData object is not immediately populated, and is instead populated when the resources are loaded/reloaded + * + * @param name The name of the book, modid: will be automatically appended to the front of the name unless that is already added + * @param appendIndex Whether an index should be added to the front of the book using a BookTransformer + * @param appendContentTable Whether a table of contents should be added to the front of each section using a BookTransformer + * @param repositories All the repositories the book will load the sections from + * @return The book object, not immediately populated + */ + public static BookData registerBook(String name, boolean appendIndex, boolean appendContentTable, BookRepository... repositories) { BookData info = new BookData(repositories); books.put(name.contains(":") ? name : Loader.instance().activeModContainer().getModId() + ":" + name, info); - info.addTransformer(new BookTransformer.IndexTranformer()); + if (appendIndex) + info.addTransformer(new BookTransformer.IndexTranformer()); + if (appendContentTable) + info.addTransformer(new BookTransformer.ContentTableTransformer()); return info; } diff --git a/src/main/java/slimeknights/mantle/client/book/BookTransformer.java b/src/main/java/slimeknights/mantle/client/book/BookTransformer.java index bda04df..e64bdfe 100644 --- a/src/main/java/slimeknights/mantle/client/book/BookTransformer.java +++ b/src/main/java/slimeknights/mantle/client/book/BookTransformer.java @@ -8,13 +8,15 @@ 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.ContentSectionList; +import slimeknights.mantle.client.book.data.content.ContentTableOfContents; +import slimeknights.mantle.client.book.data.element.TextData; public abstract class BookTransformer { /** - * Called when all the sections within the book are loaded, but before their contents are, as the pages are counted when the contents of the sections are loaded. + * Called when all the sections within the book are loaded. * - * @param book The object to the book to transform + * @param book The object to the book to be transformed */ public abstract void transform(BookData book); @@ -57,4 +59,40 @@ public abstract class BookTransformer { book.sections.add(0, index); } } + + protected static class ContentTableTransformer extends BookTransformer { + + @Override + public void transform(BookData book) { + final int ENTRIES_PER_PAGE = 24; + + for (SectionData section : book.sections) { + if (section.name.equals("index")) + continue; + + int genPages = (int) Math.ceil(section.getPageCount() * 1.F / ENTRIES_PER_PAGE); + + if (genPages == 0) + continue; + + PageData[] pages = new PageData[genPages]; + + for (int i = 0; i < pages.length; i++) { + pages[i] = new PageData(true); + pages[i].name = "tableofcontents" + i; + TextData[] text = new TextData[i > pages.length - 1 ? ENTRIES_PER_PAGE : section.getPageCount() - (genPages - 1) * ENTRIES_PER_PAGE]; + for (int j = 0; j < text.length; j++) { + text[j] = new TextData((i * ENTRIES_PER_PAGE + j + 1) + ". " + section.pages.get(i * ENTRIES_PER_PAGE + j).getTitle()); + text[j].action = "go-to-page-rtn:" + section.name + "." + section.pages.get(i * ENTRIES_PER_PAGE + j).name; + } + + pages[i].content = new ContentTableOfContents(i == 0 ? section.getTitle() : "", text); + } + + for (int i = pages.length - 1; i >= 0; i--) { + section.pages.add(0, pages[i]); + } + } + } + } } diff --git a/src/main/java/slimeknights/mantle/client/book/ResourceHelper.java b/src/main/java/slimeknights/mantle/client/book/ResourceHelper.java deleted file mode 100644 index 5b93e21..0000000 --- a/src/main/java/slimeknights/mantle/client/book/ResourceHelper.java +++ /dev/null @@ -1,121 +0,0 @@ -package slimeknights.mantle.client.book; - -import java.io.IOException; -import java.util.Iterator; -import net.minecraft.client.Minecraft; -import net.minecraft.client.resources.IResource; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; -import org.apache.commons.io.Charsets; -import org.apache.commons.io.IOUtils; - -@SideOnly(Side.CLIENT) -public class ResourceHelper { - - private static String bookRoot; - - private ResourceHelper() { - } - - public static ResourceLocation getResourceLocation(String path) { - return getResourceLocation(path, false); - } - - public static ResourceLocation getResourceLocation(String path, boolean safe) { - if (path == null) - return safe ? new ResourceLocation("") : null; - if (!path.contains(":")) { - String langPath = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode(); - String defaultLangPath = "en_US"; - - ResourceLocation res = new ResourceLocation(bookRoot + "/" + langPath + "/" + path); - if (resourceExists(res)) - return res; - res = new ResourceLocation(bookRoot + "/" + defaultLangPath + "/" + path); - if (resourceExists(res)) - return res; - res = new ResourceLocation(bookRoot + "/" + path); - if (resourceExists(res)) - return res; - return safe ? new ResourceLocation("") : null; - } else { - ResourceLocation res = new ResourceLocation(path); - if (resourceExists(res)) - return res; - return safe ? new ResourceLocation("") : null; - } - } - - public static IResource getResource(ResourceLocation loc) { - if (loc == null) - return null; - try { - return Minecraft.getMinecraft().getResourceManager().getResource(loc); - } catch (IOException e) { - return null; - } - } - - public static boolean resourceExists(String location) { - return resourceExists(new ResourceLocation(location)); - } - - public static boolean resourceExists(ResourceLocation location) { - if (location == null) - return false; - try { - Minecraft.getMinecraft().getResourceManager().getResource(location); - return true; - } catch (IOException e) { - return false; - } - } - - public static String resourceToString(IResource resource) { - return resourceToString(resource, true); - } - - public static String resourceToString(IResource resource, boolean skipCommments) { - if (resource == null) - return ""; - try { - Iterator iterator = IOUtils.readLines(resource.getInputStream(), Charsets.UTF_8).iterator(); - StringBuilder builder = new StringBuilder(); - - boolean isLongComment = false; - - while (iterator.hasNext()) { - String s = ((String) iterator.next()).trim() + "\n"; - - // Comment skipper - if (skipCommments) { - if (isLongComment) { - if (s.endsWith("*/")) - isLongComment = false; - continue; - } else { - if (s.startsWith("/*")) { - isLongComment = true; - continue; - } - } - if (s.startsWith("//")) - continue; - } - - builder.append(s); - } - - return builder.toString().trim(); - } catch (IOException e) { - e.printStackTrace(); - } - - return ""; - } - - public static void setRoot(String location) { - bookRoot = location; - } -} diff --git a/src/main/java/slimeknights/mantle/client/book/data/BookData.java b/src/main/java/slimeknights/mantle/client/book/data/BookData.java index 4718c56..65149a7 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/BookData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/BookData.java @@ -1,7 +1,10 @@ package slimeknights.mantle.client.book.data; +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; @@ -16,10 +19,6 @@ 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; -import static slimeknights.mantle.client.book.ResourceHelper.getResource; -import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation; -import static slimeknights.mantle.client.book.ResourceHelper.resourceExists; -import static slimeknights.mantle.client.book.ResourceHelper.resourceToString; @SideOnly(Side.CLIENT) public class BookData implements IDataItem { @@ -28,6 +27,7 @@ public class BookData implements IDataItem { public transient ArrayList sections = new ArrayList<>(); public transient AppearanceData appearance = new AppearanceData(); public transient ArrayList itemLinks = new ArrayList<>(); + public transient HashMap strings = new HashMap<>(); protected final transient ArrayList transformers = new ArrayList<>(); @@ -47,6 +47,10 @@ public class BookData implements IDataItem { try { List repoContents = repo.getSections(); sections.addAll(repoContents); + + for (SectionData section : repoContents) { + section.source = repo; + } } catch (Exception e) { SectionData error = new SectionData(); error.name = "errorenous"; @@ -57,36 +61,68 @@ public class BookData implements IDataItem { sections.add(error); } - if (repo.hasAppearanceData) { - ResourceLocation appearanceLocation = getResourceLocation("appearance.json"); + ResourceLocation appearanceLocation = repo.getResourceLocation("appearance.json"); - if (resourceExists(appearanceLocation)) - try { - appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class); - } catch (Exception ignored) { - } - else - appearance = new AppearanceData(); + if (repo.resourceExists(appearanceLocation)) + try { + appearance = BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(appearanceLocation)), AppearanceData.class); + } catch (Exception ignored) { + } - appearance.load(); + appearance.load(); - ResourceLocation itemLinkLocation = getResourceLocation("items.json"); + ResourceLocation itemLinkLocation = repo.getResourceLocation("items.json"); - if (resourceExists(itemLinkLocation)) { - try { - itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class))); - } catch (Exception ignored) { - } + if (repo.resourceExists(itemLinkLocation)) { + try { + itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(itemLinkLocation)), ItemStackData.ItemLink[].class))); + } catch (Exception ignored) { } } + + ResourceLocation languageLocation = repo.getResourceLocation("language.lang"); + + if (repo.resourceExists(languageLocation)) { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(repo.getResource(languageLocation).getInputStream())); + + String next = br.readLine(); + + while (next != null) { + if (!next.startsWith("//") && next.contains("=")) { + String key = next.substring(0, next.indexOf('=')); + String value = next.substring(next.indexOf('=') + 1); + + strings.put(key, value); + } + + next = br.readLine(); + } + } catch (Exception ignored) { + } + } + } + + for (SectionData section : sections) { + if (section.source == null) + section.source = BookRepository.DUMMY; + + section.parent = this; + section.load(); } 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) { - section.parent = this; - section.load(); + if (section.source == null) + section.source = BookRepository.DUMMY; + + if (section.parent == null) { + section.parent = this; + section.load(); + } } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/PageData.java b/src/main/java/slimeknights/mantle/client/book/data/PageData.java index 07d9353..d4b3b76 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/PageData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/PageData.java @@ -9,11 +9,10 @@ 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; -import slimeknights.mantle.client.book.data.element.ImageData; +import slimeknights.mantle.client.book.data.element.BlockData; +import slimeknights.mantle.client.book.data.element.DataLocation; import slimeknights.mantle.client.book.data.element.ItemStackData; -import static slimeknights.mantle.client.book.ResourceHelper.getResource; -import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation; -import static slimeknights.mantle.client.book.ResourceHelper.resourceToString; +import slimeknights.mantle.client.book.repository.BookRepository; @SideOnly(Side.CLIENT) public class PageData implements IDataItem { @@ -25,6 +24,7 @@ public class PageData implements IDataItem { public String data = ""; public transient SectionData parent; + public transient BookRepository source; public transient PageContent content; public PageData() { @@ -37,6 +37,7 @@ public class PageData implements IDataItem { } @Override + @SuppressWarnings("unchecked") public void load() { if (name == null) name = "page" + parent.unnamedPageCounter++; @@ -44,47 +45,74 @@ public class PageData implements IDataItem { name = name.toLowerCase(); if (!data.equals("no-load")) { - IResource pageInfo = getResource(getResourceLocation(data)); + IResource pageInfo = source.getResource(source.getResourceLocation(data)); if (pageInfo != null) { - String data = resourceToString(pageInfo); - if (!data.isEmpty()) + String data = source.resourceToString(pageInfo); + if (!data.isEmpty()) { + Class ctype = BookLoader.getPageType(type); + try { - content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type)); + content = BookLoader.GSON.fromJson(data, ctype); } 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); + 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) { try { content = BookLoader.getPageType(type).newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + } catch (InstantiationException | IllegalAccessException | NullPointerException e) { content = new ContentError("Failed to create a page of type \"" + type + "\", perhaps the type is not registered?"); } } + try { + content.parent = this; + content.load(); + } 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())) + continue; + + try { + if (f.get(content) == null) + continue; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + for (ValueHotswap swap : hotswaps) { - if (f.getType().isAssignableFrom(swap.t) && !Modifier.isTransient(f.getModifiers()) && !Modifier.isStatic(f.getModifiers()) && !Modifier.isFinal(f.getModifiers())) { + Class c = f.getType(); + + if (c.isArray() && c.getComponentType().isAssignableFrom(swap.t)) + try { + f.setAccessible(true); + Object[] o = (Object[]) f.get(content); + + for (Object ob : o) + swap.swap(source, ob); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + else if (swap.t.isAssignableFrom(c)) { try { f.setAccessible(true); Object o = f.get(content); - swap.swap(o); + swap.swap(source, o); } catch (IllegalAccessException e) { e.printStackTrace(); } } } - /*if (f.getType().isAssignableFrom(ImageData.class) && !Modifier.isTransient(f.getModifiers())) - try { - f.setAccessible(true); - ImageData d = (ImageData) f.get(content); - d.location = getResourceLocation(d.file, true); - } catch (IllegalAccessException e) { - e.printStackTrace(); - }*/ } } @@ -99,28 +127,41 @@ public class PageData implements IDataItem { } } + public String getTitle() { + String title = parent.parent.strings.get(parent.name + "." + name); + return title == null ? name : title; + } + static { - addSwap(ImageData.class, new ValueHotswap() { + addSwap(DataLocation.class, new ValueHotswap() { @Override - public void swap(ImageData object) { - object.location = getResourceLocation(object.file, true); + public void swap(BookRepository source, DataLocation object) { + object.location = source.getResourceLocation(object.file, true); } }.getClass()); addSwap(ItemStackData.class, new ValueHotswap() { @Override - public void swap(ItemStackData object) { - object.itemListLocation = getResourceLocation(object.itemList); + public void swap(BookRepository source, ItemStackData object) { + object.source = source; + object.itemListLocation = source.getResourceLocation(object.itemList); if (object.itemListLocation != null) object.id = "->itemList"; } }.getClass()); + addSwap(BlockData.class, new ValueHotswap() { + @Override + public void swap(BookRepository source, BlockData object) { + if (object.endPos == null) + object.endPos = object.pos; + } + }.getClass()); } public static abstract class ValueHotswap { protected Class t; - public abstract void swap(T object); + public abstract void swap(BookRepository source, T object); } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/SectionData.java b/src/main/java/slimeknights/mantle/client/book/data/SectionData.java index 748055f..85ecc5d 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/SectionData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/SectionData.java @@ -12,15 +12,12 @@ 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.ImageData; -import static slimeknights.mantle.client.book.ResourceHelper.getResource; -import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation; -import static slimeknights.mantle.client.book.ResourceHelper.resourceToString; +import slimeknights.mantle.client.book.repository.BookRepository; @SideOnly(Side.CLIENT) public class SectionData implements IDataItem { public String name = null; - public String title = ""; public ImageData icon = new ImageData(); public String[] requirements = new String[0]; public boolean hideWhenLocked = false; @@ -28,6 +25,7 @@ public class SectionData implements IDataItem { public transient int unnamedPageCounter = 0; public transient BookData parent; + public transient BookRepository source; public transient ArrayList pages = new ArrayList<>(); public SectionData() { @@ -47,9 +45,9 @@ public class SectionData implements IDataItem { name = name.toLowerCase(); if (!data.equals("no-load")) { - IResource pagesInfo = getResource(getResourceLocation(data)); + IResource pagesInfo = source.getResource(source.getResourceLocation(data)); if (pagesInfo != null) { - String data = resourceToString(pagesInfo); + String data = source.resourceToString(pagesInfo); if (!data.isEmpty()) try { pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class))); @@ -65,15 +63,21 @@ public class SectionData implements IDataItem { for (PageData page : pages) { page.parent = this; + page.source = source; page.load(); } - icon.location = getResourceLocation(icon.file, true); + icon.location = source.getResourceLocation(icon.file, true); } public void update(@Nullable StatFileWriter writer) { } + public String getTitle() { + String title = parent.strings.get(name); + return title == null ? name : title; + } + public int getPageCount() { return pages.size(); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java index 4054dda..0fb9d04 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java @@ -38,7 +38,7 @@ public class ContentError extends PageContent { text[1].color = "dark_red"; text[1].paragraph = true; - text[2] = new TextData(exception.getMessage()); + text[2] = new TextData(exception.getMessage() != null ? exception.getMessage() : exception.getClass().getSimpleName()); text[2].color = "dark_red"; text[2].paragraph = true; } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java new file mode 100644 index 0000000..d4f7065 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentStructure.java @@ -0,0 +1,53 @@ +package slimeknights.mantle.client.book.data.content; + +import java.util.ArrayList; +import net.minecraft.util.ResourceLocation; +import slimeknights.mantle.client.book.BookLoader; +import slimeknights.mantle.client.book.data.BookData; +import slimeknights.mantle.client.book.data.element.BlockData; +import slimeknights.mantle.client.book.repository.BookRepository; +import slimeknights.mantle.client.gui.book.GuiBook; +import slimeknights.mantle.client.gui.book.element.BookElement; +import slimeknights.mantle.client.gui.book.element.ElementStructure; +import slimeknights.mantle.client.gui.book.element.ElementText; + +public class ContentStructure extends PageContent { + + public String title; + public String data; + + public int[] size; + public BlockData[] structure; + + @Override + public void load() { + BookRepository repo = parent.source; + + if (data == null || data.isEmpty()) + return; + + ResourceLocation location = repo.getResourceLocation(data); + + if (location != null && repo.resourceExists(location)) { + ContentStructure structure = BookLoader.GSON.fromJson(repo.resourceToString(repo.getResource(location)), ContentStructure.class); + structure.parent = parent; + structure.load(); + size = structure.size; + this.structure = structure.structure; + } + } + + @Override + public void build(BookData book, ArrayList list) { + int y = TITLE_HEIGHT; + if (title == null || title.isEmpty()) + y = 0; + else + addTitle(list, title); + + if (size != null && size.length == 3 && structure != null && structure.length > 0) + list.add(new ElementStructure(0, y, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - y - 10, size, structure)); + + list.add(new ElementText(0, GuiBook.PAGE_HEIGHT - 10, GuiBook.PAGE_WIDTH, 10, "WIP - Not Yet Implemented")); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentTableOfContents.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTableOfContents.java new file mode 100644 index 0000000..bcea5b4 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentTableOfContents.java @@ -0,0 +1,35 @@ +package slimeknights.mantle.client.book.data.content; + +import java.util.ArrayList; +import net.minecraft.client.Minecraft; +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; +import slimeknights.mantle.client.gui.book.element.ElementText; + +public class ContentTableOfContents extends PageContent { + + public String title; + public TextData[] data; + + public ContentTableOfContents(String title, TextData... contents) { + this.title = title; + data = contents; + } + + @Override + public void build(BookData book, ArrayList list) { + int y = 0; + + if (title != null && !title.trim().isEmpty()) { + addTitle(list, title); + y += TITLE_HEIGHT; + } + + for (int i = 0; i < data.length; i++) { + TextData text = data[i]; + list.add(new ElementText(0, y + i * (int) (Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT * text.scale), GuiBook.PAGE_WIDTH, Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT, new TextData[]{text})); + } + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java b/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java index dad038c..efe64da 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/PageContent.java @@ -4,7 +4,9 @@ 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.PageData; import slimeknights.mantle.client.book.data.element.TextData; +import slimeknights.mantle.client.book.repository.BookRepository; import slimeknights.mantle.client.gui.book.GuiBook; import slimeknights.mantle.client.gui.book.element.BookElement; import slimeknights.mantle.client.gui.book.element.ElementText; @@ -14,6 +16,12 @@ public abstract class PageContent { public static final transient int TITLE_HEIGHT = 16; + public transient PageData parent; + public transient BookRepository source; + + public void load() { + } + public abstract void build(BookData book, ArrayList list); public void addTitle(ArrayList list, String title) { diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java b/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java new file mode 100644 index 0000000..c6feb15 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/element/BlockData.java @@ -0,0 +1,12 @@ +package slimeknights.mantle.client.book.data.element; + +import com.google.gson.JsonObject; + +public class BlockData { + + public int[] pos; + public int[] endPos; + public String block; + public byte meta; + public JsonObject nbt; +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/DataLocation.java b/src/main/java/slimeknights/mantle/client/book/data/element/DataLocation.java new file mode 100644 index 0000000..c2ed7ea --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/element/DataLocation.java @@ -0,0 +1,9 @@ +package slimeknights.mantle.client.book.data.element; + +import net.minecraft.util.ResourceLocation; + +public class DataLocation { + + public String file; + public transient ResourceLocation location; +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java b/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java index 4f514a0..4ca924e 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/ImageData.java @@ -1,19 +1,14 @@ package slimeknights.mantle.client.book.data.element; -import net.minecraft.block.Block; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) -public class ImageData { +public class ImageData extends DataLocation { public static final ImageData MISSING = new ImageData(); - public String file = ""; public int u = 0; public int v = 0; public int uw = 256; @@ -26,8 +21,6 @@ public class ImageData { public int height = -1; public int colorMultiplier = 0xFFFFFF; - public transient ResourceLocation location; - public ImageData() { } @@ -63,16 +56,4 @@ public class ImageData { MISSING.uw = 32; MISSING.vh = 32; } - - public static ImageData from(Block block, int meta, EnumFacing face) { - return null; - } - - public static ImageData from(Item item, int damage) { - return null; - } - - public static ImageData from(ItemStack stack) { - return null; - } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java b/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java index 479cd9d..f191ba2 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java +++ b/src/main/java/slimeknights/mantle/client/book/data/element/ItemStackData.java @@ -13,13 +13,12 @@ import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagString; import net.minecraft.util.ResourceLocation; import slimeknights.mantle.client.book.BookLoader; -import static slimeknights.mantle.client.book.ResourceHelper.getResource; -import static slimeknights.mantle.client.book.ResourceHelper.resourceExists; -import static slimeknights.mantle.client.book.ResourceHelper.resourceToString; +import slimeknights.mantle.client.book.repository.BookRepository; public class ItemStackData { public String itemList = null; + public transient BookRepository source; public transient ResourceLocation itemListLocation = null; public transient String action; @@ -29,9 +28,9 @@ public class ItemStackData { public JsonObject nbt; public ItemStack[] getItems() { - if (itemListLocation != null && resourceExists(itemListLocation)) { + if (itemListLocation != null && source.resourceExists(itemListLocation)) { try { - ItemsList itemsList = BookLoader.GSON.fromJson(resourceToString(getResource(itemListLocation)), ItemsList.class); + ItemsList itemsList = BookLoader.GSON.fromJson(source.resourceToString(source.getResource(itemListLocation)), ItemsList.class); ItemStack[] items = new ItemStack[itemsList.items.length]; for (int i = 0; i < itemsList.items.length; i++) { diff --git a/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java index c41d5e6..d4667b6 100644 --- a/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java +++ b/src/main/java/slimeknights/mantle/client/book/repository/BookRepository.java @@ -1,19 +1,33 @@ package slimeknights.mantle.client.book.repository; import java.util.List; +import net.minecraft.client.resources.IResource; +import net.minecraft.util.ResourceLocation; import slimeknights.mantle.client.book.data.SectionData; public abstract class BookRepository { - public final boolean hasAppearanceData; - - public BookRepository() { - this(false); - } - - public BookRepository(boolean hasAppearanceData) { - this.hasAppearanceData = hasAppearanceData; - } + public static final BookRepository DUMMY = new DummyRepository(); public abstract List getSections(); + + public ResourceLocation getResourceLocation(String path) { + return getResourceLocation(path, false); + } + + public abstract ResourceLocation getResourceLocation(String path, boolean safe); + + public abstract IResource getResource(ResourceLocation loc); + + public boolean resourceExists(String location) { + return resourceExists(new ResourceLocation(location)); + } + + public abstract boolean resourceExists(ResourceLocation location); + + public String resourceToString(IResource resource) { + return resourceToString(resource, true); + } + + public abstract String resourceToString(IResource resource, boolean skipCommments); } diff --git a/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java new file mode 100644 index 0000000..3eb5d2e --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/repository/DummyRepository.java @@ -0,0 +1,34 @@ +package slimeknights.mantle.client.book.repository; + +import java.util.List; +import net.minecraft.client.resources.IResource; +import net.minecraft.util.ResourceLocation; +import slimeknights.mantle.client.book.data.SectionData; + +public class DummyRepository extends BookRepository { + + @Override + public List getSections() { + return null; + } + + @Override + public ResourceLocation getResourceLocation(String path, boolean safe) { + return null; + } + + @Override + public IResource getResource(ResourceLocation loc) { + return null; + } + + @Override + public boolean resourceExists(ResourceLocation location) { + return false; + } + + @Override + public String resourceToString(IResource resource, boolean skipCommments) { + return null; + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java index 2a8c571..0ae0edf 100644 --- a/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java +++ b/src/main/java/slimeknights/mantle/client/book/repository/FileRepository.java @@ -1,32 +1,117 @@ package slimeknights.mantle.client.book.repository; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.List; +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.IResource; +import net.minecraft.util.ResourceLocation; +import org.apache.commons.io.Charsets; +import org.apache.commons.io.IOUtils; import slimeknights.mantle.client.book.BookLoader; import slimeknights.mantle.client.book.data.SectionData; -import static slimeknights.mantle.client.book.ResourceHelper.getResource; -import static slimeknights.mantle.client.book.ResourceHelper.getResourceLocation; -import static slimeknights.mantle.client.book.ResourceHelper.resourceToString; -import static slimeknights.mantle.client.book.ResourceHelper.setRoot; public class FileRepository extends BookRepository { public final String location; public FileRepository(String location) { - this(location, false); - } - - public FileRepository(String location, boolean hasAppearanceData) { - super(hasAppearanceData); - this.location = location; } @Override public List getSections() { - setRoot(location); return new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class))); } + + @Override + public ResourceLocation getResourceLocation(String path, boolean safe) { + if (path == null) + return safe ? new ResourceLocation("") : null; + if (!path.contains(":")) { + String langPath = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode(); + String defaultLangPath = "en_US"; + + ResourceLocation res = new ResourceLocation(location + "/" + langPath + "/" + path); + if (resourceExists(res)) + return res; + res = new ResourceLocation(location + "/" + defaultLangPath + "/" + path); + if (resourceExists(res)) + return res; + res = new ResourceLocation(location + "/" + path); + if (resourceExists(res)) + return res; + return safe ? new ResourceLocation("") : null; + } else { + ResourceLocation res = new ResourceLocation(path); + if (resourceExists(res)) + return res; + return safe ? new ResourceLocation("") : null; + } + } + + @Override + public IResource getResource(ResourceLocation loc) { + if (loc == null) + return null; + try { + return Minecraft.getMinecraft().getResourceManager().getResource(loc); + } catch (IOException e) { + return null; + } + } + + @Override + public boolean resourceExists(ResourceLocation location) { + if (location == null) + return false; + try { + Minecraft.getMinecraft().getResourceManager().getResource(location); + return true; + } catch (IOException e) { + return false; + } + } + + @Override + public String resourceToString(IResource resource, boolean skipCommments) { + if (resource == null) + return ""; + try { + Iterator iterator = IOUtils.readLines(resource.getInputStream(), Charsets.UTF_8).iterator(); + StringBuilder builder = new StringBuilder(); + + boolean isLongComment = false; + + while (iterator.hasNext()) { + String s = ((String) iterator.next()).trim() + "\n"; + + // Comment skipper + if (skipCommments) { + if (isLongComment) { + if (s.endsWith("*/")) + isLongComment = false; + continue; + } else { + if (s.startsWith("/*")) { + isLongComment = true; + continue; + } + } + if (s.startsWith("//")) + continue; + } + + builder.append(s); + } + + return builder.toString().trim(); + } catch (IOException e) { + e.printStackTrace(); + } + + return ""; + } } diff --git a/src/main/java/slimeknights/mantle/client/gui/book/BoxRenderer.java b/src/main/java/slimeknights/mantle/client/gui/book/BoxRenderer.java new file mode 100644 index 0000000..d14cfe3 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/BoxRenderer.java @@ -0,0 +1,64 @@ +package slimeknights.mantle.client.gui.book; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.util.MathHelper; +import static slimeknights.mantle.client.gui.book.Textures.TEX_BOX; + +public class BoxRenderer { + + public static final int TEX_SIZE = 128; + + public static void drawBox(int x, int y, int width, int height, int index) { + drawBox(x, y, width, height, index, 16, 16, 4, 4, 2, 2); + } + + public static void drawBox(int x, int y, int width, int height, int index, int sprW, int sprH, int segW, int segH, int segGW, int segGH) { + Minecraft.getMinecraft().renderEngine.bindTexture(TEX_BOX); + + int sx = x, sy = y; + + for (int i = 0; i < 3; i++) { + int sgW = width - segW * 2; + + if (i % 2 == 0) + sgW = segW; + + for (int j = 0; j < 3; j++) { + int sgH = height - segH * 2; + + if (j % 2 == 0) + sgH = segH; + + int[] matrix = getTextureMatrixCoords(index, sprW, sprH, segW, segH, segGW, segGH, i + j * 3); + Gui.drawScaledCustomSizeModalRect(sx, sy, matrix[0], matrix[1], segW, segH, sgW, sgH, TEX_SIZE, TEX_SIZE); + + sy += sgH; + } + + sx += sgW; + sy = y; + } + } + + public static int[] getTextureMatrixCoords(int index, int sprW, int sprH, int segW, int segH, int segGW, int segGH, int segId) { + int totalWidth = segW * 3 + segGW * 2; + int totalHeight = segH * 3 + segGH * 2; + + return getTextureMatrixCoords((index % (256 / sprW) * sprW) + (sprW / 2 - totalWidth / 2), (MathHelper.floor_float(index / (256F / sprH)) * sprH) + (sprH / 2 - totalHeight / 2), segW, segH, segGW, segGH, segId); + } + + public static int[] getTextureMatrixCoords(int u, int v, int segW, int segH, int segGW, int segGH, int segId) { + int[] coords = new int[4]; + + int col = segId % 3; + int row = MathHelper.floor_float(segId / 3F); + + coords[0] = u + col * segW + col * segGW; + coords[1] = v + row * segH + row * segGH; + coords[2] = coords[0] + segW; + coords[3] = coords[1] + segH; + + return coords; + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java b/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java index 83af8d4..15ca069 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/GuiArrow.java @@ -4,15 +4,13 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import static slimeknights.mantle.client.gui.book.Textures.TEX_BOOK; @SideOnly(Side.CLIENT) public class GuiArrow extends GuiButton { - private static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png"); - private static final int X = 412; public static final int WIDTH = 18; diff --git a/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java b/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java index 8f6860e..6a1b8b0 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/GuiBook.java @@ -31,6 +31,10 @@ import static slimeknights.mantle.client.gui.book.Textures.TEX_BOOKFRONT; @SideOnly(Side.CLIENT) public class GuiBook extends GuiScreen implements IProgressMeter { + public static boolean debug = false; + + public static final int TEX_SIZE = 512; + public static final int PAGE_PADDING = 8; public static final int PAGE_MARGIN = 18; @@ -71,6 +75,7 @@ public class GuiBook extends GuiScreen implements IProgressMeter { } @Override + @SuppressWarnings("ForLoopReplaceableByForEach") public void drawScreen(int mouseX, int mouseY, float partialTicks) { FontRenderer font = mc.fontRendererObj; @@ -83,6 +88,11 @@ public class GuiBook extends GuiScreen implements IProgressMeter { return; } + if (debug) { + drawRect(0, 0, font.getStringWidth("DEBUG") + 4, font.FONT_HEIGHT + 4, 0x55000000); + font.drawString("DEBUG", 2, 2, 0xFFFFFFFF); + } + GlStateManager.enableAlpha(); GlStateManager.enableBlend(); @@ -107,11 +117,11 @@ public class GuiBook extends GuiScreen implements IProgressMeter { render.bindTexture(TEX_BOOKFRONT); GlStateManager.color(coverR, coverG, coverB); - drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, 0, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, 512, 512); + drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, 0, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, TEX_SIZE, TEX_SIZE); GlStateManager.color(1F, 1F, 1F); if (!book.appearance.title.isEmpty()) { - drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, 512, 512); + drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, TEX_SIZE, TEX_SIZE); GlStateManager.pushMatrix(); @@ -132,12 +142,12 @@ public class GuiBook extends GuiScreen implements IProgressMeter { render.bindTexture(TEX_BOOK); GlStateManager.color(coverR, coverG, coverB); - drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, 0, PAGE_WIDTH_UNSCALED * 2, PAGE_HEIGHT_UNSCALED, 512, 512); + drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, 0, PAGE_WIDTH_UNSCALED * 2, PAGE_HEIGHT_UNSCALED, TEX_SIZE, TEX_SIZE); GlStateManager.color(1F, 1F, 1F); if (page != 0) { - drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, 512, 512); + drawModalRectWithCustomSizedTexture(width / 2 - PAGE_WIDTH_UNSCALED, height / 2 - PAGE_HEIGHT_UNSCALED / 2, 0, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, TEX_SIZE, TEX_SIZE); GlStateManager.pushMatrix(); drawerTransform(false); @@ -149,12 +159,15 @@ public class GuiBook extends GuiScreen implements IProgressMeter { font.drawString(pNum, PAGE_WIDTH / 2 - font.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false); } + int mX = (int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE); + int mY = (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE); + // Not foreach to prevent conmodification crashes for (int i = 0; i < leftElements.size(); i++) { BookElement element = leftElements.get(i); GlStateManager.color(1F, 1F, 1F, 1F); - element.draw((int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks); + element.draw(mX, mY, partialTicks); } // Not foreach to prevent conmodification crashes @@ -162,7 +175,7 @@ public class GuiBook extends GuiScreen implements IProgressMeter { BookElement element = leftElements.get(i); GlStateManager.color(1F, 1F, 1F, 1F); - element.drawOverlay((int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2 - PAGE_WIDTH_UNSCALED) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks); + element.drawOverlay(mX, mY, partialTicks); } GlStateManager.popMatrix(); @@ -174,7 +187,7 @@ public class GuiBook extends GuiScreen implements IProgressMeter { GlStateManager.color(1F, 1F, 1F, 1F); if ((page < book.getFullPageCount(statFile) - 1 || book.getPageCount(statFile) % 2 != 0) && page < book.getFullPageCount(statFile)) { - drawModalRectWithCustomSizedTexture(width / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, 512, 512); + drawModalRectWithCustomSizedTexture(width / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, TEX_SIZE, TEX_SIZE); GlStateManager.pushMatrix(); drawerTransform(true); @@ -186,12 +199,15 @@ public class GuiBook extends GuiScreen implements IProgressMeter { font.drawString(pNum, PAGE_WIDTH / 2 - font.getStringWidth(pNum) / 2, PAGE_HEIGHT + 15, 0xFFAAAAAA, false); } + int mX = (int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE); + int mY = (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE); + // Not foreach to prevent conmodification crashes for (int i = 0; i < rightElements.size(); i++) { BookElement element = rightElements.get(i); GlStateManager.color(1F, 1F, 1F, 1F); - element.draw((int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks); + element.draw(mX, mY, partialTicks); } // Not foreach to prevent conmodification crashes @@ -199,7 +215,7 @@ public class GuiBook extends GuiScreen implements IProgressMeter { BookElement element = rightElements.get(i); GlStateManager.color(1F, 1F, 1F, 1F); - element.drawOverlay((int) ((Mouse.getX() * this.width / this.mc.displayWidth - (width / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), (int) ((this.height - Mouse.getY() * this.height / this.mc.displayHeight - 1 - (height / 2 - PAGE_HEIGHT_UNSCALED / 2) - PAGE_PADDING - PAGE_MARGIN) / PAGE_SCALE), partialTicks); + element.drawOverlay(mX, mY, partialTicks); } GlStateManager.popMatrix(); @@ -211,16 +227,16 @@ public class GuiBook extends GuiScreen implements IProgressMeter { GlStateManager.popMatrix(); } - public void openPage(int page) { - openPage(page, false); + public int openPage(int page) { + return openPage(page, false); } - public void openPage(int page, boolean returner) { + public int openPage(int page, boolean returner) { if (loadingAchievements) - return; + return -1; if (page < 0) - return; + return -1; int bookPage; if (page == 1) @@ -234,9 +250,47 @@ public class GuiBook extends GuiScreen implements IProgressMeter { if (returner) oldPage = this.page; - this.page = bookPage; - buildPages(); + _setPage(bookPage); } + + return page % 2 == 0 ? 0 : 1; + } + + public void _setPage(int page) { + this.page = page; + buildPages(); + } + + public int getPage(int side) { + if (page == 0 && side == 0) + return -1; + else if (page == 0 && side == 1) + return 0; + else if (side == 0) + return (page - 1) * 2 + 1; + else if (side == 1) + return (page - 2) * 2 + 2; + else + return -1; + } + + public int getPage_() { + return page; + } + + public ArrayList getElements(int side) { + return side == 0 ? leftElements : side == 1 ? rightElements : null; + } + + public void openCover() { + if (loadingAchievements) + return; + + _setPage(-1); + + this.leftElements.clear(); + this.rightElements.clear(); + buildPages(); } public void itemClicked(ItemStack item) { @@ -372,10 +426,19 @@ public class GuiBook extends GuiScreen implements IProgressMeter { if (loadingAchievements) return; - if (keyCode == Keyboard.KEY_LEFT || keyCode == Keyboard.KEY_A) - actionPerformed(previousArrow); - else if (keyCode == Keyboard.KEY_RIGHT || keyCode == Keyboard.KEY_D) - actionPerformed(nextArrow); + switch (keyCode) { + case Keyboard.KEY_LEFT: + case Keyboard.KEY_A: + actionPerformed(previousArrow); + break; + case Keyboard.KEY_RIGHT: + case Keyboard.KEY_D: + actionPerformed(nextArrow); + break; + case Keyboard.KEY_F3: + debug = !debug; + break; + } } @Override diff --git a/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java b/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java index b28924e..12ab528 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/TextDataRenderer.java @@ -115,15 +115,28 @@ public class TextDataRenderer { action = item.action; } - if (atY >= boxHeight) { + if (atY >= y + boxHeight) { fr.drawString("...", atX, atY, 0, false); break; } y = atY; } - if (drawLabel != null) + if (GuiBook.debug && action != null && !action.isEmpty()) { + String[] label = drawLabel; + drawLabel = new String[label != null ? label.length + 2 : 1]; + + if (label != null) + for (int i = 0; i < label.length; i++) + drawLabel[i] = label[i]; + + drawLabel[drawLabel.length > 1 ? drawLabel.length - 2 : 0] = ""; + drawLabel[drawLabel.length - 1] = EnumChatFormatting.GRAY + "Action: " + action; + } + + if (drawLabel != null) { drawHoveringText(drawLabel, mouseX, mouseY, fr); + } return action; } diff --git a/src/main/java/slimeknights/mantle/client/gui/book/Textures.java b/src/main/java/slimeknights/mantle/client/gui/book/Textures.java index 1a604e4..4d9059b 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/Textures.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/Textures.java @@ -4,8 +4,9 @@ import net.minecraft.util.ResourceLocation; public class Textures { - public static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book.png"); - public static final ResourceLocation TEX_BOOKFRONT = new ResourceLocation("mantle:textures/gui/bookfront.png"); + public static final ResourceLocation TEX_BOOK = new ResourceLocation("mantle:textures/gui/book/book.png"); + public static final ResourceLocation TEX_BOOKFRONT = new ResourceLocation("mantle:textures/gui/book/bookfront.png"); + public static final ResourceLocation TEX_BOX = new ResourceLocation("mantle:textures/gui/box.png"); public static final ResourceLocation TEX_CRAFTING = new ResourceLocation("mantle:textures/gui/book/crafting.png"); public static final ResourceLocation TEX_SMELTING = new ResourceLocation("mantle:textures/gui/book/smelting.png"); diff --git a/src/main/java/slimeknights/mantle/client/gui/book/editor/GuiBookEditor.java b/src/main/java/slimeknights/mantle/client/gui/book/editor/GuiBookEditor.java new file mode 100644 index 0000000..0162c31 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/editor/GuiBookEditor.java @@ -0,0 +1,85 @@ +package slimeknights.mantle.client.gui.book.editor; + +import javax.annotation.Nullable; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.texture.TextureManager; +import slimeknights.mantle.client.book.data.BookData; +import slimeknights.mantle.client.gui.book.BoxRenderer; +import slimeknights.mantle.client.gui.book.GuiBook; +import slimeknights.mantle.client.gui.book.Textures; + +public class GuiBookEditor extends GuiScreen { + + private static final int SIDEBAR_WIDTH = 120; + + private GuiBook innerUi; + private int side; + + public GuiBookEditor(@Nullable BookData book) { + if (book == null) + book = new BookData(); + + innerUi = new GuiBook(book, null, null); + innerUi.mc = Minecraft.getMinecraft(); + innerUi.doneLoading(); + + innerUi._setPage(-1); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + GlStateManager.pushMatrix(); + + TextureManager render = this.mc.renderEngine; + + if (mc.gameSettings.guiScale == 1) { + GlStateManager.scale(2F, 2F, 2F); + + mouseX /= 2; + mouseY /= 2; + } + + BoxRenderer.drawBox(width / 2 - (GuiBook.PAGE_WIDTH_UNSCALED + SIDEBAR_WIDTH) / 2, height / 2 - (GuiBook.PAGE_HEIGHT_UNSCALED - GuiBook.PAGE_PADDING) / 2, SIDEBAR_WIDTH + GuiBook.PAGE_PADDING + GuiBook.PAGE_MARGIN, GuiBook.PAGE_HEIGHT_UNSCALED - GuiBook.PAGE_PADDING, 0); + + if (innerUi.getPage_() == -1) { + GlStateManager.pushMatrix(); + if (mc.gameSettings.guiScale == 1) + GlStateManager.scale(0.5F, 0.5F, 0.5F); + innerUi.openCover(); + innerUi.drawScreen(mouseX * (mc.gameSettings.guiScale == 1 ? 2 : 1), mouseY * (mc.gameSettings.guiScale == 1 ? 2 : 1), partialTicks); + GlStateManager.popMatrix(); + } else { + render.bindTexture(Textures.TEX_BOOK); + + GlStateManager.color(1F, 1F, 1F); + drawModalRectWithCustomSizedTexture(width / 2 - GuiBook.PAGE_WIDTH_UNSCALED / 2, height / 2 - GuiBook.PAGE_HEIGHT_UNSCALED / 2, 0, GuiBook.PAGE_HEIGHT_UNSCALED, GuiBook.PAGE_WIDTH_UNSCALED - GuiBook.PAGE_MARGIN - GuiBook.PAGE_PADDING, GuiBook.PAGE_HEIGHT_UNSCALED, GuiBook.TEX_SIZE, GuiBook.TEX_SIZE); + drawModalRectWithCustomSizedTexture(width / 2 - GuiBook.PAGE_WIDTH_UNSCALED / 2 + GuiBook.PAGE_MARGIN + GuiBook.PAGE_PADDING, height / 2 - GuiBook.PAGE_HEIGHT_UNSCALED / 2, GuiBook.PAGE_WIDTH_UNSCALED + GuiBook.PAGE_MARGIN + GuiBook.PAGE_PADDING, GuiBook.PAGE_HEIGHT_UNSCALED, GuiBook.PAGE_WIDTH_UNSCALED - GuiBook.PAGE_MARGIN - GuiBook.PAGE_PADDING, GuiBook.PAGE_HEIGHT_UNSCALED, GuiBook.TEX_SIZE, GuiBook.TEX_SIZE); + } + + super.drawScreen(mouseX, mouseY, partialTicks); + GlStateManager.popMatrix(); + } + + @Override + public void initGui() { + buttonList.clear(); + + innerUi.width = width + SIDEBAR_WIDTH * (mc.gameSettings.guiScale == 1 ? 2 : 1); + innerUi.height = height; + + if (mc.gameSettings.guiScale == 1) { + width /= 2F; + height /= 2F; + } + + innerUi.initGui(); + innerUi.updateScreen(); + innerUi.buttonList.clear(); + + buttonList.add(new GuiButton(0, 0, height - 20, 50, 20, "Import")); + buttonList.add(new GuiButton(1, 55, height - 20, 50, 20, "Export")); + } +} diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java b/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java index 843144b..dd2c709 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/BookElement.java @@ -5,6 +5,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.Gui; import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; import net.minecraftforge.fml.relauncher.Side; @@ -16,6 +17,10 @@ public abstract class BookElement extends Gui { public GuiBook parent; + protected Minecraft mc = Minecraft.getMinecraft(); + protected FontRenderer fontRenderer = mc.fontRendererObj; + protected TextureManager renderEngine = mc.renderEngine; + public int x, y; public BookElement(int x, int y) { @@ -33,7 +38,7 @@ public abstract class BookElement extends Gui { } public void renderToolTip(FontRenderer fontRenderer, ItemStack stack, int x, int y) { - List list = stack.getTooltip(Minecraft.getMinecraft().thePlayer, Minecraft.getMinecraft().gameSettings.advancedItemTooltips); + List list = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips); for (int i = 0; i < list.size(); ++i) { if (i == 0) { diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java index 28a9f54..673c66a 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementImage.java @@ -1,6 +1,5 @@ package slimeknights.mantle.client.gui.book.element; -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -49,7 +48,7 @@ public class ElementImage extends SizedBookElement { @Override public void draw(int mouseX, int mouseY, float partialTicks) { - Minecraft.getMinecraft().renderEngine.bindTexture(image.location); + renderEngine.bindTexture(image.location); float r = ((colorMultiplier >> 16) & 0xff) / 255.F; float g = ((colorMultiplier >> 8) & 0xff) / 255.F; diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java index bc21de0..2f769d9 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementItem.java @@ -1,7 +1,6 @@ package slimeknights.mantle.client.gui.book.element; import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.item.Item; @@ -65,7 +64,7 @@ public class ElementItem extends BookElement { GlStateManager.scale(scale, scale, 1.0F); if (currentItem < itemCycle.length) - Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(itemCycle[currentItem], 0, 0); + mc.getRenderItem().renderItemAndEffectIntoGUI(itemCycle[currentItem], 0, 0); GlStateManager.popMatrix(); } @@ -73,7 +72,7 @@ public class ElementItem extends BookElement { @Override public void drawOverlay(int mouseX, int mouseY, float partialTicks) { if (mouseX >= x && mouseY >= y && mouseX <= x + ITEM_SIZE_HARDCODED * scale && mouseY <= y + ITEM_SIZE_HARDCODED * scale && currentItem < itemCycle.length) { - renderToolTip(Minecraft.getMinecraft().fontRendererObj, itemCycle[currentItem], mouseX, mouseY); + renderToolTip(fontRenderer, itemCycle[currentItem], mouseX, mouseY); } } diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementSection.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementSection.java index ebb409f..e1a149d 100644 --- a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementSection.java +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementSection.java @@ -29,7 +29,7 @@ public class ElementSection extends BookElement { boolean hover = mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT; if (section.icon != null) { - Minecraft.getMinecraft().renderEngine.bindTexture(section.icon.location); + renderEngine.bindTexture(section.icon.location); if (unlocked) GlStateManager.color(1F, 1F, 1F, hover ? 1F : 0.5F); @@ -43,7 +43,7 @@ public class ElementSection extends BookElement { drawScaledCustomSizeModalRect(x + WIDTH / 2 - IMG_SIZE / 2, y + HEIGHT / 2 - IMG_SIZE / 2, section.icon.u, section.icon.v, section.icon.uw, section.icon.vh, IMG_SIZE, IMG_SIZE, section.icon.texWidth, section.icon.texHeight); } - Minecraft.getMinecraft().fontRendererObj.drawString(section.title, x + WIDTH / 2 - Minecraft.getMinecraft().fontRendererObj.getStringWidth(section.title) / 2, y + HEIGHT - Minecraft.getMinecraft().fontRendererObj.FONT_HEIGHT, hover ? 0xFF000000 : 0x7F000000); + fontRenderer.drawString(section.getTitle(), x + WIDTH / 2 - fontRenderer.getStringWidth(section.getTitle()) / 2, y + HEIGHT - fontRenderer.FONT_HEIGHT, hover ? 0xFF000000 : 0x7F000000); } @Override diff --git a/src/main/java/slimeknights/mantle/client/gui/book/element/ElementStructure.java b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementStructure.java new file mode 100644 index 0000000..b2652ac --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/gui/book/element/ElementStructure.java @@ -0,0 +1,324 @@ +package slimeknights.mantle.client.gui.book.element; + +import java.util.HashMap; +import java.util.List; +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.BlockModelRenderer; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.renderer.WorldRenderer; +import net.minecraft.client.renderer.block.model.BakedQuad; +import net.minecraft.client.renderer.texture.TextureMap; +import net.minecraft.client.renderer.texture.TextureUtil; +import net.minecraft.client.resources.model.IBakedModel; +import net.minecraft.init.Blocks; +import net.minecraft.nbt.JsonToNBT; +import net.minecraft.nbt.NBTException; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.BlockPos; +import net.minecraft.util.EnumFacing; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.WorldType; +import net.minecraft.world.biome.BiomeGenBase; +import org.lwjgl.opengl.GL11; +import slimeknights.mantle.client.book.data.element.BlockData; +import slimeknights.mantle.client.book.data.element.ItemStackData; + +public class ElementStructure extends SizedBookElement { + + private BlockAccess world; + private float scale = 50.0F; + private float xTranslate = 0F; + private float yTranslate = 0F; + + private float w = 0F; + private float h = 0F; + + private float rotX = -11.25F; + private float rotY = 45F; + private float rotZ = 0F; + + public ElementStructure(int x, int y, int width, int height, int[] size, BlockData[] structure) { + super(x, y, width, height); + world = new BlockAccess(size, structure); + + if (size.length == 3) { + scale = size[0] > size[1] ? width / size[0] - 10F : height / size[1] - 10F; + + if (scale * size[0] > width) + scale = width / size[0] - 10F; + + xTranslate = x + width / 2 - (size[0] * scale) / 2; + yTranslate = y + height / 2 - (size[1] * scale) / 2; + + w = size[0] * scale; + h = size[1] * scale; + } + } + + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + GlStateManager.pushMatrix(); + + GlStateManager.translate(xTranslate, yTranslate, 500F); + + GlStateManager.cullFace(GL11.GL_FRONT); + + // Prepare stencil + GL11.glEnable(GL11.GL_STENCIL_TEST); + GL11.glColorMask(false, false, false, false); + GL11.glDepthMask(false); + GL11.glStencilFunc(GL11.GL_NEVER, 1, 0xFF); + GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_KEEP, GL11.GL_KEEP); + + GL11.glStencilMask(0xFF); + GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT); + + GL11.glBegin(GL11.GL_QUADS); + GL11.glVertex2i(0, 0); + GL11.glVertex2i(0, height); + GL11.glVertex2i(width, height); + GL11.glVertex2i(width, 0); + GL11.glEnd(); + + GL11.glColorMask(true, true, true, true); + GL11.glDepthMask(true); + GL11.glStencilMask(0x00); + GL11.glStencilFunc(GL11.GL_EQUAL, 0, 0xFF); + + GlStateManager.color(0F, 1F, 0F, 1F); + GL11.glBegin(GL11.GL_QUADS); + GL11.glVertex2i(0, 0); + GL11.glVertex2i(0, height); + GL11.glVertex2i(width, height); + GL11.glVertex2i(width, 0); + GL11.glEnd(); + + GlStateManager.translate(w / 2, h / 2, 0); + GlStateManager.rotate(rotX, 1F, 0F, 0F); + GlStateManager.rotate(rotY, 0F, 1F, 0F); + GlStateManager.rotate(rotZ, 0F, 0F, 1F); + GlStateManager.translate(-w / 2, -h / 2, 0); + + for (int x = 0; x < world.getWidth(); x++) { + for (int y = 0; y < world.getHeight(); y++) { + for (int z = 0; z < world.getDepth(); z++) { + BlockPos pos = new BlockPos(x, y, z); + IBlockState state = world.getBlockState(pos); + IBakedModel model = mc.getBlockRendererDispatcher().getModelFromBlockState(world.getBlockState(pos), world, pos); + + Block block = state.getBlock(); + + if (block == Blocks.air) + continue; + + GlStateManager.pushMatrix(); + + GlStateManager.enableDepth(); + mc.renderEngine.bindTexture(TextureMap.locationBlocksTexture); + + if (block == null) + return; + + int color = block.colorMultiplier(world, pos, 0); + float red = (color >> 16 & 255) / 255.0F; + float green = (color >> 8 & 255) / 255.0F; + float blue = (color & 255) / 255.0F; + GlStateManager.color(red, green, blue); + + GlStateManager.scale(scale, scale, scale); + + GlStateManager.translate(x, world.getHeight() - y, z); + BlockModelRenderer render = mc.getBlockRendererDispatcher().getBlockModelRenderer(); + + int i = block.getRenderColor(block.getStateForEntityRender(state)); + block.setBlockBoundsBasedOnState(world, pos); + + GlStateManager.rotate(90F, 0, 1, 0); + + if (EntityRenderer.anaglyphEnable) { + i = TextureUtil.anaglyphColor(i); + } + + float r = (float) (i >> 16 & 255) / 255.0F; + float g = (float) (i >> 8 & 255) / 255.0F; + float b = (float) (i & 255) / 255.0F; + + render.renderModelBrightnessColor(model, block.colorMultiplier(world, pos, 0), r, g, b); + + GlStateManager.color(1F, 1F, 1F); + GlStateManager.popMatrix(); + renderEngine.getTexture(TextureMap.locationBlocksTexture).restoreLastBlurMipmap(); + } + } + } + + GlStateManager.disableLighting(); + GlStateManager.cullFace(GL11.GL_BACK); + GL11.glDisable(GL11.GL_STENCIL_TEST); + GlStateManager.popMatrix(); + } + + private void setupGuiTransform(int xPosition, int yPosition) { + GlStateManager.translate((float) xPosition, (float) yPosition, 100.0F + this.zLevel); + GlStateManager.translate(8.0F, 8.0F, 0.0F); + GlStateManager.scale(1.0F, 1.0F, -1.0F); + GlStateManager.scale(0.5F, 0.5F, 0.5F); + + GlStateManager.scale(40.0F, 40.0F, 40.0F); + GlStateManager.rotate(210.0F, 1.0F, 0.0F, 0.0F); + GlStateManager.rotate(-135.0F, 0.0F, 1.0F, 0.0F); + GlStateManager.enableLighting(); + } + + private void renderQuads(WorldRenderer renderer, List quads, int color, BlockPos pos) { + boolean flag = color == -1; + int i = 0; + + for (int j = quads.size(); i < j; ++i) { + BakedQuad bakedquad = quads.get(i); + int k = color; + + if (flag && bakedquad.hasTintIndex()) { + Block block = world.getBlockState(pos).getBlock(); + k = block.colorMultiplier(world, pos, 0); + + if (EntityRenderer.anaglyphEnable) { + k = TextureUtil.anaglyphColor(k); + } + + k = k | -16777216; + } + + net.minecraftforge.client.model.pipeline.LightUtil.renderQuadColor(renderer, bakedquad, k); + } + } + + private class BlockAccess implements IBlockAccess { + + private final int[][][] blocks; + private final byte[][][] meta; + private final int[] size; + private HashMap tileEntityMap = new HashMap<>(); + + public BlockAccess(int[] size, BlockData[] structure) { + this.size = size; + + blocks = new int[size[0]][size[1]][size[2]]; + meta = new byte[size[0]][size[1]][size[2]]; + + for (BlockData block : structure) { + Block tile = Block.getBlockFromName(block.block); + if (tile == null) + continue; + byte metadata = block.meta >= 0 && block.meta < 16 ? block.meta : 0; + + if (block.pos != null && block.pos.length == 3 && block.endPos != null && block.endPos.length == 3) { + for (int x = block.pos[0]; x <= block.endPos[0]; x++) { + for (int y = block.pos[1]; y <= block.endPos[1]; y++) { + for (int z = block.pos[2]; z <= block.endPos[2]; z++) { + if (x >= size[0] || y >= size[1] || z >= size[2]) + continue; + + blocks[x][y][z] = Block.getIdFromBlock(tile); + meta[x][y][z] = metadata; + + TileEntity te = tile.hasTileEntity(tile.getStateFromMeta(metadata)) ? tile.createTileEntity(Minecraft.getMinecraft().theWorld, tile.getStateFromMeta(metadata)) : null; + if (te != null) { + te.setPos(new BlockPos(x, y, z)); + tileEntityMap.put(new Integer[]{x, y, z}, te); + + if (block.nbt != null) { + try { + te.readFromNBT(JsonToNBT.getTagFromJson(ItemStackData.filterJsonQuotes(block.nbt.toString()))); + } catch (NBTException e) { + e.printStackTrace(); + } + } + } + } + } + } + } + } + } + + @Override + public TileEntity getTileEntity(BlockPos pos) { + if (!isValid(pos)) + return null; + return tileEntityMap.get(new Integer[]{pos.getX(), pos.getY(), pos.getZ()}); + } + + @Override + public int getCombinedLight(BlockPos pos, int lightValue) { + return 15; + } + + @Override + public IBlockState getBlockState(BlockPos pos) { + if (!isValid(pos)) + return Blocks.air.getDefaultState(); + Block block = Block.getBlockById(blocks[pos.getX()][pos.getY()][pos.getZ()]); + if (block == null) + return Blocks.air.getDefaultState(); + + return block.getActualState(block.getStateFromMeta(meta[pos.getX()][pos.getY()][pos.getZ()]), this, pos); + } + + @Override + public boolean isAirBlock(BlockPos pos) { + return !isValid(pos) || Block.getBlockById(blocks[pos.getX()][pos.getY()][pos.getZ()]) == Blocks.air; + } + + @Override + public BiomeGenBase getBiomeGenForCoords(BlockPos pos) { + return BiomeGenBase.jungle; + } + + @Override + public boolean extendedLevelsInChunkCache() { + return false; + } + + @Override + public int getStrongPower(BlockPos pos, EnumFacing direction) { + if (!isValid(pos)) + return 0; + + IBlockState iblockstate = this.getBlockState(pos); + return iblockstate.getBlock().getStrongPower(this, pos, iblockstate, direction); + } + + @Override + public WorldType getWorldType() { + return WorldType.DEFAULT; + } + + @Override + public boolean isSideSolid(BlockPos pos, EnumFacing side, boolean _default) { + if (!this.isValid(pos)) + return _default; + + return getBlockState(pos).getBlock().isSideSolid(this, pos, side); + } + + public boolean isValid(BlockPos pos) { + return pos != null && pos.getX() < size[0] && pos.getY() < size[1] && pos.getZ() < size[2] && pos.getX() >= 0 && pos.getY() >= 0 && pos.getZ() >= 0; + } + + public int getWidth() { + return size[0]; + } + + public int getHeight() { + return size[1]; + } + + public int getDepth() { + return size[2]; + } + } +} diff --git a/src/main/resources/assets/mantle/books/TestBook/en_US/language.lang b/src/main/resources/assets/mantle/books/TestBook/en_US/language.lang new file mode 100644 index 0000000..7644376 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/en_US/language.lang @@ -0,0 +1,11 @@ +// Sections +test section=Test Section +section 2=Locked Section +section 3=Hidden Section +// Test Section Pages +test section.page1=Blank Page +test section.pagecrafter=Crafting Page +test section.pagesmallcrafting=Small Crafting Page +test section.pagesmelting=Smelting Page +test section.pageblockinteraction=Block Interaction Page +test section.pagestructure=3D Model Page \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/en_US/section1/pagestructure.json b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/pagestructure.json new file mode 100644 index 0000000..12a2159 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/en_US/section1/pagestructure.json @@ -0,0 +1,3 @@ +{ + "data": "structures/netherportal.json" +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/index.json b/src/main/resources/assets/mantle/books/TestBook/index.json index 93377ea..863cd00 100644 --- a/src/main/resources/assets/mantle/books/TestBook/index.json +++ b/src/main/resources/assets/mantle/books/TestBook/index.json @@ -1,7 +1,6 @@ [ { "name": "test section", - "title": "Test Section", "icon": { "file": "images/test.png" }, @@ -9,7 +8,6 @@ }, { "name": "section 2", - "title": "Locked Section", "icon": { "file": "images/test.png" }, diff --git a/src/main/resources/assets/mantle/books/TestBook/sections/section1.json b/src/main/resources/assets/mantle/books/TestBook/sections/section1.json index c31d0ca..2b4cc49 100644 --- a/src/main/resources/assets/mantle/books/TestBook/sections/section1.json +++ b/src/main/resources/assets/mantle/books/TestBook/sections/section1.json @@ -28,6 +28,11 @@ "type": "block interaction", "data": "section1/pageblkinteraction.json" }, + { + "name": "PageStructure", + "type": "structure", + "data": "section1/pagestructure.json" + }, { "name": "Page2", "type": "text", diff --git a/src/main/resources/assets/mantle/books/TestBook/structures/netherportal.json b/src/main/resources/assets/mantle/books/TestBook/structures/netherportal.json new file mode 100644 index 0000000..60f2b69 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/structures/netherportal.json @@ -0,0 +1,31 @@ +{ + "size": [4, 5, 1], + "structure": [ + { + "pos": [0, 0, 0], + "endPos": [3, 0, 0], + "block": "minecraft:obsidian" + }, + { + "pos": [0, 0, 0], + "endPos": [0, 4, 0], + "block": "minecraft:obsidian" + }, + { + "pos": [3, 0, 0], + "endPos": [3, 4, 0], + "block": "minecraft:obsidian" + }, + { + "pos": [0, 4, 0], + "endPos": [3, 4, 0], + "block": "minecraft:obsidian" + }, + { + "pos": [1, 1, 0], + "endPos": [2, 3, 0], + "block": "minecraft:portal", + "meta": 2 + } + ] +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/books/TestBook/structures/stonesmeltery.json b/src/main/resources/assets/mantle/books/TestBook/structures/stonesmeltery.json new file mode 100644 index 0000000..44e7727 --- /dev/null +++ b/src/main/resources/assets/mantle/books/TestBook/structures/stonesmeltery.json @@ -0,0 +1,40 @@ +{ + "size": [5, 3, 5], + "structure": [ + { + "pos": [1, 0, 1], + "endPos": [3, 0, 3], + "block": "minecraft:stone" + }, + { + "pos": [1, 1, 4], + "endPos": [4, 2, 4], + "block": "minecraft:stone" + }, + { + "pos": [0, 1, 1], + "endPos": [0, 2, 4], + "block": "minecraft:stone" + }, + { + "pos": [4, 1, 1], + "endPos": [4, 2, 4], + "block": "minecraft:stone" + }, + { + "pos": [1, 1, 0], + "endPos": [4, 2, 0], + "block": "minecraft:stone" + }, + { + "pos": [0, 2, 0], + "block": "minecraft:stone", + "meta": 6 + }, + { + "pos": [0, 3, 0], + "block": "minecraft:stone", + "meta": 4 + } + ] +} \ No newline at end of file diff --git a/src/main/resources/assets/mantle/textures/gui/book.png b/src/main/resources/assets/mantle/textures/gui/book/book.png similarity index 100% rename from src/main/resources/assets/mantle/textures/gui/book.png rename to src/main/resources/assets/mantle/textures/gui/book/book.png diff --git a/src/main/resources/assets/mantle/textures/gui/bookfront.png b/src/main/resources/assets/mantle/textures/gui/book/bookfront.png similarity index 100% rename from src/main/resources/assets/mantle/textures/gui/bookfront.png rename to src/main/resources/assets/mantle/textures/gui/book/bookfront.png diff --git a/src/main/resources/assets/mantle/textures/gui/box.png b/src/main/resources/assets/mantle/textures/gui/box.png new file mode 100644 index 0000000..aeae336 Binary files /dev/null and b/src/main/resources/assets/mantle/textures/gui/box.png differ