mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2025-02-16 19:51:23 +01:00
Major overhaul - now using SaneLib!
This commit is contained in:
parent
3678f71dc7
commit
0506af01cc
@ -6,10 +6,10 @@
|
||||
<parent>
|
||||
<groupId>org.appledash</groupId>
|
||||
<artifactId>SaneEconomy</artifactId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>SaneEconomyCore</artifactId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -18,6 +18,11 @@
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>6.0.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -37,6 +42,48 @@
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<artifactSet>
|
||||
<includes>
|
||||
<include>org.appledash:sanelib</include>
|
||||
<include>mysql:mysql-connector-java</include>
|
||||
<include>com.zaxxer:HikariCP</include>
|
||||
<include>org.slf4j:slf4j-api</include>
|
||||
<include>org.slf4j:slf4j-nop</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<relocations>
|
||||
<relocation>
|
||||
<pattern>org.appledash.sanelib</pattern>
|
||||
<shadedPattern>org.appledash.saneeconomy.shaded.sanelib</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>com.mysql</pattern>
|
||||
<shadedPattern>org.appledash.saneeconomy.shaded.mysql</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>com.zaxxer.hikari</pattern>
|
||||
<shadedPattern>org.appledash.saneeconomy.shaded.hikari</shadedPattern>
|
||||
</relocation>
|
||||
<relocation>
|
||||
<pattern>org.slf4j</pattern>
|
||||
<shadedPattern>org.appledash.saneeconomy.shaded.slf4j</shadedPattern>
|
||||
</relocation>
|
||||
</relocations>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
|
@ -3,13 +3,13 @@ package org.appledash.saneeconomy;
|
||||
import org.appledash.saneeconomy.command.SaneEconomyCommand;
|
||||
import org.appledash.saneeconomy.command.type.*;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL;
|
||||
import org.appledash.saneeconomy.economy.logger.TransactionLogger;
|
||||
import org.appledash.saneeconomy.listeners.JoinQuitListener;
|
||||
import org.appledash.saneeconomy.updates.GithubVersionChecker;
|
||||
import org.appledash.saneeconomy.utils.I18n;
|
||||
import org.appledash.saneeconomy.utils.SaneEconomyConfiguration;
|
||||
import org.appledash.saneeconomy.vault.VaultHook;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.appledash.sanelib.SanePlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
@ -21,7 +21,7 @@ import java.util.logging.Logger;
|
||||
* Created by AppleDash on 6/13/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
||||
public class SaneEconomy extends SanePlugin implements ISaneEconomy {
|
||||
private static SaneEconomy instance;
|
||||
private EconomyManager economyManager;
|
||||
private VaultHook vaultHook;
|
||||
@ -42,6 +42,8 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
super.onEnable();
|
||||
|
||||
if (!loadConfig()) { /* Invalid backend type or connection error of some sort */
|
||||
shutdown();
|
||||
return;
|
||||
@ -64,7 +66,7 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
||||
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
|
||||
economyManager.getBackend().reloadTopPlayerBalances();
|
||||
}, 0, (20 * 300) /* Update baltop every 5 minutes */);
|
||||
I18n.getInstance().loadTranslations();
|
||||
this.getI18n().loadTranslations();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -77,6 +79,9 @@ public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
|
||||
if (economyManager != null) {
|
||||
getLogger().info("Flushing database...");
|
||||
economyManager.getBackend().waitUntilFlushed();
|
||||
if (economyManager.getBackend() instanceof EconomyStorageBackendMySQL) {
|
||||
((EconomyStorageBackendMySQL) economyManager.getBackend()).closeConnections();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,11 +4,9 @@ import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.command.exception.CommandException;
|
||||
import org.appledash.saneeconomy.command.exception.type.NoPermissionException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.UsageException;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import static org.appledash.saneeconomy.utils.I18n._;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/13/2016.
|
||||
@ -33,14 +31,14 @@ public abstract class SaneEconomyCommand implements CommandExecutor {
|
||||
onCommand(sender, args);
|
||||
} catch (UsageException e) {
|
||||
/* Invalid usage in some way, print out exactly what went wrong along with the proper usage. */
|
||||
MessageUtils.sendMessage(sender, e.getMessage());
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, e.getMessage());
|
||||
|
||||
|
||||
for (String s : getUsage()) {
|
||||
MessageUtils.sendMessage(sender, _("Usage: {1}"), _(s).replace("<command>", label));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Usage: {1}", this.saneEconomy.getI18n().translate(s.replace("<command>", label)));
|
||||
}
|
||||
} catch (CommandException e) {
|
||||
MessageUtils.sendMessage(sender, e.getMessage());
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, e.getMessage());
|
||||
}
|
||||
});
|
||||
return true;
|
||||
|
@ -5,7 +5,6 @@ import org.appledash.saneeconomy.command.SaneEconomyCommand;
|
||||
import org.appledash.saneeconomy.command.exception.CommandException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerException;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.PlayerUtils;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -51,7 +50,7 @@ public class BalanceCommand extends SaneEconomyCommand {
|
||||
playerName = args[0];
|
||||
|
||||
if (!sender.hasPermission("saneeconomy.balance.other")) {
|
||||
MessageUtils.sendMessage(sender, "You don't have permission to check the balance of {1}.", playerIdentifier);
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "You don't have permission to check the balance of {1}.", playerIdentifier);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -59,14 +58,14 @@ public class BalanceCommand extends SaneEconomyCommand {
|
||||
OfflinePlayer player = PlayerUtils.getOfflinePlayer(playerIdentifier);
|
||||
|
||||
if (player == null) {
|
||||
MessageUtils.sendMessage(sender, "That player does not exist.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "That player does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (sender == player) {
|
||||
MessageUtils.sendMessage(sender, "Your balance is {1}.", saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player)));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Your balance is {1}.", saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player)));
|
||||
} else {
|
||||
MessageUtils.sendMessage(sender, "Balance for {1} is {2}.", playerName, saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player)));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Balance for {1} is {2}.", playerName, saneEconomy.getEconomyManager().getFormattedBalance(Economable.wrap(player)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.command.SaneEconomyCommand;
|
||||
import org.appledash.saneeconomy.command.exception.CommandException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.TooManyArgumentsException;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@ -46,7 +45,7 @@ public class BalanceTopCommand extends SaneEconomyCommand {
|
||||
try {
|
||||
page = Math.abs(Integer.parseInt(args[0]));
|
||||
} catch (NumberFormatException e) {
|
||||
MessageUtils.sendMessage(sender, "{1} is not a valid number.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "{1} is not a valid number.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -56,13 +55,13 @@ public class BalanceTopCommand extends SaneEconomyCommand {
|
||||
Map<OfflinePlayer, Double> topBalances = saneEconomy.getEconomyManager().getTopPlayerBalances(nPerPage, offset);
|
||||
|
||||
if (topBalances.isEmpty()) {
|
||||
MessageUtils.sendMessage(sender, "There aren't enough players to display that page.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "There aren't enough players to display that page.");
|
||||
return;
|
||||
}
|
||||
|
||||
AtomicInteger index = new AtomicInteger(offset + 1); /* I know it's stupid, but you can't do some_int++ from within the lambda. */
|
||||
|
||||
MessageUtils.sendMessage(sender, "Top {1} players on page {2}:", topBalances.size(), page);
|
||||
topBalances.forEach((player, balance) -> MessageUtils.sendMessage(sender, "[{1:02d}] {2} - {3}", index.getAndIncrement(), player.getName(), SaneEconomy.getInstance().getEconomyManager().getCurrency().formatAmount(balance)));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Top {1} players on page {2}:", topBalances.size(), page);
|
||||
topBalances.forEach((player, balance) -> this.saneEconomy.getMessenger().sendMessage(sender, "[{1:02d}] {2} - {3}", index.getAndIncrement(), player.getName(), SaneEconomy.getInstance().getEconomyManager().getCurrency().formatAmount(balance)));
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.appledash.saneeconomy.utils.PlayerUtils;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -64,7 +63,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
OfflinePlayer targetPlayer = PlayerUtils.getOfflinePlayer(sTargetPlayer);
|
||||
|
||||
if (targetPlayer == null) {
|
||||
MessageUtils.sendMessage(sender, "That player does not exist.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "That player does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -74,7 +73,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
|
||||
|
||||
if (amount <= 0) {
|
||||
MessageUtils.sendMessage(sender, "{1} is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "{1} is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -84,7 +83,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
|
||||
double newAmount = result.getToBalance();
|
||||
|
||||
MessageUtils.sendMessage(sender, "Added {1} to {2}. Their balance is now {3}.",
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Added {1} to {2}. Their balance is now {3}.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sTargetPlayer,
|
||||
ecoMan.getCurrency().formatAmount(newAmount)
|
||||
@ -98,7 +97,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
|
||||
double newAmount = result.getFromBalance();
|
||||
|
||||
MessageUtils.sendMessage(sender, "Took {1} from {2}. Their balance is now {3}.",
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Took {1} from {2}. Their balance is now {3}.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sTargetPlayer,
|
||||
ecoMan.getCurrency().formatAmount(newAmount)
|
||||
@ -109,7 +108,7 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
if (subCommand.equalsIgnoreCase("set")) {
|
||||
double oldBal = ecoMan.getBalance(economable);
|
||||
ecoMan.setBalance(economable, amount);
|
||||
MessageUtils.sendMessage(sender, "Balance for {1} set to {2}.", sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Balance for {1} set to {2}.", sTargetPlayer, ecoMan.getCurrency().formatAmount(amount));
|
||||
|
||||
saneEconomy.getTransactionLogger().ifPresent((logger) -> {
|
||||
// FIXME: This is a silly hack to get it to log.
|
||||
|
@ -9,7 +9,6 @@ import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.appledash.saneeconomy.utils.PlayerUtils;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -57,12 +56,12 @@ public class PayCommand extends SaneEconomyCommand {
|
||||
OfflinePlayer toPlayer = PlayerUtils.getOfflinePlayer(sToPlayer);
|
||||
|
||||
if (toPlayer == null) {
|
||||
MessageUtils.sendMessage(sender, "That player does not exist or has never played before.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "That player does not exist or has never played before.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (toPlayer.getUniqueId().equals(fromPlayer.getUniqueId())) {
|
||||
MessageUtils.sendMessage(sender, "You cannot pay yourself.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "You cannot pay yourself.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -70,7 +69,7 @@ public class PayCommand extends SaneEconomyCommand {
|
||||
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
|
||||
|
||||
if (amount <= 0) {
|
||||
MessageUtils.sendMessage(sender, "{1} is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "{1} is not a positive number.", ((amount == -1) ? sAmount : String.valueOf(amount)));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -79,7 +78,7 @@ public class PayCommand extends SaneEconomyCommand {
|
||||
TransactionResult result = ecoMan.transact(transaction);
|
||||
|
||||
if (result.getStatus() != TransactionResult.Status.SUCCESS) {
|
||||
MessageUtils.sendMessage(sender, "You do not have enough money to transfer {1} to {2}.",
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "You do not have enough money to transfer {1} to {2}.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sToPlayer
|
||||
);
|
||||
@ -89,13 +88,13 @@ public class PayCommand extends SaneEconomyCommand {
|
||||
|
||||
/* Inform the relevant parties. */
|
||||
|
||||
MessageUtils.sendMessage(sender, "You have transferred {1} to {2}.",
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "You have transferred {1} to {2}.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
sToPlayer
|
||||
);
|
||||
|
||||
if (toPlayer.isOnline()) {
|
||||
MessageUtils.sendMessage(((CommandSender) toPlayer), "You have received {1} from {2}.",
|
||||
this.saneEconomy.getMessenger().sendMessage(((CommandSender) toPlayer), "You have received {1} from {2}.",
|
||||
ecoMan.getCurrency().formatAmount(amount),
|
||||
fromPlayer.getDisplayName()
|
||||
);
|
||||
|
@ -4,7 +4,6 @@ import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.command.SaneEconomyCommand;
|
||||
import org.appledash.saneeconomy.command.exception.CommandException;
|
||||
import org.appledash.saneeconomy.command.exception.type.usage.InvalidUsageException;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/**
|
||||
@ -37,9 +36,9 @@ public class SaneEcoCommand extends SaneEconomyCommand {
|
||||
String subCommand = args[0];
|
||||
|
||||
if (subCommand.equalsIgnoreCase("reload-database")) {
|
||||
MessageUtils.sendMessage(sender, "Reloading database...");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Reloading database...");
|
||||
saneEconomy.getEconomyManager().getBackend().reloadDatabase();
|
||||
MessageUtils.sendMessage(sender, "Database reloaded.");
|
||||
this.saneEconomy.getMessenger().sendMessage(sender, "Database reloaded.");
|
||||
} else {
|
||||
throw new InvalidUsageException();
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package org.appledash.saneeconomy.economy;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.sanelib.messages.MessageUtils;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
@ -5,7 +5,6 @@ import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -67,7 +66,7 @@ public class EconomyStorageBackendFlatfile extends EconomyStorageBackendCaching
|
||||
ois.readInt(); // We already know it's 1.
|
||||
|
||||
|
||||
Map<UUID, Double> oldBalances = (HashMap<UUID, Double>)ois.readObject();
|
||||
Map<UUID, Double> oldBalances = (Map<UUID, Double>) ois.readObject();
|
||||
oldBalances.forEach((uuid, balance) -> balances.put("player:" + uuid, balance));
|
||||
|
||||
ois.close();
|
||||
|
@ -1,17 +1,13 @@
|
||||
package org.appledash.saneeconomy.economy.backend.type;
|
||||
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.utils.database.DatabaseCredentials;
|
||||
import org.appledash.saneeconomy.utils.database.MySQLConnection;
|
||||
import org.appledash.sanelib.database.DatabaseCredentials;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -30,15 +26,12 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
||||
this.dbConn = new MySQLConnection(dbCredentials);
|
||||
}
|
||||
|
||||
private void createTables() {
|
||||
private void createTables() {
|
||||
try (Connection conn = dbConn.openConnection()) {
|
||||
int schemaVersion;
|
||||
|
||||
if (!checkTableExists(dbConn.getTable("saneeconomy_schema"))) {
|
||||
if (checkTableExists(dbConn.getTable("player_balances"))) {
|
||||
schemaVersion = 1;
|
||||
} else {
|
||||
schemaVersion = 0;
|
||||
}
|
||||
schemaVersion = -1;
|
||||
} else {
|
||||
PreparedStatement ps = conn.prepareStatement(String.format("SELECT `val` FROM `%s` WHERE `key` = 'schema_version'", dbConn.getTable("saneeconomy_schema")));
|
||||
ps.executeQuery();
|
||||
@ -51,43 +44,21 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
||||
schemaVersion = Integer.valueOf(rs.getString("val"));
|
||||
}
|
||||
|
||||
if (schemaVersion < 2) {
|
||||
if (schemaVersion < 1) {
|
||||
PreparedStatement ps = conn.prepareStatement(String.format("CREATE TABLE IF NOT EXISTS `%s` (player_uuid CHAR(36), balance DECIMAL(18, 2))", dbConn.getTable("player_balances")));
|
||||
ps.executeUpdate();
|
||||
}
|
||||
if (schemaVersion == -1) {
|
||||
conn.prepareStatement(String.format("CREATE TABLE IF NOT EXISTS `%s` (`key` VARCHAR(32) PRIMARY KEY, `val` TEXT)", dbConn.getTable("saneeconomy_schema"))).executeUpdate();
|
||||
upgradeSchema1To2(conn);
|
||||
conn.prepareStatement(String.format("REPLACE INTO %s (`key`, `val`) VALUES ('schema_version', 2)", dbConn.getTable("saneeconomy_schema")));
|
||||
conn.prepareStatement(String.format("CREATE TABLE `%s` (unique_identifier VARCHAR(128) PRIMARY KEY, balance DECIMAL(18, 2))", dbConn.getTable("saneeconomy_balances"))).executeUpdate();
|
||||
schemaVersion = 2;
|
||||
}
|
||||
|
||||
if (schemaVersion != 2) {
|
||||
throw new RuntimeException("Invalid database schema version!");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed to create tables!", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void upgradeSchema1To2(Connection conn) throws SQLException {
|
||||
SaneEconomy.logger().info("Upgrading database schema from version 1 to version 2. This might take a little while...");
|
||||
PreparedStatement ps = conn.prepareStatement(String.format("REPLACE INTO `%s` (`key`, `val`) VALUES ('schema_version', '2')", dbConn.getTable("saneeconomy_schema")));
|
||||
ps.executeUpdate();
|
||||
conn.prepareStatement(String.format("CREATE TABLE `%s` (unique_identifier VARCHAR(128) PRIMARY KEY, balance DECIMAL(18, 2))", dbConn.getTable("saneeconomy_balances"))).executeUpdate();
|
||||
ps = conn.prepareStatement("SELECT * FROM `player_balances`");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
Map<String, Double> oldBalances = new HashMap<>();
|
||||
|
||||
while (rs.next()) {
|
||||
oldBalances.put(rs.getString("player_uuid"), rs.getDouble("balance"));
|
||||
}
|
||||
|
||||
for (Entry<String, Double> e : oldBalances.entrySet()) {
|
||||
ps = conn.prepareStatement(String.format("INSERT INTO `%s` (unique_identifier, balance) VALUES (?, ?)", dbConn.getTable("saneeconomy_balances")));
|
||||
ps.setString(1, "player:" + e.getKey());
|
||||
ps.setDouble(2, e.getValue());
|
||||
ps.executeUpdate();
|
||||
}
|
||||
reloadDatabase();
|
||||
SaneEconomy.logger().info("Schema upgrade complete!");
|
||||
}
|
||||
|
||||
private boolean checkTableExists(String tableName) {
|
||||
try (Connection conn = dbConn.openConnection()) {
|
||||
PreparedStatement ps = conn.prepareStatement("SELECT * FROM information_schema.tables WHERE table_schema = ? AND table_name = ? LIMIT 1");
|
||||
@ -104,7 +75,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
||||
|
||||
@Override
|
||||
public synchronized void reloadDatabase() {
|
||||
waitUntilFlushed();
|
||||
// waitUntilFlushed();
|
||||
createTables();
|
||||
try (Connection conn = dbConn.openConnection()) {
|
||||
PreparedStatement ps = dbConn.prepareStatement(conn, String.format("SELECT * FROM `%s`", dbConn.getTable("saneeconomy_balances")));
|
||||
@ -125,7 +96,7 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
||||
final double oldBalance = getBalance(economable);
|
||||
balances.put(economable.getUniqueIdentifier(), newBalance);
|
||||
|
||||
dbConn.executeAsyncOperation((conn) -> {
|
||||
dbConn.executeAsyncOperation("set_balance_" + economable.getUniqueIdentifier(), (conn) -> {
|
||||
try {
|
||||
ensureAccountExists(economable, conn);
|
||||
conn.prepareStatement("LOCK TABLE " + dbConn.getTable("saneeconomy_balances") + " WRITE").execute();
|
||||
@ -166,4 +137,8 @@ public class EconomyStorageBackendMySQL extends EconomyStorageBackendCaching {
|
||||
public MySQLConnection getConnection() {
|
||||
return dbConn;
|
||||
}
|
||||
|
||||
public void closeConnections() {
|
||||
this.dbConn.getConnection().cleanup();
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package org.appledash.saneeconomy.economy.logger;
|
||||
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.utils.database.DatabaseCredentials;
|
||||
import org.appledash.saneeconomy.utils.database.MySQLConnection;
|
||||
import org.appledash.sanelib.database.DatabaseCredentials;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@ -21,9 +21,9 @@ public class TransactionLoggerMySQL implements TransactionLogger {
|
||||
}
|
||||
|
||||
private void logGeneric(String from, String to, double change, TransactionReason reason) {
|
||||
this.dbConn.executeAsyncOperation((conn) -> {
|
||||
this.dbConn.executeAsyncOperation("log_transaction", (conn) -> {
|
||||
try {
|
||||
PreparedStatement ps = conn.prepareStatement(String.format("INSERT INTO `%s` (`source`, `destination`, `amount`, `reason`) VALUES (?, ?, ?, ?)", dbConn.getTable("transaction_logs")));
|
||||
PreparedStatement ps = conn.prepareStatement(String.format("INSERT INTO `%s` (`source`, `destination`, `amount`, `reason`) VALUES (?, ?, ?, ?)", dbConn.getTable("transaction_logs")));
|
||||
ps.setString(1, from);
|
||||
ps.setString(2, to);
|
||||
ps.setDouble(3, change);
|
||||
|
@ -5,7 +5,6 @@ import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.updates.GithubVersionChecker;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -37,13 +36,13 @@ public class JoinQuitListener implements Listener {
|
||||
Economable.CONSOLE, economable, startBalance, TransactionReason.STARTING_BALANCE
|
||||
));
|
||||
if (plugin.getConfig().getBoolean("economy.notify-start-balance", true)) {
|
||||
MessageUtils.sendMessage(player, "You've been issued a starting balance of {1}!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
|
||||
this.plugin.getMessenger().sendMessage(player, "You've been issued a starting balance of {1}!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
|
||||
}
|
||||
}
|
||||
|
||||
/* Update notification */
|
||||
if (player.hasPermission("saneeconomy.update-notify") && plugin.getVersionChecker().isUpdateAvailable()) {
|
||||
MessageUtils.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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,127 +0,0 @@
|
||||
package org.appledash.saneeconomy.utils;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 8/5/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class I18n {
|
||||
private static final I18n INSTANCE = new I18n(SaneEconomy.getInstance());
|
||||
private final SaneEconomy plugin;
|
||||
private final Map<String, String> translations = new HashMap<>();
|
||||
|
||||
private I18n(SaneEconomy plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void loadTranslations() {
|
||||
File configFile = new File(plugin.getDataFolder(), "messages.yml");
|
||||
YamlConfiguration configJar = YamlConfiguration.loadConfiguration(new InputStreamReader(this.getClass().getResourceAsStream("/messages.yml")));
|
||||
|
||||
if (configFile.exists()) { // Attempt to merge any new keys from the JAR's messages.yml into the copy in the plugin's data folder
|
||||
YamlConfiguration configDisk = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
||||
List<Map<?, ?>> finalKeys = configDisk.getMapList("messages");
|
||||
|
||||
for (Map jarObject : configJar.getMapList("messages")) { // For every translation in the template config in the JAR
|
||||
String jarMessage = String.valueOf(jarObject.get("message")); // Key for this translation
|
||||
Map equivalentOnDisk = null; // Equivalent of this translation in the config file on disk
|
||||
|
||||
for (Map diskMap : configDisk.getMapList("messages")) { // For every translation in the config on disk
|
||||
if (String.valueOf(diskMap.get("message")).equals(jarMessage)) { // If the translation key on this object on disk is the same as the current one in the JAR
|
||||
equivalentOnDisk = diskMap;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (equivalentOnDisk == null) { // This one isn't on disk yet - add it.
|
||||
finalKeys.add(jarObject);
|
||||
} else {
|
||||
String currentKey = String.valueOf(equivalentOnDisk.get("message"));
|
||||
String currentTranslation = String.valueOf(equivalentOnDisk.get("translation"));
|
||||
convertKey(finalKeys, currentKey, currentTranslation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (Map diskObject : configDisk.getMapList("messages")) {
|
||||
convertKey(finalKeys, String.valueOf(diskObject.get("message")), String.valueOf(diskObject.get("translation")));
|
||||
}
|
||||
|
||||
configDisk.set("messages", finalKeys);
|
||||
|
||||
try {
|
||||
configDisk.save(configFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to save translations file.", e);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
configJar.save(configFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to save initial translations file.", e);
|
||||
}
|
||||
}
|
||||
|
||||
YamlConfiguration configFileYaml = YamlConfiguration.loadConfiguration(configFile);
|
||||
configFileYaml.getMapList("messages").stream().filter(map -> map.containsKey("translation")).forEach(map -> {
|
||||
translations.put(map.get("message").toString(), map.get("translation").toString());
|
||||
});
|
||||
}
|
||||
|
||||
private void convertKey(List<Map<?, ?>> finalKeys, String currentKey, String currentTranslation) {
|
||||
String convertedKey = convertOldTranslations(currentKey);
|
||||
|
||||
if (!currentKey.equals(convertedKey)) { // Key needs conversion
|
||||
String convertedValue = convertOldTranslations(String.valueOf(currentTranslation));
|
||||
|
||||
// Remove current key from map of things to go to the disk
|
||||
finalKeys.removeIf(map -> String.valueOf(map.get("message")).equals(currentKey));
|
||||
|
||||
// Add the converted one.
|
||||
if (convertedValue.equals("null")) {
|
||||
finalKeys.add(ImmutableMap.of("message", convertedKey));
|
||||
} else {
|
||||
finalKeys.add(ImmutableMap.of("message", convertedKey, "translation", convertedValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String convertOldTranslations(String input) {
|
||||
Matcher m = Pattern.compile("(%s)").matcher(input);
|
||||
StringBuffer converted = new StringBuffer();
|
||||
int index = 1;
|
||||
|
||||
while (m.find()) {
|
||||
m.appendReplacement(converted, String.format("{%d}", index));
|
||||
index++;
|
||||
}
|
||||
|
||||
m.appendTail(converted);
|
||||
|
||||
return converted.toString();
|
||||
}
|
||||
|
||||
private String translate(String input) {
|
||||
return translations.containsKey(input) ? ChatColor.translateAlternateColorCodes('&', translations.get(input)) : input;
|
||||
}
|
||||
|
||||
public static String _(String s) {
|
||||
return INSTANCE.translate(s);
|
||||
}
|
||||
|
||||
public static I18n getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
package org.appledash.saneeconomy.utils;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.appledash.saneeconomy.utils.I18n._;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/13/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class MessageUtils {
|
||||
/**
|
||||
* Send a formatted chat message to the given target.
|
||||
* This message will have the prefix defined in SaneEconomy's config file.
|
||||
* @param target Target CommandSender
|
||||
* @param fmt String#format format
|
||||
* @param args String#format args
|
||||
*/
|
||||
public static void sendMessage(CommandSender target, String fmt, Object... args) {
|
||||
fmt = _(fmt);
|
||||
|
||||
String prefix = ChatColor.translateAlternateColorCodes('&', SaneEconomy.getInstance().getConfig().getString("chat.prefix", ""));
|
||||
|
||||
String formatted;
|
||||
|
||||
if (fmt.contains("%s")) { // Legacy support.
|
||||
formatted = String.format(fmt, (Object[]) args);
|
||||
} else {
|
||||
formatted = indexedFormat(fmt, (Object[]) args);
|
||||
}
|
||||
|
||||
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(SaneEconomy.getInstance(), () -> target.sendMessage(prefix + formatted));
|
||||
}
|
||||
|
||||
public static void sendMessage(Object target, String fmt, Object... args) {
|
||||
if ((target instanceof OfflinePlayer) && ((OfflinePlayer) target).isOnline() && (target instanceof CommandSender)) {
|
||||
sendMessage(((CommandSender) target), fmt, (Object[])args);
|
||||
}
|
||||
}
|
||||
|
||||
public static String indexedFormat(String fmt, Object... arguments) {
|
||||
Matcher m = Pattern.compile("\\{([0-9]+)(:[^}]+)?\\}").matcher(fmt);
|
||||
StringBuffer formatted = new StringBuffer();
|
||||
|
||||
while (m.find()) {
|
||||
int index = Integer.valueOf(m.group(1)) - 1;
|
||||
|
||||
if (index > arguments.length - 1 || index < 0) {
|
||||
throw new IllegalArgumentException("Index must be within the range of the given arguments.");
|
||||
}
|
||||
|
||||
String stringRep;
|
||||
|
||||
if (!Strings.isNullOrEmpty(m.group(2))) {
|
||||
stringRep = String.format(String.format("%%%s", m.group(2).substring(1)), arguments[index]);
|
||||
} else {
|
||||
stringRep = String.valueOf(arguments[index]);
|
||||
}
|
||||
|
||||
m.appendReplacement(formatted, stringRep);
|
||||
}
|
||||
|
||||
m.appendTail(formatted);
|
||||
|
||||
return formatted.toString();
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendMySQL
|
||||
import org.appledash.saneeconomy.economy.economable.EconomableGeneric;
|
||||
import org.appledash.saneeconomy.economy.logger.TransactionLogger;
|
||||
import org.appledash.saneeconomy.economy.logger.TransactionLoggerMySQL;
|
||||
import org.appledash.saneeconomy.utils.database.DatabaseCredentials;
|
||||
import org.appledash.sanelib.database.DatabaseCredentials;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
@ -152,6 +152,7 @@ public class SaneEconomyConfiguration {
|
||||
* @return DatabaseCredentials with the information from the config.
|
||||
*/
|
||||
private DatabaseCredentials loadCredentials(ConfigurationSection config) {
|
||||
String databaseType = config.getString("type", "mysql");
|
||||
String backendHost = config.getString("host");
|
||||
int backendPort = config.getInt("port", 3306);
|
||||
String backendDb = config.getString("database");
|
||||
@ -161,7 +162,7 @@ public class SaneEconomyConfiguration {
|
||||
boolean useSsl = config.getBoolean("use_ssl", false);
|
||||
|
||||
return new DatabaseCredentials(
|
||||
backendHost, backendPort, backendUser, backendPass, backendDb, tablePrefix, useSsl
|
||||
databaseType, backendHost, backendPort, backendUser, backendPass, backendDb, tablePrefix, useSsl
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,7 @@ public class WebUtils {
|
||||
return out;
|
||||
} catch (IOException e) {
|
||||
SaneEconomy.logger().warning("Failed to get contents of URL " + url);
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to get URL contents!");
|
||||
throw new RuntimeException("Failed to get URL contents!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,57 +0,0 @@
|
||||
package org.appledash.saneeconomy.utils.database;
|
||||
|
||||
/**
|
||||
* Created by appledash on 9/18/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class DatabaseCredentials {
|
||||
private final String hostname;
|
||||
private final int port;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final String databaseName;
|
||||
private final String tablePrefix;
|
||||
private final int maxRetries;
|
||||
private final int queryTimeout;
|
||||
private final boolean useSsl;
|
||||
|
||||
public DatabaseCredentials(String hostname, int port, String username, String password, String databaseName, String tablePrefix, boolean useSsl) {
|
||||
this.hostname = hostname;
|
||||
this.port = port;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.databaseName = databaseName;
|
||||
this.tablePrefix = tablePrefix;
|
||||
this.useSsl = useSsl;
|
||||
maxRetries = 5;
|
||||
queryTimeout = 5000;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getDatabaseName() {
|
||||
return databaseName;
|
||||
}
|
||||
|
||||
public String getJDBCURL() {
|
||||
return String.format("jdbc:mysql://%s:%d/%s?useSSL=%s", hostname, port, databaseName, useSsl);
|
||||
}
|
||||
|
||||
public String getTablePrefix() {
|
||||
return tablePrefix;
|
||||
}
|
||||
|
||||
public int getMaxRetries() {
|
||||
return maxRetries;
|
||||
}
|
||||
|
||||
public int getQueryTimeout() {
|
||||
return queryTimeout;
|
||||
}
|
||||
}
|
@ -1,13 +1,11 @@
|
||||
package org.appledash.saneeconomy.utils.database;
|
||||
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.appledash.sanelib.database.DatabaseCredentials;
|
||||
import org.appledash.sanelib.database.SaneDatabase;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -17,23 +15,17 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
public class MySQLConnection {
|
||||
private static final Logger LOGGER = Logger.getLogger("MySQLConnection");
|
||||
private static final int MAX_OPEN_TRANSACTIONS = 5;
|
||||
private final DatabaseCredentials dbCredentials;
|
||||
private final AtomicInteger openTransactions = new AtomicInteger(0);
|
||||
private final SaneDatabase saneDatabase;
|
||||
|
||||
public MySQLConnection(DatabaseCredentials dbCredentials) {
|
||||
this.dbCredentials = dbCredentials;
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("No MySQL driver found.", e);
|
||||
}
|
||||
this.saneDatabase = new SaneDatabase(dbCredentials);
|
||||
}
|
||||
|
||||
public Connection openConnection() {
|
||||
try {
|
||||
return DriverManager.getConnection(dbCredentials.getJDBCURL(), dbCredentials.getUsername(), dbCredentials.getPassword());
|
||||
return this.saneDatabase.getConnection();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Database unavailable.", e);
|
||||
}
|
||||
@ -56,19 +48,12 @@ public class MySQLConnection {
|
||||
}
|
||||
}
|
||||
|
||||
public void executeAsyncOperation(Consumer<Connection> callback) {
|
||||
Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(SaneEconomy.getInstance(), () -> {
|
||||
doExecuteAsyncOperation(1, callback);
|
||||
});
|
||||
public void executeAsyncOperation(String tag, Consumer<Connection> callback) {
|
||||
this.saneDatabase.runDatabaseOperationAsync(tag, () -> doExecuteAsyncOperation(1, callback));
|
||||
}
|
||||
|
||||
// This is a bit weird because it has to account for recursion...
|
||||
private void doExecuteAsyncOperation(int levels, Consumer<Connection> callback) {
|
||||
if (levels == 1) { // First level
|
||||
waitForSlot();
|
||||
openTransactions.incrementAndGet();
|
||||
}
|
||||
|
||||
try (Connection conn = openConnection()) {
|
||||
callback.accept(conn);
|
||||
} catch (Exception e) {
|
||||
@ -80,10 +65,6 @@ public class MySQLConnection {
|
||||
e.printStackTrace();
|
||||
levels++;
|
||||
doExecuteAsyncOperation(levels, callback);
|
||||
} finally {
|
||||
if (levels == 1) { // The recursion is done, we may have thrown an exception, maybe not - but either way we need to mark the transaction as closed.
|
||||
openTransactions.decrementAndGet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,19 +76,9 @@ public class MySQLConnection {
|
||||
return dbCredentials.getTablePrefix() + tableName;
|
||||
}
|
||||
|
||||
private void waitForSlot() {
|
||||
while (openTransactions.get() >= MAX_OPEN_TRANSACTIONS) {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void waitUntilFlushed() {
|
||||
long startTime = System.currentTimeMillis();
|
||||
while (openTransactions.get() > 0) {
|
||||
while (!this.saneDatabase.isFinished()) {
|
||||
if ((System.currentTimeMillis() - startTime) > 5000) {
|
||||
LOGGER.warning("Took too long to flush all transactions - something has probably hung :(");
|
||||
break;
|
||||
@ -119,4 +90,8 @@ public class MySQLConnection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public SaneDatabase getConnection() {
|
||||
return this.saneDatabase;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
name: SaneEconomy
|
||||
author: AppleDash
|
||||
main: org.appledash.saneeconomy.SaneEconomy
|
||||
version: 0.11.0
|
||||
version: 0.12.0
|
||||
load: STARTUP
|
||||
softdepend: [Vault]
|
||||
commands:
|
||||
|
@ -1,30 +0,0 @@
|
||||
package org.appledash.saneeconomy.test;
|
||||
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by appledash on 12/15/16.
|
||||
* Blackjack is best pony.
|
||||
*/
|
||||
public class MessageUtilsTest {
|
||||
@Test
|
||||
public void testIndexedFormat() {
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("{1}, {2}!", "Hello", "world"));
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, {1}!", "world", "discarded"));
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, {2}!", "discarded", "world"));
|
||||
Assert.assertEquals("Hello, world!", MessageUtils.indexedFormat("Hello, world!", "this", "shouldn't", "change"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdvancedIndexFormat() {
|
||||
Assert.assertEquals("Temperature: 20.01 degrees", MessageUtils.indexedFormat("Temperature: {1:.2f} degrees", 20.01f));
|
||||
Assert.assertEquals("Index: 01", MessageUtils.indexedFormat("Index: {1:02d}", 1));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testBadIndexedFormat() {
|
||||
MessageUtils.indexedFormat("Hello, {3}!", "world", "something");
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>SaneEconomy</artifactId>
|
||||
<groupId>org.appledash</groupId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
<dependency>
|
||||
<groupId>org.appledash</groupId>
|
||||
<artifactId>SaneEconomyCore</artifactId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -2,8 +2,8 @@ package org.appledash.saneeconomymobkills;
|
||||
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomymobkills.listeners.EntityDamageListener;
|
||||
import org.appledash.sanelib.SanePlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -15,13 +15,14 @@ import java.util.Map;
|
||||
* Created by appledash on 12/27/16.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class SaneEconomyMobKills extends JavaPlugin {
|
||||
public class SaneEconomyMobKills extends SanePlugin {
|
||||
private SaneEconomy saneEconomy;
|
||||
private final Map<String, Double> killAmounts = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saneEconomy = (SaneEconomy)getServer().getPluginManager().getPlugin("SaneEconomy");
|
||||
super.onEnable();
|
||||
|
||||
YamlConfiguration amountsConfig;
|
||||
|
||||
@ -42,6 +43,7 @@ public class SaneEconomyMobKills extends JavaPlugin {
|
||||
}
|
||||
|
||||
getServer().getPluginManager().registerEvents(new EntityDamageListener(this), this);
|
||||
this.getI18n().loadTranslations();
|
||||
}
|
||||
|
||||
public SaneEconomy getSaneEconomy() {
|
||||
|
@ -3,7 +3,6 @@ package org.appledash.saneeconomymobkills.listeners;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomymobkills.SaneEconomyMobKills;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
@ -24,7 +23,6 @@ import java.util.UUID;
|
||||
public class EntityDamageListener implements Listener {
|
||||
private SaneEconomyMobKills plugin;
|
||||
private Map<Integer, Map<UUID, Double>> damageDealt = new HashMap<>();
|
||||
private Map<Integer, Double> damageDealtNonPlayers = new HashMap<>();
|
||||
|
||||
public EntityDamageListener(SaneEconomyMobKills plugin) {
|
||||
this.plugin = plugin;
|
||||
@ -81,7 +79,7 @@ public class EntityDamageListener implements Listener {
|
||||
|
||||
if (offlinePlayer.isOnline()) {
|
||||
Player player = Bukkit.getServer().getPlayer(offlinePlayer.getUniqueId());
|
||||
MessageUtils.sendMessage(player, "You have been awarded {1} for doing {2:.2f}% of the damage required to kill that {3}!", plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(thisAmount), thisPercent, entity.getName());
|
||||
this.plugin.getMessenger().sendMessage(player, "You have been awarded {1} for doing {2:.2f}% of the damage required to kill that {3}!", plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(thisAmount), thisPercent, entity.getName());
|
||||
}
|
||||
|
||||
plugin.getSaneEconomy().getEconomyManager().transact(new Transaction(
|
||||
|
@ -5,18 +5,18 @@
|
||||
<parent>
|
||||
<artifactId>SaneEconomy</artifactId>
|
||||
<groupId>org.appledash</groupId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>SaneEconomySignShop</artifactId>
|
||||
<version>0.1.4-SNAPSHOT</version>
|
||||
<version>0.1.5-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.appledash</groupId>
|
||||
<artifactId>SaneEconomyCore</artifactId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -8,9 +8,9 @@ import org.appledash.saneeconomysignshop.signshop.SignShopManager;
|
||||
import org.appledash.saneeconomysignshop.signshop.storage.SignShopStorageJSON;
|
||||
import org.appledash.saneeconomysignshop.util.ItemDatabase;
|
||||
import org.appledash.saneeconomysignshop.util.LimitManager;
|
||||
import org.appledash.sanelib.SanePlugin;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStreamReader;
|
||||
@ -19,7 +19,7 @@ import java.io.InputStreamReader;
|
||||
* Created by appledash on 10/2/16.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class SaneEconomySignShop extends JavaPlugin {
|
||||
public class SaneEconomySignShop extends SanePlugin {
|
||||
private ISaneEconomy saneEconomy;
|
||||
private final SignShopManager signShopManager = new SignShopManager(new SignShopStorageJSON(new File(getDataFolder(), "shops.json")));
|
||||
private final LimitManager limitManager = new LimitManager();
|
||||
@ -32,6 +32,8 @@ public class SaneEconomySignShop extends JavaPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
super.onEnable();
|
||||
|
||||
ItemDatabase.initItemDB();
|
||||
|
||||
saneEconomy = (ISaneEconomy)getServer().getPluginManager().getPlugin("SaneEconomy");
|
||||
@ -49,7 +51,7 @@ public class SaneEconomySignShop extends JavaPlugin {
|
||||
getServer().getPluginManager().registerEvents(new SignChangeListener(this), this);
|
||||
getServer().getPluginManager().registerEvents(new InteractListener(this), this);
|
||||
getServer().getPluginManager().registerEvents(new BreakListener(this), this);
|
||||
|
||||
this.getI18n().loadTranslations();
|
||||
}
|
||||
|
||||
public SignShopManager getSignShopManager() {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.appledash.saneeconomysignshop.listeners;
|
||||
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomysignshop.SaneEconomySignShop;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
@ -21,13 +20,13 @@ public class BreakListener implements Listener {
|
||||
public void onBlockBreak(BlockBreakEvent evt) {
|
||||
plugin.getSignShopManager().getSignShop(evt.getBlock().getLocation()).ifPresent((shop) -> {
|
||||
if (!evt.getPlayer().hasPermission("saneeconomy.signshop.destroy.admin")) {
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "You may not destroy that!");
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "You may not destroy that!");
|
||||
evt.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getSignShopManager().removeSignShop(shop);
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "Sign shop destroyed!");
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "Sign shop destroyed!");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package org.appledash.saneeconomysignshop.listeners;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
||||
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomysignshop.SaneEconomySignShop;
|
||||
import org.appledash.saneeconomysignshop.signshop.ShopTransaction;
|
||||
import org.appledash.saneeconomysignshop.signshop.ShopTransaction.TransactionDirection;
|
||||
@ -62,7 +61,7 @@ public class InteractListener implements Listener {
|
||||
if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||
evt.setCancelled(true);
|
||||
if (!shop.canBuy()) {
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "This shop does not permit buying.");
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "This shop does not permit buying.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -73,7 +72,7 @@ public class InteractListener implements Listener {
|
||||
if (evt.getAction() == Action.LEFT_CLICK_BLOCK) {
|
||||
evt.setCancelled(true);
|
||||
if (!shop.canSell()) {
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "This shop does not permit selling.");
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "This shop does not permit selling.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -100,7 +99,7 @@ public class InteractListener implements Listener {
|
||||
TransactionResult result = ecoMan.transact(ecoTransaction);
|
||||
|
||||
if (result.getStatus() != TransactionResult.Status.SUCCESS) {
|
||||
MessageUtils.sendMessage(player, "An error occurred attempting to perform that transaction: {1}", result.getStatus());
|
||||
this.plugin.getMessenger().sendMessage(player, "An error occurred attempting to perform that transaction: {1}", result.getStatus());
|
||||
return;
|
||||
}
|
||||
|
||||
@ -108,7 +107,7 @@ public class InteractListener implements Listener {
|
||||
stack.setAmount(quantity);
|
||||
player.getInventory().addItem(stack);
|
||||
|
||||
MessageUtils.sendMessage(player, "You have bought {1} {2} for {3}.", quantity, shop.getItemStack().getType().name(), ecoMan.getCurrency().formatAmount(shopTransaction.getPrice()));
|
||||
this.plugin.getMessenger().sendMessage(player, "You have bought {1} {2} for {3}.", quantity, shop.getItemStack().getType().name(), ecoMan.getCurrency().formatAmount(shopTransaction.getPrice()));
|
||||
LOGGER.info(String.format("%s just bought %s for %s.", player.getName(), shop.getItemStack(), ecoMan.getCurrency().formatAmount(shopTransaction.getPrice())));
|
||||
}
|
||||
|
||||
@ -118,14 +117,14 @@ public class InteractListener implements Listener {
|
||||
double price = shop.getSellPrice(quantity);
|
||||
|
||||
if (!player.getInventory().containsAtLeast(new ItemStack(shop.getItemStack()), quantity)) {
|
||||
MessageUtils.sendMessage(player, "You do not have {1} {2}!", quantity, shop.getItemStack().getType().name());
|
||||
this.plugin.getMessenger().sendMessage(player, "You do not have {1} {2}!", quantity, shop.getItemStack().getType().name());
|
||||
return;
|
||||
}
|
||||
|
||||
ShopTransaction shopTransaction = shop.makeTransaction(player, TransactionDirection.SELL, quantity);
|
||||
|
||||
if (!plugin.getLimitManager().shouldAllowTransaction(shopTransaction)) {
|
||||
MessageUtils.sendMessage(player, "You have reached your selling limit for the time being. Try back in an hour or so.");
|
||||
this.plugin.getMessenger().sendMessage(player, "You have reached your selling limit for the time being. Try back in an hour or so.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -138,7 +137,7 @@ public class InteractListener implements Listener {
|
||||
|
||||
ecoMan.transact(shopTransaction.makeEconomyTransaction());
|
||||
|
||||
MessageUtils.sendMessage(player, "You have sold {1} {2} for {3}.", quantity, shop.getItemStack().getType().name(), ecoMan.getCurrency().formatAmount(price));
|
||||
this.plugin.getMessenger().sendMessage(player, "You have sold {1} {2} for {3}.", quantity, shop.getItemStack().getType().name(), ecoMan.getCurrency().formatAmount(price));
|
||||
LOGGER.info(String.format("%s just sold %s for %s.", player.getName(), shop.getItemStack(), ecoMan.getCurrency().formatAmount(price)));
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package org.appledash.saneeconomysignshop.listeners;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.appledash.saneeconomy.utils.MessageUtils;
|
||||
import org.appledash.saneeconomysignshop.SaneEconomySignShop;
|
||||
import org.appledash.saneeconomysignshop.signshop.SignShop;
|
||||
import org.appledash.saneeconomysignshop.util.ItemDatabase;
|
||||
@ -37,7 +36,7 @@ public class SignChangeListener implements Listener {
|
||||
ParsedSignShop pss = parseSignShop(evt);
|
||||
|
||||
if (pss.error != null) {
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "Cannot create shop: {1}", pss.error);
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "Cannot create shop: {1}", pss.error);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -48,17 +47,17 @@ public class SignChangeListener implements Listener {
|
||||
SignShop signShop = pss.shop;
|
||||
plugin.getSignShopManager().addSignShop(signShop);
|
||||
evt.setLine(0, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("admin-shop-title")));
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "Sign shop created!");
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "Item: {1} x {2}", signShop.getQuantity(), signShop.getItemStack());
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "Sign shop created!");
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "Item: {1} x {2}", signShop.getQuantity(), signShop.getItemStack());
|
||||
|
||||
if (signShop.canBuy()) { // The player be buying from the shop, not the other way around.
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "Will sell to players for {1}.",
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "Will sell to players for {1}.",
|
||||
plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(signShop.getBuyPrice())
|
||||
);
|
||||
}
|
||||
|
||||
if (signShop.canSell()) { // The player be selling to the shop, not the other way around.
|
||||
MessageUtils.sendMessage(evt.getPlayer(), "Will buy from players for {1}.",
|
||||
this.plugin.getMessenger().sendMessage(evt.getPlayer(), "Will buy from players for {1}.",
|
||||
plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(signShop.getSellPrice())
|
||||
);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
name: SaneEconomySignShop
|
||||
main: org.appledash.saneeconomysignshop.SaneEconomySignShop
|
||||
author: AppleDash
|
||||
version: 0.1.4
|
||||
version: 0.1.5
|
||||
depend: [SaneEconomy]
|
||||
|
11
pom.xml
11
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>org.appledash</groupId>
|
||||
<artifactId>SaneEconomy</artifactId>
|
||||
<version>0.11.0-SNAPSHOT</version>
|
||||
<version>0.12.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
@ -26,6 +26,10 @@
|
||||
<name>Vault</name>
|
||||
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>votuvo</id>
|
||||
<url>https://mvn.votuvo.com/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
@ -39,5 +43,10 @@
|
||||
<artifactId>VaultAPI</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.appledash</groupId>
|
||||
<artifactId>sanelib</artifactId>
|
||||
<version>0.2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
Loading…
Reference in New Issue
Block a user