From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 2 Jul 2020 16:12:10 -0700
Subject: [PATCH] added PlayerTradeEvent


diff --git a/src/main/java/net/minecraft/world/entity/npc/AbstractVillager.java b/src/main/java/net/minecraft/world/entity/npc/AbstractVillager.java
index 4a7b657265cbbc91ae85409abb3db29cfc555a2c..e59a77c80a1bbe62aaa61bd4792d21b12c895a5c 100644
--- a/src/main/java/net/minecraft/world/entity/npc/AbstractVillager.java
+++ b/src/main/java/net/minecraft/world/entity/npc/AbstractVillager.java
@@ -140,13 +140,24 @@ public abstract class AbstractVillager extends AgeableMob implements InventoryCa
 
     @Override
     public void notifyTrade(MerchantOffer offer) {
-        offer.increaseUses();
-        this.ambientSoundTime = -this.getAmbientSoundInterval();
-        this.rewardTradeXp(offer);
+        // Paper - moved down
+        // Paper start
         if (this.tradingPlayer instanceof ServerPlayer) {
-            CriteriaTriggers.TRADE.trigger((ServerPlayer) this.tradingPlayer, this, offer.getResult());
+            io.papermc.paper.event.player.PlayerTradeEvent event = new io.papermc.paper.event.player.PlayerTradeEvent(((ServerPlayer) this.tradingPlayer).getBukkitEntity(), (org.bukkit.entity.AbstractVillager) this.getBukkitEntity(), offer.asBukkit(), true, true);
+            event.callEvent();
+            if (!event.isCancelled()) {
+                MerchantOffer recipe = CraftMerchantRecipe.fromBukkit(event.getTrade()).toMinecraft();
+                if (event.willIncreaseTradeUses()) recipe.increaseUses();
+                this.ambientSoundTime = -this.getAmbientSoundInterval();
+                if (event.isRewardingExp()) this.rewardTradeXp(recipe);
+                CriteriaTriggers.TRADE.trigger((ServerPlayer) this.tradingPlayer, this, recipe.getResult());
+            }
+        } else {
+            offer.increaseUses();
+            this.ambientSoundTime = -getAmbientSoundInterval();
+            this.rewardTradeXp(offer);
         }
-
+        // Paper end
     }
 
     protected abstract void rewardTradeXp(MerchantOffer offer);
