Book crafting pages

- Added crafting and smelting page types to the book
- Improved error handling
- Line breaks in ResourceHelper.getStringFromResource output
- 2 layers for pages instead of one
- ElementItem now does item cycling
This commit is contained in:
Arthur Uzulin committed 2016-02-07 05:19:13 +11:00
1 parent 413b43dfd1
commit 1ac2d95644
35 files changed
+516 -53

No files matched your search

@@ -14,10 +14,14 @@ import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.action.StringActionProcessor;
import slimeknights.mantle.client.book.action.protocol.ProtocolGoToPage;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.PageData;
import slimeknights.mantle.client.book.data.SectionData;
import slimeknights.mantle.client.book.data.content.ContentBlank;
import slimeknights.mantle.client.book.data.content.ContentCrafting;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.content.ContentImage;
import slimeknights.mantle.client.book.data.content.ContentImageText;
import slimeknights.mantle.client.book.data.content.ContentSmelting;
import slimeknights.mantle.client.book.data.content.ContentText;
import slimeknights.mantle.client.book.data.content.ContentTextImage;
import slimeknights.mantle.client.book.data.content.ContentTextLeftImage;
@@ -54,6 +58,8 @@ public class BookLoader implements IResourceManagerReloadListener {
registerPageType("text with image below", ContentTextImage.class);
registerPageType("text with left image etch", ContentTextLeftImage.class);
registerPageType("text with right image etch", ContentTextRightImage.class);
registerPageType("crafting", ContentCrafting.class);
registerPageType("smelting", ContentSmelting.class);
// Register action protocols
StringActionProcessor.registerProtocol(new ProtocolGoToPage());
@@ -126,7 +132,20 @@ public class BookLoader implements IResourceManagerReloadListener {
@Override
public void onResourceManagerReload(IResourceManager resourceManager) {
for (BookData book : books.values()) {
book.load();
try {
book.load();
} catch (Exception e) {
book.sections.clear();
SectionData section = new SectionData(true);
section.name = "errorenous";
PageData page = new PageData(true);
page.name = "errorenous";
page.content = new ContentError("Failed to load the book due to an unexpected error.", e);
section.pages.add(page);
book.sections.add(section);
e.printStackTrace();
}
}
}
}
@@ -86,7 +86,7 @@ public class ResourceHelper {
boolean isLongComment = false;
while (iterator.hasNext()) {
String s = ((String) iterator.next()).trim();
String s = ((String) iterator.next()).trim() + "\n";
// Comment skipper
if (skipCommments) {
@@ -11,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.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;
@@ -37,15 +38,32 @@ public class BookData implements IDataItem {
@Override
public void load() {
sections.clear();
appearance = new AppearanceData();
itemLinks.clear();
for (BookRepository repo : repositories) {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);
try {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);
} catch (Exception e) {
SectionData error = new SectionData();
error.name = "errorenous";
PageData page = new PageData(true);
page.name = "errorenous";
page.content = new ContentError("Failed to load repository " + repo.toString() + ".", e);
error.pages.add(page);
sections.add(error);
}
if (repo.hasAppearanceData) {
ResourceLocation appearanceLocation = getResourceLocation("appearance.json");
if (resourceExists(appearanceLocation))
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class);
try {
appearance = BookLoader.GSON.fromJson(resourceToString(getResource(appearanceLocation)), AppearanceData.class);
} catch (Exception ignored) {
}
else
appearance = new AppearanceData();
@@ -54,7 +72,10 @@ public class BookData implements IDataItem {
ResourceLocation itemLinkLocation = getResourceLocation("items.json");
if (resourceExists(itemLinkLocation)) {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
try {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
} catch (Exception ignored) {
}
}
}
}
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -43,19 +44,24 @@ public class PageData implements IDataItem {
if (pageInfo != null) {
String data = resourceToString(pageInfo);
if (!data.isEmpty())
content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type));
try {
content = BookLoader.GSON.fromJson(data, BookLoader.getPageType(type));
} catch (Exception e) {
content = new ContentError("Failed to create a page of type \"" + type + "\", perhaps the page file \"" + this.data + "\" is missing or invalid?", e);
}
}
}
if (content == null)
if (content == null) {
try {
content = BookLoader.getPageType(type) != null ? BookLoader.getPageType(type).newInstance() : new ContentError();
content = BookLoader.getPageType(type).newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
content = new ContentError("Failed to create a page of type \"" + type + "\", perhaps the type is not registered?");
}
}
for (Field f : content.getClass().getFields()) {
if (f.getType().isAssignableFrom(ImageData.class))
if (f.getType().isAssignableFrom(ImageData.class) && !Modifier.isTransient(f.getModifiers()))
try {
f.setAccessible(true);
ImageData d = (ImageData) f.get(content);
@@ -6,6 +6,7 @@ import net.minecraft.client.resources.IResource;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.data.content.ContentError;
import slimeknights.mantle.client.book.data.element.CriteriaData;
import slimeknights.mantle.client.book.data.element.ImageData;
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
@@ -46,7 +47,15 @@ public class SectionData implements IDataItem {
if (pagesInfo != null) {
String data = resourceToString(pagesInfo);
if (!data.isEmpty())
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
try {
pages = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(data, PageData[].class)));
} catch (Exception e) {
pages = new ArrayList<>();
PageData pdError = new PageData(true);
pdError.name = "errorrenous";
pdError.content = new ContentError("Failed to load section " + name + ".", e);
pages.add(pdError);
}
}
}
@@ -3,13 +3,14 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;
@SideOnly(Side.CLIENT)
public class ContentBlank extends PageContent {
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
}
}
@@ -0,0 +1,80 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraft.util.ResourceLocation;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementItem;
import slimeknights.mantle.client.gui.book.element.ElementText;
public class ContentCrafting extends PageContent {
public static final transient ResourceLocation TEX_CRAFTING = new ResourceLocation("mantle:textures/gui/book/crafting.png");
public static final transient int TEX_SIZE = 256;
public static final transient ImageData IMG_CRAFTING_LARGE = new ImageData(TEX_CRAFTING, 0, 0, 183, 114, TEX_SIZE, TEX_SIZE);
public static final transient ImageData IMG_CRAFTING_SMALL = new ImageData(TEX_CRAFTING, 0, 114, 155, 78, TEX_SIZE, TEX_SIZE);
public static final transient int X_RESULT_SMALL = 118;
public static final transient int Y_RESULT_SMALL = 23;
public static final transient int X_RESULT_LARGE = 146;
public static final transient int Y_RESULT_LARGE = 41;
public static final transient float ITEM_SCALE = 2.0F;
public static final transient int SLOT_MARGIN = 5;
public static final transient int SLOT_PADDING = 4;
public String title = "Crafting";
public String grid_size = "large";
public ItemStackData[][] grid;
public ItemStackData result;
public TextData[] description;
@Override
public void build(BookData book, ArrayList<BookElement> list) {
int x = 0;
int y = 16;
int height = 100;
int resultX = 100;
int resultY = 50;
TextData tdTitle = new TextData(title);
tdTitle.underlined = true;
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, 9, new TextData[]{tdTitle}));
if (grid_size.equalsIgnoreCase("small")) {
x = GuiBook.PAGE_WIDTH / 2 - IMG_CRAFTING_SMALL.width / 2;
height = y + IMG_CRAFTING_SMALL.height;
list.add(new ElementImage(x, y, IMG_CRAFTING_SMALL.width, IMG_CRAFTING_SMALL.height, IMG_CRAFTING_SMALL, book.appearance.coverColor));
resultX = x + X_RESULT_SMALL;
resultY = y + Y_RESULT_SMALL;
} else if (grid_size.equalsIgnoreCase("large")) {
x = GuiBook.PAGE_WIDTH / 2 - IMG_CRAFTING_LARGE.width / 2;
height = y + IMG_CRAFTING_LARGE.height;
list.add(new ElementImage(x, y, IMG_CRAFTING_LARGE.width, IMG_CRAFTING_LARGE.height, IMG_CRAFTING_LARGE, book.appearance.coverColor));
resultX = x + X_RESULT_LARGE;
resultY = y + Y_RESULT_LARGE;
}
if (grid != null)
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j].id.equals(""))
continue;
list.add(new ElementItem(x + SLOT_MARGIN + (SLOT_PADDING + Math.round(ElementItem.ITEM_SIZE_HARDCODED * ITEM_SCALE)) * j, y + SLOT_MARGIN + (SLOT_PADDING + Math.round(ElementItem.ITEM_SIZE_HARDCODED * ITEM_SCALE)) * i, ITEM_SCALE, grid[i][j].getItemStack()));
}
}
if (result != null) {
list.add(new ElementItem(resultX, resultY, ITEM_SCALE, result.getItemStack()));
}
if (description != null && description.length > 0) {
list.add(new ElementText(0, height + 5, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - height - 5, description));
}
}
}
@@ -3,13 +3,14 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;
@SideOnly(Side.CLIENT)
public class ContentDummy extends PageContent {
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
//TODO load from JSON
}
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
@@ -11,13 +12,35 @@ import slimeknights.mantle.client.gui.book.element.ElementText;
@SideOnly(Side.CLIENT)
public class ContentError extends PageContent {
private String errorStage;
private Exception exception;
public ContentError(String errorStage) {
this.errorStage = errorStage;
}
public ContentError(String errorStage, Exception e) {
this(errorStage);
this.exception = e;
}
@Override
public void build(ArrayList<BookElement> list) {
TextData[] text = new TextData[1];
text[0] = new TextData("Could not load page, perhaps the file is missing or the page type does not exist?");
public void build(BookData book, ArrayList<BookElement> list) {
TextData[] text = new TextData[1 + (exception != null ? 2 : 0)];
text[0] = new TextData(errorStage);
text[0].color = "dark_red";
text[0].underlined = true;
if (exception != null) {
text[1] = new TextData("The following error has occured: ");
text[1].color = "dark_red";
text[1].paragraph = true;
text[2] = new TextData(exception.getMessage());
text[2].color = "dark_red";
text[2].paragraph = true;
}
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, text));
}
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
@@ -14,7 +15,7 @@ public class ContentImage extends PageContent {
public ImageData image;
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (image != null && image.location != null)
list.add(new ElementImage(0, 0, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, image));
else
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
@@ -17,7 +18,7 @@ public class ContentImageText extends PageContent {
public TextData[] text;
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (image != null && image.location != null)
list.add(new ElementImage(0, 0, GuiBook.PAGE_WIDTH, 100, image));
else
@@ -1,6 +1,7 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.SectionData;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementSection;
@@ -10,15 +11,11 @@ public class ContentSectionList extends PageContent {
protected ArrayList<SectionData> sections = new ArrayList<>();
public boolean addSection(SectionData data) {
if (sections.size() >= 9) {
return false;
}
return sections.add(data);
return sections.size() < 9 && sections.add(data);
}
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
int width = (ElementSection.WIDTH + 5) * 3 - 5;
int height = (ElementSection.HEIGHT + 5) * 3 - 5;
@@ -0,0 +1,83 @@
package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
import slimeknights.mantle.client.gui.book.element.ElementImage;
import slimeknights.mantle.client.gui.book.element.ElementItem;
import slimeknights.mantle.client.gui.book.element.ElementText;
public class ContentSmelting extends PageContent {
public static final transient ResourceLocation TEX_SMELTING = new ResourceLocation("mantle:textures/gui/book/smelting.png");
public static final transient int TEX_SIZE = 128;
public static final transient ImageData IMG_SMELTING = new ImageData(TEX_SMELTING, 0, 0, 110, 114, TEX_SIZE, TEX_SIZE);
public static final transient int INPUT_X = 5;
public static final transient int INPUT_Y = 5;
public static final transient int RESULT_X = 74;
public static final transient int RESULT_Y = 41;
public static final transient int FUEL_X = 5;
public static final transient int FUEL_Y = 77;
public static final transient float ITEM_SCALE = 2.0F;
public String title = "Smelting";
public ItemStackData input;
public ItemStackData result;
public TextData[] description;
@Override
public void build(BookData book, ArrayList<BookElement> list) {
int x = GuiBook.PAGE_WIDTH / 2 - IMG_SMELTING.width / 2;
int y = 16;
TextData tdTitle = new TextData(title);
tdTitle.underlined = true;
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, 9, new TextData[]{tdTitle}));
list.add(new ElementImage(x, y, IMG_SMELTING.width, IMG_SMELTING.height, IMG_SMELTING, book.appearance.coverColor));
if (input != null) {
list.add(new ElementItem(x + INPUT_X, y + INPUT_Y, ITEM_SCALE, input.getItemStack()));
}
if (result != null) {
list.add(new ElementItem(x + RESULT_X, y + RESULT_Y, ITEM_SCALE, result.getItemStack()));
}
list.add(new ElementItem(x + FUEL_X, y + FUEL_Y, ITEM_SCALE, getFuelsList()));
if (description != null && description.length > 0) {
list.add(new ElementText(0, IMG_SMELTING.height + y + 5, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - y - 5, description));
}
}
public ItemStack[] getFuelsList() {
//TODO ask JEI for fuel list if it is present
ItemStack[] fuels = new ItemStack[13];
fuels[0] = new ItemStack(Blocks.wooden_slab);
fuels[1] = new ItemStack(Blocks.planks);
fuels[2] = new ItemStack(Blocks.coal_block);
fuels[3] = new ItemStack(Items.wooden_pickaxe);
fuels[4] = new ItemStack(Items.wooden_sword);
fuels[5] = new ItemStack(Items.wooden_hoe);
fuels[6] = new ItemStack(Items.stick);
fuels[7] = new ItemStack(Items.coal);
fuels[8] = new ItemStack(Items.lava_bucket);
fuels[9] = new ItemStack(Blocks.sapling);
fuels[10] = new ItemStack(Items.blaze_rod);
fuels[11] = new ItemStack(Items.wooden_shovel);
fuels[12] = new ItemStack(Items.wooden_axe);
return fuels;
}
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
import slimeknights.mantle.client.gui.book.element.BookElement;
@@ -14,7 +15,7 @@ public class ContentText extends PageContent {
public TextData[] text;
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (text != null && text.length > 0)
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT, text));
}
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
@@ -17,7 +18,7 @@ public class ContentTextImage extends PageContent {
public ImageData image;
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (text != null && text.length > 0)
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH, GuiBook.PAGE_HEIGHT - 105, text));
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
@@ -18,7 +19,7 @@ public class ContentTextLeftImage extends PageContent {
public TextData[] text2;
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (image != null && image.location != null)
list.add(new ElementImage(0, 0, 50, 50, image));
else
@@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.element.ImageData;
import slimeknights.mantle.client.book.data.element.TextData;
import slimeknights.mantle.client.gui.book.GuiBook;
@@ -18,7 +19,7 @@ public class ContentTextRightImage extends PageContent {
public ImageData image;
@Override
public void build(ArrayList<BookElement> list) {
public void build(BookData book, ArrayList<BookElement> list) {
if (text1 != null && text1.length > 0)
list.add(new ElementText(0, 0, GuiBook.PAGE_WIDTH - 55, 50, text1));
@@ -3,10 +3,11 @@ package slimeknights.mantle.client.book.data.content;
import java.util.ArrayList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.gui.book.element.BookElement;
@SideOnly(Side.CLIENT)
public abstract class PageContent {
public abstract void build(ArrayList<BookElement> list);
public abstract void build(BookData book, ArrayList<BookElement> list);
}
@@ -20,9 +20,38 @@ public class ImageData {
public int y = -1;
public int width = -1;
public int height = -1;
public int colorMultiplier = 0xFFFFFF;
public transient ResourceLocation location;
public ImageData() {
}
public ImageData(ResourceLocation location, int u, int v, int uw, int vh, int texWidth, int texHeight) {
this(location, u, v, uw, vh, texWidth, texHeight, uw, vh, 0xFFFFFF);
}
public ImageData(ResourceLocation location, int u, int v, int uw, int vh, int texWidth, int texHeight, int colorMultiplier) {
this(location, u, v, uw, vh, texWidth, texHeight, uw, vh);
}
public ImageData(ResourceLocation location, int u, int v, int uw, int vh, int texWidth, int texHeight, int width, int height) {
this(location, u, v, uw, vh, texWidth, texHeight, width, height, 0xFFFFFF);
}
public ImageData(ResourceLocation location, int u, int v, int uw, int vh, int texWidth, int texHeight, int width, int height, int colorMultiplier) {
this.location = location;
this.u = u;
this.v = v;
this.uw = uw;
this.vh = vh;
this.texWidth = texWidth;
this.texHeight = texHeight;
this.width = width;
this.height = height;
this.colorMultiplier = colorMultiplier;
}
static {
MISSING.location = new ResourceLocation("mantle:textures/gui/missingno.png");
MISSING.texWidth = 32;
@@ -9,25 +9,31 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import slimeknights.mantle.client.book.BookLoader;
public class ItemStackData {
public String id = "minecraft:barrier";
public String id = "";
public byte amount = 1;
public short damage = 0;
public JsonObject nbt;
public ItemStack getItemStack() {
Item item;
boolean isMissingItem = false;
try {
item = CommandGive.getItemByText(null, id);
} catch (NumberInvalidException e) {
item = Item.getItemFromBlock(Blocks.barrier);
isMissingItem = true;
}
if (item == null)
if (item == null) {
item = Item.getItemFromBlock(Blocks.barrier);
isMissingItem = true;
}
ItemStack itemStack = new ItemStack(item, amount, damage);
@@ -38,6 +44,15 @@ public class ItemStackData {
}
}
if (isMissingItem) {
NBTTagCompound display = itemStack.getSubCompound("display", true);
display.setString("Name", "\u00A7rUnknown Item");
NBTTagList lore = new NBTTagList();
lore.appendTag(new NBTTagString("\u00A7r\u00A7eItem Name:"));
lore.appendTag(new NBTTagString("\u00A7r\u00A7e" + id));
display.setTag("Lore", lore);
}
return itemStack;
}
@@ -144,6 +144,14 @@ public class GuiBook extends GuiScreen {
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);
}
// 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.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);
}
GlStateManager.popMatrix();
}
@@ -173,6 +181,14 @@ public class GuiBook extends GuiScreen {
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);
}
// 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.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);
}
GlStateManager.popMatrix();
}
}
@@ -222,15 +238,15 @@ public class GuiBook extends GuiScreen {
PageData page = book.findPage(0);
if (page != null)
page.content.build(rightElements);
page.content.build(book, rightElements);
} else {
PageData leftPage = book.findPage((page - 1) * 2 + 1);
PageData rightPage = book.findPage((page - 1) * 2 + 2);
if (leftPage != null)
leftPage.content.build(leftElements);
leftPage.content.build(book, leftElements);
if (rightPage != null)
rightPage.content.build(rightElements);
rightPage.content.build(book, rightElements);
}
for (BookElement element : leftElements)
@@ -277,7 +293,7 @@ public class GuiBook extends GuiScreen {
previousArrow.xPosition = width / 2 - 184;
nextArrow.xPosition = width / 2 + 165;
indexArrow.visible = (page - 1) * 2 + 2 > book.findSection("index").getPageCount();
indexArrow.visible = book.findSection("index") != null && (page - 1) * 2 + 2 > book.findSection("index").getPageCount();
}
previousArrow.yPosition = height / 2 + 75;
@@ -352,7 +368,7 @@ public class GuiBook extends GuiScreen {
if (this.page == -1)
BookLoader.updateSavedPage(mc.thePlayer, item, "");
else if (page != null)
else if (page != null && page.parent != null)
BookLoader.updateSavedPage(mc.thePlayer, item, page.parent.name + "." + page.name);
}
@@ -14,12 +14,10 @@ import slimeknights.mantle.client.book.data.element.TextData;
@SideOnly(Side.CLIENT)
public class TextDataRenderer {
private static String[] drawLabel;
public static String drawText(int x, int y, int boxWidth, int boxHeight, TextData[] data, int mouseX, int mouseY) {
String action = "";
drawLabel = null;
String[] drawLabel = null;
FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
@@ -4,7 +4,6 @@ import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
@@ -26,6 +25,9 @@ public abstract class BookElement extends Gui {
public abstract void draw(int mouseX, int mouseY, float partialTicks);
public void drawOverlay(int mouseX, int mouseY, float partialTicks) {
}
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
}
@@ -47,7 +49,6 @@ public abstract class BookElement extends Gui {
public void drawHoveringText(List<String> textLines, int x, int y, FontRenderer font) {
if (!textLines.isEmpty()) {
ScaledResolution res = new ScaledResolution(Minecraft.getMinecraft());
GlStateManager.disableDepth();
int i = 0;
@@ -67,12 +68,12 @@ public abstract class BookElement extends Gui {
k += 2 + (textLines.size() - 1) * 10;
}
if (l1 + i > res.getScaledWidth()) {
if (l1 + i > GuiBook.PAGE_WIDTH) {
l1 -= 28 + i;
}
if (i2 + k + 6 > res.getScaledHeight()) {
i2 = res.getScaledHeight() - k - 6;
if (i2 + k + 6 > GuiBook.PAGE_HEIGHT) {
i2 = GuiBook.PAGE_HEIGHT - k - 6;
}
int l = -267386864;
@@ -1,6 +1,7 @@
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;
import slimeknights.mantle.client.book.data.element.ImageData;
@@ -9,12 +10,21 @@ import slimeknights.mantle.client.book.data.element.ImageData;
public class ElementImage extends SizedBookElement {
public ImageData image;
public int colorMultiplier;
public ElementImage(ImageData image) {
this(image.x, image.y, image.width, image.height, image);
this(image, 0xFFFFFF);
}
public ElementImage(ImageData image, int colorMultiplier) {
this(image.x, image.y, image.width, image.height, image, colorMultiplier);
}
public ElementImage(int x, int y, int width, int height, ImageData image) {
this(x, y, width, height, image, image.colorMultiplier);
}
public ElementImage(int x, int y, int width, int height, ImageData image, int colorMultiplier) {
super(x, y, width, height);
this.image = image;
@@ -27,12 +37,26 @@ public class ElementImage extends SizedBookElement {
width = image.width;
if (image.height != -1)
height = image.height;
if (image.colorMultiplier != 0xFFFFFF)
colorMultiplier = image.colorMultiplier;
this.x = x == -1 ? 0 : x;
this.y = y == -1 ? 0 : y;
this.width = width;
this.height = height;
this.colorMultiplier = colorMultiplier;
}
@Override
public void draw(int mouseX, int mouseY, float partialTicks) {
Minecraft.getMinecraft().renderEngine.bindTexture(image.location);
float r = ((colorMultiplier >> 16) & 0xff) / 255.F;
float g = ((colorMultiplier >> 8) & 0xff) / 255.F;
float b = (colorMultiplier & 0xff) / 255.F;
GlStateManager.color(r, g, b);
drawScaledCustomSizeModalRect(x, y, image.u, image.v, image.uw, image.vh, width, height, image.texWidth, image.texHeight);
}
}
@@ -3,6 +3,7 @@ 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;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
@@ -12,10 +13,14 @@ import net.minecraftforge.fml.relauncher.SideOnly;
public class ElementItem extends BookElement {
public static final int ITEM_SIZE_HARDCODED = 16;
public static final int ITEM_SWITCH_TICKS = 90;
public ItemStack item;
public ItemStack[] itemCycle;
public float scale;
public int renderTick = 0;
public int currentItem = 0;
public ElementItem(int x, int y, float scale, Item item) {
this(x, y, scale, new ItemStack(item));
}
@@ -25,31 +30,50 @@ public class ElementItem extends BookElement {
}
public ElementItem(int x, int y, float scale, ItemStack item) {
this(x, y, scale, new ItemStack[]{item});
}
public ElementItem(int x, int y, float scale, ItemStack[] itemCycle) {
super(x, y);
this.item = item;
this.itemCycle = itemCycle;
this.scale = scale;
}
@Override
public void draw(int mouseX, int mouseY, float partialTicks) {
renderTick++;
if (renderTick > ITEM_SWITCH_TICKS) {
renderTick = 0;
currentItem++;
if (currentItem >= itemCycle.length)
currentItem = 0;
}
RenderHelper.enableGUIStandardItemLighting();
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, 0);
GlStateManager.scale(scale, scale, 1.0F);
Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(item, 0, 0);
if (currentItem < itemCycle.length)
Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(itemCycle[currentItem], 0, 0);
GlStateManager.popMatrix();
}
if (mouseX >= x && mouseY >= y && mouseX <= x + ITEM_SIZE_HARDCODED * scale && mouseY <= y + ITEM_SIZE_HARDCODED * scale) {
renderToolTip(Minecraft.getMinecraft().fontRendererObj, item, mouseX, mouseY);
@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);
}
}
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
if (mouseButton == 0 && mouseX >= x && mouseY >= y && mouseX <= x + ITEM_SIZE_HARDCODED * scale && mouseY <= y + ITEM_SIZE_HARDCODED * scale) {
parent.itemClicked(item);
if (mouseButton == 0 && mouseX >= x && mouseY >= y && mouseX <= x + ITEM_SIZE_HARDCODED * scale && mouseY <= y + ITEM_SIZE_HARDCODED * scale && currentItem < itemCycle.length) {
parent.itemClicked(itemCycle[currentItem]);
}
}
}