Teleport player on death

Discussion in 'Plugin Development' started by TheAJ471, Mar 23, 2014.

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

    TheAJ471

    So I am working on my KitPvP plugin and I want it so when a player gets killed they will respawn where I set the respawn location. Not at the server spawn.

    This is what i currently have

    Main set respawn point.

    Code:java
    1. if (cmd.getName().equalsIgnoreCase("setdeath")) {
    2. if (p.hasPermission("cpvp.admin")) {
    3. settings.getData().set("death.world", p.getLocation().getWorld().getName());
    4. settings.getData().set("death.x", p.getLocation().getX());
    5. settings.getData().set("death.y", p.getLocation().getY());
    6. settings.getData().set("death.z", p.getLocation().getZ());
    7. settings.saveData();
    8. p.sendMessage(ChatColor.AQUA + "[PVP] " + ChatColor.GREEN + "Set Death Point!");
    9. return true;
    10. }
    11. }


    Respawn event that doesnt work.

    Code:java
    1. @EventHandler
    2. public void onPlayerRespawn(PlayerRespawnEvent e){
    3. Player p = e.getPlayer();
    4. if(plugin.pvp.contains(p)){
    5. World w = Bukkit.getServer().getWorld(plugin.settings.getData().getString("death.world"));
    6. double x = plugin.settings.getData().getDouble("death.x");
    7. double y = plugin.settings.getData().getDouble("death.y");
    8. double z = plugin.settings.getData().getDouble("death.z");
    9. //p.teleport(new Location(w, x, y, z));
    10. Location loc = new Location (w, x, y, z);
    11. e.setRespawnLocation(loc);
    12. }
    13. }
     
  2. Offline

    Mattkx4

    I don't see why the p.teleport() method shouldn't work.

    Try making a new location with the coordinates that you provides + the world and the use the code:

    Code:java
    1. p.teleport(location);


    If that doesn't work, tell me and I'll try to help you out!
     
  3. Offline

    TheAJ471

    Mattkx4 So i used this and it didnt work. I just respawned back at the server spawn not the location i set with /setdeath

    Code:java
    1. @EventHandler(priority=EventPriority.HIGH)
    2. public void onPlayerRespawn(PlayerRespawnEvent e){
    3. Player p = e.getPlayer();
    4. if(plugin.pvp.contains(p)){
    5. World w = Bukkit.getServer().getWorld(plugin.settings.getData().getString("death.world"));
    6. double x = plugin.settings.getData().getDouble("death.x");
    7. double y = plugin.settings.getData().getDouble("death.y");
    8. double z = plugin.settings.getData().getDouble("death.z");
    9. Location loc = new Location (w, x, y, z);
    10. p.teleport(loc);
    11. //e.setRespawnLocation(loc);
    12. }
    13. }
     
  4. Offline

    Mattkx4

  5. Offline

    chasertw123

    I am also in the process of making a kitpvp plugin. I have setup re-spawns for my plugin! Here is an example of my class:
    Code:java
    1. package me.chasertw123.kitpvp.misc;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.Listener;
    7.  
    8. import me.chasertw123.kitpvp.KitPvP;
    9.  
    10. public class Respawn implements Listener {
    11.  
    12. private static KitPvP plugin;
    13.  
    14. public Respawn(KitPvP plugin) {Respawn.plugin = plugin;}
    15.  
    16. public static void setRespawn(Player p) {
    17. plugin.getConfig().set("Respawn.X", p.getLocation().getX());
    18. plugin.getConfig().set("Respawn.Y", p.getLocation().getY());
    19. plugin.getConfig().set("Respawn.Z", p.getLocation().getZ());
    20. plugin.getConfig().set("Respawn.Yaw", p.getLocation().getYaw());
    21. plugin.getConfig().set("Respawn.Pitch", p.getLocation().getPitch());
    22. plugin.saveConfig();
    23. Messages.sendMessage(p, ChatColor.GREEN + "You have set the respawn point!");
    24. }
    25.  
    26. public static void Spawn(Player p) {
    27. Double X = plugin.getConfig().getDouble("Respawn.X");
    28. Double Y = plugin.getConfig().getDouble("Respawn.Y");
    29. Double Z = plugin.getConfig().getDouble("Respawn.Z");
    30. Float Yaw = (float) plugin.getConfig().getDouble("Respawn.Yaw");
    31. Float Pitch = (float) plugin.getConfig().getDouble("Respawn.Pitch");
    32.  
    33. Location loc = new Location(p.getWorld(), X, Y, Z, Yaw, Pitch);
    34.  
    35. p.teleport(loc);
    36. }
    37. }
    38.  


    If you have any questions feel free to ask! Hope this helps!
     
    TheAJ471 likes this.
  6. Offline

    willy00

    try delaying it by 1 tick. It might be a problem because the code for the other plugin says to teleport you to the server spawn. Maybe IDK. try it...
     
  7. Offline

    TheAJ471

    willy00 So how would I do this and add it in?

    chasertw123 I dont really know how I would use this :/ I set it up for my plugin but now what

    Mattkx4 I dont think it would be though because i did almost the same thing to save the coordinates for joining and quiting the game

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    chasertw123

    TheAJ471 It is very simple you just call the method you created.
     
  9. Offline

    Wizehh

    I have a way to do this; however, it will force a player to respawn - it won't give them the option: is that okay?
     
  10. Offline

    TheAJ471

    Wizehh Yes that is definatly ok. That actually sounds sort of better in a way.
     
  11. Offline

    Wizehh

    Here you are*:
    PHP:
    @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"EntityPlayerint.class, boolean.class);
                    
    moveToWorld.invoke(playerlistnmsPlayer0false);
                } catch (
    Exception ex) {}
            }
        }.
    runTaskLater(plugin2);
    }
    I did not write this.
    This requires NMS, so make sure you import from the Craftbukkit JAR.
     
  12. Offline

    TheAJ471

    Wizehh Ok but where would this teleport them. How would I add in the coordinates and world from the command and use them in this?

    chasertw123 This didnt work. I just tested it out

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  13. Offline

    chasertw123

    Can you post how you used my code?
     
  14. Offline

    TheAJ471

    chasertw123 So I have this like you said.

    Code:java
    1. package me.TheAJ471.cpvp;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.Listener;
    7.  
    8. import me.TheAJ471.cpvp.Main;
    9.  
    10. public class Respawn implements Listener {
    11.  
    12. private static Main plugin;
    13.  
    14. public Respawn(Main plugin) {Respawn.plugin = plugin;}
    15.  
    16. public static void setRespawn(Player p) {
    17. plugin.settings.getData().set("Respawn.X", p.getLocation().getX());
    18. plugin.settings.getData().set("Respawn.Y", p.getLocation().getY());
    19. plugin.settings.getData().set("Respawn.Z", p.getLocation().getZ());
    20. plugin.settings.getData().set("Respawn.Yaw", p.getLocation().getYaw());
    21. plugin.settings.getData().set("Respawn.Pitch", p.getLocation().getPitch());
    22. plugin.settings.saveData();
    23. p.sendMessage(ChatColor.GREEN + "You have set the respawn point!");
    24. }
    25.  
    26. public static void Spawn(Player p) {
    27. Double X = plugin.settings.getData().getDouble("Respawn.X");
    28. Double Y = plugin.settings.getData().getDouble("Respawn.Y");
    29. Double Z = plugin.settings.getData().getDouble("Respawn.Z");
    30. Float Yaw = (float) plugin.settings.getData().getDouble("Respawn.Yaw");
    31. Float Pitch = (float) plugin.settings.getData().getDouble("Respawn.Pitch");
    32.  
    33. Location loc = new Location(p.getWorld(), X, Y, Z, Yaw, Pitch);
    34.  
    35. p.teleport(loc);
    36. }
    37. }
    38.  


    Then i have this when the player types the command /setdeath

    Code:java
    1. Respawn.setRespawn(p);


    Then I have this when the player death event happens

    Code:java
    1. Respawn.Spawn(p);
     
  15. Offline

    chasertw123

    OK so what you can do is replace:
    Code:java
    1. public Respawn(Main plugin) {Respawn.plugin = plugin;}

    With:
    Code:java
    1. @SuppressWarnings("static-access")
    2. public Respawn(Main plugin) {this.plugin = plugin;}



    Also replace Main with the name of your main class.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  16. Offline

    MordorKing78

    Cant you just use an event for this?
     
  17. Offline

    Wizehh

    Right below this line:
    PHP:
     moveToWorld.invoke(playerlistnmsPlayer0false);
    you could teleport them somewhere:
    PHP:
    e.getEntity().teleport(worldxyz);
     
  18. Offline

    CubieX

    Best would be to add some debug messages to be sure your code is properly executed. (the PlayerRespawnEvent)
    Because you need to be sure that the event actually fires and that your array
    "plugin.pvp" does contain the player who is about to respawn.
    So do some chat messages to check that.

    This should be entirely sufficient to let the player respawn where you want:
    Code:
    e.setRespawnLocation(loc);
    Wizehh Using NMS for this is overkill in my opinion. The PlayerRespawnEvent has everything to accomplish this task.
     
    Konkz likes this.
Thread Status:
Not open for further replies.

Share This Page