Check if player has sword enchantment lever over a cetain number

Discussion in 'Spigot Plugin Development' started by ISP4, Apr 21, 2017.

Thread Status:
Not open for further replies.
  1. How would I check if a player has a sword over the sharpness level?
     
  2. loop through item contents, check if item stack has enchantment, check if that enchantment's level is greater than X. if true, remove it from inventory.
     
    • Informative Informative x 1
  3. try with this:
    Code (Text):
    if(!p.getItemInHand().getEnchantments().isEmpty()){
                            if(p.getItemInHand().getEnchantments().containsKey(Enchantment.DAMAGE_ALL)){
                               int level = p.getItemInHand().getEnchantmentLevel(Enchantment.DAMAGE_ALL);
                               if(level+1 == The_level_you_want_to_check){
                             
                               }
                            }
                        }
     
    #3 DeadSilenceIV, Apr 21, 2017
    Last edited: Apr 21, 2017
    • Informative Informative x 1
  4. See, What i'm trying to achieve is ban the player if they have a sword over sharpness 3 because we are a HCF Server and if you have a OP Sword then it will ban you.
     
  5. try with this:
    Code (Text):
    @EventHandler
        public void oninteract(PlayerInteractEvent e){
            p = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK||e.getAction() == Action.LEFT_CLICK_AIR||e.getAction() == Action.LEFT_CLICK_BLOCK){
                if(!p.getItemInHand().getEnchantments().isEmpty()){
                    if(p.getItemInHand().getEnchantments().containsKey(Enchantment.DAMAGE_ALL)){
                       int level = p.getItemInHand().getEnchantmentLevel(Enchantment.DAMAGE_ALL);
                       if(level+1 == 3){
                           Bukkit.getBanList(Type.NAME).addBan(p.getName(), reason, expiration, additionalInfo);
                       }
                    }
                }
            }
        }
     
    • Informative Informative x 1
  6. See your code only states that if you have level 3 then it will ban you how would I make it go greater? eg, if(level+1 => 3){
     
  7. >= means greater than or equal to three


    Sent from my iPhone using Tapatalk
     
    • Informative Informative x 1
  8. If okay, if you want you can use >= to compare if it is equal or greater
     
    • Informative Informative x 1
  9. Use this better
    Code (Text):
    @EventHandler
        public void oninteract(PlayerInteractEvent e){
            p = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK||e.getAction() == Action.LEFT_CLICK_AIR||e.getAction() == Action.LEFT_CLICK_BLOCK){
                if(!p.getItemInHand().getEnchantments().isEmpty()){
                    if(p.getItemInHand().getEnchantments().containsKey(Enchantment.DAMAGE_ALL)){
                       int level = p.getItemInHand().getEnchantmentLevel(Enchantment.DAMAGE_ALL);
                       if(level+1 >= 3){
                           p.kickPlayer(ChatColor.translateAlternateColorCodes('&', "&4Reason"));
                           p.setBanned(true);
                       }
                    }
                }
            }
        }
     
    • Informative Informative x 1
  10. Thank You,
    I have done this with my code:

    Code (Text):
    package org.ipvp.hcf.listener;

    import org.bukkit.Bukkit;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class BadEnchant {
       
        @EventHandler
          public void oninteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
             if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK||e.getAction() == Action.LEFT_CLICK_AIR||e.getAction() == Action.LEFT_CLICK_BLOCK){
                if(!p.getItemInHand().getEnchantments().isEmpty()){
                   if(p.getItemInHand().getEnchantments().containsKey(Enchantment.DAMAGE_ALL)){
                      int level = p.getItemInHand().getEnchantmentLevel(Enchantment.DAMAGE_ALL);
                            if(level+1 >= 1){
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "ban " + e.getPlayer().getName() + " Illegal items (Sword)");
                            }
                   }
                }
             }
        }
    }
     
  11. Implements EventHandler in class Because you need to register in onenable And then go to the main class in the onenable method and place this to record the event
    Code (Text):
    @Override
        public void onEnable() {
        this.getServer().getPluginManager().registerEvents(new BadEnchant(), this);
        }
     
  12. Just out of curiosity why are you bothering with if action==whatever?

    You probably want this check to happen no matter what type of action they do right? There's no reason to exclude an item from this check based on the action. You can remove all those action checks


    Sent from my iPhone using Tapatalk
     
  13. You're right , Good suggestion
     
  14. Your gonna want to cancel the event if you hit another player because you'd still kill them Keelan.
     
  15. And also, if you want the event to be fired even when someone click air you need to use ignoreCancelled tag in the EventHandler
     
  16. Action.PHYSICAL Thats the Action i'm doing correct?

    Pretty sure Action.LEFT_CLICK_AIR already does that.
     
  17. Nope, because the event fire as already cancelled. So you need to listen for the event even if it's cancelled
     
  18. Again no need to check any action.. that event fires on EVERY action unless you specify an action to narrow it down by.

    However you do want to add ignoreCancelled as suggested because if they swing at air the event will get cancelled and your code won't run.




    Sent from my iPhone using Tapatalk
     
    • Agree Agree x 1
  19. Ah, So if I add the Actions it will pretty much disable the other actions by narrowing down to the stated actions?

    EDIT: Turns out it doesn't block the player hit even how would I disable it so that if the player trys to use it on a person it bans them?
     
Thread Status:
Not open for further replies.