How Can I "Force" Respawn Players?

Discussion in 'Plugin Development' started by Sparta, Jan 3, 2013.

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

    Sparta

    Hey guys.

    I am in need of knowing how to force respawn players. I've searched for about an hour but all of the methods of doing it is just buggy or would just not be efficient.

    I want to "force" respawn players, as in skipping the death screen.

    I don't want to use a task scheduler because it would be laggy and unnecessary.

    My plugin already has a Listener set up to give a player items when they respawn (PlayerRespawnEvent).

    All the methods are just saying to make a listener for EntityHurtEvent and make the player teleport to the spawn area and you reset their inventory, etc, which in my mind is just too in-efficient and can cause problems with other plugins.

    Is there any way I CAN do this that would be efficient and not take up much lag?
     
  2. Offline

    LaxWasHere

    Check if the damage they are gonna get is more than their HP and then TP them?
     
  3. Offline

    Sparta

    Dude. I already explained above that I don't want to do that. That will conflict with other plugins and it's in-efficient.
     
  4. Offline

    LaxWasHere

    My bad.
     
  5. Offline

    suckycomedian

    Sparta this is the best way for what you're wanting to do. You can't make them click the respawn button so once they're actually dead there's nothing you can do.
     
  6. Offline

    fireblast709

    Packet #205, though not a suggested method
    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e)
    3. {
    4. String tv = Bukkit.getVersion();
    5. int index = tv.indexOf("(MC: ")+5;
    6. int end = tv.indexOf(")", index);
    7. String version = tv.substring(index, end);
    8. try
    9. {
    10. Class packet = Class.forName("net.minecraft.server.v"+version.replace(".", "_") +".Packet205ClientCommand");
    11. Object name = packet.getConstructor(new Class[0]).newInstance(new Object[0]);
    12. Field a = packet.getDeclaredField("a");
    13. a.setAccessible(true);
    14. a.set(name, 1);
    15. Object nmsPlayer = Class.forName("org.bukkit.craftbukkit.v"+version.replace(".", "_")+".entity.CraftPlayer").getMethod("getHandle", new Class[0])
    16. .invoke(e.getEntity(), new Object[0]);
    17. Field con = Class.forName("net.minecraft.server.v"+version.replace(".", "_") +".EntityPlayer").getDeclaredField("playerConnection");
    18. con.setAccessible(true);
    19. Object handle = con.get(nmsPlayer);
    20. packet.getDeclaredMethod("handle", Class.forName("net.minecraft.server.v"+version.replace(".", "_") +".Connection"))
    21. .invoke(name, handle);
    22. }
    23. catch(Exception ex)
    24. {
    25. ex.printStackTrace();
    26. }
    27. }
    A supposedly version independant version using reflection I wrote a while ago
     
    suckycomedian likes this.
  7. Here's a thread with more in-depth discussion about this topic: http://forums.bukkit.org/threads/89891/
    There's similar methods suggested that you understandingly don't want, but later there is the solution that fireblast709 posted explained. This post and the following ones contain solutions that work with the current version, if you don't want to go the overkill way of dynamic versioned classes ;)

    Also, I would be careful with calling the packet-receiving-method-thing directly from the event. That will cause the player to respawn before the death event is fully processed, which could mess up both internal states of the server and other plugins that trust the respawn event to fire after the death event.
     
  8. Offline

    fireblast709

    1 tick delay will fix that one
     
  9. Offline

    amitlin14

    Sparta why not just check if the player died, if he did, cancel the event, set his health and food to max, teleport him to spawn, get the location he died, spawn his inventory, then clear his inventory, then spawn his armor, then clear his armor, then spawn his xp, then clear his xp, and finally, clear his potion effect.

    that should do it no? fail proof i believe.
     
  10. Offline

    fireblast709

    If you mean cancel the PlayerDeathEvent: not possible
    If you mean cancel the EntityDamageEvent: fail proof but will screw up a lot of plugins
     
  11. Offline

    Sparta

    Exactly... I need a way to do this "safely"....
     
  12. My reply from the other thread:
     
  13. Offline

    Sparta

    That is the same thing the guy said above, both of those ways are inefficient.
     
  14. Offline

    Tirelessly

    Use the packet, done.
     
  15. Offline

    Sparta

    And how do I do that? Can you please explain? Thanks.
     
  16. That's why I was quoting him. My quote out of context maybe didn't bring across my original intent: The suggested "manual" way is stupidly complicated to do reliably and without breaking anything, that's why I was suggesting faking the respawn packet to begin with ;)
     
  17. Offline

    Tirelessly

     
  18. Offline

    gyroninja

    What I have always done was setting the player's health to 20 and teleporting.Setting their health above 0 removes the respawn menu.
     
Thread Status:
Not open for further replies.

Share This Page