Starting balance support and improved logging.

This commit is contained in:
AppleDash 2016-06-13 23:29:26 -04:00
parent 1c54dc8085
commit ac956f7a87
8 changed files with 69 additions and 9 deletions

View File

@ -1,5 +1,5 @@
group 'org.appledash'
version '0.1.3-SNAPSHOT'
version '0.1.4-SNAPSHOT'
apply plugin: 'java'

View File

@ -6,6 +6,7 @@ import org.appledash.saneeconomy.economy.Currency;
import org.appledash.saneeconomy.economy.EconomyManager;
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendFlatfile;
import org.appledash.saneeconomy.listeners.JoinQuitListener;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
@ -29,15 +30,15 @@ public class SaneEconomy extends JavaPlugin {
new File(getDataFolder(), "config.yml").delete();
saveDefaultConfig();
getLogger().setLevel(Level.ALL);
getLogger().fine("Initializing currency...");
getLogger().info("Initializing currency...");
Currency currency = Currency.fromConfig(getConfig(), "currency");
getLogger().fine("Initialized currency: " + currency.getPluralName());
getLogger().info("Initialized currency: " + currency.getPluralName());
EconomyStorageBackend backend;
getLogger().fine("Initializing economy storage backend...");
getLogger().info("Initializing economy storage backend...");
String backendType = getConfig().getString("backend.type");
/* Flatfile database, currently only supported. */
@ -45,7 +46,7 @@ public class SaneEconomy extends JavaPlugin {
String backendFileName = getConfig().getString("backend.file", "economy.db");
File backendFile = new File(getDataFolder(), backendFileName);
backend = new EconomyStorageBackendFlatfile(backendFile);
getLogger().fine("Initialized flatfile backend with file " + backendFile.getAbsolutePath());
getLogger().info("Initialized flatfile backend with file " + backendFile.getAbsolutePath());
} else {
getLogger().severe("Unknown storage backend " + backendType + "!");
shutdown();
@ -55,10 +56,14 @@ public class SaneEconomy extends JavaPlugin {
economyManager = new EconomyManager(currency, backend);
getLogger().fine("Initializing commands...");
getLogger().info("Initializing commands...");
getCommand("balance").setExecutor(new BalanceCommand());
getCommand("ecoadmin").setExecutor(new EconomyAdminCommand());
getLogger().fine("Commands initialized!");
getLogger().info("Commands initialized!");
getLogger().info("Initializing listeners...");
getServer().getPluginManager().registerEvents(new JoinQuitListener(this), this);
getLogger().info("Initialized listeners!");
}
private void shutdown(){

View File

@ -35,6 +35,15 @@ public class EconomyManager {
return currency.formatAmount(backend.getBalance(player));
}
/**
* Check whether a player has used the economy system before.
* @param player Player to check
* @return True if they have used the economy system before, false otherwise
*/
public boolean accountExists(Player player) {
return backend.accountExists(player);
}
/**
* Get a player's balance.
* @param targetPlayer Player to get balance of

View File

@ -9,6 +9,13 @@ import org.bukkit.entity.Player;
* Represents our economy storage backend - whatever we're using to store economy data.
*/
public interface EconomyStorageBackend {
/**
* Check whether a player has used the economy system before.
* @param player Player
* @return True if they have, false otherwise.
*/
boolean accountExists(Player player);
/**
* Get the balance of a player.
* @param player Player

View File

@ -62,6 +62,11 @@ public class EconomyStorageBackendFlatfile implements EconomyStorageBackend {
}
}
@Override
public boolean accountExists(Player player) {
return playerBalances.containsKey(player.getUniqueId());
}
@Override
public double getBalance(Player player) {
if (!playerBalances.containsKey(player.getUniqueId())) {

View File

@ -0,0 +1,31 @@
package org.appledash.saneeconomy.listeners;
import org.appledash.saneeconomy.SaneEconomy;
import org.appledash.saneeconomy.utils.MessageUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
/**
* Created by AppleDash on 6/13/2016.
* Blackjack is still best pony.
*/
public class JoinQuitListener implements Listener {
private SaneEconomy plugin;
public JoinQuitListener(SaneEconomy plugin) {
this.plugin = plugin;
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent evt) {
Player player = evt.getPlayer();
double startBalance = plugin.getConfig().getDouble("economy.start-balance", 0.0D);
/* A starting balance is configured AND they haven't been given it yet. */
if (startBalance > 0 && !plugin.getEconomyManager().accountExists(player)) {
plugin.getEconomyManager().setBalance(player, startBalance);
MessageUtils.sendMessage(player, "You've been issued a starting balance of %s!", plugin.getEconomyManager().getCurrency().formatAmount(startBalance));
}
}
}

View File

@ -8,4 +8,7 @@ currency:
format: '0.00'
chat:
prefix: '&b[&9SaneEcon&b]&r '
prefix: '&b[&9SaneEcon&b]&r '
economy:
start-balance: 1000.0

View File

@ -1,6 +1,6 @@
name: SaneEconomy
main: org.appledash.saneeconomy.SaneEconomy
version: 0.1.2
version: 0.1.4
commands:
balance:
aliases: [bal]