From 117d0116d2a4952c9c34538f9adaa29fe3def566 Mon Sep 17 00:00:00 2001 From: Vaern Date: Thu, 20 Oct 2022 15:00:50 -0700 Subject: [PATCH] ItemBookLore, GUIBookLore, instant pain story of my life https://cdn.discordapp.com/attachments/759183012494966795/1024516652278231110/trim.1B823851-DAB1-4A47-B5CE-BFB3B7062D4E_1.mov --- src/main/java/com/hbm/handler/GUIHandler.java | 7 + .../com/hbm/inventory/gui/GUIBookLore.java | 287 ++++++++++++++++++ src/main/java/com/hbm/items/ModItems.java | 3 + .../com/hbm/items/special/ItemBookLore.java | 135 ++++++++ src/main/resources/assets/hbm/lang/en_US.lang | 9 + 5 files changed, 441 insertions(+) create mode 100644 src/main/java/com/hbm/inventory/gui/GUIBookLore.java create mode 100644 src/main/java/com/hbm/items/special/ItemBookLore.java diff --git a/src/main/java/com/hbm/handler/GUIHandler.java b/src/main/java/com/hbm/handler/GUIHandler.java index 647b1112d..8492e2666 100644 --- a/src/main/java/com/hbm/handler/GUIHandler.java +++ b/src/main/java/com/hbm/handler/GUIHandler.java @@ -21,6 +21,7 @@ import com.hbm.wiaj.GuiWorldInAJar; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import cpw.mods.fml.common.network.IGuiHandler; @@ -847,6 +848,12 @@ public class GUIHandler implements IGuiHandler { return ((IGUIProvider) block).provideGUI(ID, player, world, x, y, z); } + Item item = player.getHeldItem().getItem(); + + if(item instanceof IGUIProvider) { + return ((IGUIProvider) item).provideGUI(ID, player, world, x, y, z); + } + //stop doing this unless you absolutely have to \/ if(entity instanceof TileEntityCrateIron) { return new GUICrateIron(player.inventory, (TileEntityCrateIron) entity); } diff --git a/src/main/java/com/hbm/inventory/gui/GUIBookLore.java b/src/main/java/com/hbm/inventory/gui/GUIBookLore.java new file mode 100644 index 000000000..e0d2839fa --- /dev/null +++ b/src/main/java/com/hbm/inventory/gui/GUIBookLore.java @@ -0,0 +1,287 @@ +package com.hbm.inventory.gui; + +import java.util.ArrayList; +import java.util.List; + +import org.lwjgl.opengl.GL11; + +import com.hbm.items.special.ItemBookLore.*; +import com.hbm.items.tool.ItemGuideBook.BookType; +import com.hbm.lib.RefStrings; +import com.hbm.util.I18nUtil; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.audio.PositionedSoundRecord; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.ResourceLocation; + +// and you may ask yourself: vaer, why do this? this is basically just a copy of GUIScreenGuide. +// and I would answer, shut the fuck up nerd, the guide book system is too involved for my small +// brain to use for god knows how many tidbits of lore. i'll settle for a text box and cool textures, thanks +public class GUIBookLore extends GuiScreen { + + protected int xSize; + protected int ySize; + protected int guiLeft; + protected int guiTop; + + private BookLoreType type; + private GUIAppearance setup; + + int page = 0; + int maxPage; + + public GUIBookLore(EntityPlayer player) { + + type = BookLoreType.getTypeFromStack(player.getHeldItem()); + setup = type.appearance; + + if(setup.mainPage.isTwoPages && type.pages <= 1) { + xSize = setup.auxPage.sizeX; + ySize = setup.auxPage.sizeY; + } else { + xSize = setup.mainPage.sizeX; + ySize = setup.mainPage.sizeY; + } + + maxPage = setup.mainPage.isTwoPages ? (int)Math.ceil(type.pages / 2D) - 1 : type.pages; + System.out.print((int)Math.ceil(type.pages / 2D) - 1); + } + + @Override + public void initGui() { + super.initGui(); + this.guiLeft = (this.width - this.xSize) / 2; + this.guiTop = (this.height - this.ySize) / 2; + } + + @Override + public void drawScreen(int mouseX, int mouseY, float f) { + this.drawDefaultBackground(); + this.drawGuiContainerBackgroundLayer(f, mouseX, mouseY); + GL11.glDisable(GL11.GL_LIGHTING); + this.drawGuiContainerForegroundLayer(mouseX, mouseY); + GL11.glEnable(GL11.GL_LIGHTING); + } + + protected void drawGuiContainerBackgroundLayer(float f, int i, int j) { + GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); + + if(page == maxPage && setup.mainPage.isTwoPages && this.page * 2 >= this.type.pages) //odd numbered pages + Minecraft.getMinecraft().getTextureManager().bindTexture(setup.auxPage.texture); + else + Minecraft.getMinecraft().getTextureManager().bindTexture(setup.mainPage.texture); + + func_146110_a(guiLeft, guiTop, 0, 0, xSize, ySize, 512, 512); + + if(page > 0) + setup.button.renderButton(this, xSize, guiLeft, guiTop, false, i, j); + + if(page < maxPage) + setup.button.renderButton(this, xSize, guiLeft, guiTop, true, i, j); + } + + protected void drawGuiContainerForegroundLayer(int x, int y) { + String key = "book_lore." + type.keyI18n + ".page."; + + if(setup.mainPage.isTwoPages) { + int defacto = this.page * 2 + 1; + + if((this.page + 1) * 2 <= this.type.pages) { //TODO: change this to make it accurate for odd-numbered max pages + setup.mainPage.renderText(key + defacto, fontRendererObj, guiLeft, guiTop, false); + setup.mainPage.renderText(key + (defacto + 1), fontRendererObj, guiLeft, guiTop, true); + } else + setup.auxPage.renderText(key + defacto, fontRendererObj, guiLeft, guiTop, false); + + } else { + setup.mainPage.renderText(key + page, fontRendererObj, guiLeft, guiTop, false); + } + + } + + @Override + protected void mouseClicked(int i, int j, int k) { + int q = 0; //if both buttons are somehow simultaneously clicked then obviously something's wrong already + + if(page > 0) + q = setup.button.handleInput(xSize, guiLeft, guiTop, false, i, j); + + if(page < maxPage && q == 0) + q = setup.button.handleInput(xSize, guiLeft, guiTop, true, i, j); + + if(q != 0) { + mc.getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F)); + this.page += q; + } + } + + @Override + protected void keyTyped(char c, int key) { + if(key == 1 || key == this.mc.gameSettings.keyBindInventory.getKeyCode()) { + this.mc.thePlayer.closeScreen(); + } + } + + // turn page buttons, one-page, both page textures, sizes, positions, etc. + public enum GUIAppearance { + GUIDEBOOK(new GUIPage(272, 182, 20, 20, new ResourceLocation(RefStrings.MODID + ":textures/gui/book/book.png")).setScale(2F), + new GUIPageButton(18, 10, 24, 155, 0, 0, 500, 200, new ResourceLocation(RefStrings.MODID + ":textures/gui/book/starter6.png")), + 0); + + public int itemTexture; + + protected GUIPage mainPage; //"Main" page, usually two pages. GUI accounts for one-paged main pages. + protected GUIPage auxPage; //"Aux" page, AKA the final page if the max pages is oddly numbered. + //If two-sided, text will be positioned on the left page. Can be null if main page is one-sided. + protected GUIPageButton button; + + private GUIAppearance(GUIPage main, GUIPage aux, GUIPageButton button, int texture) { + this.mainPage = main; + this.auxPage = aux; + this.button = button; + this.itemTexture = texture; + } + + private GUIAppearance(GUIPage main, GUIPageButton button, int texture) { + this.mainPage = main; + this.auxPage = main; + this.button = button; + this.itemTexture = texture; + } + + } + + private static class GUIPage { + protected ResourceLocation texture; + + protected int sizeX; + protected int sizeY; + + //Text positioning + protected int marginX; //Boundaries of the textbook, relative to the pages' edges. + protected int marginY; //Mirrored on both sides if two-sided. + protected boolean isTwoPages = true; + protected float scale = 1.0F; + + protected GUIPage(int x, int y, int marX, int marY, ResourceLocation texture, boolean twoPages) { + this.sizeX = x; + this.sizeY = y; + this.marginX = marX; + this.marginY = marY; + this.texture = texture; + this.isTwoPages = twoPages; + } + + protected GUIPage(int x, int y, int marX, int marY, ResourceLocation texture) { + this.sizeX = x; + this.sizeY = y; + this.marginX = marX; + this.marginY = marY; + this.texture = texture; + } + + protected GUIPage setScale(float scale) { + this.scale = scale; + return this; + } + + protected void renderText(String key, FontRenderer renderer, int left, int top, boolean secondPage) { + String text = I18nUtil.resolveKey(key); + int width = isTwoPages ? (sizeX / 2) - (marginX * 2) : sizeX - (marginX * 2); + int widthScaled = (int) (width * scale); + + List lines = new ArrayList(); + String[] words = text.split(" "); + + lines.add(words[0]); + int indent = renderer.getStringWidth(words[0]); + + for(int w = 1; w < words.length; w++) { + + indent += renderer.getStringWidth(" " + words[w]); + + if(indent <= widthScaled) { + String last = lines.get(lines.size() - 1); + lines.set(lines.size() - 1, last += (" " + words[w])); + } else { + lines.add(words[w]); + indent = renderer.getStringWidth(words[w]); + } + } + + GL11.glPushMatrix(); + GL11.glScalef(1F/scale, 1F/scale, 1F); + + int sideOffset = secondPage ? sizeX - marginX - width : marginX; + + for(int l = 0; l < lines.size(); l++) { + renderer.drawString(lines.get(l), (int)((left + sideOffset) * scale), (int)((top + marginY) * scale + (12 * l)), 4210752); + } + + GL11.glPopMatrix(); + } + } + + private static class GUIPageButton { + protected ResourceLocation texture; + + protected int sizeX; //size of a single button; full texture is 2*sizeX : 2*sizeZ + protected int sizeY; + protected int x; //x position on page, relative to edge of the page it is on. + protected int y; //y position on page, relative to the top edge of the page. + + /* Left, Unsel | Right, Unsel + * Left, Sel | Right, Sel + */ + protected int u; //upper lefthand corner where the button textures lie. + protected int v; //assumes uniform size for each. + protected int sizeU; + protected int sizeV; + + protected GUIPageButton(int sizeX, int sizeY, int x, int y, int u, int v, int sizeU, int sizeV, ResourceLocation tex) { + this.sizeX = sizeX; + this.sizeY = sizeY; + this.x = x; + this.y = y; + this.u = u; + this.v = v; + this.texture = tex; + this.sizeU = sizeU; + this.sizeV = sizeV; + } + + protected void renderButton(GuiScreen screen, int width, int left, int top, boolean rightPage, int i, int j) { + Minecraft.getMinecraft().getTextureManager().bindTexture(texture); + boolean overY = j >= top + y && j < top + y + sizeY; + + if(!rightPage) { + if(i >= left + x && i < left + x + sizeX && overY) { + func_146110_a(left + x, top + y, u, v + sizeY, sizeX, sizeY, sizeX * 2, sizeY * 2); + } else { + func_146110_a(left + x, top + y, u, v, sizeX, sizeY, sizeX * 2, sizeY * 2); + } + } else { + if(i >= left + width - x - sizeX && i < left + width - x && overY) { + func_146110_a(left + width - x - sizeX, top + y, u + sizeX, v + sizeY, sizeX, sizeY, sizeX * 2, sizeY * 2); + } else { + func_146110_a(left + width - x - sizeX, top + y, u + sizeX, v, sizeX, sizeY, sizeX * 2, sizeY * 2); + } + } + } + + protected int handleInput(int width, int left, int top, boolean rightPage, int i, int j) { + boolean overY = j >= top + y && j < top + y + sizeY; + if(!rightPage) { + if(i >= left + x && i < left + x + sizeX && overY) + return -1; + } else { + if(i >= left + width - x - sizeX && i < left + width - x && overY) + return 1; + } + + return 0; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/hbm/items/ModItems.java b/src/main/java/com/hbm/items/ModItems.java index 0be9a8f18..ac60a74f1 100644 --- a/src/main/java/com/hbm/items/ModItems.java +++ b/src/main/java/com/hbm/items/ModItems.java @@ -2450,6 +2450,7 @@ public class ModItems { public static Item record_glass; public static Item book_guide; + public static Item book_lore; public static Item holotape_image; public static Item holotape_damaged; @@ -5611,6 +5612,7 @@ public class ModItems { record_glass = new ItemModRecord("glass").setUnlocalizedName("record_glass").setCreativeTab(null).setTextureName(RefStrings.MODID + ":record_glass"); book_guide = new ItemGuideBook().setUnlocalizedName("book_guide").setCreativeTab(MainRegistry.consumableTab).setTextureName(RefStrings.MODID + ":book_guide"); + book_lore = new ItemBookLore().setUnlocalizedName("book_lore").setCreativeTab(null).setTextureName(RefStrings.MODID + ":holotape"); holotape_image = new ItemHolotapeImage().setUnlocalizedName("holotape_image").setCreativeTab(null).setTextureName(RefStrings.MODID + ":holotape"); holotape_damaged = new Item().setUnlocalizedName("holotape_damaged").setCreativeTab(null).setTextureName(RefStrings.MODID + ":holotape_damaged"); @@ -8179,6 +8181,7 @@ public class ModItems { //wow we're far down the item registry, is this the cellar? GameRegistry.registerItem(book_guide, book_guide.getUnlocalizedName()); + GameRegistry.registerItem(book_lore, book_lore.getUnlocalizedName()); GameRegistry.registerItem(holotape_image, holotape_image.getUnlocalizedName()); GameRegistry.registerItem(holotape_damaged, holotape_damaged.getUnlocalizedName()); diff --git a/src/main/java/com/hbm/items/special/ItemBookLore.java b/src/main/java/com/hbm/items/special/ItemBookLore.java new file mode 100644 index 000000000..e4c6c78bd --- /dev/null +++ b/src/main/java/com/hbm/items/special/ItemBookLore.java @@ -0,0 +1,135 @@ +package com.hbm.items.special; + +import java.util.List; + +import com.hbm.inventory.gui.GUIBookLore; +import com.hbm.inventory.gui.GUIBookLore.GUIAppearance; +import com.hbm.items.tool.ItemGuideBook.BookType; +import com.hbm.lib.RefStrings; +import com.hbm.main.MainRegistry; +import com.hbm.tileentity.IGUIProvider; +import com.hbm.util.I18nUtil; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.IIcon; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.World; + +/*players can have a lil lore, as a treat. + * nothing super complex like the guidebooks, just some NBT IDs, a bit of I18n and a centered textbox. + * oh, and also different textures for both the book, the gui, and maybe the 'turn page' button based on what type of 'book' it is. + * no metadata, i want it to be fairly flexible. probably like the assembly templates + */ +public class ItemBookLore extends Item implements IGUIProvider { + + public ItemBookLore() { + this.setMaxStackSize(1); + } + + @Override + public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) { + + if(world.isRemote) + player.openGui(MainRegistry.instance, 0, world, 0, 0, 0); + + return stack; + } + + @Override + public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool) { + BookLoreType type = BookLoreType.getTypeFromStack(stack); + + if(type.hasAuthor) { + String unloc = I18nUtil.resolveKey("book_lore.author", I18nUtil.resolveKey("book_lore." + type.keyI18n + ".author")); + + list.add(unloc); + } + } + + @Override + public String getUnlocalizedName(ItemStack stack) { + BookLoreType type = BookLoreType.getTypeFromStack(stack); + + return "book_lore." + type.keyI18n; + } + + protected IIcon[] icons; + //fuck you, fuck enums, fuck guis, fuck this shitty ass fork. shove that string array up your ass. + public static String[] itemTextures = new String[] { ":book_guide" }; + + @SideOnly(Side.CLIENT) + public void registerIcons(IIconRegister reg) { + String[] iconStrings = itemTextures; + this.icons = new IIcon[itemTextures.length]; + + for(int i = 0; i < icons.length; i++) { + this.icons[i] = reg.registerIcon(RefStrings.MODID + itemTextures[i]); + } + } + + @Override + public IIcon getIconIndex(ItemStack stack) { + return this.getIcon(stack, 1); + } + + @Override + public IIcon getIcon(ItemStack stack, int pass) { + BookLoreType type = BookLoreType.getTypeFromStack(stack); + + return this.icons[type.appearance.itemTexture]; + } + + @Override + public Container provideContainer(int ID, EntityPlayer player, World world, int x, int y, int z) { + return null; + } + + @Override + public GuiScreen provideGUI(int ID, EntityPlayer player, World world, int x, int y, int z) { + return new GUIBookLore(player); + } + + public enum BookLoreType { + TEST(true, "test", 5, GUIAppearance.GUIDEBOOK); + + //Why? it's quite simple; i am too burnt out and also doing it the other way + //is too inflexible for my taste + public final GUIAppearance appearance; //gui and item texture appearance + + public boolean hasAuthor = false; + public final String keyI18n; + public final int pages; + + private BookLoreType(Boolean author, String key, int max, GUIAppearance appearance) { + this.hasAuthor = author; + this.keyI18n = key; + this.pages = max; + this.appearance = appearance; + } + + private BookLoreType(String key, int max, GUIAppearance appearance) { + this.keyI18n = key; + this.pages = max; + this.appearance = appearance; + } + + public static BookLoreType getTypeFromStack(ItemStack stack) { + if(!stack.hasTagCompound()) { + stack.stackTagCompound = new NBTTagCompound(); + } + + NBTTagCompound tag = stack.getTagCompound(); + int ordinal = tag.getInteger("bookLoreOrdinal"); + + return BookLoreType.values()[Math.abs(ordinal) % BookType.values().length]; + } + } +} diff --git a/src/main/resources/assets/hbm/lang/en_US.lang b/src/main/resources/assets/hbm/lang/en_US.lang index 00e0b8aa5..0c5288be7 100644 --- a/src/main/resources/assets/hbm/lang/en_US.lang +++ b/src/main/resources/assets/hbm/lang/en_US.lang @@ -314,6 +314,15 @@ book.starter.page18=vær is just a guy who has been trapped in the grey void fea #book.rbmk.title16=Meltdown #book.rbmk.page16=§4§lAvoid. +book_lore.author=By %s +book_lore.test.name=Test +book_lore.test.author=the dude +book_lore.test.page.1=>hello anons before i begin let me clarify that i'm not gay. >Be me >This night >18 > At hanging out with my best friend with my parents gone for a few days >We've been best friends for a year now >Been drinking a bit and playing lots of video games and ordered a pizza >We were having a blast >At a certain point in the night like around 9:00 he makes a really funny joke that I don't remember but I know that it made us both laugh really hard > With out thinking I brush my right hand through his semi-curly black hair and call him a funny boy >He blushes >I realize I'm feeling flustered >We're kinda close >All of the sudden he kisses me and for some reason I kiss him back >We make love >Cuddle together and fall asleep >Wake up in the middle of the night with his head snuggled up on my chest and neck area >It feels nice but I'm not a homosexual +book_lore.test.page.2=I'm typing this as he's asleep in my arms. How do I let my best friend down nicely? I don't want to be a [redacted] /b/ +book_lore.test.page.3=3 +book_lore.test.page.4=4 +book_lore.test.page.5=5 + cannery.f1=[ Press F1 for help ] cannery.centrifuge=Gas Centrifuge