!cashcode

Joined
Jul 13, 2008
Messages
419
Reaction score
217
Code:
if (command.equalsIgnoreCase("!cashcode")) {
            if (splitted.length < 2) {
                player.dropMessage(5, "The !cashcode command has the following syntax : !cashcode Amount");
            } else {
                int amount = Integer.parseInt(splitted[1]);

                if (amount < 1) {
                    player.dropMessage(5, "The amount of a Cash Code can not be smaller than 1.");
                } else if ((double) amount > Integer.MAX_VALUE) {
                    player.dropMessage(5, "The amount of a Cash Code can not be bigger than " + Integer.MAX_VALUE + ".");
                } else {
                    String code = createCashCode(amount);

                    if (code != null) {
                        player.dropMessage("You have created a Cash Code with an amount of " + amount + " : " + code + ".");
                    } else {
                        player.dropMessage(5, "An error occured while creating the Cash Code, please try again.");
                    }
                }
            }
        }
Code:
public String createCashCode(int amount) {
        String code = "";
        String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

        for (int i = 0; i < 7; i++) {
            code += letters[rand(0, 23)] + rand(1, 9);
        }

        try {
            Connection con = DatabaseConnection.getConnection();
            PreparedStatement ps = con.prepareStatement("SELECT * FROM nxcode WHERE code = ?");
            ps.setString(1, code);
            ResultSet rs = ps.executeQuery();

            if (!rs.next()) {
                rs.close();
                ps = con.prepareStatement("INSERT INTO nxcode(code, type, item) VALUES(?, ?, ?)");
                ps.setString(1, code);
                ps.setInt(2, 2); //Maple Points
                ps.setInt(3, amount);
                ps.executeUpdate();
                ps.close();
                
                return code;
            } else {
                return createCashCode(amount);
            }
        } catch (SQLException sqlE) {
            System.out.print(sqlE);
            return null;
        }
    }

private static int rand(int lbound, int ubound) {
        return (int) ((Math.random() * (ubound - lbound + 1)) + lbound);
    }
[/code]new CommandDefinition("cashcode", "Amount : Creates a Cash Code with the given amount of Maple Points.", 1),[/code]
 
Last edited:
Back