Add support for SaneEconomyTransactionEvent.

This commit is contained in:
AppleDash 2017-07-05 09:57:26 -04:00
parent 39e010a659
commit d95d55c4f4
3 changed files with 53 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.economy.transaction.Transaction;
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
import org.appledash.saneeconomy.event.SaneEconomyTransactionEvent;
import org.appledash.saneeconomy.utils.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@ -164,6 +165,12 @@ public class EconomyManager {
Economable receiver = transaction.getReceiver();
double amount = transaction.getAmount(); // This amount is validated upon creation of Transaction
SaneEconomyTransactionEvent evt = new SaneEconomyTransactionEvent(transaction);
Bukkit.getServer().getPluginManager().callEvent(evt);
if (evt.isCancelled()) {
return new TransactionResult(transaction, TransactionResult.Status.CANCELLED_BY_PLUGIN);
}
if (transaction.isSenderAffected()) { // Sender should have balance taken from them
if (!hasBalance(sender, amount)) {
return new TransactionResult(transaction, TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS);

View File

@ -40,6 +40,7 @@ public class TransactionResult {
public enum Status {
SUCCESS("Success."),
CANCELLED_BY_PLUGIN("That transaction was cancelled by a plugin."), // In theory this message should never be shown.
ERR_NOT_ENOUGH_FUNDS("Not enough money is available for you to complete that transaction.");
private final String message;

View File

@ -0,0 +1,45 @@
package org.appledash.saneeconomy.event;
import org.appledash.saneeconomy.economy.transaction.Transaction;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* Created by appledash on 7/5/17.
* Blackjack is best pony.
*
* This event is called whenever a Transaction occurs in the plugin. If you cancel this event, the transaction will be cancelled as well.
*/
public class SaneEconomyTransactionEvent extends Event implements Cancellable {
private static HandlerList handlerList = new HandlerList();
private final Transaction transaction;
private boolean isCancelled;
public SaneEconomyTransactionEvent(Transaction transaction) {
this.transaction = transaction;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
@Override
public boolean isCancelled() {
return this.isCancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.isCancelled = cancelled;
}
public Transaction getTransaction() {
return transaction;
}
public static HandlerList getHandlerList() {
return handlerList;
}
}