Solved Give player potion effect after respawn

Discussion in 'Spigot Plugin Development' started by jacob123, Nov 14, 2020.

  1. Code (Java):
    public class PlayerDied implements Listener {

        @EventHandler
        public void onRespawn(PlayerRespawnEvent e){
            Player player = e.getPlayer();

            if (player.hasPermission("perks.speed")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 0, true, false));
                player.sendMessage(ChatColor.GRAY + "[OPK Perks]" + ChatColor.YELLOW + " Added 'Speed' effect!");
            }

            if (player.hasPermission("perks.resistance")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 0, true, false));
                player.sendMessage(ChatColor.GRAY + "[OPK Perks]" + ChatColor.YELLOW + " Added 'Fire Resistance' effect!");
            }

            }
        }
    }

    When the player respawns, he doesn't get the potion effects.
    When I use the 'PlayerJoinEvent', everything is working fine.
    What am I doing wrong?

    Edit: I have register the listener.
     
  2. this is because it takes a bit before the player is loaded up completely. if you add a delay of 1 tick it should work just fine
     
  3. How can I do that?
     
  4. Code (Java):
    public class PlayerDied implements Listener {

        public void AtRespawn(PlayerRespawnEvent event) {
            BukkitScheduler scheduler = getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask((Plugin) this, new Runnable(){
                @Override
                public void run() {
                    Player player = event.getPlayer();
                    if (player.hasPermission("perks.speed")) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 0, true, false));
                        player.sendMessage(ChatColor.GRAY + "[OPK Perks]" + ChatColor.YELLOW + " Added 'Speed' effect!");
                    }

                    if (player.hasPermission("perks.resistance")){
                        player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 0, true, false));
                        player.sendMessage(ChatColor.GRAY + "[OPK Perks]" + ChatColor.YELLOW + " Added 'Fire Resistance' effect!");
                    }
                }
            }, 0L, 20L);
        }
    }
     
  5. well first of all you are using a repeating task right now. i don't know if you want that but this is gonna repeat every second
    if you just want this to work like you stated in the original thread. here is an example of how to go about it

    Code (Java):
    @EventHandler
        public void onRespawn(PlayerRespawnEvent e)
        {
            Bukkit.getScheduler().runTaskLater(MainClass.inst(), task -> {
                Player player = e.getPlayer();
                if (player.hasPermission("perks.speed"))
                {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 0, true, false));
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[OPK Perks] &eAdded 'Speed' Effect!"));
                }
            }, 5L);
        }
     

    what you are doing wrong i think. is the Plugin instance you are passing that very class in as Plugin, but it does not extend JavaPlugin
    BukkitScheduler#runTaskLater requires your main class extending JavaPlugin

    hope this helps!
     
  6. You may not even need to delay it at all. Just let it run on the scheduler's next tick. The respawn hasn't actually gone through at event time as all events are called pre-occurrence for obvious reasons.
     
  7. to make delayed bukkit tasks the easist (and quickest) way is like this:

    Code (Java):
    new BukkitRunnable() {
      public void run() {
        // code
      }
    }.runTaskLater(plugin, <delay in ticks>);
     
  8. I need to do it in my main class?
     
  9. Thx for the help! Everything is working fine now.

    Main class:
    Code (Java):
    public class Main

        private static Main instance;

        public Main() {
            instance = this;
        }

        public static Main getInstance(){
            return instance;
        }

    }
     
    Perks class:

    Code (Java):
    public class PlayerRespawn implements Listener {

        @EventHandler
        @Deprecated
        public void onRespawn(PlayerRespawnEvent e)
        {
            Bukkit.getScheduler().runTaskLater(Main.getInstance(), task -> {
                Player player = e.getPlayer();

                if (player.hasPermission("perks.speed")) {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 0, true, false));
                    player.sendMessage(ChatColor.GRAY + "[OPK Perks]" + ChatColor.YELLOW + " Added 'Speed' effect!");
                }
            }, 5L);
        }
    }