Skip to content

Commit 08b7fe3

Browse files
Golfing7JRoy
andauthored
Add PreTransactionEvent (#6370)
<!-- EssentialsX feature submission guide ==================================== NOTE: Failure to fill out this template properly may result in your PR being delayed or ignored without warning. NOTE: Don't type between any arrows in the template, as this text will be hidden. This includes this header block and any other explanation text blocks. Want to discuss your PR before submitting it? Join the EssentialsX Development server: https://discord.gg/CUN7qVb EssentialsX is GPL ------------------ By contributing to EssentialsX, you agree to license your code under the GNU General Public License version 3, which can be found at the link below: https://github.com/EssentialsX/Essentials/blob/2.x/LICENSE Instructions ------------ If you are submitting a new feature, please follow the following steps: 1. Fill out the template in full. This includes providing screenshots and a link to the original feature request. If there isn't an existing feature request, we strongly recommend opening a new feature request BEFORE opening your PR to implement it, as this allows us to review whether we're likely to accept your feature in advance, and also allows us to discuss possible implementations for the feature. If there is no associated feature request, your PR may be delayed or rejected without warning. You can open a new feature request by following this link: https://github.com/EssentialsX/Essentials/issues/new/choose 2. If you are fixing a performance issue, please use the "Bug fix" PR template instead. The "bug fix" template is better suited to performance issues. 3. Include a demonstration. If you are adding commands, please provide screenshots and/or a video demonstration of the feature. Similarly, if you are adding a new API, please include a link to example code that takes advantage of your proposed API. This will aid us in reviewing PRs and speed up the process significantly. --> ### Information <!-- Replace #nnnn with the number of the original issue. If this PR implements features from multiple issues, you should repeat the phrase "closes #nnnn" for each issue. --> I added a PreTransactionEvent for cancelling transactions between users. ### Details **Proposed feature:** Adding a PreTransactionEvent. **Environments tested:** <!-- Type the OS you have used below. --> OS: Linux Mint <!-- Type the JDK version (from java -version) you have used below. --> Java version: 23 <!-- Put an "x" inside the boxes for the server software you have tested this bug fix on. If this feature does not apply to a server, strike through the server software using ~~strikethrough~~. If you have tested on other environments, add a new line with relevant details. --> - [x] Most recent Paper version (1.XX.Y, git-Paper-BUILD) - [ ] CraftBukkit/Spigot/Paper 1.12.2 - [ ] CraftBukkit 1.8.8 **Demonstration:** <!-- Below this block, include screenshots/log snippets from before and after as necessary. If you have created or used a test case plugin, please link to a download of the plugin, source code and exact version used where possible. --> ```java @eventhandler public void onPay(PreTransactionEvent event) { // Prevent money from being sent outside their own faction. IUser userSource = event.getRequester().getUser(); if (userSource == null) return; FPlayer fplayerSource = FPlayers.getInstance().getByPlayer(userSource.getBase()); if (fplayerSource == null || fplayerSource.isAdminBypassing()) return; Faction faction = fplayerSource.getFaction(); if (faction == null || !isFactionVoluntaryExcluded(faction.getId())) return; // Who are they sending it to? FPlayer target = FPlayers.getInstance().getById(event.getTarget().getUUID().toString()); Faction factionTarget = target.getFaction(); if (faction != factionTarget) { event.setCancelled(true); msg(userSource.getBase(), cantPayOutsideExcludedFactionMessage); } } ``` This code prevents transactions between two users when two factions can't interact. --------- Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
1 parent 0845ea6 commit 08b7fe3

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

Essentials/src/main/java/com/earth2me/essentials/User.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.ess3.api.events.MuteStatusChangeEvent;
2424
import net.ess3.api.events.UserBalanceUpdateEvent;
2525
import net.ess3.provider.PlayerLocaleProvider;
26+
import net.essentialsx.api.v2.events.PreTransactionEvent;
2627
import net.essentialsx.api.v2.events.TransactionEvent;
2728
import net.essentialsx.api.v2.services.mail.MailSender;
2829
import org.bukkit.Location;
@@ -263,12 +264,20 @@ public void payUser(final User reciever, final BigDecimal value) throws Exceptio
263264
payUser(reciever, value, UserBalanceUpdateEvent.Cause.UNKNOWN);
264265
}
265266

266-
public void payUser(final User reciever, final BigDecimal value, final UserBalanceUpdateEvent.Cause cause) throws Exception {
267+
public void payUser(final User reciever, BigDecimal value, final UserBalanceUpdateEvent.Cause cause) throws Exception {
267268
if (value.compareTo(BigDecimal.ZERO) < 1) {
268269
throw new Exception(tlLocale(playerLocale, "payMustBePositive"));
269270
}
270271

271272
if (canAfford(value)) {
273+
// Call an event for pre-transaction
274+
final PreTransactionEvent preTransactionEvent = new PreTransactionEvent(this.getSource(), reciever, value);
275+
ess.getServer().getPluginManager().callEvent(preTransactionEvent);
276+
if (preTransactionEvent.isCancelled()) {
277+
return;
278+
}
279+
value = preTransactionEvent.getAmount();
280+
272281
setMoney(getMoney().subtract(value), cause);
273282
reciever.setMoney(reciever.getMoney().add(value), cause);
274283
sendTl("moneySentTo", AdventureUtil.parsed(NumberUtil.displayCurrency(value, ess)), reciever.getDisplayName());
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package net.essentialsx.api.v2.events;
2+
3+
import com.earth2me.essentials.CommandSource;
4+
import com.google.common.base.Preconditions;
5+
import net.ess3.api.IUser;
6+
import org.bukkit.Bukkit;
7+
import org.bukkit.event.Cancellable;
8+
import org.bukkit.event.HandlerList;
9+
10+
import java.math.BigDecimal;
11+
12+
/**
13+
* Fired when a transaction (e.g. /pay) is about to be handled.
14+
*/
15+
public class PreTransactionEvent extends TransactionEvent implements Cancellable {
16+
private static final HandlerList handlers = new HandlerList();
17+
18+
private boolean cancelled;
19+
20+
public PreTransactionEvent(final CommandSource requester, final IUser target, final BigDecimal amount) {
21+
super(!Bukkit.isPrimaryThread(), requester, target, amount);
22+
}
23+
24+
/**
25+
* Sets the amount to be subtracted from the requester's balance.
26+
* <p>
27+
* Note: Changing this amount will not verify the requester actually has enough balance to complete the transaction.
28+
* @param decimal the new amount
29+
*/
30+
public void setAmount(final BigDecimal decimal) {
31+
Preconditions.checkNotNull(decimal, "decimal cannot be null");
32+
Preconditions.checkArgument(decimal.compareTo(BigDecimal.ZERO) >= 0, "decimal cannot be negative");
33+
34+
this.amount = decimal;
35+
}
36+
37+
@Override
38+
public boolean isCancelled() {
39+
return cancelled;
40+
}
41+
42+
/**
43+
* If this event should be cancelled. If cancelled, no messages will be displayed to the users involved.
44+
* @param cancelled whether this event should be cancelled
45+
*/
46+
@Override
47+
public void setCancelled(boolean cancelled) {
48+
this.cancelled = cancelled;
49+
}
50+
51+
@Override
52+
public HandlerList getHandlers() {
53+
return handlers;
54+
}
55+
56+
public static HandlerList getHandlerList() {
57+
return handlers;
58+
}
59+
}

Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ public class TransactionEvent extends Event {
1616

1717
private final CommandSource requester;
1818
private final IUser target;
19-
private final BigDecimal amount;
19+
protected BigDecimal amount;
2020

21-
public TransactionEvent(CommandSource requester, IUser target, BigDecimal amount) {
22-
super(!Bukkit.isPrimaryThread());
21+
protected TransactionEvent(boolean async, CommandSource requester, IUser target, BigDecimal amount) {
22+
super(async);
2323
this.requester = requester;
2424
this.target = target;
2525
this.amount = amount;
2626
}
2727

28+
public TransactionEvent(CommandSource requester, IUser target, BigDecimal amount) {
29+
this(!Bukkit.isPrimaryThread(), requester, target, amount);
30+
}
31+
2832
/**
2933
* @return the user who initiated the transaction
3034
*/

0 commit comments

Comments
 (0)