ItemStack loader

- Load ItemStacks from JSON
  - items.json specifies all the global item click events
This commit is contained in:
Arthur Uzulin committed 2016-02-06 03:56:07 +11:00
1 parent 59e9e52a2a
commit 413b43dfd1
14 files changed
+227 -28

No files matched your search

@@ -2,7 +2,6 @@ package slimeknights.mantle.client.book;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Random;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
@@ -89,7 +88,7 @@ public class BookLoader implements IResourceManagerReloadListener {
* 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 name The name of the book, modid: will be automatically appended to the front of the name unless that is already added
* @param repositories All the repositories the book will load the sections from
* @return The book object, not immediately populated
*/
@@ -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.element.ItemStackData;
import slimeknights.mantle.client.book.repository.BookRepository;
import slimeknights.mantle.client.gui.book.GuiBook;
import static slimeknights.mantle.client.book.ResourceHelper.getResource;
@@ -20,9 +21,11 @@ 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 ArrayList<ItemStackData.ItemLink> itemLinks = new ArrayList<>();
protected final transient ArrayList<BookTransformer> transformers = new ArrayList<>();
@@ -34,11 +37,11 @@ public class BookData implements IDataItem {
@Override
public void load() {
for(BookRepository repo : repositories) {
for (BookRepository repo : repositories) {
List<SectionData> repoContents = repo.getSections();
sections.addAll(repoContents);
if(repo.hasAppearanceData){
if (repo.hasAppearanceData) {
ResourceLocation appearanceLocation = getResourceLocation("appearance.json");
if (resourceExists(appearanceLocation))
@@ -47,6 +50,12 @@ public class BookData implements IDataItem {
appearance = new AppearanceData();
appearance.load();
ResourceLocation itemLinkLocation = getResourceLocation("items.json");
if (resourceExists(itemLinkLocation)) {
itemLinks = new ArrayList<>(Arrays.asList(BookLoader.GSON.fromJson(resourceToString(getResource(itemLinkLocation)), ItemStackData.ItemLink[].class)));
}
}
}
@@ -130,7 +139,7 @@ public class BookData implements IDataItem {
public int getPageCount() {
int pages = 0;
for(SectionData section : sections){
for (SectionData section : sections) {
pages += section.getPageCount();
}
return pages;
@@ -140,13 +149,22 @@ public class BookData implements IDataItem {
return (int) Math.ceil((getPageCount() - 1) / 2F) + 1;
}
public String getItemAction(ItemStackData item) {
for (ItemStackData.ItemLink link : itemLinks) {
if (item.id.equals(link.item.id) && (!link.damageSensitive || item.damage == link.item.damage))
return link.action;
}
return "";
}
public void openGui(@Nullable ItemStack item) {
if(Minecraft.getMinecraft().currentScreen == null)
if (Minecraft.getMinecraft().currentScreen == null)
Minecraft.getMinecraft().displayGuiScreen(new GuiBook(this, item));
}
public void addRepository(BookRepository repository) {
if(repository != null && !this.repositories.contains(repository))
if (repository != null && !this.repositories.contains(repository))
this.repositories.add(repository);
}
@@ -5,5 +5,6 @@ import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public interface IDataItem {
void load();
}
@@ -33,7 +33,7 @@ public class PageData implements IDataItem {
@Override
public void load() {
if(name == null)
if (name == null)
name = "page" + parent.unnamedPageCounter++;
name = name.toLowerCase();
@@ -14,6 +14,7 @@ import static slimeknights.mantle.client.book.ResourceHelper.resourceToString;
@SideOnly(Side.CLIENT)
public class SectionData implements IDataItem {
public String name = null;
public String title = "";
public ImageData icon = new ImageData();
@@ -35,7 +36,7 @@ public class SectionData implements IDataItem {
@Override
public void load() {
if(name == null)
if (name == null)
name = "section" + parent.unnamedSectionCounter++;
name = name.toLowerCase();
@@ -0,0 +1,66 @@
package slimeknights.mantle.client.book.data.element;
import com.google.gson.JsonObject;
import net.minecraft.command.CommandGive;
import net.minecraft.command.NumberInvalidException;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import slimeknights.mantle.client.book.BookLoader;
public class ItemStackData {
public String id = "minecraft:barrier";
public byte amount = 1;
public short damage = 0;
public JsonObject nbt;
public ItemStack getItemStack() {
Item item;
try {
item = CommandGive.getItemByText(null, id);
} catch (NumberInvalidException e) {
item = Item.getItemFromBlock(Blocks.barrier);
}
if (item == null)
item = Item.getItemFromBlock(Blocks.barrier);
ItemStack itemStack = new ItemStack(item, amount, damage);
if (nbt != null) {
try {
itemStack.setTagCompound(JsonToNBT.getTagFromJson(nbt.toString()));
} catch (NBTException ignored) {
}
}
return itemStack;
}
public static ItemStackData getItemStackData(ItemStack stack) {
return getItemStackData(stack, false);
}
public static ItemStackData getItemStackData(ItemStack stack, boolean ignoreNbt) {
ItemStackData data = new ItemStackData();
data.id = Item.itemRegistry.getNameForObject(stack.getItem()).toString();
data.amount = (byte) stack.stackSize;
data.damage = (short) stack.getItemDamage();
if (!ignoreNbt && stack.getTagCompound() != null) {
data.nbt = BookLoader.GSON.toJsonTree(stack.getTagCompound(), NBTTagCompound.class).getAsJsonObject();
}
return data;
}
public static class ItemLink {
public ItemStackData item = new ItemStackData();
public boolean damageSensitive = false;
public String action = "";
}
}
@@ -7,11 +7,11 @@ public abstract class BookRepository {
public final boolean hasAppearanceData;
public BookRepository () {
public BookRepository() {
this(false);
}
public BookRepository (boolean hasAppearanceData) {
public BookRepository(boolean hasAppearanceData) {
this.hasAppearanceData = hasAppearanceData;
}
@@ -16,8 +16,10 @@ import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import slimeknights.mantle.client.book.BookHelper;
import slimeknights.mantle.client.book.BookLoader;
import slimeknights.mantle.client.book.action.StringActionProcessor;
import slimeknights.mantle.client.book.data.BookData;
import slimeknights.mantle.client.book.data.PageData;
import slimeknights.mantle.client.book.data.element.ItemStackData;
import slimeknights.mantle.client.gui.book.element.BookElement;
@SideOnly(Side.CLIENT)
@@ -205,6 +207,10 @@ public class GuiBook extends GuiScreen {
}
}
public void itemClicked(ItemStack item) {
StringActionProcessor.process(book.getItemAction(ItemStackData.getItemStackData(item, true)), this);
}
private void buildPages() {
leftElements.clear();
rightElements.clear();
@@ -344,7 +350,7 @@ public class GuiBook extends GuiScreen {
if (page == null)
page = book.findPage((this.page - 1) * 2 + 2);
if(this.page == -1)
if (this.page == -1)
BookLoader.updateSavedPage(mc.thePlayer, item, "");
else if (page != null)
BookLoader.updateSavedPage(mc.thePlayer, item, page.parent.name + "." + page.name);
@@ -29,9 +29,9 @@ public class TextDataRenderer {
float prevScale = 1.F;
for (TextData item : data) {
int box1X = 9999, box1Y = 9999, box1W = 9999, box1H = y + fr.FONT_HEIGHT;
int box2X = 9999, box2Y = 9999, box2W = 9999, box2H = 9999;
int box3X = 9999, box3Y = 9999, box3W = 9999, box3H = 9999;
int box1X, box1Y, box1W = 9999, box1H = y + fr.FONT_HEIGHT;
int box2X, box2Y = 9999, box2W, box2H;
int box3X = 9999, box3Y = 9999, box3W, box3H;
if (item.text == null || item.text.isEmpty())
continue;
@@ -165,9 +165,6 @@ public class TextDataRenderer {
//BEGIN METHODS FROM GUI
private static void drawHoveringText(String[] textLines, int x, int y, FontRenderer font) {
if (textLines.length > 0) {
GlStateManager.pushAttrib();
GlStateManager.disableRescaleNormal();
GlStateManager.disableLighting();
GlStateManager.disableDepth();
int i = 0;
@@ -219,9 +216,7 @@ public class TextDataRenderer {
i2 += 10;
}
GlStateManager.enableLighting();
GlStateManager.enableDepth();
GlStateManager.popAttrib();
}
}
@@ -1,6 +1,13 @@
package slimeknights.mantle.client.gui.book.element;
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;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import slimeknights.mantle.client.gui.book.GuiBook;
@@ -22,4 +29,77 @@ public abstract class BookElement extends Gui {
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
}
public void renderToolTip(FontRenderer fontRenderer, ItemStack stack, int x, int y) {
List<String> list = stack.getTooltip(Minecraft.getMinecraft().thePlayer, Minecraft.getMinecraft().gameSettings.advancedItemTooltips);
for (int i = 0; i < list.size(); ++i) {
if (i == 0) {
list.set(i, stack.getRarity().rarityColor + list.get(i));
} else {
list.set(i, EnumChatFormatting.GRAY + list.get(i));
}
}
FontRenderer font = stack.getItem().getFontRenderer(stack);
drawHoveringText(list, x, y, (font == null ? fontRenderer : font));
}
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;
for (String s : textLines) {
int j = font.getStringWidth(s);
if (j > i) {
i = j;
}
}
int l1 = x + 12;
int i2 = y - 12;
int k = 8;
if (textLines.size() > 1) {
k += 2 + (textLines.size() - 1) * 10;
}
if (l1 + i > res.getScaledWidth()) {
l1 -= 28 + i;
}
if (i2 + k + 6 > res.getScaledHeight()) {
i2 = res.getScaledHeight() - k - 6;
}
int l = -267386864;
this.drawGradientRect(l1 - 3, i2 - 4, l1 + i + 3, i2 - 3, l, l);
this.drawGradientRect(l1 - 3, i2 + k + 3, l1 + i + 3, i2 + k + 4, l, l);
this.drawGradientRect(l1 - 3, i2 - 3, l1 + i + 3, i2 + k + 3, l, l);
this.drawGradientRect(l1 - 4, i2 - 3, l1 - 3, i2 + k + 3, l, l);
this.drawGradientRect(l1 + i + 3, i2 - 3, l1 + i + 4, i2 + k + 3, l, l);
int i1 = 1347420415;
int j1 = (i1 & 16711422) >> 1 | i1 & -16777216;
this.drawGradientRect(l1 - 3, i2 - 3 + 1, l1 - 3 + 1, i2 + k + 3 - 1, i1, j1);
this.drawGradientRect(l1 + i + 2, i2 - 3 + 1, l1 + i + 3, i2 + k + 3 - 1, i1, j1);
this.drawGradientRect(l1 - 3, i2 - 3, l1 + i + 3, i2 - 3 + 1, i1, i1);
this.drawGradientRect(l1 - 3, i2 + k + 2, l1 + i + 3, i2 + k + 3, j1, j1);
for (int k1 = 0; k1 < textLines.size(); ++k1) {
String s1 = textLines.get(k1);
font.drawStringWithShadow(s1, (float) l1, (float) i2, -1);
if (k1 == 0) {
i2 += 2;
}
i2 += 10;
}
GlStateManager.enableDepth();
}
}
}
@@ -2,6 +2,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.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
@@ -10,24 +11,45 @@ import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ElementItem extends BookElement {
public static final int ITEM_SIZE_HARDCODED = 16;
public ItemStack item;
public float scale;
public ElementItem(int x, int y, Item item) {
this(x, y, new ItemStack(item));
public ElementItem(int x, int y, float scale, Item item) {
this(x, y, scale, new ItemStack(item));
}
public ElementItem(int x, int y, Block item) {
this(x, y, new ItemStack(item));
public ElementItem(int x, int y, float scale, Block item) {
this(x, y, scale, new ItemStack(item));
}
public ElementItem(int x, int y, ItemStack item) {
public ElementItem(int x, int y, float scale, ItemStack item) {
super(x, y);
this.item = item;
this.scale = scale;
}
@Override
public void draw(int mouseX, int mouseY, float partialTicks) {
Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(item, x, y);
GlStateManager.pushMatrix();
GlStateManager.translate(x, y, 0);
GlStateManager.scale(scale, scale, 1.0F);
Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(item, 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 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);
}
}
}
@@ -35,7 +35,7 @@ public class ElementSection extends BookElement {
}
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
if (section != null && mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT)
if (mouseButton == 0 && section != null && mouseX > x && mouseY > y && mouseX < x + WIDTH && mouseY < y + HEIGHT)
parent.openPage(parent.book.getFirstPageNumber(section));
}
}
@@ -35,6 +35,7 @@ public class ElementText extends SizedBookElement {
@Override
public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
doAction = true;
if (mouseButton == 0)
doAction = true;
}
}
@@ -0,0 +1,10 @@
[
{
"item": {
"id": "minecraft:golden_apple",
"damage": 1
},
"damageSensitive": true,
"action": "go-to-page-rtn:test section.page5"
}
]