1.17.x how can I have multiple event listeners

Discussion in 'Spigot Plugin Development' started by mason-walker, Jun 28, 2021.

Thread Status:
Not open for further replies.
  1. I am making a custom plugin for a series I want to do, and when I was making a listener for EntitySpawnEvent, it made my other listener for an item ability stop working, how can I have 2 event Listeners in one project?
     
  2. You can have two listeners with no problem. This shouldn't be an issue. Do you register both events? Or do you get an error?
     
  3. I did register both, and there is no error
     
  4. Can I see the code? I need the code for both of the listeners and the part where you register both listeners.
     
  5. This is my first listener:
    Code (Text):
    public class zombies implements Listener {
        @EventHandler
    public void onZombieSpawn(EntitySpawnEvent event)
    {
        if(event.getEntity() instanceof Zombie) {
                event.getEntity().setCustomName("zombie" + "§c ");
                event.getEntity().setCustomNameVisible(true);
           }
        }
    }
    this is my second:
    Code (Text):
    public class TNTWand implements Listener {

        public static World world = Bukkit.getServer().getWorlds().get(0);
        @EventHandler
        public void onRightClick(PlayerInteractEvent event)
        {
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (event.getItem() != null) {
                    if (event.getItem().getItemMeta().equals(ItemManager.TNTWand.getItemMeta())) {
                        Player player = event.getPlayer();
                        world.spawnParticle(Particle.EXPLOSION_HUGE, player.getLocation(), 1);
                        for (Entity nearby : world.getNearbyEntities(player.getLocation(), 5, 4, 5)) {
                            if (nearby instanceof Player) {

                            } else {
                                if (nearby instanceof LivingEntity) {
                                    LivingEntity entity = (LivingEntity) nearby;
                                    entity.damage(200.5);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    this is where I register them:
    Code (Text):
    public final class MainPlugin extends JavaPlugin {

        @Override
        public void onEnable() {
            // Plugin startup logic
            getServer().getPluginManager().registerEvents(new TNTWand(), this);
            getServer().getPluginManager().registerEvents(new zombies(), this);
        }
    }
     
  6. Is there an error or a problem? Does one of the listeners not work? Also, why are you getting the first world instead of getting the world the player is in? The Player class has a method called Player#getWorld (An example with your code is 'player.getWorld()'). Use that instead.
     
  7. I do not get an error, and neither event listener works, but if I remove the entity one, the TNTWand one works fine.
     
  8. Well, all seems fine.. Did u try putting an output messsge as first instruction to see if it is caught when fired?
     
  9. Dude, we're helping you out in your original post, no need to open a new one...
     
    • Like Like x 1
  10. this started off as a "can I have 2 at once"
     
  11. Close this thread and answer in the other one - it has the code already
     
Thread Status:
Not open for further replies.