Help: Spawning Exactly (x,y,z,pitch,yaw) into a file.

Discussion in 'Plugin Development' started by x86cam, Mar 19, 2012.

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

    x86cam

    This is my setspawn command

    PHP:
    package com.x86cam.EasyEssentials.commands.ServerControl;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class 
    commandSetSpawn implements CommandExecutor {
       
        public 
    boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args){
            if(
    cmd.getName().equalsIgnoreCase("setspawn")){ // If the player typed /basic then do the following...
                
    Player player = (Playersender;
                if (
    player == null) {
                    
    sender.sendMessage("This command can only be run by a player");
                    return 
    true;
                }
                
    Location spawn player.getLocation();
                if(!
    sender.isOp() || !player.hasPermission("ees.setspawn")){
                    
    sender.sendMessage(ChatColor.RED "You are not opped. You can't set spawn.");
                }else{
                
    player.getWorld().setSpawnLocation(spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ());
                
    player.sendMessage(ChatColor.BLUE "Spawn has successfully been set!");
                return 
    true;
            } 
    //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            
    }
            return 
    false;
        }
     
    }
    This is my spawn command:
    PHP:
    package com.x86cam.EasyEssentials.commands.PlayerCommands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class 
    commandSpawn implements CommandExecutor {
       
        @
    Override
        
    public boolean onCommand(CommandSender senderCommand cmdString commandLabel,
                
    String[] args) {
            if (
    cmd.getName().equalsIgnoreCase("spawn")) {
                
    Player player = (Playersender;
                if (
    player == null) {
                    
    sender.sendMessage("This command can only be run by a player");
                    return 
    true;
                }
                
    Location spawn player.getWorld().getSpawnLocation();
                
    spawn.setX(spawn.getBlockX());
                
    spawn.setY(spawn.getBlockY());
                
    spawn.setZ(spawn.getBlockZ());
                
    player.sendMessage(ChatColor.BLUE "Teleporting to Spawn!");
                
    player.teleport(spawn);
            }
     
            return 
    true;
        }
       
     
    }
    How do I set the spawn, store it in a file, then when someone types /spawn, it spawns them in the exact spot with Pitch and Yaw exact?
     
  2. Offline

    Tzeentchful

    For setting the yaw and pitch you can do this
    Code:
            Location location = new Location(Bukkit.getWorld("world"),x,y,z);
            location.setPitch(pitch here);
            location.setYaw(yaw here);
            player.teleport(location);
    As for saving to the file have a look at loading and saving to a config.
     
  3. Offline

    x86cam

    For the config, it doesn't show any way to save files from a command, and use them. Is there any way to explain putting Spawn in a yml or txt file?

    EDIT: what do the yaw here and pitch here mean? are those variables, a placement for a location that it defines, or a placement for a number.

    And where do I put it?
     
  4. Offline

    x86cam

    How do I set the yaw and pitch to where the player is?
    Because I am setting the spawn pointing towards glass and I don't spawn facing the glass?
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    A Location can have x, y, z, yaw, and pitch values, if you teleport a player to a location with those values set, their yaw and pitch values should be set to match.

    One approach of storing a Location in a a yml file is to create a wrapper class that is ConfigurationSerializable, this way you can set and get it from the configuration, with the serialization code hidden way with the wrapper object.
     
  6. Offline

    x86cam

    Um can you please code this for me? I am such a Noob.
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    And being fed code won't help you learn.
     
  8. Offline

    x86cam

    How did I make this?

    I learned.

    Code:
    package com.x86cam.EasyEssentials.commands.PlayerCommands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class commandTp implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("tp")){
                if (player == null) {
                    sender.sendMessage("This command can only be run by a player");
                }
                if(args.length == 0){
                    if(!sender.isOp() || !player.hasPermission("ees.tp")){
                        player.sendMessage(ChatColor.RED + "Sorry, " + sender.getName() + ", You do not have enough permissions to do this.");
                        return true;
                    }else{
                    player.sendMessage(ChatColor.DARK_RED + "Too little arguments");
                    player.sendMessage(ChatColor.DARK_RED + "Use /tp <target>");
                    }
                }else if(args.length == 1){
                    if(!sender.isOp() || !player.hasPermission("ees.tp")){
                        player.sendMessage(ChatColor.RED + "Sorry, " + sender.getName() + ", You do not have enough permissions to do this.");
                        return true;
                    }else{
                    Player targetPlayer = player.getServer().getPlayer(args[0]);
                    Location targetPlayerLocation = targetPlayer.getLocation();
                    player.sendMessage(ChatColor.GREEN + "Teleporting... Please wait.");
                    player.teleport(targetPlayerLocation);
                    }
                }else if(args.length == 2){
                    if(!sender.isOp() || !player.hasPermission("ees.tp")){
                        player.sendMessage(ChatColor.RED + "Sorry, " + sender.getName() + ", You do not have enough permissions to do this.");
                        return true;
                    }else{
                    Player targetPlayer = player.getServer().getPlayer(args[0]);
                    Player targetPlayer1 = player.getServer().getPlayer(args[1]);
                    Location targetPlayer1Location = targetPlayer1.getLocation();
                    player.sendMessage(ChatColor.GREEN + "Teleporting... Please wait.");
                    targetPlayer.teleport(targetPlayer1Location);
                    }
                }
            return true;
     
            }
            return false;
        }
    }
    
     
  9. Offline

    chaseoes

    That looks like code for a teleport command.. could you share what you did for the setspawn command?
     
  10. Offline

    x86cam

    Code:
    package com.x86cam.EasyEssentials.commands.ServerControl;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class commandSetSpawn implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("setspawn")){ // If the player typed /basic then do the following...
                Player player = (Player) sender;
                if (player == null) {
                    sender.sendMessage("This command can only be run by a player");
                    return true;
                }
                Location spawn = player.getLocation();
                if(!sender.isOp() || !player.hasPermission("ees.setspawn")){
                    player.sendMessage(ChatColor.RED + "Sorry, " + sender.getName() + ", You do not have enough permissions to do this.");
                }else{
                player.getWorld().setSpawnLocation(spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ());
                spawn.getYaw();
                spawn.getPitch();
                spawn.setYaw(spawn.getYaw());
                spawn.setPitch(spawn.getPitch());
                player.sendMessage(ChatColor.BLUE + "Spawn has successfully been set!");
                return true;
            } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            }
            return false;
        }
     
    }
    
    I wont be available until Sunday because boyscouts, but I have an hour
     
  11. Offline

    nicholasntp

    Ok, I would change the variable "spawn" to playerLoc or something because it isnt the actual spawn. It WILL be but not yet. And here:

    Your setting the player's Location's Yaw to it's Yaw and its Pitch to its Pitch.


    They already have those values. What you need to do is....
    Code:java
    1. Location loc = player.getLocation();
    2. World world = player.getWorld();
    3. int x = loc.getBlockX();
    4. int y = loc.getBlockY();
    5. int z = loc.getBlockZ();
    6. float yaw = loc.getYaw();
    7. float pitch = loc.getPitch();
    8. Location spawnLocation = new Location(x,y,z,yaw,pitch);
    9. world.setSpawnLocation(spawnLocation);

    It may have errors. Let me know.
     
  12. Offline

    x86cam

    This produces an error around setSpawnLocation.

    Here is my code:
    Code:
    package com.x86cam.EasyEssentials.commands.ServerControl;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class commandSetSpawn implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("setspawn")){ // If the player typed /basic then do the following...
                Player player = (Player) sender;
                if (player == null) {
                    sender.sendMessage("This command can only be run by a player");
                    return true;
                }
                if(!sender.isOp() || !player.hasPermission("ees.setspawn")){
                    player.sendMessage(ChatColor.RED + "Sorry, " + sender.getName() + ", You do not have enough permissions to do this.");
                }else{
                    Location loc = player.getLocation();
                    World world = player.getWorld();
                    int x = loc.getBlockX();
                    int y = loc.getBlockY();
                    int z = loc.getBlockZ();
                    float yaw = loc.getYaw();
                    float pitch = loc.getPitch();
                    Location spawnLocation = new Location(world,x,y,z,yaw,pitch);
                    world.setSpawnLocation(spawnLocation);
                player.sendMessage(ChatColor.BLUE + "Spawn has successfully been set!");
                return true;
            } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
            }
            return false;
        }
     
    }
    
     
  13. Offline

    nicholasntp

    What error?
     
  14. Offline

    x86cam

    Can not be resolved to world,int,int,int,float,float
     
  15. Offline

    nicholasntp

    Umm a spawn location cant be set with yaw and pitch, I just realized.
    EDIT: I do know.

    Instead of using the bukkit set spawn location, make it save the x, y, z, yaw, and pitch into a config file. Then when a player runs /spawn, have it read those values for the world, and teleport them to that Location.

    Code:
    Location spawnLocation = new Location(world,x,y,z,yaw,pitch);
    player.teleport(spawnLocation);
    
    I have done this before in one of my plugins.

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

    x86cam

    How do I make it go to the defined spawn on /spawn. I have the command set up, but it teleports to the world spawn. And I want it to be 2 separate commands. /spawn - go to spawn
    /setspawn - set the spawn, I want it to link together.
    Like it reads the location from the world and says, oh lets teleport there
     
  17. Offline

    nicholasntp

    Gotcha. So do you have config set up for this plugin?
     
  18. Offline

    x86cam

    No. not yet, but I want it to just set the WORLD spawn.

    I might even add a config soon

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  19. Offline

    nicholasntp

    I know. Have a config for the world's spawn in it.
     
  20. Offline

    x86cam

    How do I?
     
  21. Offline

    nicholasntp

    Set up the config, using this. Then get back to me.
     
  22. Offline

    x86cam

    I set up a config, but how do I program it
     
  23. Offline

    nicholasntp

    A config FILE? Or in the code, did you set it up?
     
  24. Offline

    x86cam

    I will post the code:

    Code:
        this.spawn = new ListStore(new File(pluginFolder + File.separator + "spawn.txt"));
        this.spawn.load();
    I used a tutorial to generate a config.
     
  25. Offline

    nicholasntp

    I think yml would be better, just my opinion.
    Thats what I know how to do WELL.
     
  26. Offline

    x86cam

    ok so what do I do if that is changed
     
  27. Offline

    nicholasntp

    write (worldname).(x,y,z,yaw,or pitch).(value) to it.
    Then when you type /spawn, get the values and tele the player to that location
     
  28. Offline

    rockyandirou

    You don't need to store the spawn.. there is a method on getWorld() that sets spawn in memory.
     
  29. Offline

    nicholasntp

    Read above before you post. We need the yaw and pitch. Not being rude! :)
     
  30. Offline

    Zahachos

Thread Status:
Not open for further replies.

Share This Page