Fix transaction message getting sent twice when server economy account 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.
This commit is contained in:
Phoenix616 2023-09-26 20:22:15 +01:00
parent 0fcbcbbb11
commit a413e86ccf
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
6 changed files with 46 additions and 35 deletions

View File

@ -99,6 +99,13 @@ public class CurrencyAddEvent extends EconomicEvent {
return target;
}
/**
* @param target Account from which the currency is subtracted
*/
public void setTarget(UUID target) {
this.target = target;
}
public HandlerList getHandlers() {
return handlers;
}

View File

@ -76,6 +76,13 @@ public class CurrencyAmountEvent extends EconomicEvent {
return account;
}
/**
* @param account Account that is checked
*/
public void setAccount(UUID account) {
this.account = account;
}
public HandlerList getHandlers() {
return handlers;
}

View File

@ -103,9 +103,7 @@ public class CurrencyCheckEvent extends EconomicEvent {
* Sets the account name
*
* @param account Account name
* @deprecated The account should not be changed!
*/
@Deprecated
public void setAccount(UUID account) {
this.account = account;
}

View File

@ -2,7 +2,6 @@ package com.Acrobot.ChestShop.Events.Economy;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import java.math.BigDecimal;
@ -100,6 +99,13 @@ public class CurrencySubtractEvent extends EconomicEvent {
return target;
}
/**
* @param target Account from which the currency is subtracted
*/
public void setTarget(UUID target) {
this.target = target;
}
public HandlerList getHandlers() {
return handlers;
}

View File

@ -3,7 +3,6 @@ package com.Acrobot.ChestShop.Events.Economy;
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;
@ -182,6 +181,15 @@ public class CurrencyTransferEvent extends EconomicEvent {
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
*/

View File

@ -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;
@ -27,15 +26,12 @@ public class ServerAccountCorrector implements Listener {
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)
@ -49,15 +45,12 @@ public class ServerAccountCorrector implements Listener {
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)
@ -75,16 +68,7 @@ public class ServerAccountCorrector implements Listener {
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)
@ -103,10 +87,7 @@ public class ServerAccountCorrector implements Listener {
return;
}
CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(event.getAmount(), target, event.getWorld());
ChestShop.callEvent(currencyCheckEvent);
event.hasEnough(currencyCheckEvent.hasEnough());
event.setAccount(target);
}
@EventHandler(priority = EventPriority.LOWEST)
@ -117,8 +98,15 @@ public class ServerAccountCorrector implements Listener {
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)
@ -137,9 +125,6 @@ public class ServerAccountCorrector implements Listener {
return;
}
CurrencyAmountEvent currencyAmountEvent = new CurrencyAmountEvent(target, event.getWorld());
ChestShop.callEvent(currencyAmountEvent);
event.setAmount(currencyAmountEvent.getAmount());
event.setAccount(target);
}
}