Solved How to get bookshelf power of a EnchantingTable?

Discussion in 'Plugin Development' started by Tim_M, Aug 9, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    Tim_M

    How would I go about getting the book shelf power (capped at 15) from a
    EnchantingTable?

    It doesnt seem like there is a way. So I made my own. My code is unoptimized and ignores blocks in the way, but here it is for whoever wants it:

    Code:
    public int calculateBookshelfPower(Block block)
        {
            if (block.getType() != Material.ENCHANTING_TABLE)
                return -1;
    
            int power = 0;
    
            List<Location> checked = new ArrayList<>();
    
            for (int y = 0; y < 2; y++) {
                for (int x = -2; x < 3; x++) {
                    if (!checked.contains(block.getLocation().add(new Vector(x, y, -2))) && block.getLocation().add(new Vector(x, y, -2)).getBlock().getType() == Material.BOOKSHELF)
                        power++;
    
                    checked.add(block.getLocation().add(new Vector(x, y, -2)));
    
                    if (!checked.contains(block.getLocation().add(new Vector(x, y, 2))) && block.getLocation().add(new Vector(x, y, 2)).getBlock().getType() == Material.BOOKSHELF)
                        power++;
    
                    checked.add(block.getLocation().add(new Vector(x, y, 2)));
                }
                for (int z = -2; z < 3; z++) {
                    if (!checked.contains(block.getLocation().add(new Vector(2, y, z))) && block.getLocation().add(new Vector(2, y, z)).getBlock().getType() == Material.BOOKSHELF)
                        power++;
    
                    checked.add(block.getLocation().add(new Vector(2, y, z)));
    
                    if (!checked.contains(block.getLocation().add(new Vector(-2, y, z))) && block.getLocation().add(new Vector(-2, y, z)).getBlock().getType() == Material.BOOKSHELF)
                        power++;
    
                    checked.add(block.getLocation().add(new Vector(-2, y, z)));
                }
            }
    
            if (power > 15)
                power = 15;
    
            return power;
        }
    
     
    Last edited: Aug 14, 2021
  2. Offline

    rudraksha007

  3. Offline

    Tim_M

    Last edited: Aug 10, 2021
    rudraksha007 likes this.
  4. Offline

    Tim_M

    Bump. Sorry for the impatience, I just really need this.
     
  5. Offline

    Shqep

    @Tim_M
    I tried investigating and reading minecraft's mca files to see if there are any saved values about a table's enchanting power but saw nothing. Either it is because NBTExplorer couldn't see those values, so you should get an NBT library and check the NBT tags of an enchanting table.
    If there is no saved value but a method to retrieve this power, it probably still needs to do calculations and looks for nearby bookshelves under the hood. You can probably replicate this method if there's really nothing.
     
  6. Offline

    Tim_M

    Thanks for the reply. I suppose I can make my own, it can't be so hard to make with the wiki.
     
Thread Status:
Not open for further replies.

Share This Page