mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-22 12:59:49 +00:00
Book repository system
- Books may now have several 'repositories' which they load sections from - Page numbers dynamically calculated
This commit is contained in:
1 parent
4b5c849893
commit
59e9e52a2a
10 files changed
+131
-77
No files matched your search
@@ -24,9 +24,9 @@ import slimeknights.mantle.client.book.data.content.ContentTextImage;
|
||||
import slimeknights.mantle.client.book.data.content.ContentTextLeftImage;
|
||||
import slimeknights.mantle.client.book.data.content.ContentTextRightImage;
|
||||
import slimeknights.mantle.client.book.data.content.PageContent;
|
||||
import slimeknights.mantle.client.book.repository.BookRepository;
|
||||
import slimeknights.mantle.network.NetworkWrapper;
|
||||
import slimeknights.mantle.network.book.PacketUpdateSavedPage;
|
||||
import static slimeknights.mantle.client.book.ResourceHelper.setBookRoot;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class BookLoader implements IResourceManagerReloadListener {
|
||||
@@ -42,9 +42,6 @@ public class BookLoader implements IResourceManagerReloadListener {
|
||||
|
||||
private static final NetworkWrapper wrapper = new NetworkWrapper("mantle:books");
|
||||
|
||||
private static final Random random = new Random();
|
||||
private static char[] randAlphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
|
||||
public BookLoader() {
|
||||
((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(this);
|
||||
|
||||
@@ -93,11 +90,11 @@ public class BookLoader implements IResourceManagerReloadListener {
|
||||
* 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 location The location of the book folder, prefixed with the resource domain
|
||||
* @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, String location) {
|
||||
BookData info = new BookData(location);
|
||||
public static BookData registerBook(String name, BookRepository... repositories) {
|
||||
BookData info = new BookData(repositories);
|
||||
|
||||
books.put(name.contains(":") ? name : Loader.instance().activeModContainer().getModId() + ":" + name, info);
|
||||
|
||||
@@ -124,28 +121,13 @@ public class BookLoader implements IResourceManagerReloadListener {
|
||||
wrapper.network.sendToServer(new PacketUpdateSavedPage(page));
|
||||
}
|
||||
|
||||
public static String randomName() {
|
||||
int length = random.nextInt(10);
|
||||
|
||||
String s = "";
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
s += randAlphabet[random.nextInt(randAlphabet.length)];
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads all the books, called when the resource manager reloads, such as when the resource pack or the language is changed
|
||||
*/
|
||||
@Override
|
||||
public void onResourceManagerReload(IResourceManager resourceManager) {
|
||||
for (BookData book : books.values()) {
|
||||
setBookRoot(book.bookLocation);
|
||||
|
||||
book.pageCount = book.cascadeLoad();
|
||||
book.fullPageCount = (int) Math.ceil((book.pageCount - 1) / 2F) + 1;
|
||||
book.load();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,7 +115,7 @@ public class ResourceHelper {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void setBookRoot(String location) {
|
||||
public static void setRoot(String location) {
|
||||
bookRoot = location;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ public class AppearanceData implements IDataItem {
|
||||
public boolean drawPageNumbers = true;
|
||||
|
||||
@Override
|
||||
public int cascadeLoad() {
|
||||
return 0;
|
||||
public void load() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package slimeknights.mantle.client.book.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -10,6 +11,7 @@ import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
import slimeknights.mantle.client.book.BookLoader;
|
||||
import slimeknights.mantle.client.book.BookTransformer;
|
||||
import slimeknights.mantle.client.book.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;
|
||||
@@ -18,44 +20,43 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class BookData implements IDataItem {
|
||||
|
||||
public transient int unnamedSectionCounter = 0;
|
||||
public transient ArrayList<SectionData> sections = new ArrayList<>();
|
||||
public transient AppearanceData appearance = new AppearanceData();
|
||||
public transient int pageCount;
|
||||
public transient int fullPageCount;
|
||||
|
||||
public final transient String bookLocation;
|
||||
|
||||
protected final transient ArrayList<BookTransformer> transformers = new ArrayList<>();
|
||||
|
||||
public BookData(String bookLocation) {
|
||||
this.bookLocation = bookLocation;
|
||||
private ArrayList<BookRepository> repositories;
|
||||
|
||||
public BookData(BookRepository... repositories) {
|
||||
this.repositories = new ArrayList<>(Arrays.asList(repositories));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cascadeLoad() {
|
||||
int pages = 0;
|
||||
public void load() {
|
||||
for(BookRepository repo : repositories) {
|
||||
List<SectionData> repoContents = repo.getSections();
|
||||
sections.addAll(repoContents);
|
||||
|
||||
sections = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class)));
|
||||
if(repo.hasAppearanceData){
|
||||
ResourceLocation appearanceLocation = getResourceLocation("appearance.json");
|
||||
|
||||
ResourceLocation coverLocation = getResourceLocation("appearance.json");
|
||||
if (resourceExists(appearanceLocation))
|
||||
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class);
|
||||
else
|
||||
appearance = new AppearanceData();
|
||||
|
||||
if (resourceExists(coverLocation))
|
||||
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(coverLocation)), AppearanceData.class);
|
||||
else
|
||||
appearance = new AppearanceData();
|
||||
|
||||
appearance.cascadeLoad();
|
||||
appearance.load();
|
||||
}
|
||||
}
|
||||
|
||||
for (BookTransformer transformer : transformers)
|
||||
transformer.transform(this);
|
||||
|
||||
for (SectionData section : sections) {
|
||||
section.parent = this;
|
||||
pages += section.cascadeLoad();
|
||||
section.load();
|
||||
}
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
public SectionData findSection(String name) {
|
||||
@@ -72,7 +73,7 @@ public class BookData implements IDataItem {
|
||||
if (section == sect)
|
||||
return pages + 1;
|
||||
|
||||
pages += sect.pageCount;
|
||||
pages += sect.getPageCount();
|
||||
}
|
||||
|
||||
return -1;
|
||||
@@ -84,10 +85,10 @@ public class BookData implements IDataItem {
|
||||
|
||||
int pages = 0;
|
||||
for (SectionData section : sections) {
|
||||
if (pages + section.pageCount > number)
|
||||
if (pages + section.getPageCount() > number)
|
||||
return section.pages.get(number - pages);
|
||||
else
|
||||
pages += section.pageCount;
|
||||
pages += section.getPageCount();
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -110,7 +111,7 @@ public class BookData implements IDataItem {
|
||||
|
||||
for (SectionData section : sections) {
|
||||
if (!sectionName.equals(section.name)) {
|
||||
pages += section.pageCount;
|
||||
pages += section.getPageCount();
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -127,12 +128,30 @@ public class BookData implements IDataItem {
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int getPageCount() {
|
||||
int pages = 0;
|
||||
for(SectionData section : sections){
|
||||
pages += section.getPageCount();
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
public int getFullPageCount() {
|
||||
return (int) Math.ceil((getPageCount() - 1) / 2F) + 1;
|
||||
}
|
||||
|
||||
public void openGui(@Nullable ItemStack item) {
|
||||
Minecraft.getMinecraft().displayGuiScreen(new GuiBook(this, item));
|
||||
if(Minecraft.getMinecraft().currentScreen == null)
|
||||
Minecraft.getMinecraft().displayGuiScreen(new GuiBook(this, item));
|
||||
}
|
||||
|
||||
public void addRepository(BookRepository repository) {
|
||||
if(repository != null && !this.repositories.contains(repository))
|
||||
this.repositories.add(repository);
|
||||
}
|
||||
|
||||
public void addTransformer(BookTransformer transformer) {
|
||||
if (!transformers.contains(transformer))
|
||||
if (transformer != null && !transformers.contains(transformer))
|
||||
transformers.add(transformer);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,5 @@ import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public interface IDataItem {
|
||||
|
||||
int cascadeLoad();
|
||||
void load();
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class PageData implements IDataItem {
|
||||
|
||||
public String name = BookLoader.randomName();
|
||||
public String name = null;
|
||||
public String type = "";
|
||||
public String data = "";
|
||||
|
||||
@@ -32,7 +32,10 @@ public class PageData implements IDataItem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cascadeLoad() {
|
||||
public void load() {
|
||||
if(name == null)
|
||||
name = "page" + parent.unnamedPageCounter++;
|
||||
|
||||
name = name.toLowerCase();
|
||||
|
||||
if (!data.equals("no-load")) {
|
||||
@@ -60,10 +63,6 @@ public class PageData implements IDataItem {
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println(f.isAccessible());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -14,15 +14,13 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class SectionData implements IDataItem {
|
||||
|
||||
public String name = BookLoader.randomName();
|
||||
public String name = null;
|
||||
public String title = "";
|
||||
public ImageData icon = new ImageData();
|
||||
public CriteriaData[] unlockCriteria = new CriteriaData[0];
|
||||
public String data = "";
|
||||
|
||||
public int pageCount;
|
||||
|
||||
public transient int unnamedPageCounter = 0;
|
||||
public transient BookData parent;
|
||||
public transient ArrayList<PageData> pages = new ArrayList<>();
|
||||
|
||||
@@ -36,7 +34,10 @@ public class SectionData implements IDataItem {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int cascadeLoad() {
|
||||
public void load() {
|
||||
if(name == null)
|
||||
name = "section" + parent.unnamedSectionCounter++;
|
||||
|
||||
name = name.toLowerCase();
|
||||
|
||||
if (!data.equals("no-load")) {
|
||||
@@ -50,13 +51,13 @@ public class SectionData implements IDataItem {
|
||||
|
||||
for (PageData page : pages) {
|
||||
page.parent = this;
|
||||
page.cascadeLoad();
|
||||
page.load();
|
||||
}
|
||||
|
||||
icon.location = getResourceLocation(icon.file, true);
|
||||
}
|
||||
|
||||
pageCount = pages.size();
|
||||
|
||||
return pageCount;
|
||||
public int getPageCount() {
|
||||
return pages.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package slimeknights.mantle.client.book.repository;
|
||||
|
||||
import java.util.List;
|
||||
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 abstract List<SectionData> getSections();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package slimeknights.mantle.client.book.repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
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<SectionData> getSections() {
|
||||
setRoot(location);
|
||||
return new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class)));
|
||||
}
|
||||
}
|
||||
@@ -150,7 +150,7 @@ public class GuiBook extends GuiScreen {
|
||||
// Set color back to white
|
||||
GlStateManager.color(1F, 1F, 1F, 1F);
|
||||
|
||||
if ((page < book.fullPageCount - 1 || book.pageCount % 2 != 0) && page < book.fullPageCount) {
|
||||
if ((page < book.getFullPageCount() - 1 || book.getPageCount() % 2 != 0) && page < book.getFullPageCount()) {
|
||||
drawModalRectWithCustomSizedTexture(width / 2, height / 2 - PAGE_HEIGHT_UNSCALED / 2, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, PAGE_WIDTH_UNSCALED, PAGE_HEIGHT_UNSCALED, 512, 512);
|
||||
|
||||
GlStateManager.pushMatrix();
|
||||
@@ -196,7 +196,7 @@ public class GuiBook extends GuiScreen {
|
||||
else
|
||||
bookPage = (page - 2) / 2 + 1;
|
||||
|
||||
if (bookPage >= -1 && bookPage < book.fullPageCount) {
|
||||
if (bookPage >= -1 && bookPage < book.getFullPageCount()) {
|
||||
if (returner)
|
||||
oldPage = this.page;
|
||||
|
||||
@@ -261,16 +261,17 @@ public class GuiBook extends GuiScreen {
|
||||
super.updateScreen();
|
||||
|
||||
previousArrow.visible = page != -1;
|
||||
nextArrow.visible = page < book.fullPageCount - (book.pageCount % 2 != 0 ? 0 : 1);
|
||||
nextArrow.visible = page < book.getFullPageCount() - (book.getPageCount() % 2 != 0 ? 0 : 1);
|
||||
backArrow.visible = oldPage >= -1;
|
||||
|
||||
if (page == -1) {
|
||||
nextArrow.xPosition = width / 2 + 80;
|
||||
indexArrow.visible = false;
|
||||
} else {
|
||||
previousArrow.xPosition = width / 2 - 184;
|
||||
nextArrow.xPosition = width / 2 + 165;
|
||||
|
||||
indexArrow.visible = (page - 1) * 2 + 2 > book.findSection("index").pageCount;
|
||||
indexArrow.visible = (page - 1) * 2 + 2 > book.findSection("index").getPageCount();
|
||||
}
|
||||
|
||||
previousArrow.yPosition = height / 2 + 75;
|
||||
@@ -291,8 +292,8 @@ public class GuiBook extends GuiScreen {
|
||||
page = -1;
|
||||
} else if (button == nextArrow) {
|
||||
page++;
|
||||
if (page > book.fullPageCount - (book.pageCount % 2 != 0 ? 0 : 1))
|
||||
page = book.fullPageCount - 1;
|
||||
if (page > book.getFullPageCount() - (book.getPageCount() % 2 != 0 ? 0 : 1))
|
||||
page = book.getFullPageCount() - 1;
|
||||
} else if (button == backArrow) {
|
||||
if (oldPage >= -1)
|
||||
page = oldPage;
|
||||
@@ -343,8 +344,10 @@ public class GuiBook extends GuiScreen {
|
||||
if (page == null)
|
||||
page = book.findPage((this.page - 1) * 2 + 2);
|
||||
|
||||
if (page != null || this.page == -1)
|
||||
BookLoader.updateSavedPage(mc.thePlayer, item, this.page == -1 ? "" : (page.parent.name + "." + page.name));
|
||||
if(this.page == -1)
|
||||
BookLoader.updateSavedPage(mc.thePlayer, item, "");
|
||||
else if (page != null)
|
||||
BookLoader.updateSavedPage(mc.thePlayer, item, page.parent.name + "." + page.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user