Solved Spawn help

Discussion in 'Plugin Development' started by Giorgio, Nov 12, 2012.

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

    Giorgio

    Hello everyone Ive created a plugin that is getting popular and people are asking me to fix a little thing they would like. This isssue is when someone sets spawn it sets it to were they want, but not where there looking at?

    Please if you could help me I really appreciate it! Here is what i got so far.

    NOTE: it writes the location of spawn on the config.yml.

    Code:Java
    1.  
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    4. Player player = (Player) sender;
    5.  
    6. if(commandLabel.equalsIgnoreCase("setspawn")){
    7. if(player.hasPermission("simplespawn.setspawn") || player.isOp()) {
    8. int x = player.getLocation().getBlockX();
    9. int y = player.getLocation().getBlockY();
    10. int z = player.getLocation().getBlockZ();
    11. String w = player.getLocation().getWorld().getName().toString();
    12. getConfig().set(".X", Integer.valueOf(x));
    13. getConfig().set(".Y", Integer.valueOf(y));
    14. getConfig().set(".Z", Integer.valueOf(z));
    15. getConfig().set(".W", w);
    16. saveConfig();
    17. player.sendMessage(ChatColor.GREEN + "Spawn point set");
    18. player.getWorld().setSpawnLocation(x, y, z);
    19. return true;
    20. }else{
    21. player.sendMessage(ChatColor.RED + "Permission Denied to set Spawn location");
    22. }
    23. }
    24. if(commandLabel.equalsIgnoreCase("spawn")){
    25. if(args.length == 0){
    26. if(player.hasPermission("simplespawn.spawn") || player.isOp()){
    27. int x = Integer.parseInt(getConfig().getString(".X"));
    28. int y = Integer.parseInt(getConfig().getString(".Y"));
    29. int z = Integer.parseInt(getConfig().getString(".Z"));
    30. World w = getServer().getWorld(getConfig().getString(".W"));
    31. Location loc = new Location(w, x, y, z);
    32. player.teleport(loc);
    33. return true;
    34. }else{
    35. player.sendMessage(ChatColor.RED + "Permission Denied to spawn");
    36. }
    37. }else if(args.length == 1){
    38. if(player.hasPermission("simplespawn.spawn.other") || player.isOp()){
    39. if (sender.getServer().getPlayer(args[0]) != null) {
    40. Player targetPlayer = sender.getServer().getPlayer(args[0]);
    41. int x = Integer.parseInt(getConfig().getString(".X"));
    42. int y = Integer.parseInt(getConfig().getString(".Y"));
    43. int z = Integer.parseInt(getConfig().getString(".Z"));
    44. World w = getServer().getWorld(getConfig().getString(".W"));
    45. Location loc = new Location(w, x, y, z);
    46. targetPlayer.teleport(loc);
    47. }else{
    48. player.sendMessage(ChatColor.RED + "Player not online!");
    49. }
    50. }else{
    51. player.sendMessage(ChatColor.RED + "Permission Denied to spawn another user");
    52. }
    53. }
    54. }
     
  2. Offline

    cman1885

    You need to set pitch and yaw as well as coordinates.
     
  3. Offline

    Kodfod

    Which would look like this Giorgio:

    Code:java
    1. Location loc = new Location(w, x, y, z, yaw, pitch);
     
  4. Offline

    Giorgio

    Kodfod
    cman1885

    I don't know how to define yaw and pitch. Kodfod when i use your code it just gives me an error saying:


     
  5. Offline

    Kodfod

    Giorgio
    Have you tried to get the yaw and pitch and set them to a float var?

    Code:java
    1. Player p = (Player)sender;
    2. float yaw = p.getLocation().getYaw();
    3. float pitch = p.getLocation().getPitch();
    4. World w = p.getLocation().getWorld();
    5. int x = p.getLocation().getBlockX();
    6. int y = p.getLocation().getBlockY();
    7. int z = p.getLocation().getBlockZ();
    8. Location loc = new Location(w, x, y, z, yaw, pitch);
     
  6. Offline

    Giorgio

    Kodfod

    Ok I have not tried this, this is how you define it right? but now how do you set this in as your spawn point? and also how would someone teleport to this location?
     
  7. Offline

    javoris767

    I think this :3
    Code:java
    1. player.teleport(player.getWorld().getSpawnLocation());
     
  8. Offline

    Kodfod

    yes that is how you would define it, and then you could do:

    Code:java
    1. w.setSpawnLocation(loc);


    Then do what the poster above me said.
     
  9. Offline

    fireblast709

  10. Offline

    Kodfod

  11. Offline

    Giorgio

    This method doesn't work.
     
  12. Offline

    Kodfod

    Are there any errors?
     
  13. Offline

    Giorgio

    Kodfod

    yes, this is what i get in red.
     
  14. Offline

    Kodfod

    Ah because it already has the world argument so then just grab the x, y, and z. or you can listen for the /spawn command and then player.teleport(location); as javoris767 above stated
     
  15. Offline

    Giorgio

    Im confused, I'm trying to get the players looking location in order to spawn the users where they are looking.
     
  16. Offline

    cman1885

  17. Offline

    one4me

    Giorgio
    This should fix your problems, just make sure you call setSpawn() in onEnable().
    Code:
    public Location spawn;
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
      if(cmd.getName().equalsIgnoreCase("setspawn")) {
        if(!(sender instanceof Player)) {
          sender.sendMessage(ChatColor.RED + "You cannot use this command from console");
        }
        else if(!sender.hasPermission("simplespawn.setspawn")) {
          sender.sendMessage(ChatColor.RED + "Permission Denied to set Spawn location");
        }
        else {
          if(args.length != 0) {
            sender.sendMessage(ChatColor.RED + "Usage: /setspawn");
          }
          else {
            Player player = (Player)sender;
            Location location = player.getLocation();
            World world = location.getWorld();
            double x = location.getX();
            double y = location.getY();
            double z = location.getZ();
            float yaw = location.getYaw();
            float pitch = location.getPitch();
            FileConfiguration config = getConfig();
            config.set(".World", world.getName().toString());
            config.set(".X", x);
            config.set(".Y", y);
            config.set(".Z", z);
            config.set(".Yaw", yaw);
            config.set(".Pitch", pitch);
            saveConfig();
            world.setSpawnLocation((int)x, (int)y, (int)z);
            this.spawn = location;
            sender.sendMessage(ChatColor.GREEN + "Spawn point set");
          }
        }
      }
      else if(cmd.getName().equalsIgnoreCase("spawn")) {
        if(args.length > 1) {
          sender.sendMessage(ChatColor.RED + "Usage: /spawn <player>");
        }
        else if(args.length == 0) {
          if(!(sender instanceof Player)) {
            sender.sendMessage(ChatColor.RED + "You cannot use this command from console");
          }
          else if(!sender.hasPermission("simplespawn.spawn")) {
            sender.sendMessage(ChatColor.RED + "Permission Denied to spawn");
          }
          else {
            ((Player)sender).teleport(this.spawn);
            return true;
          }
        }
        else if(args.length == 1) {
          if(!sender.hasPermission("simplespawn.spawn.other")) {
            sender.sendMessage(ChatColor.RED + "Permission Denied to spawn another user");
          }
          else {
            Player target = Bukkit.getPlayerExact(args[0]);
            if(target == null) {
              sender.sendMessage(ChatColor.RED + "Player not online!");
            }
            else {
              target.teleport(this.spawn);
            }
          }
        }
      }
      return true;
    }
    public void setSpawn() {
      try {
        FileConfiguration config = getConfig();
        this.spawn = new Location( 
          Bukkit.getWorld(config.getString(".World")),
          Double.parseDouble(config.getString(".X")),
          Double.parseDouble(config.getString(".Y")),
          Double.parseDouble(config.getString(".Z")),
          Float.parseFloat(config.getString(".Yaw")),
          Float.parseFloat(config.getString(".Pitch"))
        );
      }
      catch(Exception e) {
        getLogger().log(Level.WARNING, "Failed to load spawn location, reverting to default!");
        this.spawn = Bukkit.getWorlds().get(0).getSpawnLocation();
      }
    }
    
     
  18. Offline

    Giorgio

    one4me

    This is GREAT! thank you so MUCH!!!
     
Thread Status:
Not open for further replies.

Share This Page