[Solved] args problem

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

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

    herghost

    Hi all

    I have a problem with my command if an arg is used.

    Basically the idea is that you can set more than one home.

    The command /sethome works fine, however if you use /sethome 1 or /sethome 2 then I can an Unhandled Exception error. I have tried echoing out the arg[1] into the logger but nothing is displayed so I am guessing the arg isnt picked up, I just cant tell why.

    Code:
    package me.herghost.Fiery.commands;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    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 sethomeCommand 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 false;
    
    	}
    	private void readCommand(Player player, String cmd, String[] args )
    	{
    		if(cmd.equalsIgnoreCase("sethome")&& player instanceof Player)
    		{
    			if(args.length == 0)
    			{
    			Player p = (Player) player;
    			double x = p.getLocation().getX();
    			double y = p.getLocation().getY();
    			double z = p.getLocation().getZ();
    			log.info("" + x + ", " + y + ", " + z + ", " + p.getName() + "");
    			try
    			{
    				Connection conn = DriverManager.getConnection(url, user, pass);
    				PreparedStatement sampleQueryStatement = conn.prepareStatement("REPLACE INTO userhomes SET p_name = '" + p.getName() + "', home_x = '" + x + "', home_y = '" + y + "', home_z = '" + z + "'");
    				sampleQueryStatement.executeUpdate();
    				sampleQueryStatement.close();
    				p.sendMessage("Home Set Successfully");
    					}
    			catch
    			(SQLException e1)
    			{
    				e1.printStackTrace();
            	}
    		}
    			if(args.length == 1)
    			{
    				Player p = (Player) player;
    				double x = p.getLocation().getX();
    				double y = p.getLocation().getY();
    				double z = p.getLocation().getZ();
    				log.info("" + args[1] + "");
    				try
    				{
    					Connection conn = DriverManager.getConnection(url, user, pass);
    					PreparedStatement sampleQueryStatement = conn.prepareStatement("REPLACE INTO userhomes SET p_name = '" + p.getName() + "', home'" + args[1] + "'_x = '" + x + "', home'" + args[1] + "'_y = '" + y + "', home'" + args[1] + "'_z = '" + z + "'");
    					sampleQueryStatement.executeUpdate();
    					sampleQueryStatement.close();
    					p.sendMessage("Home " + args[1] + " Set Successfully");
    						}
    				catch
    				(SQLException e1)
    				{
    					e1.printStackTrace();
    	        	}
    			}
    		}
    	}}
    
    
    can anyone point me in the right direction?
     
  2. I just took a very quick look at your code and noticed this:
    Code:java
    1. if(args.length == 1) {
    2. //stuff
    3. log.info("" + args[1] + "");
    4. }

    Java is zero-indexed so args[1] is null. args[0] is what you want.
     
  3. Offline

    herghost

    How the hell did i forget that! :mad:

    Thanks dude.
     
    r3Fuze likes this.
Thread Status:
Not open for further replies.

Share This Page