From 4e4fc76f2c27c4153fd9d30c92da1edc786f1fd9 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 29 Aug 2015 17:32:39 +0200 Subject: [PATCH] When an item that is used in a crafting recipe has its own page, clicking on the item will open that page --- .../inventory/gui/booklet/GuiBooklet.java | 34 ++++++++++----- .../inventory/gui/booklet/IBookletPage.java | 6 +++ .../inventory/gui/booklet/InitBooklet.java | 1 + .../inventory/gui/booklet/PageCrafting.java | 8 +++- .../gui/booklet/PageCrusherRecipe.java | 8 +++- .../inventory/gui/booklet/PageFurnace.java | 8 +++- .../inventory/gui/booklet/PageText.java | 43 +++++++++++++++++++ 7 files changed, 95 insertions(+), 13 deletions(-) diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/GuiBooklet.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/GuiBooklet.java index 02b73a5ae..71dec8279 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/GuiBooklet.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/GuiBooklet.java @@ -28,6 +28,7 @@ import org.lwjgl.opengl.GL11; import java.util.ArrayList; import java.util.Collections; +import java.util.List; @SideOnly(Side.CLIENT) public class GuiBooklet extends GuiScreen{ @@ -72,7 +73,7 @@ public class GuiBooklet extends GuiScreen{ if(key != 1 && this.searchField.isFocused()){ this.searchField.textboxKeyTyped(theChar, key); - if(this.currentIndexEntry == InitBooklet.allAndSearch){ + if(this.currentIndexEntry instanceof BookletEntryAllSearch){ BookletEntryAllSearch currentEntry = (BookletEntryAllSearch)this.currentIndexEntry; if(this.searchField.getText() != null && !this.searchField.getText().isEmpty()){ currentEntry.chapters.clear(); @@ -122,7 +123,6 @@ public class GuiBooklet extends GuiScreen{ this.searchField = new GuiTextField(this.unicodeRenderer, guiLeft+148, guiTop+162, 66, 10); this.searchField.setMaxStringLength(30); this.searchField.setEnableBackgroundDrawing(false); - this.searchField.setVisible(false); this.currentPage = null; this.currentChapter = null; @@ -140,13 +140,17 @@ public class GuiBooklet extends GuiScreen{ super.renderToolTip(stack, x, y); } + public void drawHoveringText(List list, int x, int y){ + super.func_146283_a(list, x, y); + } + @Override public void drawScreen(int x, int y, float f){ GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.mc.getTextureManager().bindTexture(resLoc); this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); - if(this.currentIndexEntry == InitBooklet.allAndSearch && this.currentChapter == null){ + if(this.currentIndexEntry instanceof BookletEntryAllSearch && this.currentChapter == null){ this.drawTexturedModalRect(this.guiLeft+146, this.guiTop+160, 146, 80, 70, 14); } @@ -254,7 +258,8 @@ public class GuiBooklet extends GuiScreen{ if(this.currentIndexEntry != null){ if(this.currentChapter == null){ if(actualButton < this.currentIndexEntry.chapters.size()){ - this.openChapter(currentIndexEntry.chapters.get(actualButton+(12*this.pageOpenInIndex-12))); + BookletChapter chap = currentIndexEntry.chapters.get(actualButton+(12*this.pageOpenInIndex-12)); + this.openChapter(chap, chap.pages[0]); } } } @@ -278,10 +283,10 @@ public class GuiBooklet extends GuiScreen{ @SuppressWarnings("unchecked") private void openIndexEntry(BookletIndexEntry entry, int page, boolean resetTextField){ if(resetTextField){ - this.searchField.setVisible(entry == InitBooklet.allAndSearch); - this.searchField.setFocused(entry == InitBooklet.allAndSearch); + this.searchField.setVisible(entry instanceof BookletEntryAllSearch); + this.searchField.setFocused(entry instanceof BookletEntryAllSearch); this.searchField.setText(""); - if(entry == InitBooklet.allAndSearch){ + if(entry instanceof BookletEntryAllSearch){ entry.chapters = (ArrayList)((BookletEntryAllSearch)entry).allChapters.clone(); } } @@ -316,7 +321,7 @@ public class GuiBooklet extends GuiScreen{ } } - private void openChapter(BookletChapter chapter){ + public void openChapter(BookletChapter chapter, IBookletPage page){ if(chapter == null) return; this.searchField.setVisible(false); @@ -324,9 +329,9 @@ public class GuiBooklet extends GuiScreen{ this.searchField.setText(""); this.currentChapter = chapter; - this.currentPage = currentChapter.pages[0]; + this.currentPage = page != null && this.hasPage(chapter, page) ? page : chapter.pages[0]; - this.getButton(BUTTON_FORWARD_ID).visible = this.getNextPage(chapter, currentPage) != null; + this.getButton(BUTTON_FORWARD_ID).visible = this.getNextPage(chapter, this.currentPage) != null; this.getButton(BUTTON_BACK_ID).visible = false; for(int i = 0; i < 12; i++){ @@ -335,6 +340,15 @@ public class GuiBooklet extends GuiScreen{ } } + private boolean hasPage(BookletChapter chapter, IBookletPage page){ + for(IBookletPage aPage : chapter.pages){ + if(aPage == page){ + return true; + } + } + return false; + } + private static class IndexButton extends GuiButton{ private FontRenderer renderer; diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/IBookletPage.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/IBookletPage.java index 14951d7d9..9b9bb3645 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/IBookletPage.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/IBookletPage.java @@ -10,15 +10,21 @@ package ellpeck.actuallyadditions.inventory.gui.booklet; +import net.minecraft.item.ItemStack; + public interface IBookletPage{ int getID(); void setChapter(BookletChapter chapter); + BookletChapter getChapter(); + String getText(); void renderPre(GuiBooklet gui, int mouseX, int mouseY); void render(GuiBooklet gui, int mouseX, int mouseY); + + ItemStack getItemStackForPage(); } diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/InitBooklet.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/InitBooklet.java index 95518c8c7..51e3381dd 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/InitBooklet.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/InitBooklet.java @@ -22,6 +22,7 @@ import java.util.ArrayList; public class InitBooklet{ public static ArrayList entries = new ArrayList(); + public static ArrayList pagesWithItemStackData = new ArrayList(); public static BookletIndexEntry entryFunctionalNonRF = new BookletIndexEntry("functionalNoRF"); public static BookletIndexEntry entryMisc = new BookletIndexEntry("misc"); diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrafting.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrafting.java index a7eb506ac..ed24358de 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrafting.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrafting.java @@ -32,6 +32,12 @@ public class PageCrafting extends PageText{ public PageCrafting(int id, IRecipe recipe){ super(id); this.recipe = recipe; + InitBooklet.pagesWithItemStackData.add(this); + } + + @Override + public ItemStack getItemStackForPage(){ + return this.recipe == null ? null : this.recipe.getRecipeOutput(); } @Override @@ -109,7 +115,7 @@ public class PageCrafting extends PageText{ } else{ if(mouseX >= xShow && mouseX <= xShow+16 && mouseY >= yShow && mouseY <= yShow+16){ - gui.renderToolTip(stack, mouseX, mouseY); + this.renderTooltipAndTransfer(gui, stack, mouseX, mouseY, true); } } } diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrusherRecipe.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrusherRecipe.java index 99f19edc1..531d4b139 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrusherRecipe.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageCrusherRecipe.java @@ -24,6 +24,7 @@ public class PageCrusherRecipe extends PageText{ public PageCrusherRecipe(int id, ItemStack input){ super(id); this.input = input; + InitBooklet.pagesWithItemStackData.add(this); } @Override @@ -34,6 +35,11 @@ public class PageCrusherRecipe extends PageText{ } } + @Override + public ItemStack getItemStackForPage(){ + return CrusherRecipeManualRegistry.getOutput(this.input, false); + } + @SuppressWarnings("unchecked") @Override public void render(GuiBooklet gui, int mouseX, int mouseY){ @@ -66,7 +72,7 @@ public class PageCrusherRecipe extends PageText{ } else{ if(mouseX >= xShow && mouseX <= xShow+16 && mouseY >= yShow && mouseY <= yShow+16){ - gui.renderToolTip(stack, mouseX, mouseY); + this.renderTooltipAndTransfer(gui, stack, mouseX, mouseY, j == 0); } } } diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageFurnace.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageFurnace.java index 6e5ae72af..1765681ec 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageFurnace.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageFurnace.java @@ -26,6 +26,12 @@ public class PageFurnace extends PageText{ public PageFurnace(int id, ItemStack result){ super(id); this.result = result; + InitBooklet.pagesWithItemStackData.add(this); + } + + @Override + public ItemStack getItemStackForPage(){ + return this.result; } @Override @@ -61,7 +67,7 @@ public class PageFurnace extends PageText{ } else{ if(mouseX >= xShow && mouseX <= xShow+16 && mouseY >= yShow && mouseY <= yShow+16){ - gui.renderToolTip(stack, mouseX, mouseY); + this.renderTooltipAndTransfer(gui, stack, mouseX, mouseY, x == 0); } } } diff --git a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageText.java b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageText.java index 8fd4e2a37..f45229603 100644 --- a/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageText.java +++ b/src/main/java/ellpeck/actuallyadditions/inventory/gui/booklet/PageText.java @@ -12,6 +12,10 @@ package ellpeck.actuallyadditions.inventory.gui.booklet; import ellpeck.actuallyadditions.util.ModUtil; import ellpeck.actuallyadditions.util.StringUtil; +import net.minecraft.item.ItemStack; +import org.lwjgl.input.Mouse; + +import java.util.List; public class PageText implements IBookletPage{ @@ -32,6 +36,11 @@ public class PageText implements IBookletPage{ this.chapter = chapter; } + @Override + public BookletChapter getChapter(){ + return this.chapter; + } + @Override public String getText(){ return StringUtil.localize("booklet."+ModUtil.MOD_ID_LOWER+".chapter."+this.chapter.getUnlocalizedName()+".text."+this.id); @@ -46,4 +55,38 @@ public class PageText implements IBookletPage{ public void render(GuiBooklet gui, int mouseX, int mouseY){ gui.unicodeRenderer.drawSplitString(gui.currentPage.getText(), gui.guiLeft+14, gui.guiTop+11, 115, 0); } + + @Override + public ItemStack getItemStackForPage(){ + return null; + } + + @SuppressWarnings("unchecked") + protected void renderTooltipAndTransfer(GuiBooklet gui, ItemStack stack, int x, int y, boolean checkAndTransfer){ + List list = stack.getTooltip(gui.mc.thePlayer, gui.mc.gameSettings.advancedItemTooltips); + + for(int k = 0; k < list.size(); ++k){ + if(k == 0){ + list.set(k, stack.getRarity().rarityColor+(String)list.get(k)); + } + else{ + list.set(k, StringUtil.GRAY+list.get(k)); + } + } + + if(checkAndTransfer){ + for(IBookletPage page : InitBooklet.pagesWithItemStackData){ + if(page.getItemStackForPage() != null && page.getItemStackForPage().isItemEqual(stack)){ + list.add(StringUtil.ORANGE+"Click to see Recipe!"); + + if(Mouse.isButtonDown(0)){ + gui.openChapter(page.getChapter(), page); + } + break; + } + } + } + + gui.drawHoveringText(list, x, y); + } }