Solved Scheduler on event problem.

Discussion in 'Spigot Plugin Development' started by Nosmakos, May 13, 2016.

  1. hey guys im trying to make a scheduler delayed task for when zombie hits the player after 200L = 10 seconds to do the effects than doing them when player get hits.
    any idea why it's not working?

    Code (Text):
        @EventHandler
        public void onZombieHitPlayer(final EntityDamageByEntityEvent e) {
            final int wither = plugin.getConfig().getInt("ZombieHitEffects.InfectionEffect");
     
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                @Override
                public void run(){
                    if (((e.getDamager() instanceof Zombie)) && ((e.getEntity() instanceof Player))) {
                        ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.WITHER, wither, 1));
                    }
                }
            },200L);
        }
    }
    btw it's a class not the main and i have registered the class because effects are working but not the scheduler.
     
    #1 Nosmakos, May 13, 2016
    Last edited: May 13, 2016
  2. Are you sure it's the scheduler and not just the potion effect that isn't working?
    What value does "wither" have in that code, because the duration of a potion effect is in ticks not seconds.
    To convert it to ticks just do wither*20 since there are 20 ticks in a second on a MC server.
     
  3. well i tested it potion effects are working just the scheduler is not thats really weird. When zombie hits it does the effects it's not waiting the 200L i set to it. I tested to add message too but message didnt send it to the player. not even Bukkit.broadcastMessage("1"); not working before the scheduler.
     
  4. Strange, I've never seen a task like that activate right away, I'm kinda tired though so maybe I'm missing something.
     
  5. well im not sure what's happening i read about scheduler and saw tutorials if i have anything wrong but it seems that it should be working tho.
     
  6. Code (Text):
        @EventHandler
        public void onZombieHitPlayer(EntityDamageByEntityEvent e) {
            if ((e.getDamager() instanceof Zombie) && (e.getEntity() instanceof Player)) {

                Player player = (Player) e.getEntity();
                int wither = 20; //change this obviously
                new BukkitRunnable() {

                    public void run() {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, wither, 1));
                        player.sendMessage("Poison is taking effect!");
                        cancel();
                    }

                }.runTaskLater(this, 200L);
            }
        }
    Just muck around code, im sure you can make it better
     
    • Agree Agree x 1
    • Useful Useful x 1
  7. Do you have the event being loaded in the main class?
     
  8. You think this can be solve t?
     
  9. The event is a class with zombies but i have registered and everything
     
  10. Try it :p
    The delay did work when i tried it.
     
  11. okay cool i hope but the wither is in the config too and u changed that do i really have to make it like that or i can have it in the config to?
     
  12. i fixed it! now i added a message and it says that he has to find a cure before he die by infection but if zombie hit him again it sends again the message... how i can do if player get this message to not get it again? or if this event happend once to not repeat because if zombie hit the player more than 1 time it send more messages.
     
    #12 Nosmakos, May 13, 2016
    Last edited: May 13, 2016
  13. well potion effects are happening 30 seconds after..., what do i check on the potioneffecttype?
     
  14. Oh, just save the player's UUID to a list and check if the list contains that player's UUID when they are hit.
    Then when the player is cured (or dies) just remove their UUID from the list.
     
  15. do i have to make a new yml file to save it or just a uuid?
     
  16. You could save it to a new .yml file if you'd like, it will certainly make the main config less cluttered.
    If you save it to a config you should convert it to a string using toString() and use fromString(uuid) after to convert it back to a UUID.
    I would save it in an ArrayList<UUID> personally and only save it to the config when the server stops. Then just load it when the server starts.
     
    • Agree Agree x 1
  17. cool thanks will do!
     
  18. Please let it save after 30 secs
     
  19. well i get an error you see anything wrong?

    Code (Text):
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            Player player = e.getEntity();
            effects.remove(player.getPlayer().getUniqueId());
        }

        @EventHandler
        public void onZombieHitPlayer(final EntityDamageByEntityEvent e) {
            if (((e.getDamager() instanceof Zombie)) && ((e.getEntity() instanceof Player))) {
                Player player = (Player) e.getEntity();
                if (!this.effects.contains(player.getPlayer().getUniqueId())){
                    effects.add(player.getPlayer().getUniqueId());
               
                    final int wither = plugin.getConfig().getInt("ZombieHitEffects.InfectionEffect");
                    final int nausea = plugin.getConfig().getInt("ZombieHitEffects.NauseaEffect");
                    final int blindness = plugin.getConfig().getInt("ZombieHitEffects.BlindnessEffect");
                    final int slowness = plugin.getConfig().getInt("ZombieHitEffects.SlownessEffect");
                    final int weakness = plugin.getConfig().getInt("ZombieHitEffects.WeaknessEffect");
                    e.getEntity().sendMessage("You just got bited! You have to find a cure before you die by infection.");
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
                        @Override
                        public void run(){
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.WITHER, wither, 1));
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, slowness, 1));
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, nausea, 5));
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, blindness, 1));
                                ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, weakness, 1));
                        }
                    }, 600L);
                }
            }
        }
    on enable
    Code (Text):
            effects = new ArrayList<>();
    before onenable
    Code (Text):
    private ArrayList<UUID> effects;
    error is this
    Code (Text):
    final int wither = plugin.getConfig().getInt("ZombieHitEffects.InfectionEffect");