Solved PlayerRespawnEvent - Problem with setting PotionEffects

Discussion in 'Plugin Development' started by lexmint, Jun 15, 2014.

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

    lexmint

    Hello!
    I've a problem with PlayerRespawnEvent in my first plugin associated with kits and PvP. When I'm trying to give player an item or armor - it appears in player's inventory, but when I'm trying to give player some PotionEffects - it doesn't. With PlayerJoinEvent it works.
    Here's the code. I'd be thankful to you for solving this trouble.

    Code:java
    1. public class HSKitsPvP extends JavaPlugin {
    2. Logger pluginLogger;
    3. public final PlayerJoinListener pj = new PlayerJoinListener();
    4. public final PlayerRespawnListener pr = new PlayerRespawnListener();
    5. @Override
    6. public void onEnable() {
    7. PluginManager pm = getServer().getPluginManager();
    8. pm.registerEvents(this.pj, this);
    9. pm.registerEvents(this.pr, this);
    10. }
    11. @Override
    12. public void onDisable() {
    13. }
    14.  
    15. }
    16.  
    17. public class PlayerRespawnListener implements Listener {
    18. public static HSKitsPvP plugin;
    19. @EventHandler
    20. public void onPlayerRespawn(PlayerRespawnEvent event) {
    21. Player player = event.getPlayer();
    22.  
    23. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 99999,
    24. 1));
    25. player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST,
    26. 99999, 20));
    27. }
    28. }
    29.  
    30. public class PlayerJoinListener implements Listener {
    31. public static HSKitsPvP plugin;
    32.  
    33. @EventHandler
    34. public void onPlayerJoin(PlayerJoinEvent event) {
    35. Player player = event.getPlayer();
    36.  
    37. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 99999,
    38. 1));
    39. player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST,
    40. 99999, 20));
    41. }
    42. }


    Also, there's one more question. How to change player's spawn when player joins the server? (I want to set up a couple of random spawn locations)
    P.S. Sorry for bad English, trying to improve.
     
  2. Offline

    Deleted user

    1) You have to use a delayed task (1 tick).
    2) You can use Player.teleport().
     
    lexmint likes this.
  3. Offline

    lexmint

    Thank you!
    Using of delayed task like this? I got error in console.
    Code:java
    1. public void onPlayerRespawn(PlayerRespawnEvent event) {
    2. final Player player = event.getPlayer();
    3. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    4. public void run() {
    5. player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 99999,
    6. 1));
    7. player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST,
    8. 99999, 20));
    9. }}, 1L);
    10. }
     
  4. Offline

    RainoBoy97

  5. Yes, but:
    1. Use runTaskLater, scheduleSyncDelayedTask is less efficient (done tests).
    2. Check if player != null.

    For your error, make sure plugin is not null.
     
    lexmint likes this.
  6. Offline

    RawCode

    player respawn event thrown before player is actually respawned, this same for nearly all event in bukkit.
     
    lexmint likes this.
  7. Offline

    lexmint

    Thank you for explanations, problem is solved! It was connected with null in the field "plugin" in PlayerRespawnListener - I was so inattentive :)
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page