Plugin Help Can't register events for multiple classes

Discussion in 'Plugin Help/Development/Requests' started by yankeesblocks, Apr 19, 2015.

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

    yankeesblocks

    Hi, I am making a plugin, and it is getting lengthy so I want to reorganize it into different classes, but I cannot get any event listeners to work in any class other than the main one, I'm assuming it is because the events wont register. Here is the line of code in the main class attempting to register the events:
    Code:
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new ZListener(), this);
        //Extraneous stuff
    }
     
  2. Offline

    yankeesblocks

    Yes
    Code:
    public class ZListener implements Listener {
     
  3. Offline

    timtower Administrator Administrator Moderator

  4. Offline

    yankeesblocks

    If you mean did I add that tag above the event, yes I did. Here is one of the many events in the class ZListener:
    Code:
    //Throw
            @EventHandler
            public void EntityEffectByEntityDamage(EntityDamageByEntityEvent damage) {
                if (damage.getDamager() instanceof Zombie) {
                    Zombie z = (Zombie) damage.getDamager();
                    if (z.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE)) {
                        int random = (int)(Math.random()* 10 ) + 1;
                        if (random == 1) {
                            Player p = (Player) damage.getEntity();
                            z.getLocation().getWorld().playSound(z.getLocation(), Sound.EXPLODE, 1, 1);
                            p.getLocation().getWorld().playEffect(p.getLocation(), Effect.SMOKE, 50);
                            p.setVelocity(new Vector(p.getVelocity().getX(), 2, p.getVelocity().getY()));
                            p.sendMessage(ChatColor.DARK_GREEN + "§lZombie Boss" + ChatColor.RESET + " hit you with " + ChatColor.DARK_GREEN + "WhirlWind");
                        }
                    }
                }
            }
     
  5. Offline

    timtower Administrator Administrator Moderator

    @yankeesblocks Tried to add debug messages to the methods to see if they are running?
     
  6. Offline

    yankeesblocks

    @timtower sorry, I dont know all the in's and out's of Eclipse, how do I do that?
     
  7. Offline

    timtower Administrator Administrator Moderator

    @yankeesblocks Just a simple System.out.println or plugin.getLogger().log
    Has nothing to do with eclipse.
     
  8. Offline

    yankeesblocks

    @timtower I'm slightly confused as to what you are asking me to do, but the event is not running. In the event I put Bukkit.broadcastMessage("event"); and it was not triggered.
     
  9. Offline

    timtower Administrator Administrator Moderator

  10. Offline

    yankeesblocks

    @timtower yes, everything contained in the main class, and everything in other classes that is not in an event, works.

    @timtower I think the problem is that the events are not registering.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Apr 19, 2015
  11. Offline

    timtower Administrator Administrator Moderator

  12. Offline

    yankeesblocks

    Okay I did that, and it looks like the events wont activate in any class. Here is what I wrote in the main class:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent move) {
            move.getPlayer().sendMessage("Main Class is working");
        }
    and in the second class:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent move) {
            move.getPlayer().sendMessage("Second Class is working");
        }
    and neither fired. I have no idea what the problem is, so here is the entire code for each class:
    MobBosses
    Show Spoiler

    Code:
    package me.yankeesblock.mobBosses;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Skeleton;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class MobBosses extends JavaPlugin implements Listener {
       
        public void onEnable() {
            saveDefaultConfig();
            getServer().getPluginManager().registerEvents(new ZListener(), this);
            getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getLogger().info(ChatColor.GREEN + "Mob Bosses plugin by " + ChatColor.RED + "Yankeesblock " + ChatColor.RED + "loaded successfully!");
            Bukkit.getLogger().info(ChatColor.GOLD + "Bosses enabled:");
            Bukkit.getLogger().info(" ");
            if (!(getConfig().getInt("zombie") == 0)) {Bukkit.getLogger().info("     Zombie");}
            if (!(getConfig().getInt("skeleton") == 0)) {Bukkit.getLogger().info("     Skeleton");}
            //if (!(getConfig().getInt("witherSkelton") == 0)) {Bukkit.getLogger().info("     Wither Skelton");}
            //if (!(getConfig().getInt("blaze") == 0)) {Bukkit.getLogger().info("     Blaze");}
            //if (!(getConfig().getInt("enderman") == 0)) {Bukkit.getLogger().info("     EnderMan");}
            //if (getConfig().getBoolean("giant") == true) {Bukkit.getLogger().info("     " + ChatColor.RED + "Caution: Giant is enabled");}
        }
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent move) {
            move.getPlayer().sendMessage("Main Class is working");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            //Zombie
            if(cmd.getName().equalsIgnoreCase("summonboss")) {
                if (!(sender.hasPermission("mobbosses.summon.zombie"))) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage("You must specify a boss");
                    return true;
                }
                if (args[0].equalsIgnoreCase("zombie")) {
                    sender.sendMessage("Boss succusfully summoned");
                    Player comm = (Player) sender;
                    Zombie mob = (Zombie) comm.getLocation().getWorld().spawnEntity(comm.getLocation(), EntityType.ZOMBIE);
                    final Zombie z = mob;
                    //Particles
                    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            Location zl = z.getLocation();
                            if (!(z.isDead())) {
                            z.getLocation().getWorld().playEffect(zl, Effect.ENDER_SIGNAL, 1);
                            }
                        }
                    }, 0, 1);
                    //Fireball
                    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                        public void run() {
                            if (!(z.isDead())) {
                                if (z.hasLineOfSight(z.getTarget())) {
                                        Fireball f = z.launchProjectile(Fireball.class);
                                }
                            }
                        }
                    }, 0, 600);
                    ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                    sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 6);
                    sword.addUnsafeEnchantment(Enchantment.DAMAGE_ARTHROPODS, 6);
                    sword.addUnsafeEnchantment(Enchantment.DAMAGE_UNDEAD, 6);
                    sword.addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 10);
                    sword.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
                    sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
                    sword.setDurability((short) 1562);
                    //Potion Effects
                    z.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 3));
                    z.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 99999, 1));
                    z.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 99999, 3));
                    //Armor
                    z.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
                    z.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE, 1));
                    z.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS, 1));
                    z.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
                   
                    //Weapon
                    z.getEquipment().setItemInHand(sword);
                    z.getEquipment().getItemInHand().setDurability((short) 1562);
                    z.getEquipment().setItemInHandDropChance(0);
                    //Etc
                    z.setCustomNameVisible(true);
                    z.setCustomName(ChatColor.DARK_GREEN + "§lZombie Boss");
                    return true;
                }
            }
            return true;
        }
       
    }
    


    ZListener
    Show Spoiler

    Code:
    package me.yankeesblock.mobBosses;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    public class ZListener extends JavaPlugin implements Listener {
       
            //Death
            @EventHandler
            public void onEntityDeathEvent(EntityDeathEvent death) {
                if (death.getEntityType() == EntityType.ZOMBIE) {
                    if (death.getEntity().hasPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)) {
                        death.getEntity().getWorld().playSound(death.getEntity().getLocation(), Sound.ENDERDRAGON_DEATH, 1, 1);
                        death.getEntity().getWorld().playEffect(death.getEntity().getLocation(), Effect.MOBSPAWNER_FLAMES, 10);
                        death.setDroppedExp(1000);
                        ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                        sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 6);
                        sword.addUnsafeEnchantment(Enchantment.DAMAGE_ARTHROPODS, 6);
                        sword.addUnsafeEnchantment(Enchantment.DAMAGE_UNDEAD, 6);
                        sword.addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 10);
                        sword.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
                        sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
                        death.getDrops().add(sword);
                    }
                }
            }
            @EventHandler
            public void onPlayerMove(PlayerMoveEvent move) {
                move.getPlayer().sendMessage("Seconds Class is working");
                return;
            }
            //Hit
            @EventHandler
            public void onEntityDamageEvent(EntityDamageEvent damage){
                if (damage.getEntityType() == EntityType.ZOMBIE) {
                    Zombie z = (Zombie) damage.getEntity();
                    if (z.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE)) {
                        Bukkit.broadcastMessage("test");
                        Location zl = z.getLocation();
                        z.getLocation().getWorld().playSound(z.getLocation(), Sound.ENDERDRAGON_HIT, 1, 1);
                        z.getLocation().getWorld().playEffect(zl, Effect.SMOKE, 10);
                        return;
                    }
                    return;
                }
            }
            //Throw
            @EventHandler
            public void EntityEffectByEntityDamage(EntityDamageByEntityEvent damage) {
                if (damage.getDamager() instanceof Zombie) {
                    Zombie z = (Zombie) damage.getDamager();
                    if (z.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE)) {
                        int random = (int)(Math.random()* 10 ) + 1;
                        if (random == 1) {
                            Player p = (Player) damage.getEntity();
                            z.getLocation().getWorld().playSound(z.getLocation(), Sound.EXPLODE, 1, 1);
                            p.getLocation().getWorld().playEffect(p.getLocation(), Effect.SMOKE, 50);
                            p.setVelocity(new Vector(p.getVelocity().getX(), 2, p.getVelocity().getY()));
                            p.sendMessage(ChatColor.DARK_GREEN + "§lZombie Boss" + ChatColor.RESET + " hit you with " + ChatColor.DARK_GREEN + "WhirlWind");
                        }
                    }
                }
            }
            //Spawn
            @EventHandler
            public void onCreatureSpawn(CreatureSpawnEvent mob) {
                Bukkit.getServer().broadcastMessage("spawn");
                if (mob.getEntityType() == EntityType.ZOMBIE) {
                    if (!(getConfig().getInt("zombie") == 0)) {
                        int random = (int)(Math.random()* getConfig().getInt("zombie") ) + 1;
                            if (random == 1) {
                                final Zombie z = (Zombie) mob.getEntity();
                                //Particles
                                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                                    public void run() {
                                        Location zl = z.getLocation();
                                        if (!(z.isDead())) {
                                        z.getLocation().getWorld().playEffect(zl, Effect.ENDER_SIGNAL, 1);
                                        }
                                    }
                                }, 0, 1);
                                //Fireball
                                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
                                    public void run() {
                                        if (!(z.isDead())) {
                                            if (z.hasLineOfSight(z.getTarget())) {
                                                    Fireball f = z.launchProjectile(Fireball.class);
                                            }
                                        }
                                    }
                                }, 0, 600);
                                ItemStack sword = new ItemStack(Material.DIAMOND_SWORD, 1);
                                sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 6);
                                sword.addUnsafeEnchantment(Enchantment.DAMAGE_ARTHROPODS, 6);
                                sword.addUnsafeEnchantment(Enchantment.DAMAGE_UNDEAD, 6);
                                sword.addUnsafeEnchantment(Enchantment.LOOT_BONUS_MOBS, 10);
                                sword.addUnsafeEnchantment(Enchantment.DURABILITY, 10);
                                sword.addUnsafeEnchantment(Enchantment.FIRE_ASPECT, 1);
                                sword.setDurability((short) 1562);
                                //Potion Effects
                                z.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 99999, 3));
                                z.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 99999, 1));
                                z.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 99999, 3));
                                //Armor
                                z.getEquipment().setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
                                z.getEquipment().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE, 1));
                                z.getEquipment().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS, 1));
                                z.getEquipment().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
                               
                                //Weapon
                                z.getEquipment().setItemInHand(sword);
                                z.getEquipment().getItemInHand().setDurability((short) 1562);
                                z.getEquipment().setItemInHandDropChance(0);
                                //Etc
                                z.setCustomNameVisible(true);
                                z.setCustomName(ChatColor.DARK_GREEN + "§lZombie Boss");
                        }
                    }
                }
            }   
    
    }
    
     
Thread Status:
Not open for further replies.

Share This Page