Apply potion effect after respawn

Discussion in 'Plugin Development' started by ollethunberg, Aug 4, 2019.

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

    ollethunberg

    Hello. I want to apply a potion effect after respawn. My code does work in 1.13.2 but not in 1.14. I don't know why.
    The code that I got:
    Code:
        @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent event) {
            Player p = event.getPlayer();
           
            String worldname = p.getWorld().getName();
           
            int checkWorld = getConfig().getInt("World."+worldname);
            if(checkWorld == 1) {
                p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
                p.getInventory().addItem(new ItemStack(Material.BOW, 1));
                p.getInventory().addItem(new ItemStack(Material.GOLDEN_APPLE, 5));
                p.getInventory().addItem(new ItemStack(Material.COOKED_BEEF, 16));
                p.getInventory().addItem(new ItemStack(Material.EXPERIENCE_BOTTLE, 16));
                p.getInventory().addItem(new ItemStack(Material.ARROW, 64));
    
                p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
                p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
                p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS));
                p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
               
                Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(this, new Runnable() {
                    public void run() {
                    p.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 200, 2));
                    }
                }, 20);
            }
    
        }

    Error in console:

    Code:
    [19:27:14] [Craft Scheduler Thread - 3/WARN]: [ItemsOnRespawn] Plugin ItemsOnRespawn v1.0 generated an exception while executing task 9
    java.lang.IllegalStateException: EntityPotionEffectEvent cannot be triggered asynchronously from another thread.
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:510) ~[server.jar:git-Bukkit-06efc9e]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callEntityPotionEffectChangeEvent(CraftEventFactory.java:1400) ~[server.jar:git-Bukkit-06efc9e]
            at org.bukkit.craftbukkit.v1_14_R1.event.CraftEventFactory.callEntityPotionEffectChangeEvent(CraftEventFactory.java:1388) ~[server.jar:git-Bukkit-06efc9e]
            at net.minecraft.server.v1_14_R1.EntityLiving.addEffect(EntityLiving.java:841) ~[server.jar:git-Bukkit-06efc9e]
            at org.bukkit.craftbukkit.v1_14_R1.entity.CraftLivingEntity.addPotionEffect(CraftLivingEntity.java:317) ~[server.jar:git-Bukkit-06efc9e]
            at org.bukkit.craftbukkit.v1_14_R1.entity.CraftLivingEntity.addPotionEffect(CraftLivingEntity.java:306) ~[server.jar:git-Bukkit-06efc9e]
            at me.bukkit.itemsonrespawn.main$1.run(main.java:76) ~[?:?]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftTask.run(CraftTask.java:77) ~[server.jar:git-Bukkit-06efc9e]
            at org.bukkit.craftbukkit.v1_14_R1.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:54) [server.jar:git-Bukkit-06efc9e]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_191]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_191]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
     
  2. Offline

    KarimAKL

    @ollethunberg Don't use 'runTaskLaterAsynchronously', just use 'runTaskLater'.
     
Thread Status:
Not open for further replies.

Share This Page