Transfer to new economy handling

No bank support... sorry @jrtc27, it's coming later!
This commit is contained in:
Acrobot 2013-07-13 23:14:10 +02:00
parent b4a569aaf7
commit bca38b8a79
21 changed files with 560 additions and 263 deletions

View File

@ -12,6 +12,9 @@ import com.Acrobot.ChestShop.Listeners.Block.BlockPlace;
import com.Acrobot.ChestShop.Listeners.Block.Break.ChestBreak;
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak;
import com.Acrobot.ChestShop.Listeners.Block.SignCreate;
import com.Acrobot.ChestShop.Listeners.Economy.Plugins.RegisterListener;
import com.Acrobot.ChestShop.Listeners.Economy.ServerAccountCorrector;
import com.Acrobot.ChestShop.Listeners.Economy.TaxModule;
import com.Acrobot.ChestShop.Listeners.Item.ItemMoveListener;
import com.Acrobot.ChestShop.Listeners.ItemInfoListener;
import com.Acrobot.ChestShop.Listeners.Modules.DiscountModule;
@ -240,6 +243,13 @@ public class ChestShop extends JavaPlugin {
private void registerModules() {
registerEvent(new DiscountModule());
registerEvent(new PriceRestrictionModule());
registerEconomicalModules();
}
private void registerEconomicalModules() {
registerEvent(new ServerAccountCorrector());
registerEvent(new TaxModule());
}
public void registerEvent(Listener listener) {

View File

@ -1,226 +1,55 @@
package com.Acrobot.ChestShop.Economy;
import com.Acrobot.Breeze.Utils.NumberUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyFormatEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uName;
import org.bukkit.World;
import org.bukkit.inventory.Inventory;
import static com.Acrobot.Breeze.Utils.NumberUtil.roundUp;
import java.math.BigDecimal;
/**
* @author Acrobot
* Economy management
*/
public class Economy {
private static EconomyManager manager = new EconomyManager();
public static boolean transactionCanFail() {
return manager.transactionCanFail();
public static String getServerAccountName() {
return Properties.SERVER_ECONOMY_ACCOUNT;
}
public static boolean isOwnerEconomicallyActive(Inventory inventory) {
return !ChestShopSign.isAdminShop(inventory) || !getServerAccountName().isEmpty();
}
public static boolean hasAccount(String player) {
return !player.isEmpty() && manager.hasAccount(uName.getName(player));
}
public static String getServerAccountName() {
return Properties.SERVER_ECONOMY_ACCOUNT;
}
public static boolean isServerAccount(String acc) {
return ChestShopSign.isAdminShop(acc);
}
public static boolean isBank(String name) {
return name.startsWith(uName.BANK_PREFIX);
}
public static boolean hasBankSupport() {
return manager.hasBankSupport();
}
public static boolean bankExists(String name) {
if (hasBankSupport()) {
return manager.bankExists(name);
} else {
return false;
}
}
public static boolean add(String name, double amount) {
if (isServerAccount(name)) {
if (!getServerAccountName().isEmpty()) {
name = getServerAccountName();
} else {
return true;
}
}
float taxAmount = isServerAccount(name) ? Properties.SERVER_TAX_AMOUNT : isBank(name) ? Properties.BANK_TAX_AMOUNT : Properties.TAX_AMOUNT;
double tax = getTax(taxAmount, amount);
if (tax != 0) {
if (!getServerAccountName().isEmpty()) {
manager.add(getServerAccountName(), tax);
}
amount -= tax;
}
name = uName.getName(name);
amount = roundUp(amount);
if (isBank(name)) {
if (hasBankSupport()) {
return manager.bankAdd(uName.stripBankPrefix(name), amount);
} else {
return false;
}
} else {
return manager.add(name, amount);
}
}
public static double getTax(float tax, double price) {
return NumberUtil.roundDown((tax / 100F) * price);
}
public static boolean subtract(String name, double amount) {
if (isServerAccount(name)) {
if (!getServerAccountName().isEmpty()) {
name = getServerAccountName();
} else {
return true;
}
}
name = uName.getName(name);
amount = roundUp(amount);
if (isBank(name)) {
if (hasBankSupport()) {
return manager.bankSubtract(uName.stripBankPrefix(name), amount);
} else {
return false;
}
} else {
return manager.subtract(name, amount);
}
}
public static boolean canHold(String name, double amount) {
if (!transactionCanFail()) {
return true;
}
if (isServerAccount(name)) {
if (!getServerAccountName().isEmpty()) {
name = getServerAccountName();
} else {
return true;
}
}
name = uName.getName(name);
amount = roundUp(amount);
if (isBank(name)) {
if (hasBankSupport()) {
if (!manager.bankAdd(name, amount)) {
return false;
} else {
manager.bankSubtract(name, amount);
}
} else {
return false;
}
} else {
if (!manager.add(name, amount)) {
return false;
} else {
manager.subtract(name, amount);
}
}
public static boolean add(String name, World world, double amount) {
CurrencyAddEvent event = new CurrencyAddEvent(BigDecimal.valueOf(amount), name, world);
ChestShop.callEvent(event);
return true;
}
public static boolean hasEnough(String name, double amount) {
if (amount <= 0) {
return true;
}
public static boolean subtract(String name, World world, double amount) {
CurrencySubtractEvent event = new CurrencySubtractEvent(BigDecimal.valueOf(amount), name, world);
ChestShop.callEvent(event);
if (isServerAccount(name)) {
if (!getServerAccountName().isEmpty()) {
name = getServerAccountName();
} else {
return true;
}
}
name = uName.getName(name);
amount = roundUp(amount);
if (isBank(name)) {
if (hasBankSupport()) {
return manager.bankHasEnough(uName.stripBankPrefix(name), amount);
} else {
return false;
}
} else {
return manager.hasEnough(name, amount);
}
return true;
}
public static double getBalance(String name) {
if (isServerAccount(name)) {
if (!getServerAccountName().isEmpty()) {
name = getServerAccountName();
} else {
return Double.MAX_VALUE;
}
}
public static boolean hasEnough(String name, World world, double amount) {
CurrencyCheckEvent event = new CurrencyCheckEvent(BigDecimal.valueOf(amount), name, world);
ChestShop.callEvent(event);
name = uName.getName(name);
if (isBank(name)) {
if (hasBankSupport()) {
return manager.bankBalance(uName.stripBankPrefix(name));
} else {
return 0;
}
} else {
return manager.balance(name);
}
return event.hasEnough();
}
public static String formatBalance(double amount) {
return manager.format(roundUp(amount));
}
CurrencyFormatEvent event = new CurrencyFormatEvent(BigDecimal.valueOf(amount));
ChestShop.callEvent(event);
public static boolean isBankOwner(String player, String bank) {
if (hasBankSupport()) {
return manager.isBankOwner(player, bank);
} else {
return false;
}
}
public static boolean isBankMember(String player, String bank) {
if (hasBankSupport()) {
return manager.isBankMember(player, bank);
} else {
return false;
}
}
public static void setPlugin(EconomyManager plugin) {
manager = plugin;
}
public static EconomyManager getManager() {
return manager;
}
public static boolean isLoaded() {
return manager.getClass() != EconomyManager.class;
return event.getFormattedAmount();
}
}

View File

@ -29,7 +29,7 @@ public class AccountCheckEvent extends Event {
/**
* @return Event's outcome (does the account exist?)
*/
public boolean getOutcome() {
public boolean hasAccount() {
return outcome;
}
@ -38,7 +38,7 @@ public class AccountCheckEvent extends Event {
*
* @param outcome Outcome of the check
*/
public void setOutcome(boolean outcome) {
public void hasAccount(boolean outcome) {
this.outcome = outcome;
}

View File

@ -15,6 +15,8 @@ import java.math.BigDecimal;
public class CurrencyAddEvent extends Event {
private static final HandlerList handlers = new HandlerList();
boolean added;
private BigDecimal amount;
private String target;
private World world;
@ -63,6 +65,22 @@ public class CurrencyAddEvent extends Event {
this.amount = BigDecimal.valueOf(amount);
}
/**
* @return Was the money already added to the account?
*/
public boolean isAdded() {
return added;
}
/**
* Set if the money was added to the account
*
* @param added Was the money added?
*/
public void setAdded(boolean added) {
this.added = added;
}
/**
* @return The world in which the transaction occurs
*/

View File

@ -1,7 +1,86 @@
package com.Acrobot.ChestShop.Events.Economy;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.math.BigDecimal;
/**
* Created by Andrzej on 16.06.13.
* Checks the amount of currency available
*
* @author Acrobot
*/
public class CurrencyAmountEvent {
public class CurrencyAmountEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private BigDecimal amount = BigDecimal.ZERO;
private String account;
private World world;
public CurrencyAmountEvent(String account, World world) {
this.account = account;
this.world = world;
}
public CurrencyAmountEvent(Player player) {
this(player.getName(), player.getWorld());
}
/**
* @return Amount of currency
*/
public BigDecimal getAmount() {
return amount;
}
/**
* @return Amount of currency, as a double
* @deprecated Use {@link #getAmount()} if possible
*/
public double getDoubleAmount() {
return amount.doubleValue();
}
/**
* Sets the amount of currency
*
* @param amount Amount available
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
/**
* Sets the amount of currency
*
* @param amount Amount available
* @deprecated Use {@link #setAmount(java.math.BigDecimal)} if possible
*/
public void setAmount(double amount) {
this.amount = BigDecimal.valueOf(amount);
}
/**
* @return The world in which the check occurs
*/
public World getWorld() {
return world;
}
/**
* @return Account that is checked
*/
public String getAccount() {
return account;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,13 +1,14 @@
package com.Acrobot.ChestShop.Events.Economy;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.math.BigDecimal;
/**
* Represents a check for the existance of specified currency amount
* Represents a check for the existence of specified currency amount
*
* @author Acrobot
*/
@ -26,10 +27,14 @@ public class CurrencyCheckEvent extends Event {
this.world = world;
}
public CurrencyCheckEvent(BigDecimal amount, Player player) {
this(amount, player.getName(), player.getWorld());
}
/**
* @return Does the account have enough currency available?
*/
public boolean getOutcome() {
public boolean hasEnough() {
return outcome;
}

View File

@ -14,7 +14,7 @@ public class CurrencyFormatEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final BigDecimal amount;
private String formattedAmount;
private String formattedAmount = "";
public CurrencyFormatEvent(BigDecimal amount) {
this.amount = amount;

View File

@ -1,7 +1,114 @@
package com.Acrobot.ChestShop.Events.Economy;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.math.BigDecimal;
/**
* Checks if the account can hold the amount of currency
*
* @author Acrobot
*/
public class CurrencyHoldEvent {
public class CurrencyHoldEvent extends Event {
private static final HandlerList handlers = new HandlerList();
boolean canHold = true;
private BigDecimal amount;
private String account;
private World world;
public CurrencyHoldEvent(BigDecimal amount, String account, World world) {
this.amount = amount;
this.account = account;
this.world = world;
}
public CurrencyHoldEvent(BigDecimal amount, Player target) {
this(amount, target.getName(), target.getWorld());
}
/**
* @return Can the account hold the amount of currency?
*/
public boolean canHold() {
return canHold;
}
/**
* Sets if the account can hold the amount of currency
*
* @param canHold Can the account hold the currency?
*/
public void canHold(boolean canHold) {
this.canHold = canHold;
}
/**
* @return Amount of currency
*/
public BigDecimal getAmount() {
return amount;
}
/**
* @return Amount of currency, as a double
* @deprecated Use {@link #getAmount()} if possible
*/
public double getDoubleAmount() {
return amount.doubleValue();
}
/**
* Sets the amount of currency to check
*
* @param amount Amount to check
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
/**
* Sets the amount of currency to check
*
* @param amount Amount to check
* @deprecated Use {@link #setAmount(java.math.BigDecimal)} if possible
*/
public void setAmount(double amount) {
this.amount = BigDecimal.valueOf(amount);
}
/**
* @return The world in which the check occurs
*/
public World getWorld() {
return world;
}
/**
* @return Account that is checked
*/
public String getAccount() {
return account;
}
/**
* Sets the account name
*
* @param account Account name
*/
public void setAccount(String account) {
this.account = account;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -15,6 +15,8 @@ import java.math.BigDecimal;
public class CurrencySubtractEvent extends Event {
private static final HandlerList handlers = new HandlerList();
boolean subtracted;
private BigDecimal amount;
private String target;
private World world;
@ -63,6 +65,22 @@ public class CurrencySubtractEvent extends Event {
this.amount = BigDecimal.valueOf(amount);
}
/**
* @return Was the money already subtracted?
*/
public boolean isSubtracted() {
return subtracted;
}
/**
* Set if the money was subtracted from the account
*
* @param subtracted Was the money subtracted?
*/
public void setSubtracted(boolean subtracted) {
this.subtracted = subtracted;
}
/**
* @return The world in which the transaction occurs
*/

View File

@ -18,6 +18,7 @@ public class CurrencyTransferEvent extends Event {
private World world;
private String sender;
private String receiver;
private boolean success;
public CurrencyTransferEvent(BigDecimal amount, String sender, String receiver, World world) {
this.amount = amount;
@ -65,6 +66,22 @@ public class CurrencyTransferEvent extends Event {
this.amount = BigDecimal.valueOf(amount);
}
/**
* @return If the currency has been successfully transferred
*/
public boolean hasBeenTransferred() {
return success;
}
/**
* Sets the transaction's outcome
*
* @param success If the currency has been successfully transferred
*/
public void setTransferred(boolean success) {
this.success = success;
}
/**
* @return The world in which the transaction occurs
*/

View File

@ -8,6 +8,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import javax.annotation.Nullable;
import java.math.BigDecimal;
/**
* Represents a Register connector
@ -31,6 +32,15 @@ public class RegisterListener implements Listener {
return new RegisterListener(method);
}
@EventHandler
public void onAmountCheck(CurrencyAmountEvent event) {
if (!event.getAmount().equals(BigDecimal.ZERO)) {
return;
}
event.setAmount(paymentMethod.getAccount(event.getAccount()).balance());
}
@EventHandler
public void onCurrencyCheck(CurrencyCheckEvent event) {
if (event.hasEnough()) {
@ -93,7 +103,7 @@ public class RegisterListener implements Listener {
return;
}
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(event.getAmount(), event.getReceiver(), event.getWorld());
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld());
ChestShop.callEvent(currencyAddEvent);
}
}

View File

@ -1,5 +1,6 @@
package com.Acrobot.ChestShop.Listeners.Economy.Plugins;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.Economy.*;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
@ -10,6 +11,7 @@ import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredServiceProvider;
import javax.annotation.Nullable;
import java.math.BigDecimal;
/**
* Represents a Vault connector
@ -44,6 +46,15 @@ public class VaultListener implements Listener {
}
}
@EventHandler
public void onAmountCheck(CurrencyAmountEvent event) {
if (!event.getAmount().equals(BigDecimal.ZERO)) {
return;
}
event.setAmount(provider.getBalance(event.getAccount(), event.getWorld().getName()));
}
@EventHandler
public void onCurrencyCheck(CurrencyCheckEvent event) {
if (event.hasEnough()) {
@ -52,8 +63,8 @@ public class VaultListener implements Listener {
World world = event.getWorld();
if (!provider.has(event.getAccount(), world.getName(), event.getDoubleAmount())) {
event.hasEnough(false);
if (provider.has(event.getAccount(), world.getName(), event.getDoubleAmount())) {
event.hasEnough(true);
}
}
@ -104,17 +115,30 @@ public class VaultListener implements Listener {
}
@EventHandler
public void onCurrencyTransfer(CurrencyTransferEvent event) {
public static void onCurrencyTransfer(CurrencyTransferEvent event) {
if (event.hasBeenTransferred()) {
return;
}
World world = event.getWorld();
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), event.getSender(), event.getWorld());
ChestShop.callEvent(currencySubtractEvent);
EconomyResponse response = provider.withdrawPlayer(event.getSender(), world.getName(), event.getDoubleAmount());
if (response.transactionSuccess()) {
provider.depositPlayer(event.getReceiver(), world.getName(), event.getDoubleAmount());
if (!currencySubtractEvent.isSubtracted()) {
return;
}
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(currencySubtractEvent.getAmount(), event.getReceiver(), event.getWorld());
ChestShop.callEvent(currencyAddEvent);
}
@EventHandler
public void onCurrencyHoldCheck(CurrencyHoldEvent event) {
EconomyResponse response = provider.depositPlayer(event.getAccount(), event.getWorld().getName(), event.getDoubleAmount());
if (!response.transactionSuccess()) {
event.canHold(false);
}
provider.withdrawPlayer(event.getAccount(), event.getWorld().getName(), event.getDoubleAmount());
}
}

View File

@ -1,7 +1,101 @@
package com.Acrobot.ChestShop.Listeners.Economy;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.Economy.*;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
/**
* @author Acrobot
*/
public class ServerAccountCorrector {
public class ServerAccountCorrector implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public static void onCurrencyAdd(CurrencyAddEvent event) {
String target = event.getTarget();
if (!ChestShopSign.isAdminShop(target)) {
return;
}
if (Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
event.setAdded(true);
return;
} else {
target = Properties.SERVER_ECONOMY_ACCOUNT;
}
event.setAdded(true);
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencyAddEvent);
}
@EventHandler(priority = EventPriority.LOWEST)
public static void onCurrencySubtract(CurrencySubtractEvent event) {
String target = event.getTarget();
if (!ChestShopSign.isAdminShop(target)) {
return;
}
if (Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
event.setSubtracted(true);
return;
} else {
target = Properties.SERVER_ECONOMY_ACCOUNT;
}
event.setSubtracted(true);
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencySubtractEvent);
}
@EventHandler(priority = EventPriority.LOWEST)
public static void onCurrencyCheck(CurrencyCheckEvent event) {
String target = event.getAccount();
if (!ChestShopSign.isAdminShop(target)) {
return;
}
if (Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
event.hasEnough(true);
return;
} else {
target = Properties.SERVER_ECONOMY_ACCOUNT;
}
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencyCheckEvent);
event.hasEnough(currencyCheckEvent.hasEnough());
}
@EventHandler(priority = EventPriority.LOWEST)
public static void onBalanceCheck(CurrencyAmountEvent event) {
String target = event.getAccount();
if (!ChestShopSign.isAdminShop(target)) {
return;
}
if (Properties.SERVER_ECONOMY_ACCOUNT.isEmpty()) {
event.setAmount(BigDecimal.valueOf(Double.MAX_VALUE));
return;
} else {
target = Properties.SERVER_ECONOMY_ACCOUNT;
}
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(target, event.getWorld());
ChestShop.callEvent(currencyAmountEvent);
event.setAmount(currencyAmountEvent.getAmount());
}
}

View File

@ -1,7 +1,51 @@
package com.Acrobot.ChestShop.Listeners.Economy;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
/**
* @author Acrobot
*/
public class TaxModule {
public class TaxModule implements Listener {
private static BigDecimal getTax(BigDecimal price, float taxAmount) {
return price.multiply(BigDecimal.valueOf(taxAmount).divide(BigDecimal.valueOf(100)));
}
private static boolean isServerAccount(String name) {
return ChestShopSign.isAdminShop(name);
}
@EventHandler(priority = EventPriority.LOW)
public static void onCurrencyAdd(CurrencyAddEvent event) {
String target = event.getTarget();
if (target.equals(Economy.getServerAccountName())) {
return;
}
float taxAmount = isServerAccount(target) ? Properties.SERVER_TAX_AMOUNT : Properties.TAX_AMOUNT;
if (taxAmount == 0) {
return;
}
BigDecimal tax = getTax(event.getAmount(), taxAmount);
if (!Economy.getServerAccountName().isEmpty()) {
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(tax, Economy.getServerAccountName(), event.getWorld());
ChestShop.callEvent(currencyAddEvent);
}
event.setAmount(event.getAmount().subtract(tax));
}
}

View File

@ -1,8 +1,10 @@
package com.Acrobot.ChestShop.Listeners.PostShopCreation;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
@ -10,6 +12,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
@ -36,7 +40,8 @@ public class CreationFeeGetter implements Listener {
return;
}
Economy.subtract(player.getName(), shopCreationPrice);
CurrencySubtractEvent subtractionEvent = new CurrencySubtractEvent(BigDecimal.valueOf(shopCreationPrice), player);
ChestShop.callEvent(subtractionEvent);
player.sendMessage(Messages.prefix(Messages.SHOP_FEE_PAID.replace("%amount", Economy.formatBalance(shopCreationPrice))));
}

View File

@ -1,10 +1,15 @@
package com.Acrobot.ChestShop.Listeners.PostTransaction;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
@ -18,11 +23,11 @@ public class EconomicModule implements Listener {
return;
}
if (Economy.isOwnerEconomicallyActive(event.getOwnerInventory())) {
Economy.add(event.getOwner().getName(), event.getPrice());
}
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(BigDecimal.valueOf(event.getPrice()), event.getOwner().getName(), event.getSign().getWorld());
ChestShop.callEvent(currencyAddEvent);
Economy.subtract(event.getClient().getName(), event.getPrice());
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
ChestShop.callEvent(currencySubtractEvent);
}
@EventHandler
@ -31,10 +36,10 @@ public class EconomicModule implements Listener {
return;
}
if (Economy.isOwnerEconomicallyActive(event.getOwnerInventory())) {
Economy.subtract(event.getOwner().getName(), event.getPrice());
}
CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(BigDecimal.valueOf(event.getPrice()), event.getOwner().getName(), event.getSign().getWorld());
ChestShop.callEvent(currencySubtractEvent);
Economy.add(event.getClient().getName(), event.getPrice());
CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
ChestShop.callEvent(currencyAddEvent);
}
}

View File

@ -1,7 +1,8 @@
package com.Acrobot.ChestShop.Listeners.PreShopCreation;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
@ -9,6 +10,8 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Events.PreShopCreationEvent.CreationOutcome.NOT_ENOUGH_MONEY;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
@ -36,7 +39,10 @@ public class MoneyChecker implements Listener {
return;
}
if (!Economy.hasEnough(player.getName(), shopCreationPrice)) {
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(shopCreationPrice), player);
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
event.setOutcome(NOT_ENOUGH_MONEY);
}
}

View File

@ -22,24 +22,6 @@ public class NameChecker implements Listener {
String name = event.getSignLine(NAME_LINE);
Player player = event.getPlayer();
if (name.startsWith(uName.BANK_PREFIX)) {
name = uName.stripBankPrefix(name);
boolean bankExists = Economy.bankExists(name);
boolean hasAccess = Economy.hasBankSupport() && Permission.has(player, Permission.BANK);
if (Properties.BANK_MEMBERS_ALLOWED) {
hasAccess = hasAccess && Economy.isBankMember(player.getName(), name);
} else {
hasAccess = hasAccess && Economy.isBankOwner(player.getName(), name);
}
if (!bankExists || (!hasAccess && !Permission.has(player, Permission.ADMIN))) {
event.setSignLine(NAME_LINE, uName.stripName(player));
}
return;
}
if (name.isEmpty() || (!uName.canUseName(player, name) && !Permission.has(player, Permission.ADMIN))) {
String shortName = uName.stripName(player);
event.setSignLine(NAME_LINE, shortName);

View File

@ -1,13 +1,17 @@
package com.Acrobot.ChestShop.Listeners.PreTransaction;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Events.PreTransactionEvent.TransactionOutcome.*;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL;
@ -16,8 +20,9 @@ import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL
* @author Acrobot
*/
public class AmountAndPriceChecker implements Listener {
@EventHandler
public static void onItemCheck(PreTransactionEvent event) {
public static void onBuyItemCheck(PreTransactionEvent event) {
if (event.isCancelled() || event.getTransactionType() != BUY) {
return;
}
@ -25,7 +30,10 @@ public class AmountAndPriceChecker implements Listener {
ItemStack[] stock = event.getStock();
Inventory ownerInventory = event.getOwnerInventory();
if (!Economy.hasEnough(event.getClient().getName(), event.getPrice())) {
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
event.setCancelled(CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY);
return;
}
@ -44,7 +52,10 @@ public class AmountAndPriceChecker implements Listener {
ItemStack[] stock = event.getStock();
Inventory clientInventory = event.getClientInventory();
if (Economy.isOwnerEconomicallyActive(event.getOwnerInventory()) && !Economy.hasEnough(event.getOwner().getName(), event.getPrice())) {
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()), event.getOwner().getName(), event.getSign().getWorld());
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
event.setCancelled(SHOP_DOES_NOT_HAVE_ENOUGH_MONEY);
return;
}

View File

@ -2,7 +2,11 @@ package com.Acrobot.ChestShop.Listeners.PreTransaction;
import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAmountEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyCheckEvent;
import com.Acrobot.ChestShop.Events.Economy.CurrencyHoldEvent;
import com.Acrobot.ChestShop.Events.PreTransactionEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -11,6 +15,7 @@ import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
@ -22,6 +27,7 @@ import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.SELL
* @author Acrobot
*/
public class PartialTransactionModule implements Listener {
@EventHandler(priority = EventPriority.LOW)
public static void onPreBuyTransaction(PreTransactionEvent event) {
if (event.isCancelled() || event.getTransactionType() != BUY) {
@ -29,14 +35,20 @@ public class PartialTransactionModule implements Listener {
}
Player client = event.getClient();
String clientName = client.getName();
ItemStack[] stock = event.getStock();
double price = event.getPrice();
double pricePerItem = event.getPrice() / InventoryUtil.countItems(stock);
double walletMoney = Economy.getBalance(clientName);
if (!Economy.hasEnough(clientName, price)) {
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(client);
ChestShop.callEvent(currencyAmountEvent);
BigDecimal walletMoney = currencyAmountEvent.getAmount();
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(price), client);
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
int amountAffordable = getAmountOfAffordableItems(walletMoney, pricePerItem);
if (amountAffordable < 1) {
@ -50,7 +62,10 @@ public class PartialTransactionModule implements Listener {
String seller = event.getOwner().getName();
if (!Economy.canHold(seller, price)) {
CurrencyHoldEvent currencyHoldEvent = new CurrencyHoldEvent(BigDecimal.valueOf(price), seller, client.getWorld());
ChestShop.callEvent(currencyHoldEvent);
if (!currencyHoldEvent.canHold()) {
event.setCancelled(SHOP_DEPOSIT_FAILED);
return;
}
@ -77,29 +92,41 @@ public class PartialTransactionModule implements Listener {
return;
}
String player = event.getClient().getName();
Player client = event.getClient();
String ownerName = event.getOwner().getName();
ItemStack[] stock = event.getStock();
double price = event.getPrice();
double pricePerItem = event.getPrice() / InventoryUtil.countItems(stock);
double walletMoney = Economy.getBalance(ownerName);
if (Economy.isOwnerEconomicallyActive(event.getOwnerInventory()) && !Economy.hasEnough(ownerName, price)) {
int amountAffordable = getAmountOfAffordableItems(walletMoney, pricePerItem);
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(ownerName, client.getWorld());
ChestShop.callEvent(currencyAmountEvent);
if (amountAffordable < 1) {
event.setCancelled(SHOP_DOES_NOT_HAVE_ENOUGH_MONEY);
return;
BigDecimal walletMoney = currencyAmountEvent.getAmount();
if (Economy.isOwnerEconomicallyActive(event.getOwnerInventory())) {
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(price), ownerName, client.getWorld());
ChestShop.callEvent(currencyCheckEvent);
if (!currencyCheckEvent.hasEnough()) {
int amountAffordable = getAmountOfAffordableItems(walletMoney, pricePerItem);
if (amountAffordable < 1) {
event.setCancelled(SHOP_DOES_NOT_HAVE_ENOUGH_MONEY);
return;
}
event.setPrice(amountAffordable * pricePerItem);
event.setStock(getCountedItemStack(stock, amountAffordable));
}
event.setPrice(amountAffordable * pricePerItem);
event.setStock(getCountedItemStack(stock, amountAffordable));
}
stock = event.getStock();
if (!Economy.canHold(player, price)) {
CurrencyHoldEvent currencyHoldEvent = new CurrencyHoldEvent(BigDecimal.valueOf(price), client);
ChestShop.callEvent(currencyHoldEvent);
if (!currencyHoldEvent.canHold()) {
event.setCancelled(CLIENT_DEPOSIT_FAILED);
return;
}
@ -118,8 +145,8 @@ public class PartialTransactionModule implements Listener {
}
}
private static int getAmountOfAffordableItems(double walletMoney, double pricePerItem) {
return (int) Math.floor(walletMoney / pricePerItem);
private static int getAmountOfAffordableItems(BigDecimal walletMoney, double pricePerItem) {
return (int) Math.floor(walletMoney.doubleValue() / pricePerItem);
}
private static ItemStack[] getItems(ItemStack[] stock, Inventory inventory) {

View File

@ -1,8 +1,10 @@
package com.Acrobot.ChestShop.Listeners;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Utils.uName;
@ -10,6 +12,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import java.math.BigDecimal;
import static com.Acrobot.ChestShop.Permission.NOFEE;
import static com.Acrobot.ChestShop.Signs.ChestShopSign.NAME_LINE;
@ -26,7 +30,9 @@ public class ShopRefundListener implements Listener {
}
String owner = uName.getName(event.getSign().getLine(NAME_LINE));
Economy.add(owner, refundPrice);
CurrencyAddEvent currencyEvent = new CurrencyAddEvent(BigDecimal.valueOf(refundPrice), owner, event.getSign().getWorld());
ChestShop.callEvent(currencyEvent);
String message = Messages.SHOP_REFUNDED.replace("%amount", Economy.formatBalance(refundPrice));
event.getDestroyer().sendMessage(Messages.prefix(message));