Skip to content

Commit

Permalink
Reformat book code
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Apr 10, 2016
1 parent ebd2ad2 commit 13e1a8f
Show file tree
Hide file tree
Showing 39 changed files with 560 additions and 348 deletions.
10 changes: 6 additions & 4 deletions src/main/java/slimeknights/mantle/client/book/BookHelper.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package slimeknights.mantle.client.book;

import java.util.Arrays;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;

import java.util.Arrays;

public class BookHelper {

public static String getSavedPage(ItemStack item) {
if (item != null && item.hasTagCompound()) {
if(item != null && item.hasTagCompound()) {
NBTTagCompound mantleBook = item.getTagCompound().getCompoundTag("mantle").getCompoundTag("book");

if (mantleBook.hasKey("page", Arrays.asList(NBTBase.NBT_TYPES).indexOf("STRING"))) {
if(mantleBook.hasKey("page", Arrays.asList(NBTBase.NBT_TYPES).indexOf("STRING"))) {
return mantleBook.getString("page");
}
}
Expand All @@ -22,8 +23,9 @@ public static String getSavedPage(ItemStack item) {
public static void writeSavedPage(ItemStack item, String page) {
NBTTagCompound compound = item.getTagCompound();

if (compound == null)
if(compound == null) {
compound = new NBTTagCompound();
}

NBTTagCompound mantle = compound.getCompoundTag("mantle");
NBTTagCompound book = mantle.getCompoundTag("book");
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/slimeknights/mantle/client/book/BookLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.HashMap;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
Expand All @@ -13,6 +13,9 @@
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.HashMap;

import slimeknights.mantle.Mantle;
import slimeknights.mantle.client.book.action.StringActionProcessor;
import slimeknights.mantle.client.book.action.protocol.ProtocolGoToPage;
Expand Down Expand Up @@ -42,7 +45,8 @@
public class BookLoader implements IResourceManagerReloadListener {

/** GSON object to be used for book loading purposes */
public static final Gson GSON = new GsonBuilder().registerTypeAdapter(int.class, new HexStringDeserializer()).create();
public static final Gson GSON = new GsonBuilder().registerTypeAdapter(int.class, new HexStringDeserializer())
.create();

/** Maps page content presets to names */
private static final HashMap<String, Class<? extends PageContent>> typeToContentMap = new HashMap<>();
Expand Down Expand Up @@ -84,8 +88,9 @@ public BookLoader() {
* @RecommendedInvoke init
*/
public static void registerPageType(String name, Class<? extends PageContent> clazz) {
if (typeToContentMap.containsKey(name))
if(typeToContentMap.containsKey(name)) {
throw new IllegalArgumentException("Page type " + name + " already in use.");
}

typeToContentMap.put(name, clazz);
}
Expand Down Expand Up @@ -127,10 +132,12 @@ public static BookData registerBook(String name, boolean appendIndex, boolean ap

books.put(name.contains(":") ? name : Loader.instance().activeModContainer().getModId() + ":" + name, info);

if (appendIndex)
if(appendIndex) {
info.addTransformer(new BookTransformer.IndexTranformer());
if (appendContentTable)
}
if(appendContentTable) {
info.addTransformer(new BookTransformer.ContentTableTransformer());
}

return info;
}
Expand All @@ -146,8 +153,9 @@ public static BookData getBook(String name) {
}

public static void updateSavedPage(EntityPlayer player, ItemStack item, String page) {
if (player.getHeldItem(EnumHand.MAIN_HAND) == null)
if(player.getHeldItem(EnumHand.MAIN_HAND) == null) {
return;
}

BookHelper.writeSavedPage(item, page);
wrapper.network.sendToServer(new PacketUpdateSavedPage(page));
Expand All @@ -161,10 +169,10 @@ public void onResourceManagerReload(IResourceManager resourceManager) {
Mantle.logger.info("Started loading books...");
long time = System.nanoTime();

for (BookData book : books.values()) {
for(BookData book : books.values()) {
try {
book.load();
} catch (Exception e) {
} catch(Exception e) {
book.sections.clear();
SectionData section = new SectionData(true);
section.name = "errorenous";
Expand Down
31 changes: 19 additions & 12 deletions src/main/java/slimeknights/mantle/client/book/BookTransformer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package slimeknights.mantle.client.book;

import net.minecraft.stats.StatFileWriter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.minecraft.stats.StatFileWriter;

import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.PageData;
import slimeknights.mantle.client.book.data.SectionData;
Expand Down Expand Up @@ -31,22 +33,23 @@ public void update(StatFileWriter writer) {

List<SectionData> visibleSections = parent.getVisibleSections(writer);

if (visibleSections.isEmpty())
if(visibleSections.isEmpty()) {
return;
}

visibleSections.remove(0);

PageData[] pages = new PageData[(int) Math.ceil(visibleSections.size() / 9F)];

for (int i = 0; i < pages.length; i++) {
for(int i = 0; i < pages.length; i++) {
pages[i] = new PageData(true);

pages[i].name = "page" + (i + 1);

ContentSectionList content = new ContentSectionList();
pages[i].content = content;

for (int j = i * 9; j - i * 9 < 9 && j < visibleSections.size(); j++) {
for(int j = i * 9; j - i * 9 < 9 && j < visibleSections.size(); j++) {
content.addSection(visibleSections.get(j));
}
}
Expand All @@ -66,30 +69,34 @@ protected static class ContentTableTransformer extends BookTransformer {
public void transform(BookData book) {
final int ENTRIES_PER_PAGE = 24;

for (SectionData section : book.sections) {
if (section.name.equals("index"))
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)
if(genPages == 0) {
continue;
}

PageData[] pages = new PageData[genPages];

for (int i = 0; i < pages.length; i++) {
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());
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--) {
for(int i = pages.length - 1; i >= 0; i--) {
section.pages.add(0, pages[i]);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.action;

import java.util.HashMap;

import slimeknights.mantle.client.book.action.protocol.ActionProtocol;
import slimeknights.mantle.client.gui.book.GuiBook;

Expand All @@ -11,23 +12,27 @@ public class StringActionProcessor {
private static final HashMap<String, ActionProtocol> protocols = new HashMap<>();

public static void registerProtocol(ActionProtocol protocol) {
if (protocol == null || protocol.protocol == null || protocol.protocol.isEmpty())
if(protocol == null || protocol.protocol == null || protocol.protocol.isEmpty()) {
throw new IllegalArgumentException("Protocol must be defined and must not have an empty protocol identifier.");
if (protocols.containsKey(protocol.protocol))
}
if(protocols.containsKey(protocol.protocol)) {
throw new IllegalArgumentException("Protocol " + protocol.protocol + " already registered.");
}

protocols.put(protocol.protocol, protocol);
}

//Format: action://param
public static void process(String action, GuiBook book) {
if (!action.contains(PROTOCOL_SEPARATOR))
if(!action.contains(PROTOCOL_SEPARATOR)) {
return;
}

String protoId = action.substring(0, action.indexOf(PROTOCOL_SEPARATOR));
String protoParam = action.substring(action.indexOf(PROTOCOL_SEPARATOR) + PROTOCOL_SEPARATOR.length());

if (protocols.containsKey(protoId))
if(protocols.containsKey(protoId)) {
protocols.get(protoId).processCommand(book, protoParam);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public ProtocolGoToPage(boolean returner, String suffix) {
public void processCommand(GuiBook book, String param) {
int pageNum = book.book.findPageNumber(param);

if (pageNum >= 0)
if(pageNum >= 0) {
book.openPage(pageNum, returner);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package slimeknights.mantle.client.book.data.content;

import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.ArrayList;

import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data.content;

import java.util.ArrayList;

import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.ItemStackData;
Expand All @@ -10,6 +11,7 @@
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementItem;
import slimeknights.mantle.client.gui.book.element.ElementText;

import static slimeknights.mantle.client.gui.book.Textures.TEX_MISC;

public class ContentBlockInteraction extends PageContent {
Expand All @@ -35,22 +37,23 @@ public void build(BookData book, ArrayList<BookElement> list) {
int x = GuiBook.PAGE_WIDTH / 2 - IMG_SMITHING.width / 2 - 10;
int y = TITLE_HEIGHT;

if (title == null || title.isEmpty())
if(title == null || title.isEmpty()) {
y = 0;
else
} else {
addTitle(list, title);
}

list.add(new ElementImage(x, y, IMG_SMITHING.width, IMG_SMITHING.height, IMG_SMITHING, book.appearance.coverColor));

if (input != null && !input.id.equals("")) {
if(input != null && !input.id.equals("")) {
list.add(new ElementItem(x + INPUT_X, y + INPUT_Y, ITEM_SCALE, input.getItems(), input.action));
}

if (block != null && !block.id.equals("")) {
if(block != null && !block.id.equals("")) {
list.add(new ElementItem(x + BLOCK_X, y + BLOCK_Y, BLOCK_SCALE, block.getItems(), block.action));
}

if (description != null && description.length > 0) {
if(description != null && description.length > 0) {
list.add(new ElementText(0, IMG_SMITHING.height + y + 50, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - IMG_SMITHING.height - y - 50, description));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data.content;

import java.util.ArrayList;

import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.ItemStackData;
Expand All @@ -10,6 +11,7 @@
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementItem;
import slimeknights.mantle.client.gui.book.element.ElementText;

import static slimeknights.mantle.client.gui.book.Textures.TEX_CRAFTING;

public class ContentCrafting extends PageContent {
Expand Down Expand Up @@ -45,34 +47,39 @@ public void build(BookData book, ArrayList<BookElement> list) {
tdTitle.underlined = true;
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, 9, new TextData[]{tdTitle}));

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

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

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

if (description != null && description.length > 0) {
if(description != null && description.length > 0) {
list.add(new ElementText(0, height + 5, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - height - 5, description));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package slimeknights.mantle.client.book.data.content;

import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.ArrayList;

import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;

Expand Down
Loading

0 comments on commit 13e1a8f

Please sign in to comment.