setspawnlocation with yaw and pitch

Discussion in 'Spigot Plugin Development' started by Waterman2707, May 1, 2015.

  1. i trying to setSpawnLocation, but it don't include yaw and pitch. I can't get it to work, this is basicly new method i try. The code i use to it is:
    Code (Text):
    p.getWorld().setSpawnLocation((int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
            p.getWorld().getSpawnLocation().setPitch(loc.getPitch());
            p.getWorld().getSpawnLocation().setPitch(loc.getYaw());
    If you want more code just say it.

    //Water

    PS. no errors ;)

    PPS. i just think it's me that do it in the wrong way :p'


    OBS! it need to respawn the player in the world he died in!
     
    #1 Waterman2707, May 1, 2015
    Last edited: May 1, 2015
  2. just create a location variable set the location of it and on join of a player or respawn teleport the player to that location
     
  3. In code would be:
    Code (Text):
    Location respawn = new Location((int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
    respawn.setPitch(loc.getPitch());
    respawn.setPitch(loc.getYaw());
    //Save this location in a static var.
     
    Then in the EventListener create a PlayerSpawnEvent method and then teleport the player to the location respawn.
    Code (Text):
      @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent e){
            Player p = e.getPlayer();
            p.teleport(respawn);
        }
     
  4. Learn to Java if you need static for this. Also, you probably need to delay the teleport by a tick or use setRespawnLocation
     
  5. Ups now I see you don't need it to be static, sorry
     
  6. Also no need to cast to an int when you can use loc.getBlockX(), loc.getBlockY() and loc.getBlockZ() which returns integers that you can use to create a new location.
     
  7. I just copied the same as written above but yes it would be better to use block position
     
  8. I just noticed that my bad xD
     
  9. I've made a spawn plugin before, what I did was this:

    1. Listen for PlayerRespawnEvent.
    2. Teleport them 1 tick later, it never worked for me if I teleported players instantly.

    To teleport them I just used this:

    Code (Java):
    Location loc = new Location(world, x, y, z, pitch, yaw);
    p.teleport(loc);
     
  10. How do i wait one tick?, that will be usefull to a lot of things ;)
     
  11. Code (Java):

    new BukkitRunnable() {

    public void run() {
    // Code
    }

    }.runTaskLater(plugin, 1);
     
     
  12. You can just set the respawn location with the respawn event.. event.setRespawnLocation(location); simple as that. But with the PlayerJoinEvent you should delay it a little like you did to teleport them as you said it wont work if you teleport the player instantly when they join.
     
  13. I think it's better practice to use PlayerRespawnEvent like the posters before already said, but in your code you got something wrong:
    You're using setPitch twice. One time for yaw and one time for pitch, which is wrong.
    You have to use it like so:
    Code (Text):

    p.getWorld().setSpawnLocation((int) loc.getX(), (int) loc.getY(), (int) loc.getZ());
    p.getWorld().getSpawnLocation().setPitch(loc.getPitch());
    p.getWorld().getSpawnLocation().setYaw(loc.getYaw());
     
    But I don't know if this is working with "getSpawnLocation()", because I don't know if it is a reference to the spawn location or a copy of the object.
     
  14. Oh in that case you could just use:

    Code (Java):
    e.setRespawnLocation(new Location(world, x, y, z, pitch, yaw));
     
    • Like Like x 1