Check item in hand's enchantment levels

Discussion in 'Plugin Development' started by Meatiex, Jul 23, 2014.

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

    Meatiex

    How do I check the enchantment levels of a item?

    PlayerInteractEvent
    I want to get rid of all overpowered enchantments that players have, I enchanted stuff for people, but now I don't want them to have it, and I sure cant go around manually doing this..

    Any help is awesome, thanks :D
     
  2. Offline

    Gater12

  3. Offline

    Deleted user


    Well first, you need to decide how you're going to check for this item.
    Should it be..
    1. iterate the player's inventory contents on join
    2. listening to PlayerItemHeldEvent
    3. or even PlayerInteractEvent cuz yolo
    Honestly, I'm not sure which one I'd use... it depends on the type of approach you're trying to take, so yeah man.
    (I'd say check on join, however that doesn't account for items stored in chest and what else: therefore I'd probablytake advantage on the PlayerItemHeldEvent)

    So once you've chosen your approach you then
    1. PlayerJoinEvent - get the player's inventory, iterate said inventory (don't for-getArmorContents()?)
    2. PlayerItemHeldEvent - get the item *cough* getNewSlot() *cough*
    3. PlayerInteractEvent - get the player and then get the item, I believe getItem() does it.
    Once we have obtained the desired item to check for enchants, according to ItemStack() we may then use getEnchantmentLevel() to check the amplifier of an existing enchantment.
    However, before we can check for the enchantment level of an enchant on an ItemStack we must first check to see if it is enchanted as well as check for all present enchantments. To do this we can check to see if getEnchantments() is null (or isEmpty() - whatever). If enchantments are present then loop through them and check for each enchantment's level.
    Finally,
    If the ItemStack's enchantment type or level is that of the undesired, execute desired solution rather that be deceasing the enchantment level or simply removing the enchant. Let me know if you run into any problems or need some example code.
     
  4. Offline

    Meatiex

    @Eballer48, I read this topic, but that didn't get me very far...
    With your instructions and that other topic I was able to gather the code below, but still... it's not working.
    Code:java
    1. @EventHandler
    2. public void interact(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. ItemStack item = player.getItemInHand();
    5. if (player.getItemInHand().getEnchantments() != null) {
    6.  
    7. Map<Enchantment, Integer> enchantments = item.getEnchantments();
    8. //ps: is their any way to loop through enchantments instead of cheeking them all separately?
    9. if (enchantments.containsKey(Enchantment.DURABILITY)) {
    10. int level = enchantments.get(Enchantment.DURABILITY);
    11. if (level > 10) {
    12. player.sendMessage("[Test] DURABILITY Above Enchantments Level 10.");
    13. }
    14. }
    15.  
    16.  
    17. }
    18. }
     
Thread Status:
Not open for further replies.

Share This Page