Solved Getting enchanted book enchantment?

Discussion in 'Plugin Development' started by MattexFilms, Aug 28, 2013.

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

    MattexFilms

    How can I get the enchantment of the enchanted book a player is holding. This is what I want to do: PlayerA is holding enchanted book > type command > book is removed from PlayerA inventory > given to PlayerB.

    Trying to get the enchantment through .getItemMeta() doesn't work. Are enchantments on enchanted books different to items?
     
  2. MattexFilms Just get his item in hand, get the itemstack and give it to the other player?
     
  3. Offline

    MattexFilms

    CaptainBern I tried that, all that did was give PlayerB an enchanted book called enchanted book, the enchantment and purple glow were removed.
     
  4. Offline

    MattexFilms

    CaptainBern

    This is getting the item in hand from PlayerA and giving it to PlayerB. All it does is give PlayerB an item with the enchanted book texture called Enchanted Book, any enchantment and purple glow is removed.
    Code:java
    1. if(pl.getItemInHand().getType().equals(Material.ENCHANTED_BOOK)){
    2. ItemStack book = new ItemStack(pl.getItemInHand().getType());
    3. pl.remove(pl.getItemInHand());
    4. tl.addItem(new ItemStack(book));
    5. }
     
  5. That won't work because an Enchanted book has enchantment "data". You need to do:
    Code:
    if (pl.getItemInHand().getType().equals(Material.ENCHANTED_BOOK)) {
        ItemStack theBook = pl.getItemInHand();
        pl.remove(pl.getItemInHand());
        tl.addItem(theBook);
    }
    
    You said:
    Code:
    tl.addItem(new ItemStack(book));
    Why would you create a new ItemStack with the same itemstack in the parameter? o.o

    The way I told you previously I've never done before but I know you can definitely do:
    Code:
    if (pl.getItemInHand().getType().equals(Material.ENCHANTED_BOOK)) {
        ItemStack theBook = new ItemStack(pl.getItemInHand().getType(), pl.getItemInHand().getAmount));
        theBook.setItemMeta(pl.getItemInHand().getItemMeta());
        pl.remove(pl.getItemInHand());
        tl.addItem(theBook);
    }
    
    MattexFilms
    Just look at what I did, you did something similar except you could just leave it as an ItemMeta and set the new ItemStack meta to it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    MattexFilms likes this.
  6. Offline

    MattexFilms

    Thanks, all working now.
     
    KingFaris11 likes this.
  7. No problem.
     
Thread Status:
Not open for further replies.

Share This Page