Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Updated to newer Utils version and introduced caching
Browse files Browse the repository at this point in the history
Fixed #61 #63 #70 #71
  • Loading branch information
Flibio committed May 6, 2018
1 parent e70957e commit 58350df
Show file tree
Hide file tree
Showing 33 changed files with 334 additions and 264 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ EconomyLite is a lightweight, yet advanced economy plugin for your Sponge server
- EconomyLite is multi-threaded for the best possible performance
- Multiple Storage Options
- Store your data however you like, locally or on a MySQL database
- Migration
- EconomyLite allows you to easily migrate your data from another economy plugin
- Customizable
- Much of EconomyLite can be changed to suit your needs
- Username Change Support
- Any player can change their name, and EconomyLite can handle it
- Virtual Management
- EconomyLite includes commands to manage virtual accounts
- Loans
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {

dependencies {
compile "org.spongepowered:spongeapi:${project.apiVersion}"
compile 'com.github.flibiostudio:utils:5f41eac'
compile 'com.github.flibiostudio:utils:2115eae'
}

shadowJar {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name=EconomyLite
owner=Flibio
inceptionYear=2015
currentYear=2018
version=3.0.0
version=2.14.0
apiVersion=7.1.0-SNAPSHOT
106 changes: 49 additions & 57 deletions src/main/java/io/github/flibio/economylite/EconomyLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import io.github.flibio.economylite.modules.loan.LoanModule;
import io.github.flibio.economylite.modules.sql.SqlModule;
import io.github.flibio.utils.commands.CommandLoader;
import io.github.flibio.utils.config.ConfigManager;
import io.github.flibio.utils.file.FileManager;
import io.github.flibio.utils.message.MessageStorage;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.config.ConfigDir;
Expand All @@ -60,6 +62,8 @@
import java.util.List;
import java.util.Optional;

import javax.xml.stream.events.Comment;

@Plugin(id = PluginInfo.ID, name = PluginInfo.NAME, version = PluginInfo.VERSION, description = PluginInfo.DESCRIPTION)
public class EconomyLite {

Expand All @@ -75,7 +79,8 @@ public class EconomyLite {

@Inject private PluginContainer container;

private static FileManager fileManager;
private static ConfigManager currencyManager;
private static ConfigManager configManager;
private static MessageStorage messageStorage;
private static EconomyLite instance;

Expand All @@ -94,16 +99,12 @@ public void onServerInitialize(GamePreInitializationEvent event) {
logger.info("EconomyLite " + PluginInfo.VERSION + " is initializing!");
instance = this;
// File setup
if (new File(configDir.toString()).isAbsolute()) {
fileManager = FileManager.createInstance(this, configDir.toString());
} else {
fileManager = FileManager.createInstance(this, "./" + configDir.toString());
}
configManager = ConfigManager.create(configDir, "config.conf", logger);
currencyManager = ConfigManager.create(configDir, "currencies.conf", logger);
initializeFiles();
initializeCurrencies();
// Load Message Storage
messageStorage = MessageStorage.createInstance(this, configDir.toString());
initializeMessage();
messageStorage = MessageStorage.create(configDir, "messages", logger);
// Load modules
List<Module> postInitModules = new ArrayList<>();
getModules().forEach(m -> {
Expand Down Expand Up @@ -160,72 +161,59 @@ public void onServerInitialize(GamePreInitializationEvent event) {
}

private void initializeFiles() {
fileManager.setDefault("config.conf", "default-balance", Double.class, 0.0);
fileManager.setDefault("config.conf", "virt-default-balance", Double.class, 0.0);
fileManager.setDefault("config.conf", "debug-logging", Boolean.class, true);
fileManager.setDefault("config.conf", "notify-on-admin-commands", Boolean.class, false);
fileManager.setDefault("config.conf", "confirm-offline-payments", Boolean.class, false);
}

public static boolean isEnabled(String path) {
if (fileManager.getValue("config.conf", path, Boolean.class).isPresent()) {
return fileManager.getValue("config.conf", path, Boolean.class).get();
} else {
getInstance().getLogger().error("An error has occurred loading config value " + path + "! Defaulting to false.");
return false;
}
configManager.setDefault("Default balance of player accounts.", Double.class, 0.0, "default-balance", "player");
configManager.setDefault("Default balance of virtual accounts.", Double.class, 0.0, "default-balance", "virtual");
configManager.setDefault("Enables or disables debug logging.", Boolean.class, true, "debug-logging");
configManager.setDefault("If enabled players will be notified when an admin changes their balance", Boolean.class, false,
"notify-on-admin-commands");
configManager.setDefault("If enabled players will be asked to confirm payments to an offline player.", Boolean.class, true,
"confirm-offline-payments");
configManager.save();
}

private void initializeCurrencies() {
// Initialize the default currency into file
fileManager.setDefault("currencies.conf", "current", String.class, "coin");
fileManager.setDefault("currencies.conf", "coin.singular", String.class, "Coin");
fileManager.setDefault("currencies.conf", "coin.plural", String.class, "Coins");
fileManager.setDefault("currencies.conf", "coin.symbol", String.class, "C");
currencyManager.setDefault(String.class, "current", "coin");
currencyManager.setDefault(String.class, "Coin", "coin", "singular");
currencyManager.setDefault(String.class, "Coins", "coin", "plural");
currencyManager.setDefault(String.class, "C", "coin", "symbol");
Currency defaultCurrency =
new LiteCurrency(fileManager.getValue("currencies.conf", "coin.singular", String.class).get(), fileManager.getValue(
"currencies.conf", "coin.plural", String.class).get(), fileManager.getValue("currencies.conf", "coin.symbol", String.class)
.get(), true, 2);
new LiteCurrency(currencyManager.getValue("", String.class, "coin", "singular"),
currencyManager.getValue("", String.class, "coin", "plural"), currencyManager.getValue("", String.class, "coin", "symbol"),
true, 2);
currencyEconService = new CurrencyService(defaultCurrency);
// Load all of the currencies
Optional<ConfigurationNode> fOpt = fileManager.getFile("currencies.conf");
if (fOpt.isPresent()) {
ConfigurationNode root = fOpt.get();
for (Object raw : root.getChildrenMap().keySet()) {
if (raw instanceof String) {
String currencyId = (String) raw;
Optional<String> sOpt = fileManager.getValue("currencies.conf", currencyId + ".singular", String.class);
Optional<String> pOpt = fileManager.getValue("currencies.conf", currencyId + ".plural", String.class);
Optional<String> syOpt = fileManager.getValue("currencies.conf", currencyId + ".symbol", String.class);
if (sOpt.isPresent() && pOpt.isPresent() && syOpt.isPresent() && !currencyId.equals("coin")) {
Currency currency = new LiteCurrency(sOpt.get(), pOpt.get(), syOpt.get(), false, 2);
currencyEconService.addCurrency(currency);
}
CommentedConfigurationNode root = currencyManager.getNode();
for (Object raw : root.getChildrenMap().keySet()) {
if (raw instanceof String) {
String currencyId = (String) raw;
String singular = currencyManager.getValue("", String.class, currencyId, "singular");
String plural = currencyManager.getValue("", String.class, currencyId, "plural");
String symbol = currencyManager.getValue("", String.class, currencyId, "symbol");
if (!singular.isEmpty() && !plural.isEmpty() && !symbol.isEmpty() && !currencyId.equals("coin")) {
Currency currency = new LiteCurrency(singular, plural, symbol, false, 2);
currencyEconService.addCurrency(currency);
}
}
}
// Attempt to load the current currency
Optional<String> cOpt = fileManager.getValue("currencies.conf", "current", String.class);
if (cOpt.isPresent()) {
String currentCur = cOpt.get();
String current = currencyManager.getValue("", String.class, "current");
if (!current.isEmpty()) {
currencyEconService.getCurrencies().forEach(c -> {
if (("economylite:" + currentCur).equalsIgnoreCase(c.getId())) {
if (("economylite:" + current).equalsIgnoreCase(c.getId())) {
// This is the current currency
currencyEconService.setCurrentCurrency(c);
}
});
}
// If the current currency string failed to load set it to default
if (currencyEconService.getCurrentCurrency().equals(defaultCurrency)) {
fileManager.setValue("currencies.conf", "current", String.class, "coin");
currencyManager.forceValue("coin", "current");
currencyManager.save();
}
logger.info("Using currency: " + currencyEconService.getCurrentCurrency().getName());
}

private void initializeMessage() {
messageStorage.defaultMessages("messages");
}

public Logger getLogger() {
return logger;
}
Expand All @@ -234,12 +222,20 @@ public Game getGame() {
return game;
}

public Path getConfigDir() {
return configDir;
}

public PluginContainer getPluginContainer() {
return container;
}

public static FileManager getFileManager() {
return fileManager;
public static ConfigManager getCurrencyManager() {
return currencyManager;
}

public static ConfigManager getConfigManager() {
return configManager;
}

public static MessageStorage getMessageStorage() {
Expand Down Expand Up @@ -270,10 +266,6 @@ public static List<Module> getModules() {
return ImmutableList.of(new SqlModule(), new LoanModule());
}

public String getConfigDir() {
return configDir.toString();
}

public String getMainDir() {
return mainDir.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface PlayerEconService {

/**
* Gets the balance of a player.
*
*
* @param uuid The UUID of the player to get the balance of.
* @param currency The currency to use.
* @param cause What is getting the balance.
Expand All @@ -29,7 +29,7 @@ public interface PlayerEconService {

/**
* Sets the balance of a player.
*
*
* @param uuid The UUID of the player to set the balance of.
* @param balance The new balance of the uuid.
* @param currency The currency to use.
Expand All @@ -40,7 +40,7 @@ public interface PlayerEconService {

/**
* Removes currency from a player's balance.
*
*
* @param uuid The UUID of the player to remove currency from.
* @param amount The amount of currency to remove.
* @param currency The currency to use.
Expand All @@ -53,7 +53,7 @@ default public boolean withdraw(UUID uuid, BigDecimal amount, Currency currency,

/**
* Adds currency to a player's balance.
*
*
* @param uuid The UUID of the player to add currency to.
* @param amount The amount of currency to add.
* @param currency The currency to use.
Expand All @@ -64,18 +64,9 @@ default public boolean deposit(UUID uuid, BigDecimal amount, Currency currency,
return setBalance(uuid, getBalance(uuid, currency, cause).add(amount), currency, cause);
}

/**
* Checks if a player exists in the system with any currency.
*
* @param uuid The UUID of the player to check for.
* @param cause What is checking if the account exists.
* @return If the player exists or not.
*/
public boolean accountExists(UUID uuid, Cause cause);

/**
* Checks if a player exists in the system for the specified currency.
*
*
* @param uuid The UUID of the player to check for.
* @param currency The currency to check against.
* @param cause What is checking if the account exists.
Expand All @@ -85,15 +76,15 @@ default public boolean deposit(UUID uuid, BigDecimal amount, Currency currency,

/**
* Clears a currency from the database.
*
*
* @param currency The currency to clear.
* @param cause What is clearing the balances.
*/
public void clearCurrency(Currency currency, Cause cause);

/**
* Gets the top unique accounts registered in the EconomyLite system.
*
*
* @param start The starting account to get.
* @param end The ending account to get.
* @param cause What is getting the accounts.
Expand All @@ -103,7 +94,7 @@ default public boolean deposit(UUID uuid, BigDecimal amount, Currency currency,

/**
* Sets the balance of all players.
*
*
* @param balance The new balance.
* @param currency The currency to use.
* @param cause What caused the balance change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ default public boolean deposit(String id, BigDecimal amount, Currency currency,
return setBalance(id, getBalance(id, currency, cause).add(amount), currency, cause);
}

/**
* Checks if an account exists in the system.
*
* @param id The name of the account to check for.
* @param cause What is checking if the account exists.
* @return If the account exists or not.
*/
public boolean accountExists(String id, Cause cause);

/**
* Checks if an account exists in the system for the specified currency.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.command.spec.CommandSpec.Builder;
import org.spongepowered.api.service.economy.Currency;
import org.spongepowered.api.text.Text;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@AsyncCommand
Expand Down Expand Up @@ -105,10 +108,17 @@ public void run(CommandSource src, CommandContext args) {
PlayerServiceCommon sqlService = (PlayerServiceCommon) s;
// Load the data service
PlayerDataService dataService = new PlayerDataService();
// Get Currency Data
Map<String, Currency> cIds = new HashMap<>();
EconomyLite.getCurrencyService().getCurrencies().forEach(c -> {
cIds.put(c.getId(), c);
});
// Insert new data
dataService.getAccountsMigration().forEach(r -> {
String[] d = r.split("%-%");
sqlService.setRawData(d[0], d[1], d[2]);
if (cIds.get(d[2]) != null) {
sqlService.setRawData(d[0], d[1], cIds.get(d[2]));
}
});
src.sendMessage(messageStorage.getMessage("command.migrate.completed"));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.github.flibio.utils.commands.BaseCommandExecutor;
import io.github.flibio.utils.commands.Command;
import io.github.flibio.utils.message.MessageStorage;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void run(Player src, CommandContext args) {
src.sendMessage(messageStorage.getMessage("command.pay.invalid"));
} else {
User target = args.<User>getOne("player").get();
if (!EconomyLite.isEnabled("confirm-offline-payments") || target.isOnline()) {
if (!EconomyLite.getConfigManager().getValue(Boolean.class, false, "confirm-offline-payments") || target.isOnline()) {
// Complete the payment
pay(target, amount, src);
} else {
Expand All @@ -75,19 +76,20 @@ private void pay(User target, BigDecimal amount, Player src) {
Optional<UniqueAccount> tOpt = ecoService.getOrCreateAccount(target.getUniqueId());
if (uOpt.isPresent() && tOpt.isPresent()) {
if (uOpt.get()
.transfer(tOpt.get(), ecoService.getDefaultCurrency(), amount, Cause.of(EventContext.empty(),(EconomyLite.getInstance())))
.transfer(tOpt.get(), ecoService.getDefaultCurrency(), amount, Cause.of(EventContext.empty(), (EconomyLite.getInstance())))
.getResult().equals(ResultType.SUCCESS)) {
Text label = ecoService.getDefaultCurrency().getPluralDisplayName();
if (amount.equals(BigDecimal.ONE)) {
label = ecoService.getDefaultCurrency().getDisplayName();
}
src.sendMessage(messageStorage.getMessage("command.pay.success", "target", Text.of(targetName), "amountandlabel",
Text.of(String.format(Locale.ENGLISH, "%,.2f", amount) + " ").toBuilder().append(label).build()));
if (target instanceof Player) {
((Player) target).sendMessage(messageStorage.getMessage("command.pay.target", "amountandlabel",
Text.of(String.format(Locale.ENGLISH, "%,.2f", amount) + " ").toBuilder().append(label).build(), "sender",
uOpt.get().getDisplayName()));
}
src.sendMessage(messageStorage.getMessage("command.pay.success", "target", targetName, "amountandlabel",
String.format(Locale.ENGLISH, "%,.2f", amount) + " " + label.toPlain()));
final Text curLabel = label;
Sponge.getServer().getPlayer(target.getUniqueId()).ifPresent(p -> {
p.sendMessage(messageStorage.getMessage("command.pay.target", "amountandlabel",
String.format(Locale.ENGLISH, "%,.2f", amount) + " " + curLabel.toPlain(), "sender",
uOpt.get().getDisplayName().toPlain()));
});
} else {
src.sendMessage(messageStorage.getMessage("command.pay.failed", "target", targetName));
}
Expand Down
Loading

0 comments on commit 58350df

Please sign in to comment.