Plugin Listener Not Working

Discussion in 'Plugin Development' started by stageosu, Sep 20, 2019.

  1. Offline

    stageosu

    Hello, I am very new to Minecraft plugins, but have lots of experience with programming, so I apologize if I'm making some blatantly obvious mistakes. I've looked far and wide for a solution but everything I've tried has not worked. What I am trying to do is, when a player right clicks with my special enchanted ender eye, give them some health buffs and return them home with the "home" command.

    The command to give a player the ender eye (/archgive) works perfectly fine. However, my "listener" does not. What my listener is supposed to do is "listen" for when a player right clicks either a block or the air with this item in their hand.

    ArchangelEyeListener
    Code:
    package me.kaguyabot.customitemgenerator;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.permissions.PermissionAttachment;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import java.util.*;
    
    public class ArchangelEyeListener implements Listener {
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                Player p = e.getPlayer();
                if(e.getItem() != null && e.getItem().getType().equals(Material.ENDER_EYE)
                && e.getItem().containsEnchantment(Enchantment.CHANNELING)
                && e.getItem().getI18NDisplayName().equalsIgnoreCase(
                        ChatColor.LIGHT_PURPLE + "Archangel's Eye of Liberation")) {
                    Logger logger = new Logger();
                    HashMap<UUID, PermissionAttachment> perms = new HashMap<UUID, PermissionAttachment>();
    
                    CustomItemGenerator customItemGeneratorClass = new CustomItemGenerator();
    
                    PermissionAttachment attachment = p.addAttachment(customItemGeneratorClass);
                    perms.put(p.getUniqueId(), attachment);
    
                    PermissionAttachment pperms = perms.get(p.getUniqueId());
                    pperms.setPermission("essentials.home", true);
    
                    p.getInventory().getItemInMainHand().subtract(1);
    
                    p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 30, 1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 15, 2));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 30, 2));
    
                    p.sendMessage(ChatColor.GOLD + "A voice from above whispers..." + ChatColor.ITALIC + "\"You have been liberated...\"");
    
                    p.playSound(p.getLocation(), Sound.AMBIENT_CAVE, 2, 0);
                    p.playSound(p.getBedSpawnLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 2, 0);
    
                    p.performCommand("home");
    
                    pperms.setPermission("essentials.home", false);
    
                    logger.log(String.format("Player {0} consumed 1x" + ChatColor.AQUA +
                            "Archangel's Eye of Liberation to return to safety.", p.getName()));
                }
            }
        }
    }
    
    My primary (Main) class:

    CustomItemGenerator
    Code:
    package me.kaguyabot.customitemgenerator;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.permissions.PermissionAttachment;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import java.util.*;
    
    public class ArchangelEyeListener implements Listener {
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                Player p = e.getPlayer();
                if(e.getItem() != null && e.getItem().getType().equals(Material.ENDER_EYE)
                && e.getItem().containsEnchantment(Enchantment.CHANNELING)
                && e.getItem().getI18NDisplayName().equalsIgnoreCase(
                        ChatColor.LIGHT_PURPLE + "Archangel's Eye of Liberation")) {
                    Logger logger = new Logger();
                    HashMap<UUID, PermissionAttachment> perms = new HashMap<UUID, PermissionAttachment>();
    
                    CustomItemGenerator customItemGeneratorClass = new CustomItemGenerator();
    
                    PermissionAttachment attachment = p.addAttachment(customItemGeneratorClass);
                    perms.put(p.getUniqueId(), attachment);
    
                    PermissionAttachment pperms = perms.get(p.getUniqueId());
                    pperms.setPermission("essentials.home", true);
    
                    p.getInventory().getItemInMainHand().subtract(1);
    
                    p.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 30, 1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 15, 2));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 30, 2));
    
                    p.sendMessage(ChatColor.GOLD + "A voice from above whispers..." + ChatColor.ITALIC + "\"You have been liberated...\"");
    
                    p.playSound(p.getLocation(), Sound.AMBIENT_CAVE, 2, 0);
                    p.playSound(p.getBedSpawnLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 2, 0);
    
                    p.performCommand("home");
    
                    pperms.setPermission("essentials.home", false);
    
                    logger.log(String.format("Player {0} consumed 1x" + ChatColor.AQUA +
                            "Archangel's Eye of Liberation to return to safety.", p.getName()));
                }
            }
        }
    }
    
    Any help is appreciated, thank you very much.
     
  2. Offline

    timtower Moderator Moderator

    @stageosu Did you register the listeners in your main class?
     

Share This Page