2013-07-13 23:12:02 +02:00
package com.Acrobot.ChestShop.Listeners.Economy ;
2013-07-13 23:14:10 +02:00
import com.Acrobot.ChestShop.ChestShop ;
import com.Acrobot.ChestShop.Configuration.Properties ;
import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent ;
2019-04-25 19:44:22 +02:00
import com.Acrobot.ChestShop.Events.Economy.CurrencyTransferEvent ;
import com.Acrobot.ChestShop.Permission ;
2014-06-14 20:46:59 +02:00
import com.Acrobot.ChestShop.UUIDs.NameManager ;
2013-07-13 23:14:10 +02:00
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.Listener ;
import java.math.BigDecimal ;
2014-06-14 20:46:59 +02:00
import java.util.UUID ;
2013-07-13 23:14:10 +02:00
2013-07-13 23:12:02 +02:00
/ * *
* @author Acrobot
* /
2013-07-13 23:14:10 +02:00
public class TaxModule implements Listener {
2021-10-15 16:17:09 +02:00
private static final String TAX_RECEIVED_MESSAGE = " Applied a tax of %1$f percent (%2$.2f) to the received amount for a resulting price of %3$.2f " ;
2021-10-15 16:39:01 +02:00
private static final String TAX_SENT_MESSAGE = " Reduced buy price by tax of %1$f percent (%2$.2f) for a resulting price of %3$.2f as the buyer has the buy tax bypass permission " ;
2013-07-13 23:14:10 +02:00
2019-04-25 19:44:22 +02:00
private static float getTax ( UUID partner ) {
float taxAmount = NameManager . isAdminShop ( partner ) | | NameManager . isServerEconomyAccount ( partner )
? Properties . SERVER_TAX_AMOUNT : Properties . TAX_AMOUNT ;
if ( taxAmount = = 0 ) {
return 0 ;
}
return taxAmount ;
2013-07-13 23:14:10 +02:00
}
2019-04-25 19:44:22 +02:00
private static BigDecimal getTaxAmount ( BigDecimal price , float taxAmount ) {
2019-04-28 16:41:20 +02:00
return price . multiply ( BigDecimal . valueOf ( taxAmount ) ) . divide ( BigDecimal . valueOf ( 100 ) , Properties . PRICE_PRECISION , BigDecimal . ROUND_HALF_UP ) ;
2013-07-13 23:14:10 +02:00
}
@EventHandler ( priority = EventPriority . LOW )
2019-04-25 19:44:22 +02:00
public static void onCurrencyTransfer ( CurrencyTransferEvent event ) {
2020-05-21 02:08:00 +02:00
if ( event . wasHandled ( ) ) {
2014-02-10 12:27:52 +01:00
return ;
}
2019-04-25 19:44:22 +02:00
float taxAmount = getTax ( event . getPartner ( ) ) ;
2013-07-13 23:14:10 +02:00
if ( taxAmount = = 0 ) {
return ;
}
2019-04-25 19:44:22 +02:00
if ( ! Permission . has ( event . getInitiator ( ) , event . getDirection ( ) = = CurrencyTransferEvent . Direction . PARTNER ? Permission . NO_BUY_TAX : Permission . NO_SELL_TAX ) ) {
if ( ! NameManager . isServerEconomyAccount ( event . getReceiver ( ) ) ) {
BigDecimal tax = getTaxAmount ( event . getAmountReceived ( ) , taxAmount ) ;
2021-10-15 16:17:09 +02:00
BigDecimal taxedAmount = event . getAmountReceived ( ) . subtract ( tax ) ;
event . setAmountReceived ( taxedAmount ) ;
2019-04-25 19:44:22 +02:00
if ( NameManager . getServerEconomyAccount ( ) ! = null ) {
ChestShop . callEvent ( new CurrencyAddEvent (
tax ,
NameManager . getServerEconomyAccount ( ) . getUuid ( ) ,
event . getWorld ( ) ) ) ;
}
2021-10-15 16:17:09 +02:00
ChestShop . getBukkitLogger ( ) . info ( String . format ( TAX_RECEIVED_MESSAGE , taxAmount , tax , taxedAmount ) ) ;
2019-04-25 19:44:22 +02:00
}
} else if ( event . getDirection ( ) = = CurrencyTransferEvent . Direction . PARTNER & & Permission . has ( event . getInitiator ( ) , Permission . NO_BUY_TAX ) ) {
2021-10-15 16:39:01 +02:00
// Reduce paid amount as the buyer has permission to not pay taxes
2021-10-15 16:17:09 +02:00
BigDecimal taxSent = getTaxAmount ( event . getAmountSent ( ) , taxAmount ) ;
BigDecimal taxedSentAmount = event . getAmountSent ( ) . subtract ( taxSent ) ;
event . setAmountSent ( taxedSentAmount ) ;
ChestShop . getBukkitLogger ( ) . info ( String . format ( TAX_SENT_MESSAGE , taxAmount , taxSent , taxedSentAmount ) ) ;
2021-10-15 16:39:01 +02:00
// Reduce the amount that the seller receives anyways even though tax wasn't paid as that shouldn't make a difference for the seller
2021-10-15 16:17:09 +02:00
BigDecimal taxReceived = getTaxAmount ( event . getAmountReceived ( ) , taxAmount ) ;
2021-10-15 16:26:44 +02:00
BigDecimal taxedReceivedAmount = event . getAmountReceived ( ) . subtract ( taxReceived ) ;
2021-10-15 16:17:09 +02:00
event . setAmountReceived ( taxedReceivedAmount ) ;
ChestShop . getBukkitLogger ( ) . info ( String . format ( TAX_RECEIVED_MESSAGE , taxAmount , taxReceived , taxedReceivedAmount ) ) ;
2013-07-13 23:14:10 +02:00
}
}
2013-07-13 23:12:02 +02:00
}