Skip to content

Commit

Permalink
Book repository system
Browse files Browse the repository at this point in the history
- Books may now have several 'repositories' which they load sections from
- Page numbers dynamically calculated
  • Loading branch information
fuj1n committed Feb 3, 2016
1 parent 4b5c849 commit 59e9e52
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 77 deletions.
28 changes: 5 additions & 23 deletions src/main/java/slimeknights/mantle/client/book/BookLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
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 {
Expand All @@ -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);

Expand Down Expand Up @@ -93,11 +90,11 @@ public static Class<? extends PageContent> getPageType(String name) {
* 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);

Expand All @@ -124,28 +121,13 @@ public static void updateSavedPage(EntityPlayer player, ItemStack item, String p
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static String resourceToString(IResource resource, boolean skipCommments)
return "";
}

public static void setBookRoot(String location) {
public static void setRoot(String location) {
bookRoot = location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AppearanceData implements IDataItem {
public boolean drawPageNumbers = true;

@Override
public int cascadeLoad() {
return 0;
public void load() {

}
}
71 changes: 45 additions & 26 deletions src/main/java/slimeknights/mantle/client/book/data/BookData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -10,6 +11,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.BookTransformer;
import slimeknights.mantle.client.book.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;
Expand All @@ -18,44 +20,43 @@

@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;

sections = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionData[].class)));
public void load() {
for(BookRepository repo : repositories) {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);

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

if (resourceExists(coverLocation))
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(coverLocation)), AppearanceData.class);
else
appearance = new AppearanceData();
if (resourceExists(appearanceLocation))
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), 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) {
Expand All @@ -72,7 +73,7 @@ public int getFirstPageNumber(SectionData section) {
if (section == sect)
return pages + 1;

pages += sect.pageCount;
pages += sect.getPageCount();
}

return -1;
Expand All @@ -84,10 +85,10 @@ public PageData findPage(int number) {

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;
Expand All @@ -110,7 +111,7 @@ public int findPageNumber(String location) {

for (SectionData section : sections) {
if (!sectionName.equals(section.name)) {
pages += section.pageCount;
pages += section.getPageCount();
continue;
}

Expand All @@ -127,12 +128,30 @@ public int findPageNumber(String location) {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@

@SideOnly(Side.CLIENT)
public interface IDataItem {

int cascadeLoad();
void load();
}
11 changes: 5 additions & 6 deletions src/main/java/slimeknights/mantle/client/book/data/PageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@SideOnly(Side.CLIENT)
public class PageData implements IDataItem {

public String name = BookLoader.randomName();
public String name = null;
public String type = "";
public String data = "";

Expand All @@ -32,7 +32,10 @@ public PageData(boolean custom) {
}

@Override
public int cascadeLoad() {
public void load() {
if(name == null)
name = "page" + parent.unnamedPageCounter++;

name = name.toLowerCase();

if (!data.equals("no-load")) {
Expand Down Expand Up @@ -60,10 +63,6 @@ public int cascadeLoad() {
} catch (IllegalAccessException e) {
e.printStackTrace();
}

System.out.println(f.isAccessible());
}

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

@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<>();

Expand All @@ -36,7 +34,10 @@ public SectionData(boolean custom) {
}

@Override
public int cascadeLoad() {
public void load() {
if(name == null)
name = "section" + parent.unnamedSectionCounter++;

name = name.toLowerCase();

if (!data.equals("no-load")) {
Expand All @@ -50,13 +51,13 @@ public int cascadeLoad() {

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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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)));
}
}
Loading

0 comments on commit 59e9e52

Please sign in to comment.