Where to return false?

Discussion in 'Plugin Development' started by herghost, Dec 1, 2011.

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

    herghost

    Hi all

    I am having some problems in locating where I would add return false in my command if its entered incorrectly to display the message from config.yml

    here is my code

    Code:
    package me.herghost.Fiery.commands;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import me.herghost.Fiery.util.Configuration;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    
    public class banCommand implements CommandExecutor {
    
    	@Override
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    	{
    		readCommand((Player) sender, commandLabel, args);
    		return true;
    	}
    	@SuppressWarnings("unused")
    	private void readCommand(Player player, String cmd, String[] args )
    	{
    		if(cmd.equalsIgnoreCase("ban")&& player instanceof Player)
    		{
    			Bukkit.getOfflinePlayer(args[0]).setBanned(true);
    			if (Bukkit.getPlayer(args[0]) != null) Bukkit.getPlayer(args[0]).kickPlayer("Banned by admin.");
    		    Command.broadcastCommandMessage(player, "Banning " + args[0]);
    			String user = Configuration.getString("settings.mysql.user");
    			String pass = Configuration.getString("settings.mysql.pass");
    			String url = "jdbc:mysql://localhost:3306/Fiery";
    			String v = Configuration.getString("money.iscalled");
    			boolean t = Configuration.getBoolean("money.isenabled");
    			int cost = Configuration.getInt("commandcharge.ban");
    			int balance;
    		     Player p = (Player) player;
    			try
    			{
    				Connection conn = DriverManager.getConnection(url, user, pass);
    				Statement select = conn.createStatement();
    				ResultSet rs = select.executeQuery("SELECT balance FROM money WHERE p_name ='" + p.getName() + "'");
    				while (rs.next())
    				{
    					balance = rs.getInt("balance");
    					if(t = true && cost > 0)
    					{
    						int nbalance;
    						if(cost < balance )
    						{
    							nbalance = balance - cost;
    							Statement select0 = conn.createStatement();
    							int rs1 = select0.executeUpdate("UPDATE money SET balance = '" + nbalance + "'WHERE p_name ='" + p.getName() + "'");
    							p.sendMessage("You have been charged " + cost + " " + v + " - your new balance is " + nbalance + " " + v + "");
    						}
    						else
    						{
    							p.sendMessage("Sorry, your balance is to low to execute this command");
    						}
    					}
    				}
    				select.close();
    			}
    			catch
    			(SQLException e1)
    			{
    				e1.printStackTrace();
    			}
    
    			}
    		}
    	}
    
    
    Everywhere I place it seems to end in an error.
     
  2. Offline

    SuicideBunnyNL

    I might be wrong here, but I believe return false will only be returned if a non-existent command is sent to the server, thus this needs to be placed in the onCommand. Also, why do you handle the command outside onCommand? I don't see any real benefit to this solution and just makes things more complex. I would also split up the handling of making the SQL connection into a seperate function within the class. But that's just my style of coding.
     
  3. Offline

    herghost

    hmm, I was under the impression that returning false printed the usage message in plugin.yml?

    I have moved the command all in the onCommand and I will look at the sql connection method.

    got it :)

    if
    (args.length == 0)
    {
    return false;
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page