[solved]Help with command charge

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

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

    herghost

    Hi all

    I have the following code that looks at my config and charges for a command if a) money is enabled, b) the price of the command is greater than 0 and c) the players balance is higher than the cost of the command.

    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;
    	}
    	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";
    			boolean t = Configuration.getBoolean("money.isenabled");
    			int cost = Configuration.getInt("command.charge.ban.amount");
    			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;
    							int rs1 = select.executeUpdate("UPDATE money SET balance = '" + nbalance + "'WHERE p_name ='" + p.getName() + "'");
    							p.sendMessage("You have been charged " + cost + " - your new balance is " + nbalance + "");
    						}
    						else
    						{
    							p.sendMessage("Sorry, your balance is to low to execute this command");
    						}
    					}
    				}
    			}
    			catch
    			(SQLException e1)
    			{
    				e1.printStackTrace();
    			}
    
    			}
    		}
    	}
    
    
    Everything seems to work fine, when the command is run the balance in the db gets updated and the new balance is displayed to the user. However I am getting this error in the console

    Code:
    2011-11-30 21:05:50 [SEVERE] java.sql.SQLException: Operation not allowed after ResultSet closed
    2011-11-30 21:05:50 [SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    2011-11-30 21:05:50 [SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    2011-11-30 21:05:50 [SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    2011-11-30 21:05:50 [SEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    2011-11-30 21:05:50 [SEVERE] 	at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
    2011-11-30 21:05:50 [SEVERE] 	at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7145)
    2011-11-30 21:05:50 [SEVERE] 	at me.herghost.Fiery.commands.banCommand.readCommand(banCommand.java:57)
    2011-11-30 21:05:50 [SEVERE] 	at me.herghost.Fiery.commands.banCommand.onCommand(banCommand.java:23)
    2011-11-30 21:05:50 [SEVERE] 	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    2011-11-30 21:05:50 [SEVERE] 	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
    2011-11-30 21:05:50 [SEVERE] 	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:364)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
    2011-11-30 21:05:50 [SEVERE] 	at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)
    
    Line 57: while (rs.next())
    Line 23: readCommand((Player) sender, commandLabel, args);

    Could anyone enlighten me at whats wrong? As far as I can see the only thing I can see that I am calling after the while is closed is the catch statement?

    Cheers
    Dave

    fixed, actually quite simple

    just needed to start a new query

    Statement select0 = conn.createStatement();
    int rs1 = select0.executeUpdate("UPDATE money SET balance = '" + nbalance + "'WHERE p_name ='" + p.getName() + "'");

    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