Class CurrencyChangeEvent
- All Implemented Interfaces:
org.bukkit.event.Cancellable
Addons can use it to:
- Cancel the change entirely
- Modify the new balance amount
- Log or track balance changes
- Implement custom restrictions
This event is cancellable. Cancelling prevents the change.
Example
@EventHandler
public void onCurrencyChange(CurrencyChangeEvent event) {
UUID playerId = event.getPlayerId();
double oldBalance = event.getOldBalance();
double newBalance = event.getNewBalance();
double change = event.getChangeAmount();
ChangeReason reason = event.getReason();
// Log the currency change
getLogger().info(String.format(
"Currency change for %s: %.2f -> %.2f (%s%.2f) [%s]",
event.getPlayerName(),
oldBalance, newBalance,
change >= 0 ? "+" : "", change,
reason.name()
));
// Prevent a negative balance
if (newBalance < 0) {
event.setNewBalance(0);
}
// Block changes from selected sources
if (reason == ChangeReason.ADDON && !hasPermission(playerId)) {
event.setCancelled(true);
event.getPlayer().ifPresent(p ->
p.sendMessage("Currency change blocked!"));
}
// Apply a tax to large gains
if (change > 1000) {
double taxedAmount = change * 0.9; // 10% tax
event.setNewBalance(oldBalance + taxedAmount);
}
}
- Since:
- 1.0.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumRepresents the reason for a currency balance change.Nested classes/interfaces inherited from class org.bukkit.event.Event
org.bukkit.event.Event.Result -
Constructor Summary
ConstructorsConstructorDescriptionCurrencyChangeEvent(@NotNull UUID playerId, @NotNull String playerName, double oldBalance, double newBalance, @NotNull CurrencyChangeEvent.ChangeReason reason) Constructs a new CurrencyChangeEvent without a source.CurrencyChangeEvent(@NotNull UUID playerId, @NotNull String playerName, double oldBalance, double newBalance, @NotNull CurrencyChangeEvent.ChangeReason reason, @Nullable String source) Constructs a new CurrencyChangeEvent.CurrencyChangeEvent(@NotNull UUID playerId, @NotNull String playerName, double oldBalance, double newBalance, @NotNull CurrencyChangeEvent.ChangeReason reason, @Nullable String source, double multiplier, double bonusAmount) Constructs a new CurrencyChangeEvent carrying the reward multiplier Zentrix applied. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBonus(double bonus) Adds a bonus amount to the new balance.doubleGets the requested change without the reward multiplier's bonus.doubleGets how much of the requested change Zentrix's reward multiplier added, before any listener.doubleGets the amount of change (positive for gains, negative for losses).static @NotNull org.bukkit.event.HandlerListGets the static handler list for this event type.@NotNull org.bukkit.event.HandlerListGets the handler list for this event.doubleGets the reward multiplier Zentrix applied to this change, such as the player'srewards.bonusperk or azentrix.multiplier.*permission.doubleGets the player's balance after the change.doubleGets the player's balance before the change.@NotNull Optional<org.bukkit.entity.Player> Gets the Bukkit player instance if online.@NotNull UUIDGets the UUID of the player whose balance is changing.@NotNull StringGets the name of the player whose balance is changing.@NotNull CurrencyChangeEvent.ChangeReasonGets the reason for the balance change.Gets the source identifier for this change.booleanChecks if this change was made by an addon.booleanChecks if this change was made by an administrator.booleanbooleanChecks if this change was triggered by a game event reward.booleanisGain()Checks if this is a balance increase (gain).booleanisLoss()Checks if this is a balance decrease (loss).booleanChecks if there is no actual change in balance.voidmultiplyChange(double multiplier) Applies a multiplier to the change amount.voidsetCancelled(boolean cancel) voidsetNewBalance(double newBalance) Sets the new balance that will be applied.Methods inherited from class org.bukkit.event.Event
callEvent, getEventName, isAsynchronous
-
Constructor Details
-
CurrencyChangeEvent
public CurrencyChangeEvent(@NotNull @NotNull UUID playerId, @NotNull @NotNull String playerName, double oldBalance, double newBalance, @NotNull @NotNull CurrencyChangeEvent.ChangeReason reason, @Nullable @Nullable String source) Constructs a new CurrencyChangeEvent.- Parameters:
playerId- The UUID of the player whose balance is changingplayerName- The name of the playeroldBalance- The balance before the changenewBalance- The balance after the change (can be modified)reason- The reason for the balance changesource- Optional source identifier (e.g., addon name, event type)
-
CurrencyChangeEvent
public CurrencyChangeEvent(@NotNull @NotNull UUID playerId, @NotNull @NotNull String playerName, double oldBalance, double newBalance, @NotNull @NotNull CurrencyChangeEvent.ChangeReason reason, @Nullable @Nullable String source, double multiplier, double bonusAmount) Constructs a new CurrencyChangeEvent carrying the reward multiplier Zentrix applied.- Parameters:
multiplier- the multiplier already included innewBalance, such as the player'srewards.bonusperk; 1.0 when nonebonusAmount- how much the multiplier added to the requested change; 0 when none- Since:
- 1.13.0
-
CurrencyChangeEvent
public CurrencyChangeEvent(@NotNull @NotNull UUID playerId, @NotNull @NotNull String playerName, double oldBalance, double newBalance, @NotNull @NotNull CurrencyChangeEvent.ChangeReason reason) Constructs a new CurrencyChangeEvent without a source.- Parameters:
playerId- The UUID of the player whose balance is changingplayerName- The name of the playeroldBalance- The balance before the changenewBalance- The balance after the changereason- The reason for the balance change
-
-
Method Details
-
getPlayerId
Gets the UUID of the player whose balance is changing.- Returns:
- The player's UUID (never null)
-
getPlayerName
Gets the name of the player whose balance is changing.- Returns:
- The player's name (never null)
-
getPlayer
Gets the Bukkit player instance if online.The player may be offline if the change is being made asynchronously or from storage operations.
- Returns:
- Optional containing the player, or empty if offline
-
getOldBalance
public double getOldBalance()Gets the player's balance before the change.- Returns:
- The old balance
-
getNewBalance
public double getNewBalance()Gets the player's balance after the change.This value can be modified by event handlers before the change is applied.
- Returns:
- The new balance
-
setNewBalance
public void setNewBalance(double newBalance) Sets the new balance that will be applied.Use this to modify the final balance. The value will be clamped to a minimum of 0 when applied.
- Parameters:
newBalance- The new balance to set
-
getChangeAmount
public double getChangeAmount()Gets the amount of change (positive for gains, negative for losses).This is calculated as
newBalance - oldBalance.- Returns:
- The change amount
-
getMultiplier
public double getMultiplier()Gets the reward multiplier Zentrix applied to this change, such as the player'srewards.bonusperk or azentrix.multiplier.*permission.The change already includes it. An addon that boosts rewards can compare against it rather than stack on it unnoticed.
- Returns:
- The multiplier; 1.0 when none
- Since:
- 1.13.0
-
getBonusAmount
public double getBonusAmount()Gets how much of the requested change Zentrix's reward multiplier added, before any listener.Listeners may change the final balance afterwards, so this is the bonus the multiplier contributed to the change as it was requested.
- Returns:
- The bonus amount; 0 when no multiplier applied
- Since:
- 1.13.0
-
getBaseChangeAmount
public double getBaseChangeAmount()Gets the requested change without the reward multiplier's bonus.This is
getChangeAmount() - getBonusAmount(), so an addon can base its own boost on the reward before Zentrix's multiplier, or add to the final change deliberately.- Returns:
- The change with Zentrix's reward bonus removed
- Since:
- 1.13.0
-
isGain
public boolean isGain()Checks if this is a balance increase (gain).- Returns:
trueif the new balance is higher than the old balance
-
isLoss
public boolean isLoss()Checks if this is a balance decrease (loss).- Returns:
trueif the new balance is lower than the old balance
-
isNoChange
public boolean isNoChange()Checks if there is no actual change in balance.- Returns:
trueif old and new balance are equal
-
getReason
Gets the reason for the balance change.- Returns:
- The change reason (never null)
-
getSource
Gets the source identifier for this change.This can be an addon name, event type, or other identifier that helps track where the change originated from.
- Returns:
- Optional containing the source, or empty if not specified
-
isEventReward
public boolean isEventReward()Checks if this change was triggered by a game event reward.- Returns:
trueif the reason isCurrencyChangeEvent.ChangeReason.EVENT_REWARD
-
isAdminChange
public boolean isAdminChange()Checks if this change was made by an administrator.- Returns:
trueif the reason isCurrencyChangeEvent.ChangeReason.ADMIN
-
isAddonChange
public boolean isAddonChange()Checks if this change was made by an addon.- Returns:
trueif the reason isCurrencyChangeEvent.ChangeReason.ADDON
-
addBonus
public void addBonus(double bonus) Adds a bonus amount to the new balance.Adds to the new balance without requiring its current value.
- Parameters:
bonus- The amount to add (can be negative to reduce)
-
multiplyChange
public void multiplyChange(double multiplier) Applies a multiplier to the change amount.This modifies the new balance by multiplying the change amount. For example, a multiplier of 2.0 will double the gain/loss.
- Parameters:
multiplier- The multiplier to apply to the change amount
-
isCancelled
public boolean isCancelled()- Specified by:
isCancelledin interfaceorg.bukkit.event.Cancellable
-
setCancelled
public void setCancelled(boolean cancel) - Specified by:
setCancelledin interfaceorg.bukkit.event.Cancellable
-
getHandlers
@NotNull public @NotNull org.bukkit.event.HandlerList getHandlers()Gets the handler list for this event.- Overrides:
getHandlersin classZentrixEvent- Returns:
- The handler list
-
getHandlerList
@NotNull public static @NotNull org.bukkit.event.HandlerList getHandlerList()Gets the static handler list for this event type.- Returns:
- The handler list
-