Solved Force Respawn

Discussion in 'Plugin Development' started by caderape, May 12, 2015.

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

    caderape

    Hello, i'm develloping a MiniGame plugin. But i have a problem.
    I would like to find a way to force the respawn of a player.

    When a player die in the game, and when it causes the end of the game, the plugin teleport the players in the lobby then delete the world created for the game.

    But it doesnt teleport dead people. So when the player respawn, it causes a worldconflictexception.

    I tried different way to wait the respawn for delete the world, but it doent work. getWorld().getPlayers() don't contains dead players cuz it returns 0 when i try..
     
  2. Offline

    CyranoTrinity

    @caderape
    Maybe you could add the players to an ArrayList or HashMap and teleport the players in the list to lobby
     
  3. Offline

    caderape

    @CyranoTrinity
    I tried that.

    Code:
    Iterator<UUID> it = this.players.iterator();
            while(it.hasNext()) {
                Player p = Bukkit.getPlayer(it.next());
                if (p != null && !p.isDead()) {
                    it.remove();
                } else {
                    Bukkit.broadcastMessage("dead");
                }
            }
    But the broadbast 'dead' does not perform. It's like p.isdead() is not recognize. I tried to schedul it 2 second later, but it still do not know if the player is dead or no
     
  4. Offline

    Abs0rbed

    @caderape Death events aren't cancellable, but you can listen to the damage event and figure out if the hit will kill them. At that point if you use lives, subtract a life, set their health to full, cancel the event and if they're out then move them to the lobby.

    As for worlds, you can turn off autosaving so that anything the players do to the world isn't saved and then when everyone's out, you can just unload the world and everything should revert back
     
  5. @Abs0rbed They are cancellable, but it loops and glitchs

    @caderape onPlayerDamage, check if the damage is greater than the player's hp. If it's, cancel the event, heal the player and do death stuff.
     
  6. Offline

    Abs0rbed

    @Juancomaster1998 Pretty sure you can't cancel them, at least not from what I've seen from the Javadocs, just went and double checked.
    As for the playerdamage, that was pretty much what I was thinking he should do
     
  7. @Abs0rbed
    Well, I remember cancelling it once just to see what happens (or restoring the player's hp, I don't remember which)
     
  8. Offline

    API_Tutorials

    @Juancomaster1998
    Code:
    @EventHandler
        public void onPlayerDeath(final PlayerDeathEvent e)
        {
            new BukkitRunnable()
            {
                public void run()
                {
                    try
                    {
                        Object nmsPlayer = e.getEntity().getClass().getMethod("getHandle").invoke(e.getEntity());
                        Object con = nmsPlayer.getClass().getDeclaredField("playerConnection").get(nmsPlayer);
                       
                        Class< ? > EntityPlayer = Class.forName(nmsPlayer.getClass().getPackage().getName() + ".EntityPlayer");
                       
                        Field minecraftServer = con.getClass().getDeclaredField("minecraftServer");
                        minecraftServer.setAccessible(true);
                        Object mcserver = minecraftServer.get(con);
                       
                        Object playerlist = mcserver.getClass().getDeclaredMethod("getPlayerList").invoke(mcserver);
                        Method moveToWorld = playerlist.getClass().getMethod("moveToWorld" , EntityPlayer , int.class , boolean.class);
                        moveToWorld.invoke(playerlist , nmsPlayer , 0 , false);
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }.runTaskLater(plugin , 2);
        }
     
  9. Offline

    caderape

    Okay guys thx. I will try to do something with this.
     
Thread Status:
Not open for further replies.

Share This Page