Teleporting to coordinates

Discussion in 'Plugin Development' started by StyL_TwisT, Feb 19, 2013.

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

    StyL_TwisT

    Hello,

    I am trying to make it so when a player types /spawn it will teleport them to coordinates of my choice and send them the message "You have been teleported to the spawn location."
    I am having issues with defining what the coordinates to teleport to are, how do i do this?
     
  2. Offline

    slater96

    Location loc = new Location(world, x, y, z);
    p.teleport(Loc);
     
  3. Offline

    Burnett1

    You would use a config file to save the co ords. Then make a Location with those co ords and teleport the player to it.
    Here's some code.
    Code:
    package me.kyle.burnett.test;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Test extends JavaPlugin{
     
        public final Logger logger = Logger.getLogger("Minecraft");
     
       
        @SuppressWarnings("unused")
        @Override
        public void onEnable(){
           
            PluginManager pm = this.getServer().getPluginManager();
           
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
           
        }
       
        @Override
        public void onDisable(){
           
           
            this.saveConfig();
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
           
            if(commandLabel.equalsIgnoreCase("setspawn")){
               
                this.getConfig().set("Spawn" + ".X", player.getLocation().getBlockX());
                this.getConfig().set("Spawn" + ".Y", player.getLocation().getBlockY());
                this.getConfig().set("Spawn" + ".Z", player.getLocation().getBlockZ());
                this.getConfig().set("Spawn" + ".Yaw", player.getLocation().getYaw());
                this.getConfig().set("Spawn" + ".Pitch", player.getLocation().getPitch());
                this.getConfig().set("Spawn" + ".World", player.getLocation().getWorld());
               
               
                player.sendMessage(ChatColor.GREEN + "Spawn set!");
            }
           
            if(commandLabel.equalsIgnoreCase("spawn")){
               
                int spawnX = this.getConfig().getInt("Spawn" + ".X");
                int spawnY = this.getConfig().getInt("Spawn" + ".Y");
                int spawnZ = this.getConfig().getInt("Spawn" + ".Z");
                int spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
                int spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
                Object world = this.getConfig().get("Spawn" + ".World");
               
                Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
               
                player.teleport(spawn);
               
                player.sendMessage(ChatColor.GREEN + "You are at spawn!");
            }
           
           
           
           
           
           
           
           
            return false;
        }   
    }
    
    This should not through out any errors but you might have trouble with the config being empty. Also click your whole plugin folder. Right click - new file - called "config.yml".
    Make sure you register the command with the plugin.yml.

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

    StyL_TwisT

    Thanks, but for some reason, it puts me on the corner of the block + I am facing the wrong way. Any way I can fix that?

    I'm not bothered about setting the spawn in a config because there is simply no need to.

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

    slater96

    StyL_TwisT
    loc.add(0.5, 0, 0.5) should put you in the center and you need to set the yaw and pitch to set the direction your facing.
     
  6. Offline

    StyL_TwisT

    This may seem a little noobish but I don't know how to use yaw and pitch
     
  7. Offline

    Burnett1

    Yaw and pitch are like the way you are facing and how high (i think lol).
     
  8. Offline

    slater96

  9. Offline

    Burnett1

    I added the Yaw and pitch

    Update add replace the old section with this.
    Code:
                this.getConfig().set("Spawn" + ".X", player.getLocation().getX());
                this.getConfig().set("Spawn" + ".Y", player.getLocation().getY());
                this.getConfig().set("Spawn" + ".Z", player.getLocation().getZ());
                this.getConfig().set("Spawn" + ".Yaw", player.getLocation().getYaw());
                this.getConfig().set("Spawn" + ".Pitch", player.getLocation().getPitch());
                this.getConfig().set("Spawn" + ".World", player.getLocation().getWorld());
                
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    StyL_TwisT

    I told you I don't need this besides the problem is solved now.
     
  11. Offline

    Burnett1

    Yes you do. Otherwise u have to set the co ords every time u start the server.

    update again.
    Code:
    double spawnX = this.getConfig().getDouble("Spawn" + ".X");
                double spawnY = this.getConfig().getDouble("Spawn" + ".Y");
                double spawnZ = this.getConfig().getDouble("Spawn" + ".Z");
                float spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
                float spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
                Object world = this.getConfig().get("Spawn" + ".World");
               
                Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitc
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  12. Offline

    StyL_TwisT

    Thanks the spawn command is sorted.
    While i was waiting i added a few commands and they are all working fine besides one.
    Code:
    package me.styltwist.twistedmain;
    
    import java.util.logging.Logger;
    
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TwistedMain extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static TwistedMain plugin;
        
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("spawn")){
                Player player = (Player) sender;
                World world = player.getWorld();
                Location loc = new Location(world, 97,73,441,-90, 0);
                loc.add(0.5, 0, 0.5);
                player.teleport(loc);
                player.sendMessage("§3You have been teleported to the spawn location.");
            }else if(commandLabel.equalsIgnoreCase("creative")){
                Player player = (Player) sender;
                if(player.isOp());
                    if(args.length == 0){
                        player.setGameMode(GameMode.CREATIVE);
                        player.sendMessage("§3You are now in creative mode.");
                    }else if(args.length == 1){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        targetPlayer.setGameMode(GameMode.CREATIVE);
                        player.sendMessage("§3" + targetPlayer.getName() + " is now in creative mode.");
                        targetPlayer.sendMessage("§3You are now in creative mode.");
                }else if(!player.isOp()){
                    player.sendMessage("§cYou don't have permission.");
                }
                return false;
            }
            if(commandLabel.equalsIgnoreCase("survival")){
                Player player = (Player) sender;
                if(player.isOp()){
                    if(args.length == 0){
                        player.setGameMode(GameMode.SURVIVAL);
                        player.sendMessage("§3You are now in survival mode.");
                    }else if(args.length == 1){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        targetPlayer.setGameMode(GameMode.SURVIVAL);
                        player.sendMessage("§3" + targetPlayer.getName() + " is now in survival mode.");
                        targetPlayer.sendMessage("§3You are now in survival mode.");
                }else if(!player.isOp()){
                    player.sendMessage("§cYou don't have permission.");
                }
                return false;
            }
            if(commandLabel.equalsIgnoreCase("adventure")){
                if(player.isOp()){
                    if(args.length == 0){
                        player.setGameMode(GameMode.ADVENTURE);
                        player.sendMessage("§3You are now in adventure mode.");
                    }else if(args.length == 1){
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        targetPlayer.setGameMode(GameMode.ADVENTURE);
                        player.sendMessage("§3" + targetPlayer.getName() + " is now in adventure mode.");
                        targetPlayer.sendMessage("§3You are now in adventure mode.");
                }else if(!player.isOp()){
                    player.sendMessage("§cYou don't have permission.");
                }
                }
            }
            }
            return false;
        }
    }
    The adventure command is not working.
    here is my plugin.yml
    Code:
    name: TwistedMain
    main: me.styltwist.twistedmain.TwistedMain
    version: 1.0
    description: >
                 TwistedMain v1.0 By StyL TwisT.
    commands:
      spawn:
        description: Teleports you to the spawn location.
      creative:
        description: Sets gamemode to creative.
      survival:
        description: Sets gamemode to survival.
      adventure:
        description: Sets gamemode to adventure.
    
    Any idea why it isn't working?
     
  13. Offline

    Burnett1

    ok i see what you mean, as a private plugin this is ok.

    Any errors?

    Where is your onEnable and onDisable?

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

    StyL_TwisT

    I have added them but still no difference.
     
  15. Offline

    Burnett1

    Any errors, or just not running?
     
  16. Offline

    StyL_TwisT

    Everything works besides /adventure, there are no errors in the console.
     
Thread Status:
Not open for further replies.

Share This Page