Solved Running code after event is finished

Discussion in 'Spigot Plugin Development' started by Eilux, Jun 13, 2021.

  1. I have the following event:
    Code (Java):
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            UUID id = event.getEntity().getUniqueId();
            playerLives.replace(id, playerLives.get(id) - 1);
            if (deathMode.equals(DeathMode.BAN) && playerLives.get(id) <= 0){
                Bukkit.getBanList(BanList.Type.NAME).addBan(event.getEntity().getName(),"§fGame Over.",null,null);
                event.getEntity().kickPlayer("§fGame Over.");
            }
            if (deathMode.equals(DeathMode.SPECTATE) && playerLives.get(id) <= 0){
                event.getEntity().setGameMode(GameMode.SPECTATOR);
            }
        }
    The part that causes problems is the part where the player is kicked, it functions but it throws a bunch of exceptions in the server logs because it kicks the player while the event is happening to them. Is there a way to run that part after the event is completed in order to avoid causing errors?

    This is the error that occurred:
    Code (Text):
    [19:16:51] [Server thread/ERROR]: Could not pass event PlayerDeathEvent to HardcoreMultiLife v1.0-ALPHA
    java.lang.IllegalStateException: Removing entity while ticking!
        at net.minecraft.server.v1_16_R3.WorldServer.removeEntity(WorldServer.java:1592) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.WorldServer.removePlayer(WorldServer.java:1612) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.PlayerList.disconnect(PlayerList.java:652) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1856) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.PlayerConnection.disconnect(PlayerConnection.java:469) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.PlayerConnection.disconnect(PlayerConnection.java:432) ~[patched_1.16.5.jar:git-Paper-775]
        at org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer.kickPlayer(CraftPlayer.java:502) ~[patched_1.16.5.jar:git-Paper-775]
        at dev.bodner.jack.hardcore.HardcoreMultiLife.onPlayerDeath(HardcoreMultiLife.java:204) ~[?:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor3.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.16.5.jar:git-Paper-775]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.16.5.jar:git-Paper-775]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.16.5.jar:git-Paper-775]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:624) ~[patched_1.16.5.jar:git-Paper-775]
        at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:876) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityPlayer.die(EntityPlayer.java:818) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityLiving.damageEntity(EntityLiving.java:1362) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityHuman.damageEntity(EntityHuman.java:870) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityPlayer.damageEntity(EntityPlayer.java:982) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityThrownTrident.a(EntityThrownTrident.java:129) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.IProjectile.a(IProjectile.java:150) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.IProjectile.preOnHit(IProjectile.java:141) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityArrow.tick(EntityArrow.java:214) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.EntityThrownTrident.tick(EntityThrownTrident.java:92) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.WorldServer.entityJoinedWorld(WorldServer.java:960) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.World.a(World.java:955) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.WorldServer.doTick(WorldServer.java:646) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.MinecraftServer.b(MinecraftServer.java:1490) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:436) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1342) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1130) ~[patched_1.16.5.jar:git-Paper-775]
        at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:291) ~[patched_1.16.5.jar:git-Paper-775]
        at java.lang.Thread.run(Thread.java:831) [?:?]
     
    #1 Eilux, Jun 13, 2021
    Last edited: Jun 13, 2021
  2. console log please
     
  3. wow seems like we both have problems with playerdeathevent, welcome to the club buddy
     
  4. Just use a bukkitrunnable to execute this code like 5 ticks later. Alternatively you could force respawn the player with player.spigot().respawn().
     
  5. added
     
  6. Do you know of any good sources on using bukkitrunnable? never mind found this: https://bukkit.fandom.com/wiki/Scheduler_Programming, Also it does work
     
    #6 Eilux, Jun 13, 2021
    Last edited: Jun 13, 2021
    • Agree Agree x 1