Setting a spawn and getting that world and players direction

Discussion in 'Plugin Development' started by slater96, Jun 16, 2012.

  1. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Title says all really, I have the code which sets a spawn and saves the coords to a config. It doesn't keep the world though that you've set it too, it just does it to the world your on when you teleport there. Also it always faces a certain direction (north i assume) but I want to to keep the players direction when they do the command.
    Here's the setspawn code:
    Code:
    } else if (args[1].equalsIgnoreCase("Lobby")) {
                            double x = p.getLocation().getBlockX();
                            double y = p.getLocation().getBlockY();
                            double z = p.getLocation().getBlockZ();
                            this.getConfig().set("Spawn_Lobby", x + "," + y + "," + z);
                            saveConfig();
                            reloadConfig();
                            p.sendMessage(ChatColor.DARK_RED + "[MCWar]" + ChatColor.DARK_AQUA + " You have set the spawn for the lobby!");
    Here's how I teleport there:
    Code:
    this.getConfig().getString("Spawn_Lobby");
                            String[] spawnlobby = this.getConfig().getString("Spawn_Lobby").split(",");
                            double x = Double.parseDouble(spawnlobby[0]);
                            double y = Double.parseDouble(spawnlobby[1]);
                            double z = Double.parseDouble(spawnlobby[2]);
                            Location lobby = new Location(w, x, y, z);
                            p.teleport(lobby);
    Thanks
  2. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    use pitch and yaw.

    you can get them from a location with loc.getPitch() and loc.getYaw()
  3. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I've done this...
    In my setspawn code:
    Code:
                            p.getLocation().getPitch();
                            p.getLocation().getYaw();
    And this in my teleport code:
    Code:
                            lobby.getPitch();
                            lobby.getYaw();
    But it's still not working :(

    This post has been edited 1 time. It was last edited by slater96 Jun 16, 2012.
  4. Offline

    LucasEmanuel

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Can you give us your entire code?
  5. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Think I may of fixed it now. Here's my code if I haven't.
    Setspawn:
    Code:
                        } else if (args[1].equalsIgnoreCase("Lobby")) {
                            double x = p.getLocation().getBlockX();
                            double y = p.getLocation().getBlockY();
                            double z = p.getLocation().getBlockZ();
                            p.getWorld();
                            p.getLocation().getPitch();
                            p.getLocation().getYaw();
                            this.getConfig().set("Spawn_Lobby", x + "," + y + "," + z);
                            saveConfig();
                            reloadConfig();
                            p.sendMessage(ChatColor.DARK_RED + "[MCWar]" + ChatColor.DARK_AQUA + " You have set the spawn for the lobby!");
    And my teleport:
    Code:
                            this.getConfig().getString("Spawn_Lobby");
                            String[] spawnlobby = this.getConfig().getString("Spawn_Lobby").split(",");
                            World w = p.getWorld();
                            double x = Double.parseDouble(spawnlobby[0]);
                            double y = Double.parseDouble(spawnlobby[1]);
                            double z = Double.parseDouble(spawnlobby[2]);
                            Location lobby = new Location(w, x, y, z);
                            lobby.setPitch(p.getLocation().getPitch());
                            lobby.setYaw(p.getLocation().getYaw());
                            p.teleport(lobby);
  6. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It works for the direction now that the player stands when they do the setspawn, but it still doesn't get the world you were at when you do setspawn...
    It just takes you to the coords which are set on any world you are on.
  7. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Can you show your p.teleport code please. It's something you doing in there.
  8. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    This is my whole teleport join lobby command:
    Code:
    if (args[0].equalsIgnoreCase("Join")) {
                    if (p.hasPermission("mcwar.join")) {
                        String name = p.getName();
                        if (this.inLobby.contains(p) || (this.Teams.contains(p) || this.Zombies.contains(p) ||this.FreeForAll.contains(p))) {
                            p.sendMessage(ChatColor.RED + "You are already playing!");
                        } else if (this.Dead.contains(p) || (this.Quit.contains(p))) {
                            p.sendMessage(ChatColor.RED + "You Died/Quit! Please wait until the next game.");
                        } else if (this.canjoin) {
                            p.sendMessage(ChatColor.RED + "The game is in already in progress! Please wait until the next game.");                   
                        } else if (this.Spectate.contains(p)) {
                            p.sendMessage(ChatColor.RED + "    You must stop spectating before you can join!");
                        } else {
                            this.getConfig().getString("Spawn_Lobby");
                            String[] spawnlobby = this.getConfig().getString("Spawn_Lobby").split(",");
                            double x = Double.parseDouble(spawnlobby[0]);
                            double y = Double.parseDouble(spawnlobby[1]);
                            double z = Double.parseDouble(spawnlobby[2]);
                            Location lobby = new Location(p.getWorld(), x, y, z);
                            lobby.setPitch(p.getLocation().getPitch());
                            lobby.setYaw(p.getLocation().getYaw());
                            p.teleport(lobby);
                            this.inLobby.add(p);
                            p.setGameMode(GameMode.SURVIVAL);
                            getServer().broadcastMessage(ChatColor.DARK_RED + "[MCWar] " + ChatColor.GREEN + name + ChatColor.DARK_AQUA + " has joined the game!");
                        }
                    } else {
                        p.sendMessage(ChatColor.DARK_RED + "You don't have permission to do this command!");
                    }
                }
  9. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    In your save spawn location you must also save the world name

    this.getConfig().set("Spawn_Lobby.Loc", x + "," + y + "," + z);
    this.getConfig().set("Spawn_Lobby.World",p.getWorld().getName());

    In your spawn part you then do this

    String[] spawnlobby = this.getConfig().getString("Spawn_Lobby.Loc").split(",");
    double x = Double.parseDouble(spawnlobby[0]);
    double y = Double.parseDouble(spawnlobby[1]);
    double z = Double.parseDouble(spawnlobby[2]);
    String newWorld = this.getConfig().getString("Spawn_Lobby.World");
    Location lobby = new Location(p.getServer().getWorld(newWorld), x, y, z);

    This should do it.
  10. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Thank you! Works great :D
  11. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Your welcome
  12. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Still a bit of a problem with the teleporting to the spawn i've set. Whichever way your looking, when you teleport you stay, but I want it so the way and direction and line of sight your looking when you setspawn stays like that when you teleport.
  13. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    OK in your spawn save do the same thing you did for x,y,z
    this.getConfig().set("Spawn_Lobby.Yaw", yaw);
    this.getConfig().set("Spawn_Lobby.Pitch", pitch);

    and in you spawn

    just get them and use them to set
    Location lobby = new Location(p.getServer().getWorld(newWorld), x, y, z,yaw,pitch);
    lobby.setPitch(p.getLocation().getPitch());
    lobby.setYaw(p.getLocation().getYaw());
    slater96 likes this.
  14. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ah right, thanks. Is there any other way you can do it without saving it to config? If not i'll do it like that.
  15. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    If you want the Yaw and Pitch of the spawn location you created then YES you gota save it or how would it know how to face them.
  16. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ok, i've added this:
    Code:
                        String yaw = this.getConfig().getString("Spawn_Lobby.Yaw");
                            String pitch = this.getConfig().getString("Spawn_Lobby.Pitch");
                            Location lobby = new Location(p.getServer().getWorld(newWorld), x, y, z, yaw, pitch);
    But on the location lobby part it says 'remove arguments to match world, double, double, double?
  17. Offline

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    use this contructor:

    (
    World

    world, double x, double y, double z, float yaw, float pitch)
    EDIT: annoying line breaks...

    This post has been edited 1 time. It was last edited by ferrybig Jun 17, 2012.
  18. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Am I changing the right thing, because it's not working...
    Code:
    World lobby = new Location(p.getServer().getWorld(newWorld), x, y, z, yaw, pitch));
  19. Offline

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    are the yaw an pitfch floats?
  20. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Umm, not sure...
    This is what i'm using to get the yaw and pitch.
    Code:
                            String yaw = this.getConfig().getString("Spawn_Lobby.Yaw");
                            String pitch = this.getConfig().getString("Spawn_Lobby.Pitch");

    This post has been edited 3 times. It was last edited by slater96 Jun 17, 2012.
  21. Offline

    ferrybig

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    so, instead of float, you try to pass strings, and excepting the code to magicly turn them into floats?
  22. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Sorry i still don't understand?
    doing float yaw = doesn't work?
  23. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Location lobby = new Location(p.getServer().getWorld(newWorld), x, y, z,yaw,pitch);
    yaw and pitch sould be of type float
    so you need to pass float values yaw and pitch to the above call not strings.

    This post has been edited 2 times. It was last edited by LexLaiden Jun 17, 2012.
  24. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yeah but...how do I do that, i've tried doing float yaw and float pitch but neither worked :(
    It says change it back to string when i do that.

    This post has been edited 1 time. It was last edited by slater96 Jun 17, 2012.
  25. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Then how are you retrieving it from the config file? You have to get it from there (and save it to there) as a float.
  26. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Because your using
    String pitch = this.getConfig().getString("Spawn_Lobby.Pitch"); to get the pitch and yaw.
    So if you are saving them as a string and getting them as a string thin you need to convert them to a float.
    Float.parseFloat(pitch)
    be sure you wrap a try{ }catch(NumberFormatException e){} around the section of code you use the Float.parseFloat()
    slater96 likes this.
  27. Offline

    LexLaiden

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You good??
  28. Offline

    slater96

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yup thanks, done it now :)

Share This Page