[solved!]Help with creating location from coordinates

Discussion in 'Plugin Development' started by herghost, Nov 17, 2011.

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

    herghost

    Hi all,
    I have a bit of a mind block and cant figure out the next part of my code. Basically I am getting the saved x,y,z coordinates for a players home from a database, which works fine. I am stuck on actually moving the player to the coordinates.

    Here is my code so far:

    Code:
    package me.herghost.Fiery.commands;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.logging.Logger;
    
    
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class homeCommand implements CommandExecutor {
    	Logger log = Logger.getLogger("Minecraft");
    
    	String user = "bukkitdev";
    	String pass = "//removed";
    	String url = "jdbc:mysql://localhost:3306/Fiery";
    
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    		readCommand((Player) sender, commandLabel, args);
    		return true;
    
    	}
    	private void readCommand(Player player, String cmd, String[] args )
    	{
    		if(cmd.equalsIgnoreCase("home")&& player instanceof Player)
    		{
    			if(args.length == 0)
    			{
    			Player p = (Player) player;
    			try
    			{
    				Connection conn = DriverManager.getConnection(url, user, pass);
    				Statement select = conn.createStatement();
    				ResultSet result = select.executeQuery("SELECT home_x,home_y,home_z FROM userhomes WHERE p_name = '" + p.getName() + "' LIMIT 1");
    				while(result.next())
    				{
    					double x = result.getDouble(1);
    					double y = result.getDouble(2);
    					double z = result.getDouble(3);
    				log.info("" + x + "," + y + "," + z +"");
    				}
    			}
    			 catch( Exception e ) {
    			      e.printStackTrace();
    			}
    		}
    	}
    	}}
    
    I basically dont know where to go after the log info statement (will be removed, just for testing!)
    I have had a look at Location add() but I cant create the statement.

    Many thanks for any pointers
     
  2. You need to know the world name, too! Then you can easily teleport like this:

    player.teleport(new Location(getServer().getWorld("worldName"), x, y, z));

    alternative you can just use the world the player is in, but this is all other than multiworld compatible:

    player.teleport(new Location(player.getWorld(), x, y, z));
     
    epe07, Gunpowder and herghost like this.
  3. Offline

    herghost

    Thanks, that definitely helped, however I am still stuck!

    I cant get the world as a string and then use it in Location(world,x,y,z)
    If I change to World world = result.getString(1) then it will not pull from database as its not a string?
     
  4. Offline

    halley

    V10lator already explained that you can't just assign or cast String to World; your last message suggests you didn't understand that.

    Code:
    String worldName = result.getString(/*column*/1);
    World world = plugin.getServer().getWorld(worldName);
     
    herghost likes this.
  5. Did you create the database entry and did you verify that the worldname is stored in it?
    Sorry... ;)
     
  6. Offline

    herghost

    Fantastic! thanks!

    I had to use Bukkit.getServer().getWorld(worldName);

    but it works!
     
Thread Status:
Not open for further replies.

Share This Page