From f9e8a868e516a614d316b7cb5823aee18ef3d751 Mon Sep 17 00:00:00 2001 From: AppleDash Date: Sat, 23 Sep 2017 07:24:28 -0400 Subject: [PATCH] Implement some various code improvements - credit to @sgdc3 - see #58 --- .../appledash/saneeconomy/SaneEconomy.java | 40 ++++++++++--------- .../backend/EconomyStorageBackend.java | 7 +++- .../type/EconomyStorageBackendCaching.java | 7 ++++ .../type/EconomyStorageBackendMySQL.java | 18 +++++++++ .../listeners/JoinQuitListener.java | 8 ++-- SaneEconomyCore/src/main/resources/config.yml | 3 ++ SaneEconomyCore/src/main/resources/plugin.yml | 2 +- .../src/main/resources/plugin.yml | 2 +- .../src/main/resources/plugin.yml | 2 +- .../src/main/resources/plugin.yml | 2 +- pom.xml | 6 +++ 11 files changed, 70 insertions(+), 27 deletions(-) diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java index fc36e82..d26bf9c 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/SaneEconomy.java @@ -1,5 +1,6 @@ package org.appledash.saneeconomy; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; @@ -21,10 +22,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import java.io.File; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.logging.Logger; /** @@ -74,29 +72,34 @@ public class SaneEconomy extends SanePlugin implements ISaneEconomy { getLogger().info("Not hooking into Vault because it isn't loaded."); } - versionChecker = new GithubVersionChecker("SaneEconomyCore", this.getDescription().getVersion()); - getServer().getScheduler().scheduleAsyncDelayedTask(this, versionChecker::checkUpdateAvailable); + if (this.getConfig().getBoolean("update-check", true)) { + versionChecker = new GithubVersionChecker("SaneEconomyCore", this.getDescription().getVersion()); + getServer().getScheduler().scheduleAsyncDelayedTask(this, versionChecker::checkUpdateAvailable); + } getServer().getScheduler().runTaskTimerAsynchronously(this, () -> { economyManager.getBackend().reloadTopPlayerBalances(); - }, 0, (20 * 300) /* Update baltop every 5 minutes */); + }, 0, (20 * this.getConfig().getInt("economy.baltop-update-interval", 300)) /* Update baltop every 5 minutes by default */); if (this.getConfig().getBoolean("multi-server-sync", false)) { this.getServer().getPluginManager().registerEvents(new Listener() { @EventHandler public void onTransaction(SaneEconomyTransactionEvent evt) { // Trust me, I'm a doctor. - OfflinePlayer player = evt.getTransaction().getReceiver().tryCastToPlayer(); + Set playersToSync = ImmutableSet.of(evt.getTransaction().getSender().tryCastToPlayer(), evt.getTransaction().getReceiver().tryCastToPlayer()); - if (player != null && !player.isOnline()) { - Player fakeSender = Iterables.getFirst(SaneEconomy.this.getServer().getOnlinePlayers(), null); + Player fakeSender = Iterables.getFirst(SaneEconomy.this.getServer().getOnlinePlayers(), null); - if (fakeSender != null) { - ByteArrayDataOutput bado = ByteStreams.newDataOutput(); - bado.writeUTF("SaneEconomy"); - bado.writeUTF("SyncDatabase"); - fakeSender.sendPluginMessage(SaneEconomy.this, "BungeeCord", bado.toByteArray()); - } + if (fakeSender == null) { + return; } + + playersToSync.stream().filter(p -> p != null && !p.isOnline()).forEach(p -> { + ByteArrayDataOutput bado = ByteStreams.newDataOutput(); + bado.writeUTF("SaneEconomy"); + bado.writeUTF("SyncPlayer"); + bado.writeUTF(p.getUniqueId().toString()); + fakeSender.sendPluginMessage(SaneEconomy.this, "BungeeCord", bado.toByteArray()); + }); } }, this); this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", (channel, player, bytes) -> { @@ -110,8 +113,9 @@ public class SaneEconomy extends SanePlugin implements ISaneEconomy { if (subChannel.equals("SaneEconomy")) { String opCode = badi.readUTF(); - if (opCode.equals("SyncDatabase")) { - SaneEconomy.this.getEconomyManager().getBackend().reloadDatabase(); + if (opCode.equals("SyncPlayer")) { + String playerUuid = badi.readUTF(); + SaneEconomy.this.getEconomyManager().getBackend().reloadEconomable(String.format("player:%s", playerUuid)); } else { SaneEconomy.this.getLogger().warning("Invalid OpCode received on SaneEconomy plugin message channel: " + opCode); } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java index bbf79cb..6d28c0b 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/EconomyStorageBackend.java @@ -36,7 +36,6 @@ public interface EconomyStorageBackend { /** * Get the UUIDs of the players who have the most money, along with how much money they have. - * @param amount Maximum number to get. * @return Map of player UUIDs to amounts. */ LinkedHashMap getTopPlayerBalances(); @@ -46,6 +45,12 @@ public interface EconomyStorageBackend { */ void reloadDatabase(); + /** + * Reload data for just the Economable with the given unique identifier. + * @param uniqueIdentifier Unique identifier of Economable to reload data for. + */ + void reloadEconomable(String uniqueIdentifier); + /** * Reload this backend's top balances. */ diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java index df78d26..20724cf 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendCaching.java @@ -1,6 +1,7 @@ package org.appledash.saneeconomy.economy.backend.type; import com.google.common.collect.ImmutableMap; +import org.appledash.saneeconomy.SaneEconomy; import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend; import org.appledash.saneeconomy.economy.economable.Economable; import org.appledash.saneeconomy.utils.MapUtil; @@ -54,4 +55,10 @@ public abstract class EconomyStorageBackendCaching implements EconomyStorageBack public Map getAllBalances() { return ImmutableMap.copyOf(balances); } + + @Override + public void reloadEconomable(String uniqueIdentifier) { + SaneEconomy.logger().warning("Trying to reload a single Economable from backend which does not support this - " + this.getClass().getSimpleName() + ". Recommend switching to MySQL backend for multi-server support."); + this.reloadDatabase(); + } } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java index 66899c9..a866b2f 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/economy/backend/type/EconomyStorageBackendMySQL.java @@ -140,4 +140,22 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching { public void closeConnections() { this.dbConn.getConnection().cleanup(); } + + + @Override + public void reloadEconomable(String uniqueIdentifier) { + dbConn.executeAsyncOperation("reload_economable_" + uniqueIdentifier, (conn) -> { + try { + PreparedStatement ps = conn.prepareStatement(String.format("SELECT balance FROM `%s` WHERE `unique_identifier` = ?", dbConn.getTable("saneeconomy_balances"))); + ps.setString(1, uniqueIdentifier); + ResultSet rs = ps.executeQuery(); + + if (rs.next()) { + this.balances.put(uniqueIdentifier, rs.getDouble("balance")); + } + } catch (SQLException e) { + throw new RuntimeException("SQL error has occured", e); + } + }); + } } diff --git a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java index 39133d6..b012e26 100644 --- a/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java +++ b/SaneEconomyCore/src/main/java/org/appledash/saneeconomy/listeners/JoinQuitListener.java @@ -41,15 +41,15 @@ public class JoinQuitListener implements Listener { } /* Update notification */ - if (player.hasPermission("saneeconomy.update-notify") && plugin.getVersionChecker().isUpdateAvailable()) { + if (plugin.getVersionChecker() != null && player.hasPermission("saneeconomy.update-notify") && plugin.getVersionChecker().isUpdateAvailable()) { this.plugin.getMessenger().sendMessage(player, "An update is available! The currently-installed version is {1}, but the newest available is {2}. Please go to {3} to update!", plugin.getDescription().getVersion(), plugin.getVersionChecker().getNewestVersion(), GithubVersionChecker.DOWNLOAD_URL); } } @EventHandler public void onPlayerLogin(AsyncPlayerPreLoginEvent evt) { - Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, () -> { - plugin.getEconomyManager().getBackend().reloadDatabase(); // TODO: If servers start to lag when lots of people join, this is why. - }, 0); + Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { + plugin.getEconomyManager().getBackend().reloadEconomable(String.format("player:%s", evt.getUniqueId())); // TODO: If servers start to lag when lots of people join, this is why. + }); } } diff --git a/SaneEconomyCore/src/main/resources/config.yml b/SaneEconomyCore/src/main/resources/config.yml index d48a882..fb39fdf 100644 --- a/SaneEconomyCore/src/main/resources/config.yml +++ b/SaneEconomyCore/src/main/resources/config.yml @@ -17,6 +17,9 @@ economy: start-balance: 0.0 notify-start-balance: true server-account: '$SERVER$' + baltop-update-interval: 300 +multi-server-sync: false +update-check: true locale-override: false debug: false diff --git a/SaneEconomyCore/src/main/resources/plugin.yml b/SaneEconomyCore/src/main/resources/plugin.yml index 74c7164..9397ddb 100644 --- a/SaneEconomyCore/src/main/resources/plugin.yml +++ b/SaneEconomyCore/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: SaneEconomy author: AppleDash main: org.appledash.saneeconomy.SaneEconomy -version: 0.13.0 +version: ${project.version} load: STARTUP softdepend: [Vault] commands: diff --git a/SaneEconomyMobKills/src/main/resources/plugin.yml b/SaneEconomyMobKills/src/main/resources/plugin.yml index f2682ce..3fece31 100644 --- a/SaneEconomyMobKills/src/main/resources/plugin.yml +++ b/SaneEconomyMobKills/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: SaneEconomyMobKills description: A plugin to give players experience when they kill mobs. -version: 0.1.1 +version: ${project.version} author: AppleDash main: org.appledash.saneeconomymobkills.SaneEconomyMobKills depend: [SaneEconomy] diff --git a/SaneEconomyOnlineTime/src/main/resources/plugin.yml b/SaneEconomyOnlineTime/src/main/resources/plugin.yml index 3712c60..79ac147 100644 --- a/SaneEconomyOnlineTime/src/main/resources/plugin.yml +++ b/SaneEconomyOnlineTime/src/main/resources/plugin.yml @@ -2,5 +2,5 @@ name: SaneEconomyOnlineTime main: org.appledash.saneeconomy.onlinetime.SaneEconomyOnlineTime description: Pays players for being online! author: AppleDash -version: 0.1.0 +version: ${project.version} depend: [SaneEconomy] \ No newline at end of file diff --git a/SaneEconomySignShop/src/main/resources/plugin.yml b/SaneEconomySignShop/src/main/resources/plugin.yml index 2f55f19..0267c67 100644 --- a/SaneEconomySignShop/src/main/resources/plugin.yml +++ b/SaneEconomySignShop/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SaneEconomySignShop main: org.appledash.saneeconomysignshop.SaneEconomySignShop author: AppleDash -version: 0.1.5 +version: ${project.version} depend: [SaneEconomy] diff --git a/pom.xml b/pom.xml index 4b3b8b8..5fb4e52 100644 --- a/pom.xml +++ b/pom.xml @@ -62,5 +62,11 @@ + + + src/main/resources + true + +