Add Potion Effect

Discussion in 'Plugin Development' started by XDemonic25, Feb 25, 2012.

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

    XDemonic25

    I saw somewhere that bukkit added a Potion API. I wanted so that on player respawn they get a FIRE_RESISTENCE potion effect on them for like.. lets say 10 seconds.

    I tried this code here:
    Code:
    evt.getPlayer().addPotionEffect(PotionType.FIRE_RESISTANCE);
    
    But I get a error saying i cant use PotionType is not a valid Argument. Also Cant understand on how to make a time limit on this.

    Any help would be awesome. If theres no way of doing this, Ill just have to set a Timer for no damage x.x.
     
  2. Offline

    Njol

    Use something like this:
    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 200, 1));
    where 200 is the duration (20 ticks/sec * 10 seconds) and 1 is the amplifier (I think this has no effect on fire resistance)
     
    XDemonic25 likes this.
  3. Offline

    Muddr

    Njol beat me to it, while I was testing. ha ha

    After some testing it won't work on respawn since it fires before you're done spawning. This is one way I found to do it. setup a delayed task.

    Code:
    @EventHandler
    public void onSpawn(PlayerRespawnEvent event) {
        final Player player = event.getPlayer();
        plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
            public void run() {             
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 200, 1));
            }
        }, 20); // ******** The 20. is the delay for the task in TICKS ********
    }
     
    maxmar628 likes this.
  4. Offline

    XDemonic25

    Thank you thank you very much... I normally try to find my own ways around bugs when it comes to coding, but some help is awesome. One step closer into my awesome plugin

    hmmm nvm.. it looked like it would work... But i got my plugin to set up to respawn where u died. so if u died in lava.. it would suck right?

    but it doesnt seem to put the potion effect on the player, and it gives out no error.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  5. Offline

    Muddr

    I tested mine some more and this does work spawning in lava..

    Code:
    @EventHandler
    public void onSpawn(PlayerRespawnEvent event) {
        final Player player = event.getPlayer();
        plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
            public void run() {                
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 200, 1));
            }
        }, 1);
    }
    [CODE]
     
    XDemonic25 likes this.
  6. Offline

    XDemonic25

    yea I just now tested it too, it works. Thank you thank you, 2nd time helping me lol xD
     
  7. Offline

    NullCity

    Now the player isn't acceptable for arguments error finally went away. Huzzah!
     
  8. NEVER DO THAT, dont use other threads to access the player obj, use the main server thread, if other users are having problems, this is the cause.
    EDIT: if I sound to flaming, then its because I really hate the thread safety bugs
     
  9. Offline

    Muddr

    Then what would be the correct way of doing this??
     
  10. using a sync task instead of the async one.
     
  11. Offline

    flatd

    Why can't we just create new side threads for our plugins, it would make stuff more safe then everything running on one or two threads.
     
  12. Offline

    Muddr

  13. Offline

    MinecraftMart

    So what is the correct code?
     
  14. Offline

    WaywardGeek

    I just ran across this problem, and while it's more complex, here's my code:
    Code:
    class SyncPlayerTask extends BukkitRunnable {
        Player p;
       
        SyncPlayerTask(Player player) {
            p = player;
        }
           
        public void run() {           
            p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000000, 5));
        }
    }
     
    @EventHandler
    public void onPlayerSpawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
        getServer().getScheduler().scheduleSyncDelayedTask(this, new SyncPlayerTask(player), 1);
    }
    
    On the positive side, you don't need to delay by a second, and you don't need to worry that the server got loaded down and didn't complete what it needed to in 1 second. I delay by one tick just to be sure player spawn is complete, but I suspect 0 might work, especially if you do this at low priority.
     
Thread Status:
Not open for further replies.

Share This Page