Event only runs once succesfully

Discussion in 'Spigot Plugin Help' started by CyberDrain, May 30, 2016.

  1. Good evening all,

    I am working on a plugin with multiple "powers". Each powers give the player some items and they can do stuff with that. Each power has his own class, with his own event listeners. For my "Bear" power class, I got a problem:


    Code (Text):
    package com.cyber.anniversary;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Wolf;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;

    /**
    *
    * @author robin
    */
    public class PowerBear extends Power implements Listener{
        Player user;
        ItemStack hugWand = new ItemStack(Material.DEAD_BUSH, 1);
        ItemStack wolfArmy = new ItemStack(Material.BONE, 1);
        Wolf wolf = null;
        List<Wolf> wolfs = new ArrayList();
       
        @Override
        public void enable(Player p, Plugin plugin){
            this.user = p;
            user.getServer().getPluginManager().registerEvents(this, plugin);
            user.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 999999, 3, true, false));
           
            ItemMeta hugit = hugWand.getItemMeta();
            hugit.setDisplayName(ChatColor.DARK_RED+"Bear Hug");
            hugWand.setItemMeta(hugit);
            user.getInventory().addItem(hugWand);
           
           
            ItemMeta wolfit = wolfArmy.getItemMeta();
            wolfit.setDisplayName(ChatColor.BOLD+"Tamed Wolf Spawner");
            wolfArmy.setItemMeta(wolfit);
            user.getInventory().addItem(wolfArmy);
        }
       
        @Override
        public void disable(){
            user.getInventory().removeItem(hugWand);
            user.getInventory().removeItem(wolfArmy);
            user.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
           
            for(Wolf w : wolfs){
                w.remove();
            }
            wolfs.clear();
            user = null;
        }
       
        @EventHandler
        public void clickEvent(PlayerInteractEvent e){
            if(e.getPlayer().equals(user) && (e.getAction().name().equals("LEFT_CLICK_AIR") || e.getAction().name().equals("LEFT_CLICK_BLOCK")) && e.getPlayer().getInventory().getItemInMainHand().isSimilar(hugWand)){
                String output = "You trapped: ";
                for(Entity entity : user.getNearbyEntities(5, 5, 5)){
                    if(entity instanceof Player){
                        Player target = (Player)entity;
                        if(!target.getDisplayName().equals(user.getDisplayName())){
                            output = output+target.getDisplayName()+", ";
                            target.sendMessage(ChatColor.RED+"You got to close to Thom! Prepare for the HUG!");

                            List<PotionEffect> effects = new ArrayList<>();
                            effects.add(new PotionEffect(PotionEffectType.SLOW, 140, 9000, false, false));
                            effects.add(new PotionEffect(PotionEffectType.BLINDNESS, 140, 9000, false, false));
                            effects.add(new PotionEffect(PotionEffectType.WEAKNESS, 140, 9000, false, false));
                            effects.add(new PotionEffect(PotionEffectType.UNLUCK, 140, 9000, false, false));
                            effects.add(new PotionEffect(PotionEffectType.SLOW_DIGGING, 140, 9000, false, false));
                            effects.add(new PotionEffect(PotionEffectType.JUMP, 140, -10, false, false));
                            effects.add(new PotionEffect(PotionEffectType.WITHER, 140, 1, false, false));
                            user.sendMessage(effects.size()+"");

                            target.addPotionEffects(effects);
                        }
                    }
                }
                user.sendMessage(ChatColor.GRAY+output);
            }
           
            else if(e.getPlayer().equals(user) && (e.getAction().name().equals("LEFT_CLICK_AIR") || e.getAction().name().equals("LEFT_CLICK_BLOCK")) && e.getPlayer().getInventory().getItemInMainHand().isSimilar(wolfArmy)){
                Block targetBlock = user.getTargetBlock((Set<Material>) null, 100);
                Location spawnLoc = targetBlock.getLocation();
                spawnLoc.setY(targetBlock.getY()+1);
                Wolf wolf = (Wolf)user.getWorld().spawnEntity(spawnLoc, EntityType.WOLF);
                wolf.setOwner(user);
                wolf.setMaxHealth(25);
                wolf.setHealth(25);
                wolf.setCustomName(ChatColor.DARK_GRAY+user.getDisplayName()+"'s loyal dog");
                wolf.setAngry(true);
               
                wolfs.add(wolf);
            }
        }
       
        @EventHandler
        public void deadEvent(EntityDeathEvent e){
            if(e.getEntity().equals(user)){
                super.defeat((Player)e.getEntity());
            }
        }
       
        @EventHandler
        public void throwPlayersAway(EntityDamageEvent e){
            if(e.getCause() == EntityDamageEvent.DamageCause.WITHER){
                if(e.getEntity() instanceof Player){
                    Player target = (Player)e.getEntity();
                    for(PotionEffect pe : target.getActivePotionEffects()){
                       
                        if(pe.getType().equals(PotionEffectType.BLINDNESS) && pe.getDuration()<40){
                            target.removePotionEffect(PotionEffectType.UNLUCK);
                            target.removePotionEffect(PotionEffectType.SLOW);
                            target.removePotionEffect(PotionEffectType.WEAKNESS);
                            target.removePotionEffect(PotionEffectType.SLOW_DIGGING);
                            target.removePotionEffect(PotionEffectType.JUMP);
                            target.removePotionEffect(PotionEffectType.WITHER);
                            target.removePotionEffect(PotionEffectType.BLINDNESS);

                            blowAway(target, user);
                        }
                    }
                }
            }
        }
       
        private void blowAway(Player target, Player boss){
            target.getLocation().setY(target.getLocation().getY()+3);
            Vector direction = target.getLocation().toVector().subtract(boss.getLocation().toVector()).normalize().multiply(2);
            target.setVelocity(direction);
        }
       
    }
    Before you are going full rage mode, I know this code is not the cleanest or most efficient. I am n a rush to fix this plugin, so as long as it works, Im fine with it :p
    Anyhow, the above code works correctly, but only for once. The targeted player gets the effects, get levitated and blown away, but after using the "HugWand" again, the player only takes 1 tick wither damage, but the effects dissapear immediatly. This only occurs after the second call. I assume there is a variable which has not been reset somewhere, but I was unable to find it.

    The wolf spawner works correctly btw.

    Thanks in regards :)
     
  2. Is a new instance of this class created for every player?
     
  3. Anyone who might know the problem Im having here?
     
  4. K, I got this problem again with another power.
    It seems that when you give a player multiple potion effects, it goes wrong.
    At least, the first time it goes alright, but on a second hit, it fails...

    Code (Text):

                            target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 140, 255, true, false));
                            target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 140, 255, true, false));
                            target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 140, 255, true, false));
                            target.addPotionEffect(new PotionEffect(PotionEffectType.HUNGER, 140, 5, true, false));
                            target.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 140, -10, true, false));