Search in sources :

Example 1 with SQLQuery

use of com.erigitic.sql.SQLQuery in project TotalEconomy by Erigitic.

the class TEJobManager method setJob.

/**
     * Set the users's job.
     *
     * @param user User object
     * @param jobName name of the job
     */
public boolean setJob(User user, String jobName) {
    UUID userUUID = user.getUniqueId();
    // Just in case the job name was not passed in as lowercase, make it lowercase
    jobName = jobName.toLowerCase();
    if (databaseActive) {
        SQLQuery sqlQuery = SQLQuery.builder(sqlHandler.dataSource).update("totaleconomy.accounts").set("job").equals(jobName).where("uid").equals(userUUID.toString()).build();
        if (sqlQuery.getRowsAffected() > 0) {
            return true;
        } else {
            logger.warn("[TE] An error occurred while changing the job of " + user.getUniqueId() + "/" + user.getName() + "!");
            return false;
        }
    } else {
        accountConfig.getNode(userUUID.toString(), "job").setValue(jobName);
        accountConfig.getNode(userUUID.toString(), "jobstats", jobName, "level").setValue(accountConfig.getNode(userUUID.toString(), "jobstats", jobName, "level").getInt(1));
        accountConfig.getNode(userUUID.toString(), "jobstats", jobName, "exp").setValue(accountConfig.getNode(userUUID.toString(), "jobstats", jobName, "exp").getInt(0));
        try {
            accountManager.getConfigManager().save(accountConfig);
        } catch (IOException e) {
            logger.warn("[TE] An error occurred while changing the job of " + user.getUniqueId() + "/" + user.getName() + "!");
        }
        return true;
    }
}
Also used : IOException(java.io.IOException) SQLQuery(com.erigitic.sql.SQLQuery)

Example 2 with SQLQuery

use of com.erigitic.sql.SQLQuery in project TotalEconomy by Erigitic.

the class TEJobManager method addExp.

/**
     * Add exp to player's current job
     *
     * @param player The player to give experience to
     * @param expAmount The amount of experience to add
     */
public void addExp(Player player, int expAmount) {
    String jobName = getPlayerJob(player);
    UUID playerUUID = player.getUniqueId();
    boolean jobNotifications = accountManager.getJobNotificationState(player);
    if (databaseActive) {
        int newExp = getJobExp(jobName, player) + expAmount;
        SQLQuery sqlQuery = SQLQuery.builder(sqlHandler.dataSource).update("totaleconomy.experience").set(jobName).equals(String.valueOf(newExp)).where("uid").equals(playerUUID.toString()).build();
        if (sqlQuery.getRowsAffected() > 0) {
            if (jobNotifications) {
                player.sendMessage(Text.of(TextColors.GRAY, "You have gained ", TextColors.GOLD, expAmount, TextColors.GRAY, " exp in the ", TextColors.GOLD, jobName, TextColors.GRAY, " job."));
            }
        } else {
            logger.warn("[TE] An error occurred while updating job experience in the database!");
            player.sendMessage(Text.of(TextColors.RED, "[TE] Error adding experience! Consult an administrator!"));
        }
    } else {
        int curExp = accountConfig.getNode(playerUUID.toString(), "jobstats", jobName, "exp").getInt();
        accountConfig.getNode(playerUUID.toString(), "jobstats", jobName, "exp").setValue(curExp + expAmount);
        if (jobNotifications)
            player.sendMessage(Text.of(TextColors.GRAY, "You have gained ", TextColors.GOLD, expAmount, TextColors.GRAY, " exp in the ", TextColors.GOLD, jobName, TextColors.GRAY, " job."));
        try {
            accountManager.getConfigManager().save(accountConfig);
        } catch (IOException e) {
            logger.warn("[TE] An error occurred while saving the account configuration file!");
        }
    }
}
Also used : IOException(java.io.IOException) SQLQuery(com.erigitic.sql.SQLQuery)

Example 3 with SQLQuery

use of com.erigitic.sql.SQLQuery in project TotalEconomy by Erigitic.

the class TEJobManager method getJobLevel.

/**
     * Get the players level for the passed in job
     *
     * @param jobName the name of the job
     * @param user the user object
     * @return int the job level
     */
public int getJobLevel(String jobName, User user) {
    UUID playerUUID = user.getUniqueId();
    // Just in case the job name was not passed in as lowercase, make it lowercase
    jobName = jobName.toLowerCase();
    if (!jobName.equals("unemployed")) {
        if (databaseActive) {
            SQLQuery sqlQuery = SQLQuery.builder(sqlHandler.dataSource).select(jobName).from("totaleconomy.levels").where("uid").equals(playerUUID.toString()).build();
            return sqlQuery.getInt(1);
        } else {
            return accountConfig.getNode(user.getUniqueId().toString(), "jobstats", jobName, "level").getInt(1);
        }
    }
    return 1;
}
Also used : SQLQuery(com.erigitic.sql.SQLQuery)

Example 4 with SQLQuery

use of com.erigitic.sql.SQLQuery in project TotalEconomy by Erigitic.

the class TEJobManager method getJobExp.

/**
     * Get the players exp for the passed in job.
     *
     * @param jobName the name of the job
     * @param user the user object
     * @return int the job exp
     */
public int getJobExp(String jobName, User user) {
    UUID playerUUID = user.getUniqueId();
    // Just in case the job name was not passed in as lowercase, make it lowercase
    jobName = jobName.toLowerCase();
    if (!jobName.equals("unemployed")) {
        if (databaseActive) {
            SQLQuery sqlQuery = SQLQuery.builder(sqlHandler.dataSource).select(jobName).from("totaleconomy.experience").where("uid").equals(playerUUID.toString()).build();
            return sqlQuery.getInt(0);
        } else {
            return accountConfig.getNode(playerUUID.toString(), "jobstats", jobName, "exp").getInt(0);
        }
    }
    return 0;
}
Also used : SQLQuery(com.erigitic.sql.SQLQuery)

Example 5 with SQLQuery

use of com.erigitic.sql.SQLQuery in project TotalEconomy by Erigitic.

the class AccountManager method toggleNotifications.

/**
     * Toggle a player's exp/money notifications for jobs
     *
     * @param player an object representing the player toggling notifications
     */
public void toggleNotifications(Player player) {
    boolean jobNotifications = !getJobNotificationState(player);
    UUID playerUUID = player.getUniqueId();
    if (databaseActive) {
        SQLQuery sqlQuery = SQLQuery.builder(sqlHandler.dataSource).update("totaleconomy.accounts").set("job_notifications").equals(jobNotifications ? "1" : "0").where("uid").equals(playerUUID.toString()).build();
        if (sqlQuery.getRowsAffected() <= 0) {
            player.sendMessage(Text.of(TextColors.RED, "[TE] Error toggling notifications! Try again. If this keeps showing up, notify the server owner or plugin developer."));
            logger.warn("[TE] An error occurred while updating the notification state in the database!");
        }
    } else {
        accountConfig.getNode(player.getUniqueId().toString(), "jobnotifications").setValue(jobNotifications);
        try {
            loader.save(accountConfig);
        } catch (IOException e) {
            player.sendMessage(Text.of(TextColors.RED, "[TE] Error toggling notifications! Try again. If this keeps showing up, notify the server owner or plugin developer."));
            logger.warn("[TE] An error occurred while updating the notification state!");
        }
    }
    if (jobNotifications == true) {
        player.sendMessage(Text.of(TextColors.GRAY, "Notifications are now ", TextColors.GREEN, "ON"));
    } else {
        player.sendMessage(Text.of(TextColors.GRAY, "Notifications are now ", TextColors.RED, "OFF"));
    }
}
Also used : IOException(java.io.IOException) UUID(java.util.UUID) SQLQuery(com.erigitic.sql.SQLQuery)

Aggregations

SQLQuery (com.erigitic.sql.SQLQuery)7 IOException (java.io.IOException)3 TransactionResult (org.spongepowered.api.service.economy.transaction.TransactionResult)2 UUID (java.util.UUID)1