aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0229-Added-PlayerTradeEvent.patch
blob: 11ad0190219bbf9fcd12b2f0d7062f385d3d59fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 2 Jul 2020 16:10:10 -0700
Subject: [PATCH] Added PlayerTradeEvent

[Amendment: Alexander <protonull@protonmail.com>]
PlayerTradeEvent is used for player purchases from villagers and wandering
traders, but not custom merchants created via Bukkit.createMerchant(). During
discussions in Discord it was decided that it'd be better to add a new event
that PlayerTradeEvent inherits from than change getVillager()'s annotation to
@Nullable, especially since that'd also infringe on the implication of the
event being about villager trades.

diff --git a/src/main/java/io/papermc/paper/event/player/PlayerPurchaseEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerPurchaseEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..61c62877c38e27eacc20aa43ef02dc43e9b50bfc
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerPurchaseEvent.java
@@ -0,0 +1,109 @@
+package io.papermc.paper.event.player;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.inventory.MerchantRecipe;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a player trades with a standalone merchant GUI.
+ */
+public class PlayerPurchaseEvent extends PlayerEvent implements Cancellable {
+
+    private static final HandlerList HANDLER_LIST = new HandlerList();
+
+    private boolean rewardExp;
+    private boolean increaseTradeUses;
+    private MerchantRecipe trade;
+
+    private boolean cancelled;
+
+    @ApiStatus.Internal
+    public PlayerPurchaseEvent(@NotNull Player player,
+                               @NotNull MerchantRecipe trade,
+                               boolean rewardExp,
+                               boolean increaseTradeUses) {
+        super(player);
+        setTrade(trade);
+        this.rewardExp = rewardExp;
+        this.increaseTradeUses = increaseTradeUses;
+    }
+
+    /**
+     * Gets the associated trade with this event
+     *
+     * @return the trade
+     */
+    @NotNull
+    public MerchantRecipe getTrade() {
+        return this.trade;
+    }
+
+    /**
+     * Sets the trade. This is then used to determine the next prices
+     *
+     * @param trade the trade to use
+     */
+    public void setTrade(@NotNull MerchantRecipe trade) {
+        Preconditions.checkArgument(trade != null, "Trade cannot be null!");
+        this.trade = trade;
+    }
+
+    /**
+     * @return will trade try to reward exp
+     */
+    public boolean isRewardingExp() {
+        return this.rewardExp;
+    }
+
+    /**
+     * Sets whether the trade will try to reward exp
+     *
+     * @param rewardExp try to reward exp
+     */
+    public void setRewardExp(boolean rewardExp) {
+        this.rewardExp = rewardExp;
+    }
+
+    /**
+     * @return whether the trade will count as a use of the trade
+     */
+    public boolean willIncreaseTradeUses() {
+        return this.increaseTradeUses;
+    }
+
+    /**
+     * Sets whether the trade will count as a use
+     *
+     * @param increaseTradeUses {@code true} to count, {@code false} otherwise
+     */
+    public void setIncreaseTradeUses(boolean increaseTradeUses) {
+        this.increaseTradeUses = increaseTradeUses;
+    }
+
+    @Override
+    public boolean isCancelled() {
+        return this.cancelled;
+    }
+
+    @Override
+    public void setCancelled(boolean cancel) {
+        this.cancelled = cancel;
+    }
+
+    @NotNull
+    @Override
+    public HandlerList getHandlers() {
+        return HANDLER_LIST;
+    }
+
+    @NotNull
+    public static HandlerList getHandlerList() {
+        return HANDLER_LIST;
+    }
+
+}
diff --git a/src/main/java/io/papermc/paper/event/player/PlayerTradeEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerTradeEvent.java
new file mode 100755
index 0000000000000000000000000000000000000000..559d1a3c783e6c726f48d1c88b2ff8c0888890ac
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerTradeEvent.java
@@ -0,0 +1,32 @@
+package io.papermc.paper.event.player;
+
+import org.bukkit.entity.AbstractVillager;
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.MerchantRecipe;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a player trades with a villager or wandering trader
+ */
+public class PlayerTradeEvent extends PlayerPurchaseEvent {
+
+    private final AbstractVillager villager;
+
+    @ApiStatus.Internal
+    public PlayerTradeEvent(@NotNull Player player, @NotNull AbstractVillager villager, @NotNull MerchantRecipe trade, boolean rewardExp, boolean increaseTradeUses) {
+        super(player, trade, rewardExp, increaseTradeUses);
+        this.villager = villager;
+    }
+
+    /**
+     * Gets the Villager or Wandering trader associated with this event
+     *
+     * @return the villager or wandering trader
+     */
+    @NotNull
+    public AbstractVillager getVillager() {
+        return this.villager;
+    }
+
+}