mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-22 10:05:16 +01:00
Implement some various code improvements - credit to @sgdc3 - see #58
This commit is contained in:
parent
be8206ce10
commit
f9e8a868e5
@ -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.");
|
||||
}
|
||||
|
||||
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<OfflinePlayer> 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);
|
||||
|
||||
if (fakeSender != null) {
|
||||
if (fakeSender == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
playersToSync.stream().filter(p -> p != null && !p.isOnline()).forEach(p -> {
|
||||
ByteArrayDataOutput bado = ByteStreams.newDataOutput();
|
||||
bado.writeUTF("SaneEconomy");
|
||||
bado.writeUTF("SyncDatabase");
|
||||
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);
|
||||
}
|
||||
|
@ -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<UUID, Double> 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.
|
||||
*/
|
||||
|
@ -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<String, Double> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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]
|
||||
|
@ -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]
|
@ -1,5 +1,5 @@
|
||||
name: SaneEconomySignShop
|
||||
main: org.appledash.saneeconomysignshop.SaneEconomySignShop
|
||||
author: AppleDash
|
||||
version: 0.1.5
|
||||
version: ${project.version}
|
||||
depend: [SaneEconomy]
|
||||
|
Loading…
Reference in New Issue
Block a user