Clean up code a bunch and fix the MySQL bug.

This commit is contained in:
AppleDash 2016-08-14 16:18:43 -04:00
parent 25c925dcb3
commit 2db1ac617b
16 changed files with 46 additions and 33 deletions

View File

@ -6,10 +6,10 @@
<parent>
<groupId>org.appledash</groupId>
<artifactId>SaneEconomy</artifactId>
<version>0.6.7-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
</parent>
<artifactId>SaneEconomyCore</artifactId>
<version>0.6.7-SNAPSHOT</version>
<version>0.7.0-SNAPSHOT</version>
<dependencies>
<dependency>

View File

@ -176,6 +176,6 @@ public class SaneEconomy extends JavaPlugin {
* @return Plugin logger.
*/
public static Logger logger(){
return getInstance().getLogger();
return instance.getLogger();
}
}

View File

@ -17,6 +17,6 @@ public class CommandException extends Exception {
* @return The right exception
*/
public static CommandException makeArgumentException(int expectedCount, int actualCount) {
return actualCount > expectedCount ? new TooManyArgumentsException() : new TooFewArgumentsException();
return (actualCount > expectedCount) ? new TooManyArgumentsException() : new TooFewArgumentsException();
}
}

View File

@ -69,7 +69,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, _("%s is not a positive number."), (amount == -1 ? sAmount : amount + ""));
MessageUtils.sendMessage(sender, _("%s is not a positive number."), ((amount == -1) ? sAmount : String.valueOf(amount)));
return;
}
@ -82,7 +82,9 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
ecoMan.getCurrency().formatAmount(newAmount)
);
return;
} else if (subCommand.equalsIgnoreCase("take")) {
}
if (subCommand.equalsIgnoreCase("take")) {
double newAmount = ecoMan.subtractBalance(economable, amount);
MessageUtils.sendMessage(sender, _("Took %s from %s. Their balance is now %s."),
@ -91,7 +93,9 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
ecoMan.getCurrency().formatAmount(newAmount)
);
return;
} else if (subCommand.equalsIgnoreCase("set")) {
}
if (subCommand.equalsIgnoreCase("set")) {
ecoMan.setBalance(economable, amount);
MessageUtils.sendMessage(sender, _("Balance for %s set to %s."), sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
return;

View File

@ -62,7 +62,7 @@ public class PayCommand extends SaneEconomyCommand {
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
if (amount <= 0) {
MessageUtils.sendMessage(sender, "%s is not a positive number.", (amount == -1 ? sAmount : amount + ""));
MessageUtils.sendMessage(sender, "%s is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
return;
}

View File

@ -15,7 +15,7 @@ import java.util.UUID;
*/
public abstract class EconomyStorageBackendCaching implements EconomyStorageBackend {
protected HashMap<String, Double> balances = new HashMap<>();
protected Map<UUID, Double> topPlayerBalances = new LinkedHashMap<>();
private Map<UUID, Double> topPlayerBalances = new LinkedHashMap<>();
@Override
public boolean accountExists(Economable economable) {

View File

@ -7,6 +7,7 @@ import org.bukkit.Bukkit;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* Created by AppleDash on 6/14/2016.
@ -38,8 +39,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
}
public boolean testConnection() {
try {
openConnection().close();
try (Connection conn = openConnection()) {
createTables();
return true;
} catch (Exception e) {
@ -96,7 +96,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
oldBalances.put(rs.getString("player_uuid"), rs.getDouble("balance"));
}
for (Map.Entry<String, Double> e : oldBalances.entrySet()) {
for (Entry<String, Double> e : oldBalances.entrySet()) {
ps = conn.prepareStatement("INSERT INTO `saneeconomy_balances` (unique_identifier, balance) VALUES (?, ?)");
ps.setString(1, "player:" + e.getKey());
ps.setDouble(2, e.getValue());
@ -148,6 +148,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
statement.setDouble(1, newBalance);
statement.setString(2, economable.getUniqueIdentifier());
statement.executeUpdate();
SaneEconomy.logger().info("Updated.");
} catch (SQLException e) {
/* Roll it back */
balances.put(economable.getUniqueIdentifier(), oldBalance);
@ -157,7 +158,9 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
}
private synchronized void ensureAccountExists(Economable economable, Connection conn) throws SQLException {
SaneEconomy.logger().info("Ensuring account exists.");
if (!accountExists(economable, conn)) {
SaneEconomy.logger().info("It didn't, creating it.");
PreparedStatement statement = conn.prepareStatement("INSERT INTO `saneeconomy_balances` (unique_identifier, balance) VALUES (?, 0.0)");
statement.setString(1, economable.getUniqueIdentifier());
statement.executeUpdate();
@ -165,7 +168,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
}
private synchronized boolean accountExists(Economable economable, Connection conn) throws SQLException {
PreparedStatement statement = conn.prepareStatement("SELECT COUNT(*) FROM `saneeconomy_balances` WHERE `unique_identifier` = ?");
PreparedStatement statement = conn.prepareStatement("SELECT 1 FROM `saneeconomy_balances` WHERE `unique_identifier` = ?");
statement.setString(1, economable.getUniqueIdentifier());
ResultSet rs = statement.executeQuery();

View File

@ -5,7 +5,7 @@ package org.appledash.saneeconomy.economy.economable;
* Blackjack is still best pony.
*/
public class EconomableFaction implements Economable {
private String factionUuid;
private final String factionUuid;
public EconomableFaction(String factionUuid) {
this.factionUuid = factionUuid;

View File

@ -5,7 +5,7 @@ package org.appledash.saneeconomy.economy.economable;
* Blackjack is still best pony.
*/
public class EconomableGeneric implements Economable {
private String uniqueIdentifier;
private final String uniqueIdentifier;
public EconomableGeneric(String uniqueIdentifier) {
this.uniqueIdentifier = uniqueIdentifier;

View File

@ -7,7 +7,7 @@ import org.bukkit.OfflinePlayer;
* Blackjack is still best pony.
*/
public class EconomablePlayer implements Economable {
private OfflinePlayer handle;
private final OfflinePlayer handle;
public EconomablePlayer(OfflinePlayer handle) {
this.handle = handle;
@ -15,6 +15,6 @@ public class EconomablePlayer implements Economable {
@Override
public String getUniqueIdentifier() {
return "player:" + handle.getUniqueId().toString();
return "player:" + handle.getUniqueId();
}
}

View File

@ -27,14 +27,14 @@ public class JoinQuitListener implements Listener {
double startBalance = plugin.getConfig().getDouble("economy.start-balance", 0.0D);
/* A starting balance is configured AND they haven't been given it yet. */
if (startBalance > 0 && !plugin.getEconomyManager().accountExists(economable)) {
if ((startBalance > 0) && !plugin.getEconomyManager().accountExists(economable)) {
plugin.getEconomyManager().setBalance(economable, startBalance);
MessageUtils.sendMessage(player, "You've been issued a starting balance of %s!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
}
/* Update notification */
if (player.hasPermission("saneeconomy.update-notify") && GithubVersionChecker.isUpdateAvailable()) {
MessageUtils.sendMessage(player, "An update is available! The current version is %s, but the newest available is %s. Please go to %s to update!", plugin.getDescription().getVersion(), GithubVersionChecker.getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL);
MessageUtils.sendMessage(player, "An update is available! The currently-installed version is %s, but the newest available is %s. Please go to %s to update!", plugin.getDescription().getVersion(), GithubVersionChecker.getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL);
}
}
}

View File

@ -14,8 +14,8 @@ import org.appledash.saneeconomy.utils.WebUtils;
public class GithubVersionChecker {
public static final String DOWNLOAD_URL = "https://github.com/AppleDash/SaneEconomy/releases";
private static final String RELEASES_URL = "https://api.github.com/repos/AppleDash/SaneEconomy/releases";
private static boolean updateChecked = false;
private static boolean updateAvailable = false;
private static boolean updateChecked;
private static boolean updateAvailable;
private static String newestVersion;
public static void checkUpdateAvailable() {
@ -30,6 +30,12 @@ public class GithubVersionChecker {
for (JsonElement elem : array) {
if (elem instanceof JsonObject) {
JsonObject releaseObj = (JsonObject)elem;
boolean isPrerelease = releaseObj.get("prerelease").getAsBoolean();
if (isPrerelease) { // Don't tell them to update to prereleases, which I might release for individual users to test.
continue;
}
String versionStr = releaseObj.get("tag_name").getAsString();
int version = releaseToInt(versionStr);

View File

@ -17,18 +17,14 @@ import java.util.Map;
* Blackjack is still best pony.
*/
public class I18n {
private static I18n INSTANCE = new I18n(SaneEconomy.getInstance());
private SaneEconomy plugin;
private Map<String, String> translations = new HashMap<>();
private static final I18n INSTANCE = new I18n(SaneEconomy.getInstance());
private final SaneEconomy plugin;
private final Map<String, String> translations = new HashMap<>();
public I18n(SaneEconomy plugin) {
private I18n(SaneEconomy plugin) {
this.plugin = plugin;
}
public String translate(String input) {
return translations.containsKey(input) ? translations.get(input) : input;
}
public void loadTranslations() {
File configFile = new File(plugin.getDataFolder(), "messages.yml");
YamlConfiguration configJar = YamlConfiguration.loadConfiguration(new InputStreamReader(this.getClass().getResourceAsStream("/messages.yml")));
@ -72,6 +68,10 @@ public class I18n {
});
}
private String translate(String input) {
return translations.containsKey(input) ? translations.get(input) : input;
}
public static String _(String s) {
return INSTANCE.translate(s);
}

View File

@ -21,7 +21,7 @@ public class PlayerUtils {
player = Bukkit.getServer().getOfflinePlayer(playerName);
}
if (player != null && !player.hasPlayedBefore()) {
if ((player != null) && !player.hasPlayedBefore()) {
return null;
}

View File

@ -10,8 +10,8 @@ import org.bukkit.plugin.ServicePriority;
* Blackjack is still best pony.
*/
public class VaultHook {
private SaneEconomy plugin;
private EconomySaneEconomy provider = new EconomySaneEconomy();
private final SaneEconomy plugin;
private final Economy provider = new EconomySaneEconomy();
public VaultHook(SaneEconomy plugin) {
this.plugin = plugin;

View File

@ -1,7 +1,7 @@
name: SaneEconomy
author: AppleDash
main: org.appledash.saneeconomy.SaneEconomy
version: 0.6.7
version: 0.7.0
softdepend: [Vault]
commands:
balance: