Skip to content

Commit

Permalink
Add enchantment bonus to bookshelfs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
KnightMiner committed Mar 31, 2018
1 parent e8692ae commit 8f244ec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Expand Up @@ -382,6 +382,21 @@ public void getDrops(NonNullList<ItemStack> 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,
Expand Down
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -65,6 +70,9 @@ public void setInventorySlotContents(int slot, ItemStack itemstack) {
}
}
}

// clear bonus to recalculate it
enchantBonus = Float.NaN;
}

/*
Expand Down Expand Up @@ -108,7 +116,7 @@ public GuiContainer createGui(InventoryPlayer inventoryplayer, World world, Bloc


/*
* Redstone logic
* Extra logic
*/

public int getPower() {
Expand All @@ -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
Expand Down Expand Up @@ -185,5 +214,4 @@ public void readFromNBT(NBTTagCompound tags) {
forgeData.removeTag(TextureBlockUtil.TAG_TEXTURE);
}
}

}

0 comments on commit 8f244ec

Please sign in to comment.