Trigger transaction message from CurrencyTransferEvent and consider tax (#534)

Triggers TransactionMessageSender from CurrencyTransferEvent, wrapping TransactionEvent inside of it.
Add logic to showamount after taxes on the message.
This commit is contained in:
Felipe Foschiera 2022-10-12 12:37:18 -03:00 committed by GitHub
parent 34df7e368a
commit 9842f47f37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 33 deletions

View File

@ -1,5 +1,6 @@
package com.Acrobot.ChestShop.Events.Economy; package com.Acrobot.ChestShop.Events.Economy;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event; import org.bukkit.event.Event;
@ -25,26 +26,32 @@ public class CurrencyTransferEvent extends EconomicEvent {
private Direction direction; private Direction direction;
private final TransactionEvent transactionEvent;
public CurrencyTransferEvent(BigDecimal amount, Player initiator, UUID partner, Direction direction) { public CurrencyTransferEvent(BigDecimal amount, Player initiator, UUID partner, Direction direction) {
this(amount, amount, initiator, partner, direction); this(amount, amount, initiator, partner, direction);
} }
public CurrencyTransferEvent(BigDecimal amountSent, BigDecimal amountReceived, Player initiator, UUID partner, Direction direction) { public CurrencyTransferEvent(BigDecimal amountSent, BigDecimal amountReceived, Player initiator, UUID partner, Direction direction) {
this(amountSent, amountReceived, initiator, partner, direction, null);
}
public CurrencyTransferEvent(BigDecimal amount, Player initiator, UUID partner, Direction direction, TransactionEvent transactionEvent) {
this(amount, amount, initiator, partner, direction, transactionEvent);
}
public CurrencyTransferEvent(BigDecimal amountSent, BigDecimal amountReceived, Player initiator, UUID partner, Direction direction, TransactionEvent transactionEvent) {
this.amountSent = amountSent; this.amountSent = amountSent;
this.amountReceived = amountReceived; this.amountReceived = amountReceived;
this.initiator = initiator; this.initiator = initiator;
this.partner = partner; this.partner = partner;
this.direction = direction; this.direction = direction;
this.transactionEvent = transactionEvent;
} }
/**
* @deprecated Use {{@link #CurrencyTransferEvent(BigDecimal, Player, UUID, Direction)}
*/
@Deprecated
public CurrencyTransferEvent(double amount, Player initiator, UUID partner, Direction direction) {
this(BigDecimal.valueOf(amount), initiator, partner, direction);
}
/** /**
* @return Amount of currency sent * @return Amount of currency sent
@ -150,6 +157,15 @@ public class CurrencyTransferEvent extends EconomicEvent {
return direction; return direction;
} }
/**
* Gets the {@link TransactionEvent} associated with this currency transfer event.
*
* @return the transaction event.
*/
public TransactionEvent getTransactionEvent() {
return transactionEvent;
}
/** /**
* Get the player who initiated this transaction * Get the player who initiated this transaction
* *

View File

@ -80,7 +80,8 @@ public class ServerAccountCorrector implements Listener {
event.getAmountReceived(), event.getAmountReceived(),
event.getInitiator(), event.getInitiator(),
partner, partner,
event.getDirection() event.getDirection(),
event.getTransactionEvent()
); );
ChestShop.callEvent(currencyTransferEvent); ChestShop.callEvent(currencyTransferEvent);
event.setHandled(currencyTransferEvent.wasHandled()); event.setHandled(currencyTransferEvent.wasHandled());

View File

@ -19,7 +19,8 @@ public class EconomicModule implements Listener {
event.getExactPrice(), event.getExactPrice(),
event.getClient(), event.getClient(),
event.getOwnerAccount().getUuid(), event.getOwnerAccount().getUuid(),
event.getTransactionType() == BUY ? CurrencyTransferEvent.Direction.PARTNER : CurrencyTransferEvent.Direction.INITIATOR event.getTransactionType() == BUY ? CurrencyTransferEvent.Direction.PARTNER : CurrencyTransferEvent.Direction.INITIATOR,
event
); );
ChestShop.callEvent(currencyTransferEvent); ChestShop.callEvent(currencyTransferEvent);
if (!currencyTransferEvent.wasHandled()) { if (!currencyTransferEvent.wasHandled()) {

View File

@ -6,6 +6,7 @@ import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Economy.Economy;
import com.Acrobot.ChestShop.Events.Economy.CurrencyTransferEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent; import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Utils.ItemUtil; import com.Acrobot.ChestShop.Utils.ItemUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -15,6 +16,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import java.math.BigDecimal;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -23,65 +25,87 @@ import java.util.Map;
*/ */
public class TransactionMessageSender implements Listener { public class TransactionMessageSender implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onTransaction(TransactionEvent event) { public static void onCurrencyTransfer(CurrencyTransferEvent event) {
if (event.getTransactionType() == TransactionEvent.TransactionType.BUY) { if (event.getTransactionEvent() == null) {
return;
}
if (event.getTransactionEvent().getTransactionType() == TransactionEvent.TransactionType.BUY) {
sendBuyMessage(event); sendBuyMessage(event);
} else { } else {
sendSellMessage(event); sendSellMessage(event);
} }
} }
protected static void sendBuyMessage(TransactionEvent event) { protected static void sendBuyMessage(CurrencyTransferEvent event) {
Player player = event.getClient(); TransactionEvent transactionEvent = event.getTransactionEvent();
Player player = transactionEvent.getClient();
if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) { if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) {
sendMessage(player, event.getClient().getName(), Messages.YOU_BOUGHT_FROM_SHOP, event, "owner", event.getOwnerAccount().getName()); sendMessage(player, transactionEvent.getClient().getName(), Messages.YOU_BOUGHT_FROM_SHOP, event, MessageTarget.BUYER, "owner", transactionEvent.getOwnerAccount().getName());
} }
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) { if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(transactionEvent.getOwnerAccount().getUuid())) {
Player owner = Bukkit.getPlayer(event.getOwnerAccount().getUuid()); Player owner = Bukkit.getPlayer(transactionEvent.getOwnerAccount().getUuid());
sendMessage(owner, event.getOwnerAccount().getName(), Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, event, "buyer", player.getName()); sendMessage(owner, transactionEvent.getOwnerAccount().getName(), Messages.SOMEBODY_BOUGHT_FROM_YOUR_SHOP, event, MessageTarget.SELLER, "buyer", player.getName());
} }
} }
protected static void sendSellMessage(TransactionEvent event) { protected static void sendSellMessage(CurrencyTransferEvent event) {
Player player = event.getClient(); TransactionEvent transactionEvent = event.getTransactionEvent();
Player player = transactionEvent.getClient();
if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) { if (Properties.SHOW_TRANSACTION_INFORMATION_CLIENT) {
sendMessage(player, event.getClient().getName(), Messages.YOU_SOLD_TO_SHOP, event, "buyer", event.getOwnerAccount().getName()); sendMessage(player, transactionEvent.getClient().getName(), Messages.YOU_SOLD_TO_SHOP, event, MessageTarget.SELLER, "buyer", transactionEvent.getOwnerAccount().getName());
} }
if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(event.getOwnerAccount().getUuid())) { if (Properties.SHOW_TRANSACTION_INFORMATION_OWNER && !Toggle.isIgnoring(transactionEvent.getOwnerAccount().getUuid())) {
Player owner = Bukkit.getPlayer(event.getOwnerAccount().getUuid()); Player owner = Bukkit.getPlayer(transactionEvent.getOwnerAccount().getUuid());
sendMessage(owner, event.getOwnerAccount().getName(), Messages.SOMEBODY_SOLD_TO_YOUR_SHOP, event, "seller", player.getName()); sendMessage(owner, transactionEvent.getOwnerAccount().getName(), Messages.SOMEBODY_SOLD_TO_YOUR_SHOP, event, MessageTarget.BUYER, "seller", player.getName());
} }
} }
private static void sendMessage(Player player, String playerName, Messages.Message rawMessage, TransactionEvent event, String... replacements) { private static void sendMessage(Player player, String playerName, Messages.Message rawMessage, CurrencyTransferEvent event, MessageTarget messageTarget, String... replacements) {
Location loc = event.getSign().getLocation(); TransactionEvent transactionEvent = event.getTransactionEvent();
BigDecimal actualAmount = getTransactionActualAmount(event, messageTarget);
Location loc = transactionEvent.getSign().getLocation();
Map<String, String> replacementMap = new LinkedHashMap<>(); Map<String, String> replacementMap = new LinkedHashMap<>();
replacementMap.put("price", Economy.formatBalance(event.getExactPrice())); replacementMap.put("price", Economy.formatBalance(actualAmount));
replacementMap.put("world", loc.getWorld().getName()); replacementMap.put("world", loc.getWorld().getName());
replacementMap.put("x", String.valueOf(loc.getBlockX())); replacementMap.put("x", String.valueOf(loc.getBlockX()));
replacementMap.put("y", String.valueOf(loc.getBlockY())); replacementMap.put("y", String.valueOf(loc.getBlockY()));
replacementMap.put("z", String.valueOf(loc.getBlockZ())); replacementMap.put("z", String.valueOf(loc.getBlockZ()));
replacementMap.put("material", "%item"); replacementMap.put("material", "%item");
for (int i = 0; i + 1 < replacements.length; i+=2) { for (int i = 0; i + 1 < replacements.length; i += 2) {
replacementMap.put(replacements[i], replacements[i + 1]); replacementMap.put(replacements[i], replacements[i + 1]);
} }
if (Properties.SHOWITEM_MESSAGE && MaterialUtil.Show.sendMessage(player, playerName, rawMessage, event.getStock(), replacementMap)) { if (Properties.SHOWITEM_MESSAGE && MaterialUtil.Show.sendMessage(player, playerName, rawMessage, transactionEvent.getStock(), replacementMap)) {
return; return;
} }
if (player != null) { if (player != null) {
replacementMap.put("item", ItemUtil.getItemList(event.getStock())); replacementMap.put("item", ItemUtil.getItemList(transactionEvent.getStock()));
rawMessage.sendWithPrefix(player, replacementMap); rawMessage.sendWithPrefix(player, replacementMap);
} else if (playerName != null) { } else if (playerName != null) {
replacementMap.put("item", ItemUtil.getItemList(event.getStock())); replacementMap.put("item", ItemUtil.getItemList(transactionEvent.getStock()));
ChestShop.sendBungeeMessage(playerName, rawMessage, replacementMap); ChestShop.sendBungeeMessage(playerName, rawMessage, replacementMap);
} }
} }
private static BigDecimal getTransactionActualAmount(CurrencyTransferEvent event, MessageTarget messageTarget) {
if (messageTarget == MessageTarget.SELLER) {
return event.getAmountReceived();
} else {
return event.getAmountSent();
}
}
private enum MessageTarget {
BUYER,
SELLER
}
} }