PaperMC/patches/api/0222-Added-PlayerTradeEvent.patch

162 lines
5.1 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
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..1691b53f157f17117116e841cbfc769ea0e14364
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerPurchaseEvent.java
@@ -0,0 +1,104 @@
2021-06-11 14:02:28 +02:00
+package io.papermc.paper.event.player;
+
2024-02-01 10:15:57 +01:00
+import com.google.common.base.Preconditions;
2021-06-11 14:02:28 +02:00
+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;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Called when a player trades with a standalone merchant GUI.
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class PlayerPurchaseEvent extends PlayerEvent implements Cancellable {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ private boolean rewardExp;
2024-02-01 10:15:57 +01:00
+ private boolean increaseTradeUses;
2021-06-11 14:02:28 +02:00
+ private MerchantRecipe trade;
+
2024-02-01 10:15:57 +01:00
+ private boolean cancelled;
+
+ @ApiStatus.Internal
+ public PlayerPurchaseEvent(final Player player, final MerchantRecipe trade, final boolean rewardExp, final boolean increaseTradeUses) {
2024-02-01 10:15:57 +01:00
+ super(player);
+ this.trade = trade;
2021-06-11 14:02:28 +02:00
+ this.rewardExp = rewardExp;
+ this.increaseTradeUses = increaseTradeUses;
+ }
+
+ /**
+ * Gets the associated trade with this event
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return the trade
+ */
+ public MerchantRecipe getTrade() {
+ return this.trade;
+ }
+
+ /**
+ * Sets the trade. This is then used to determine the next prices
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @param trade the trade to use
+ */
+ public void setTrade(final MerchantRecipe trade) {
2024-02-01 10:15:57 +01:00
+ Preconditions.checkArgument(trade != null, "Trade cannot be null!");
+ this.trade = trade;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * @return will trade try to reward exp
+ */
+ public boolean isRewardingExp() {
+ return this.rewardExp;
+ }
+
+ /**
+ * Sets whether the trade will try to reward exp
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @param rewardExp try to reward exp
+ */
+ public void setRewardExp(final boolean rewardExp) {
2021-06-11 14:02:28 +02:00
+ this.rewardExp = rewardExp;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * @return whether the trade will count as a use of the trade
2021-06-11 14:02:28 +02:00
+ */
+ public boolean willIncreaseTradeUses() {
+ return this.increaseTradeUses;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Sets whether the trade will count as a use
+ *
+ * @param increaseTradeUses {@code true} to count, {@code false} otherwise
2021-06-11 14:02:28 +02:00
+ */
+ public void setIncreaseTradeUses(final boolean increaseTradeUses) {
2021-06-11 14:02:28 +02:00
+ this.increaseTradeUses = increaseTradeUses;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ @Override
+ public void setCancelled(final boolean cancel) {
2021-06-11 14:02:28 +02:00
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+}
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..46bdc178c19feb7dbb71eebee6d0774cf16c1042
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerTradeEvent.java
2024-02-01 10:15:57 +01:00
@@ -0,0 +1,32 @@
2021-06-11 14:02:28 +02:00
+package io.papermc.paper.event.player;
+
+import org.bukkit.entity.AbstractVillager;
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.MerchantRecipe;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Called when a player trades with a villager or wandering trader
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class PlayerTradeEvent extends PlayerPurchaseEvent {
+
+ private final AbstractVillager villager;
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
+ public PlayerTradeEvent(final Player player, final AbstractVillager villager, final MerchantRecipe trade, final boolean rewardExp, final boolean increaseTradeUses) {
2021-06-11 14:02:28 +02:00
+ super(player, trade, rewardExp, increaseTradeUses);
+ this.villager = villager;
+ }
+
+ /**
+ * Gets the Villager or Wandering trader associated with this event
2024-02-01 10:15:57 +01:00
+ *
2021-06-11 14:02:28 +02:00
+ * @return the villager or wandering trader
+ */
+ public AbstractVillager getVillager() {
+ return this.villager;
+ }
+
+}