From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alexander <protonull@protonmail.com>
Date: Thu, 6 May 2021 13:01:25 +0100
Subject: [PATCH] Have CraftMerchantCustom emit PlayerPurchaseEvent


diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java
@@ -0,0 +0,0 @@ public class CraftMerchantCustom extends CraftMerchant {
 
         @Override
         public void notifyTrade(MerchantOffer offer) {
+            // Paper start
+            /** Based on {@link net.minecraft.world.entity.npc.EntityVillagerAbstract#b(MerchantRecipe)} */
+            if (getTradingPlayer() instanceof net.minecraft.server.level.ServerPlayer) {
+                final net.minecraft.server.level.ServerPlayer trader = (net.minecraft.server.level.ServerPlayer) getTradingPlayer();
+                final io.papermc.paper.event.player.PlayerPurchaseEvent event = new io.papermc.paper.event.player.PlayerPurchaseEvent(
+                    trader.getBukkitEntity(),
+                    offer.asBukkit(),
+                    false, // reward xp?
+                    true); // should increase uses?
+                event.callEvent();
+                if (event.isCancelled()) {
+                    return;
+                }
+                final org.bukkit.inventory.MerchantRecipe eventTrade = event.getTrade();
+                if (event.willIncreaseTradeUses()) {
+                    eventTrade.setUses(eventTrade.getUses() + 1);
+                }
+                if (event.isRewardingExp() && eventTrade.hasExperienceReward()) {
+                    /** Based on {@link net.minecraft.world.entity.npc.EntityVillagerTrader#b(MerchantRecipe)} */
+                    final int xp = 3 + net.minecraft.world.entity.Entity.SHARED_RANDOM.nextInt(4);
+                    final Level world = trader.getCommandSenderWorld();
+                    world.addFreshEntity(new net.minecraft.world.entity.ExperienceOrb(
+                        world, trader.getX(), trader.getY() + 0.5d, trader.getZ(), xp,
+                        org.bukkit.entity.ExperienceOrb.SpawnReason.VILLAGER_TRADE, trader, null));
+                }
+                return;
+            }
+            // Paper end
+
             // increase recipe's uses
             offer.increaseUses();
         }
