/** * Retrieves karma value of a player from the database. Forces player to be added to database if * they don't exist * * @param Player name * @return karma value associated with name */ public static int getPlayerKarma(String name) throws SQLException { int karma = RootConfig.getInt(ConfigNode.KARMA_PLAYER_DEFAULT); if (RootConfig.getBoolean(ConfigNode.KARMA_ECONOMY)) { double balance = plugin.getEconomy().getBalance(name); karma = (int) balance; } else { boolean has = false; String query = "SELECT * FROM " + Table.PLAYERS.getName() + " WHERE playername='" + name + "';"; final Query rs = plugin.getDatabaseHandler().select(query); // Retrieve karma from database try { if (rs.getResult().next()) { do { // Grab player karma value karma = rs.getResult().getInt("karma"); has = true; } while (rs.getResult().next()); } rs.closeQuery(); if (!has) { if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) { plugin.getLogger().info("getPlayerKarma(" + name + ") not in database, adding"); } // Player not in database, therefore add them query = "INSERT INTO " + Table.PLAYERS.getName() + " (playername,karma) VALUES ('" + name + "','" + karma + "');"; plugin.getDatabaseHandler().standardQuery(query); } } catch (SQLException e) { if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) { plugin.getLogger().warning("SQLException on getPlayerKarma(" + name + ")"); } throw e; } } return karma; }
/** * Updates the player's karma * * @param Name of player * @param Amount of karma to add * @param Boolean if to add to current karma, or to set it as given value * @throws SQLException */ public static void updatePlayerKarma(String name, int k) throws SQLException { if (RootConfig.getBoolean(ConfigNode.KARMA_ECONOMY)) { EconomyResponse response = null; if (k < 0) { // negative, take away response = plugin.getEconomy().withdrawPlayer(name, (k * -1)); } else { // give response = plugin.getEconomy().depositPlayer(name, k); } if (response == null) { return; } switch (response.type) { case FAILURE: { try { plugin .getServer() .getPlayer(name) .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!"); } catch (NullPointerException npe) { // IGNORE } if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin .getLogger() .warning( "Failed economy transaction on updatePlayerKarma(" + name + ", " + k + ") : " + response.errorMessage); } break; } case NOT_IMPLEMENTED: { try { plugin .getServer() .getPlayer(name) .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!"); } catch (NullPointerException npe) { // IGNORE } if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin .getLogger() .warning( "Unimplemented economy transaction on updatePlayerKarma(" + name + ", " + k + ") for eco " + plugin.getEconomy().getName() + " : " + response.errorMessage); } break; } case SUCCESS: { if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin .getLogger() .info( "Successful economy transaction on updatePlayerKarma(" + name + ", " + k + ")"); } break; } default: { if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin.getLogger().warning("Unknown response type:" + response.type.toString()); } break; } } } else { try { // Retrieve karma from database int karma = getPlayerKarma(name); // Add to existing value karma += k; String query; // Check updated karma value to limits in config if (karma <= RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT)) { // Updated karma value is beyond lower limit, so set to min query = "UPDATE " + Table.PLAYERS.getName() + " SET karma='" + RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT) + "' WHERE playername='" + name + "';"; } else if (karma >= RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT)) { // Updated karma value is beyond upper limit, so set to max query = "UPDATE " + Table.PLAYERS.getName() + " SET karma='" + RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT) + "' WHERE playername='" + name + "';"; } else { // Updated karma value is within acceptable range query = "UPDATE " + Table.PLAYERS.getName() + " SET karma='" + karma + "' WHERE playername='" + name + "';"; } plugin.getDatabaseHandler().standardQuery(query); } catch (SQLException e) { if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) { plugin.getLogger().warning("SQLException on updatePlayerKarma(" + name + "," + k + ")"); } throw e; } } }
public static void setPlayerKarma(String name, int karma) { if (RootConfig.getBoolean(ConfigNode.KARMA_ECONOMY)) { EconomyResponse response = null; try { int balance = getPlayerKarma(name); if (balance > karma) { // Give difference response = plugin.getEconomy().depositPlayer(name, (balance - karma)); } else { // Take difference response = plugin.getEconomy().withdrawPlayer(name, (karma - balance)); } } catch (SQLException e) { // Won't happen since we're using economy } if (response == null) { return; } switch (response.type) { case FAILURE: { try { plugin .getServer() .getPlayer(name) .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!"); } catch (NullPointerException npe) { // IGNORE } if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin .getLogger() .warning( "Failed economy transaction on setPlayerKarma(" + name + ", " + karma + ") : " + response.errorMessage); } break; } case NOT_IMPLEMENTED: { try { plugin .getServer() .getPlayer(name) .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!"); } catch (NullPointerException npe) { // IGNORE } if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin .getLogger() .warning( "Unimplemented economy transaction on setPlayerKarma(" + name + ", " + karma + ") for eco " + plugin.getEconomy().getName() + " : " + response.errorMessage); } break; } case SUCCESS: { if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin .getLogger() .info( "Successful economy transaction on setPlayerKarma(" + name + ", " + karma + ")"); } break; } default: { if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) { plugin.getLogger().warning("Unknown response type:" + response.type.toString()); } break; } } } else { String query; // Check updated karma value to limits in config if (karma <= RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT)) { // Updated karma value is beyond lower limit, so set to min query = "UPDATE " + Table.PLAYERS.getName() + " SET karma='" + RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT) + "' WHERE playername='" + name + "';"; } else if (karma >= RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT)) { // Updated karma value is beyond upper limit, so set to max query = "UPDATE " + Table.PLAYERS.getName() + " SET karma='" + RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT) + "' WHERE playername='" + name + "';"; } else { // Updated karma value is within acceptable range query = "UPDATE " + Table.PLAYERS.getName() + " SET karma='" + karma + "' WHERE playername='" + name + "';"; } plugin.getDatabaseHandler().standardQuery(query); } }