Checking inventory for enchanted book

Discussion in 'Plugin Development' started by PauleFaule, Jan 14, 2015.

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

    PauleFaule

    Dear Community,
    I was trying to make small plugin that is able to transfer an enchantment from an enchanted book to the item in hand. My problem: How do I check for the enchantment? I can check for the item "enchanted book" in inventory, but I absolutely don't know how to continue via GetInventory...

    My Code:

    if(p.getInventory().getContents().equals(new itemStack(Material.ENCHANTED_BOOK).containsEnchantment(Enchantment.DAMAGE_ALL))){
    p.getInventory().remove(new ItemStack(Material.ENCHANTED_BOOK, 1));
    p.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, 1);
    }


    It looks a bit chaotic, but as I said, I don't know any further... If you know a solution, please help.
    Thanks in advance,
    PauleFaule
     
  2. Offline

    nverdier

    @PauleFaule
    1) Iterate through contents of inventory.
    2) If the current item in the iteration is an enchanted book, then get the enchantments and levels that are on it.
    3) Add the enchantments to the current item in hand.
    4) Do whatever you want to the book (e.g. Remove it from the player's inventory).
     
  3. Offline

    PauleFaule

    Ok thanks, I'm just gonna try it :)

    Okay... I did what you told me, but I still get a CommandException and already tried so much, but it will stay...
    My actual code is:

    for(ItemStack Item : p.getInventory()){
    if(Item.getType() == Material.ENCHANTED_BOOK && Item.getEnchantmentLevel(Enchantment.DAMAGE_ALL) == 0){
    p.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, 0);
    p.getInventory().removeItem(Item);
    }
    }

    Do you know what's wrong?
     
    Last edited by a moderator: Jan 14, 2015
  4. Offline

    Skionz

    @PauleFaule Post all relevant classes and any stack-traces in their entirety.
     
  5. Offline

    PauleFaule

    I don't exactly know what you mean, because I'm not really versed in the java - jargon, but I think this will do it:

    Code:java
    1.  
    2.  
    3. public class TestPlugin extends JavaPlugin {
    4.  
    5. @Override
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    7.  
    8. Player p = null;
    9. if(sender instanceof Player){
    10. p = (Player)sender;
    11.  
    12. }
    13.  
    14. if(cmd.getName().equalsIgnoreCase("enchant")){
    15.  
    16. for(ItemStack Item : p.getInventory()){
    17. if(Item.getType() == Material.ENCHANTED_BOOK && Item.getEnchantmentLevel(Enchantment.DAMAGE_ALL) == 0){
    18. p.getItemInHand().addEnchantment(Enchantment.DAMAGE_ALL, 0);
    19. p.getInventory().removeItem(Item);
    20. return true;
    21. } else {
    22. p.sendMessage("You don't have the required enchanted book.");
    23.  
    24. return true;
    25. }
    26. }
    27. }
    28. return false;
    29. }
    30. }
    31.  
     
    Last edited: Jan 15, 2015
  6. Offline

    PreFiXAUT

    @PauleFaule You don't check any NPE's at all.
    • You don't check p. You just set it if it is a Player. When the Console uses it, it will break preety hard, so return/stop when he isn't a Player.
    • You don't check the Item in the Players hand (p.getItemInHand()) could return null when he isn't holding anything, or you're trying to enchant AIR, which will basicly don't do anything.
    • You check if the Enchantment-Level is 0, and if so, add the Enchantment with Level 0 to it...this will not add anything (I think, I didn't do much with Enchantments), so set it to 1 at least. Over here you should simply do this: Get the Metadata/Enchantments of the Book, and simply add them to the Item. Otherwise you need to do it all manually which doesn't make any sense (Except if you have a White-/Blacklist)
     
  7. Offline

    PauleFaule

    @PreFiXAUT I know that I would have to check them all, but I always just tried as a player, with an unenchanted sword in my hand and enchantment- and potionlevels start counting at 0. I don't think that this would change anything...
     
  8. Offline

    PreFiXAUT

    @PauleFaule Did you simply try to debug it? broadcast everything -> When you found a Enchanted-Book "found book on slot 'bla' with entchantment 'foo'", "Current Item in Hand: 'sword'" etc.
     
  9. Offline

    PauleFaule

    @PreFiXAUT Okay, I'm getting closer and closer. You were right: The enchantments start counting at 1. But now I got the problem: He doesn't find the book anymore!

    Code:java
    1.  
    2. if(args[1].equalsIgnoreCase("1")){
    3. if(Book.getType() == Material.ENCHANTED_BOOK && Book.getEnchantmentLevel(Enchantment.DAMAGE_ALL) == 1){
    4. p.getInventory().removeItem(Book);
    5. Hand.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    6. return true;
    7. } else {
    8. p.sendMessage("You don't have the required book.");
    9. return true;
    10. }
    11. }
    12.  
    13.  


    Like in this example, I always get the message, that I don't have the required book, although I have an enchanted book with sharpness lvl1...
     
    Last edited: Jan 15, 2015
  10. Offline

    drpk

    @PauleFaule don't loop through the inventory, loop through the contents of the inventory.
     
  11. Offline

    PauleFaule

    @drpk Okay, so I changed the "ItemStack Book : p.getInventory()" to "ItemStack Book : p.getInventory().getContents()" ... But that didn't change anything...
     
  12. Offline

    drpk

  13. Offline

    Experminator

    Code:
    while(p.getInventory().getContents().contains(new ItemStack(Material.ENCHANTED_BOOK))){
        p.getInventory().remove(new ItemStack(Material.ENCHANTED_BOOK));
    }
    
    @PauleFaule This example would work, i think.
     
  14. Offline

    PauleFaule

    @Experminator It's not just the item, but also the enchantment... That was my main-problem...
     
  15. Offline

    Experminator

    @PauleFaul I believe there is a hasEnchantments method in the ItemStack class.
     
Thread Status:
Not open for further replies.

Share This Page