Paper/Spigot-Server-Patches/Have-CraftMerchantCustom-emit-PlayerPurchaseEvent.patch

60 lines
3.2 KiB
Diff

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/net/minecraft/world/item/trading/IMerchant.java b/src/main/java/net/minecraft/world/item/trading/IMerchant.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/item/trading/IMerchant.java
+++ b/src/main/java/net/minecraft/world/item/trading/IMerchant.java
@@ -0,0 +0,0 @@ public interface IMerchant {
MerchantRecipeList getOffers();
- void a(MerchantRecipe merchantrecipe);
+ void a(MerchantRecipe merchantrecipe); default void handlePurchase(MerchantRecipe merchantRecipe) { a(merchantRecipe); } // Paper - OBFHELPER
void k(ItemStack itemstack);
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 a(MerchantRecipe merchantrecipe) {
+ // Paper start
+ /** Based on {@link net.minecraft.world.entity.npc.EntityVillagerAbstract#b(MerchantRecipe)} */
+ if (getTrader() instanceof net.minecraft.server.level.EntityPlayer) {
+ final net.minecraft.server.level.EntityPlayer trader = (net.minecraft.server.level.EntityPlayer) getTrader();
+ final io.papermc.paper.event.player.PlayerPurchaseEvent event = new io.papermc.paper.event.player.PlayerPurchaseEvent(
+ trader.getBukkitEntity(),
+ merchantrecipe.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 World world = trader.getWorld();
+ world.addEntity(new net.minecraft.world.entity.EntityExperienceOrb(
+ world, trader.locX(), trader.locY() + 0.5d, trader.locZ(), xp,
+ org.bukkit.entity.ExperienceOrb.SpawnReason.VILLAGER_TRADE, trader, null));
+ }
+ return;
+ }
+ // Paper end
+
// increase recipe's uses
merchantrecipe.increaseUses();
}