mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-29 05:26: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;
|
package org.appledash.saneeconomy;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.io.ByteArrayDataInput;
|
import com.google.common.io.ByteArrayDataInput;
|
||||||
import com.google.common.io.ByteArrayDataOutput;
|
import com.google.common.io.ByteArrayDataOutput;
|
||||||
@ -21,10 +22,7 @@ import org.bukkit.event.EventHandler;
|
|||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.logging.Logger;
|
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.");
|
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());
|
versionChecker = new GithubVersionChecker("SaneEconomyCore", this.getDescription().getVersion());
|
||||||
getServer().getScheduler().scheduleAsyncDelayedTask(this, versionChecker::checkUpdateAvailable);
|
getServer().getScheduler().scheduleAsyncDelayedTask(this, versionChecker::checkUpdateAvailable);
|
||||||
|
}
|
||||||
|
|
||||||
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
|
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
|
||||||
economyManager.getBackend().reloadTopPlayerBalances();
|
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)) {
|
if (this.getConfig().getBoolean("multi-server-sync", false)) {
|
||||||
this.getServer().getPluginManager().registerEvents(new Listener() {
|
this.getServer().getPluginManager().registerEvents(new Listener() {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onTransaction(SaneEconomyTransactionEvent evt) { // Trust me, I'm a doctor.
|
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);
|
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();
|
ByteArrayDataOutput bado = ByteStreams.newDataOutput();
|
||||||
bado.writeUTF("SaneEconomy");
|
bado.writeUTF("SaneEconomy");
|
||||||
bado.writeUTF("SyncDatabase");
|
bado.writeUTF("SyncPlayer");
|
||||||
|
bado.writeUTF(p.getUniqueId().toString());
|
||||||
fakeSender.sendPluginMessage(SaneEconomy.this, "BungeeCord", bado.toByteArray());
|
fakeSender.sendPluginMessage(SaneEconomy.this, "BungeeCord", bado.toByteArray());
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", (channel, player, bytes) -> {
|
this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", (channel, player, bytes) -> {
|
||||||
@ -110,8 +113,9 @@ public class SaneEconomy extends SanePlugin implements ISaneEconomy {
|
|||||||
if (subChannel.equals("SaneEconomy")) {
|
if (subChannel.equals("SaneEconomy")) {
|
||||||
String opCode = badi.readUTF();
|
String opCode = badi.readUTF();
|
||||||
|
|
||||||
if (opCode.equals("SyncDatabase")) {
|
if (opCode.equals("SyncPlayer")) {
|
||||||
SaneEconomy.this.getEconomyManager().getBackend().reloadDatabase();
|
String playerUuid = badi.readUTF();
|
||||||
|
SaneEconomy.this.getEconomyManager().getBackend().reloadEconomable(String.format("player:%s", playerUuid));
|
||||||
} else {
|
} else {
|
||||||
SaneEconomy.this.getLogger().warning("Invalid OpCode received on SaneEconomy plugin message channel: " + opCode);
|
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.
|
* 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.
|
* @return Map of player UUIDs to amounts.
|
||||||
*/
|
*/
|
||||||
LinkedHashMap<UUID, Double> getTopPlayerBalances();
|
LinkedHashMap<UUID, Double> getTopPlayerBalances();
|
||||||
@ -46,6 +45,12 @@ public interface EconomyStorageBackend {
|
|||||||
*/
|
*/
|
||||||
void reloadDatabase();
|
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.
|
* Reload this backend's top balances.
|
||||||
*/
|
*/
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.appledash.saneeconomy.economy.backend.type;
|
package org.appledash.saneeconomy.economy.backend.type;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.appledash.saneeconomy.SaneEconomy;
|
||||||
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
|
||||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||||
import org.appledash.saneeconomy.utils.MapUtil;
|
import org.appledash.saneeconomy.utils.MapUtil;
|
||||||
@ -54,4 +55,10 @@ public abstract class EconomyStorageBackendCaching implements EconomyStorageBack
|
|||||||
public Map<String, Double> getAllBalances() {
|
public Map<String, Double> getAllBalances() {
|
||||||
return ImmutableMap.copyOf(balances);
|
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() {
|
public void closeConnections() {
|
||||||
this.dbConn.getConnection().cleanup();
|
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 */
|
/* 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);
|
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
|
@EventHandler
|
||||||
public void onPlayerLogin(AsyncPlayerPreLoginEvent evt) {
|
public void onPlayerLogin(AsyncPlayerPreLoginEvent evt) {
|
||||||
Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, () -> {
|
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||||
plugin.getEconomyManager().getBackend().reloadDatabase(); // TODO: If servers start to lag when lots of people join, this is why.
|
plugin.getEconomyManager().getBackend().reloadEconomable(String.format("player:%s", evt.getUniqueId())); // TODO: If servers start to lag when lots of people join, this is why.
|
||||||
}, 0);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ economy:
|
|||||||
start-balance: 0.0
|
start-balance: 0.0
|
||||||
notify-start-balance: true
|
notify-start-balance: true
|
||||||
server-account: '$SERVER$'
|
server-account: '$SERVER$'
|
||||||
|
baltop-update-interval: 300
|
||||||
|
|
||||||
|
multi-server-sync: false
|
||||||
|
update-check: true
|
||||||
locale-override: false
|
locale-override: false
|
||||||
debug: false
|
debug: false
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
name: SaneEconomy
|
name: SaneEconomy
|
||||||
author: AppleDash
|
author: AppleDash
|
||||||
main: org.appledash.saneeconomy.SaneEconomy
|
main: org.appledash.saneeconomy.SaneEconomy
|
||||||
version: 0.13.0
|
version: ${project.version}
|
||||||
load: STARTUP
|
load: STARTUP
|
||||||
softdepend: [Vault]
|
softdepend: [Vault]
|
||||||
commands:
|
commands:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
name: SaneEconomyMobKills
|
name: SaneEconomyMobKills
|
||||||
description: A plugin to give players experience when they kill mobs.
|
description: A plugin to give players experience when they kill mobs.
|
||||||
version: 0.1.1
|
version: ${project.version}
|
||||||
author: AppleDash
|
author: AppleDash
|
||||||
main: org.appledash.saneeconomymobkills.SaneEconomyMobKills
|
main: org.appledash.saneeconomymobkills.SaneEconomyMobKills
|
||||||
depend: [SaneEconomy]
|
depend: [SaneEconomy]
|
||||||
|
@ -2,5 +2,5 @@ name: SaneEconomyOnlineTime
|
|||||||
main: org.appledash.saneeconomy.onlinetime.SaneEconomyOnlineTime
|
main: org.appledash.saneeconomy.onlinetime.SaneEconomyOnlineTime
|
||||||
description: Pays players for being online!
|
description: Pays players for being online!
|
||||||
author: AppleDash
|
author: AppleDash
|
||||||
version: 0.1.0
|
version: ${project.version}
|
||||||
depend: [SaneEconomy]
|
depend: [SaneEconomy]
|
@ -1,5 +1,5 @@
|
|||||||
name: SaneEconomySignShop
|
name: SaneEconomySignShop
|
||||||
main: org.appledash.saneeconomysignshop.SaneEconomySignShop
|
main: org.appledash.saneeconomysignshop.SaneEconomySignShop
|
||||||
author: AppleDash
|
author: AppleDash
|
||||||
version: 0.1.5
|
version: ${project.version}
|
||||||
depend: [SaneEconomy]
|
depend: [SaneEconomy]
|
||||||
|
Loading…
Reference in New Issue
Block a user