Skip to content

Commit

Permalink
Fix transaction message getting sent twice when server economy accoun…
Browse files Browse the repository at this point in the history
…t is set

This was due to the ServerAccountCorrector calling the economy events another time with the new account.
 Directly setting the new account is the far better approach and has been adjusted for all currency events.
  • Loading branch information
Phoenix616 committed Sep 26, 2023
1 parent 0fcbcbb commit a413e86
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public UUID getTarget() {
return target;
}

/**
* @param target Account from which the currency is subtracted
*/
public void setTarget(UUID target) {
this.target = target;
}

public HandlerList getHandlers() {
return handlers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public UUID getAccount() {
return account;
}

/**
* @param account Account that is checked
*/
public void setAccount(UUID account) {
this.account = account;
}

public HandlerList getHandlers() {
return handlers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public UUID getAccount() {
* Sets the account name
*
* @param account Account name
* @deprecated The account should not be changed!
*/
@Deprecated
public void setAccount(UUID account) {
this.account = account;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import java.math.BigDecimal;
Expand Down Expand Up @@ -100,6 +99,13 @@ public UUID getTarget() {
return target;
}

/**
* @param target Account from which the currency is subtracted
*/
public void setTarget(UUID target) {
this.target = target;
}

public HandlerList getHandlers() {
return handlers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import java.math.BigDecimal;
Expand Down Expand Up @@ -182,6 +181,15 @@ public UUID getPartner() {
return partner;
}

/**
* Set the partner of the transaction
*
* @param partner the new partner of this transaction
*/
public void setPartner(UUID partner) {
this.partner = partner;
}

/**
* @return The world in which the transaction occurs
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.Acrobot.ChestShop.Listeners.Economy;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.Economy.*;
import com.Acrobot.ChestShop.UUIDs.NameManager;
Expand All @@ -27,15 +26,12 @@ public static void onCurrencyAdd(CurrencyAddEvent event) {
Account account = NameManager.getServerEconomyAccount();
target = account != null ? account.getUuid() : null;

event.setHandled(true);
if (target == null) {
event.setHandled(true);
return;
}

CurrencyAddEvent currencyAddEvent = new CurrencyAddEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencyAddEvent);

event.setHandled(currencyAddEvent.wasHandled());
event.setTarget(target);
}

@EventHandler(priority = EventPriority.LOWEST)
Expand All @@ -49,15 +45,12 @@ public static void onCurrencySubtract(CurrencySubtractEvent event) {
Account account = NameManager.getServerEconomyAccount();
target = account != null ? account.getUuid() : null;

event.setHandled(true);
if (target == null) {
event.setHandled(true);
return;
}

CurrencySubtractEvent currencySubtractEvent = new CurrencySubtractEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencySubtractEvent);

event.setHandled(currencySubtractEvent.wasHandled());
event.setTarget(target);
}

@EventHandler(priority = EventPriority.LOWEST)
Expand All @@ -75,16 +68,7 @@ public static void onCurrencyTransfer(CurrencyTransferEvent event) {
return;
}

CurrencyTransferEvent currencyTransferEvent = new CurrencyTransferEvent(
event.getAmountSent(),
event.getAmountReceived(),
event.getInitiator(),
partner,
event.getDirection(),
event.getTransactionEvent()
);
ChestShop.callEvent(currencyTransferEvent);
event.setHandled(currencyTransferEvent.wasHandled());
event.setPartner(partner);
}

@EventHandler(priority = EventPriority.LOWEST)
Expand All @@ -103,10 +87,7 @@ public static void onCurrencyCheck(CurrencyCheckEvent event) {
return;
}

CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencyCheckEvent);

event.hasEnough(currencyCheckEvent.hasEnough());
event.setAccount(target);
}

@EventHandler(priority = EventPriority.LOWEST)
Expand All @@ -117,8 +98,15 @@ public static void onCurrencyHoldCheck(CurrencyHoldEvent event) {
return;
}

event.canHold(true);
event.setAccount(null);
Account account = NameManager.getServerEconomyAccount();
target = account != null ? account.getUuid() : null;

if (target == null) {
event.canHold(true);
return;
}

event.setAccount(target);
}

@EventHandler(priority = EventPriority.LOWEST)
Expand All @@ -137,9 +125,6 @@ public static void onBalanceCheck(CurrencyAmountEvent event) {
return;
}

CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(target, event.getWorld());
ChestShop.callEvent(currencyAmountEvent);

event.setAmount(currencyAmountEvent.getAmount());
event.setAccount(target);
}
}

0 comments on commit a413e86

Please sign in to comment.