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 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 onCurrencyHoldCheck(CurrencyHoldEvent event) {
        String target = event.getAccount();

        if (!ChestShopSign.isAdminShop(target)) {
            return;
        }

        event.canHold(true);
        event.setAccount("");
    }

    @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());
    }
}

@@ -377,16 +377,102 @@
 cimal;%0A%0A
+import static com.Acrobot.ChestShop.Configuration.Properties.SERVER_ECONOMY_ACCOUNT;%0A%0A
 /**%0A * @
@@ -745,32 +745,84 @@
 dminShop(target)
+ %7C%7C event.getTarget().equals(SERVER_ECONOMY_ACCOUNT)
 ) %7B%0A            
@@ -848,35 +848,24 @@
         if (
-Properties.
 SERVER_ECONO
@@ -976,35 +976,24 @@
    target = 
-Properties.
 SERVER_ECONO
@@ -1417,32 +1417,84 @@
 dminShop(target)
+ %7C%7C event.getTarget().equals(SERVER_ECONOMY_ACCOUNT)
 ) %7B%0A            
@@ -1520,35 +1520,24 @@
         if (
-Properties.
 SERVER_ECONO
@@ -1653,35 +1653,24 @@
    target = 
-Properties.
 SERVER_ECONO
@@ -2114,32 +2114,85 @@
 dminShop(target)
+ %7C%7C event.getAccount().equals(SERVER_ECONOMY_ACCOUNT)
 ) %7B%0A            
@@ -2218,35 +2218,24 @@
         if (
-Properties.
 SERVER_ECONO
@@ -2347,35 +2347,24 @@
    target = 
-Properties.
 SERVER_ECONO
@@ -2821,32 +2821,85 @@
 dminShop(target)
+ %7C%7C event.getAccount().equals(SERVER_ECONOMY_ACCOUNT)
 ) %7B%0A            
@@ -3192,16 +3192,69 @@
 (target)
+ %7C%7C event.getAccount().equals(SERVER_ECONOMY_ACCOUNT)
 ) %7B%0A    
@@ -3292,27 +3292,16 @@
     if (
-Properties.
 SERVER_E
@@ -3453,27 +3453,16 @@
 arget = 
-Properties.
 SERVER_E

Fix possible StackOverflowExceptions