From 8f244ec49dd21c70def8af0394a09015535e67b2 Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Fri, 30 Mar 2018 23:33:45 -0500 Subject: [PATCH] Add enchantment bonus to bookshelfs A full shelf of normal books gives 1.5 times that of a normal bookshelf, while a full shelf of enchanted books is 2.5 times that of a normal shelf --- .../building/block/BlockBookshelf.java | 15 +++++++++ .../building/tileentity/TileBookshelf.java | 32 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/main/java/knightminer/inspirations/building/block/BlockBookshelf.java b/src/main/java/knightminer/inspirations/building/block/BlockBookshelf.java index a05038fc..a5c66512 100644 --- a/src/main/java/knightminer/inspirations/building/block/BlockBookshelf.java +++ b/src/main/java/knightminer/inspirations/building/block/BlockBookshelf.java @@ -382,6 +382,21 @@ public void getDrops(NonNullList drops, IBlockAccess world, BlockPos drops.add(TextureBlockUtil.getBlockItemStack(world, pos, state)); } + /** + * Determines the amount of enchanting power this block can provide to an enchanting table. + * @param world The World + * @param pos Block position in world + * @return The amount of enchanting power this block produces. + */ + @Override + public float getEnchantPowerBonus(World world, BlockPos pos) { + TileEntity te = world.getTileEntity(pos); + if(te instanceof TileBookshelf) { + return ((TileBookshelf) te).getEnchantPower(); + } + return 0; + } + public static enum BookshelfType implements IStringSerializable { NORMAL, RAINBOW, diff --git a/src/main/java/knightminer/inspirations/building/tileentity/TileBookshelf.java b/src/main/java/knightminer/inspirations/building/tileentity/TileBookshelf.java index 3351300f..e69e2b7a 100644 --- a/src/main/java/knightminer/inspirations/building/tileentity/TileBookshelf.java +++ b/src/main/java/knightminer/inspirations/building/tileentity/TileBookshelf.java @@ -16,6 +16,7 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; @@ -34,8 +35,12 @@ public class TileBookshelf extends TileInventory implements IInventoryGui { + /** Cached enchantment bonus, so we are not constantly digging the inventory */ + private float enchantBonus; + public TileBookshelf() { super("gui.inspirations.bookshelf.name", 14, 1); + enchantBonus = Float.NaN; } @Override @@ -65,6 +70,9 @@ public void setInventorySlotContents(int slot, ItemStack itemstack) { } } } + + // clear bonus to recalculate it + enchantBonus = Float.NaN; } /* @@ -108,7 +116,7 @@ public GuiContainer createGui(InventoryPlayer inventoryplayer, World world, Bloc /* - * Redstone logic + * Extra logic */ public int getPower() { @@ -124,6 +132,27 @@ public int getPower() { return 0; } + public float getEnchantPower() { + // if we have a cached value, use that + if(!Float.isNaN(enchantBonus)) { + return enchantBonus; + } + // simple sum of all books with the power of a full shelf + float books = 0; + for(int i = 0; i < this.getSizeInventory(); i++) { + if(isStackInSlot(i)) { + if(getStackInSlot(i).getItem() == Items.ENCHANTED_BOOK) { + books += 2.5; + } else { + books += 1.5; + } + } + } + + // divide by 14 since that is the number of books in a shelf + enchantBonus = books / 14; + return enchantBonus; + } /* * Rendering @@ -185,5 +214,4 @@ public void readFromNBT(NBTTagCompound tags) { forgeData.removeTag(TextureBlockUtil.TAG_TEXTURE); } } - }