From 52f95706d2293068efa01655707615f3bcc3d407 Mon Sep 17 00:00:00 2001 From: Shane Freeder Date: Sun, 5 Dec 2021 03:00:10 +0000 Subject: Fix trader merchants NPE when interacting with a merchant with non-active trades --- ...-PlayerTradeEvent-and-PlayerPurchaseEvent.patch | 31 +++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/patches/server/0555-Add-PlayerTradeEvent-and-PlayerPurchaseEvent.patch b/patches/server/0555-Add-PlayerTradeEvent-and-PlayerPurchaseEvent.patch index 33f6700f18..80e0d5adbf 100644 --- a/patches/server/0555-Add-PlayerTradeEvent-and-PlayerPurchaseEvent.patch +++ b/patches/server/0555-Add-PlayerTradeEvent-and-PlayerPurchaseEvent.patch @@ -168,10 +168,10 @@ index 589527215b10aa848a079b964e748c8c2e6137a1..a6e036165ce1a387195cf3db190a42c5 return itemstack; diff --git a/src/main/java/net/minecraft/world/inventory/MerchantResultSlot.java b/src/main/java/net/minecraft/world/inventory/MerchantResultSlot.java -index 74b28315197b81f80334ae6023113904e4fac4c3..9e65c9535e01b3c858050a7881aff3f99edbeaac 100644 +index 74b28315197b81f80334ae6023113904e4fac4c3..1fb70ac171788d099e220b83421958976281f127 100644 --- a/src/main/java/net/minecraft/world/inventory/MerchantResultSlot.java +++ b/src/main/java/net/minecraft/world/inventory/MerchantResultSlot.java -@@ -47,13 +47,30 @@ public class MerchantResultSlot extends Slot { +@@ -47,13 +47,35 @@ public class MerchantResultSlot extends Slot { @Override public void onTake(Player player, ItemStack stack) { @@ -180,20 +180,25 @@ index 74b28315197b81f80334ae6023113904e4fac4c3..9e65c9535e01b3c858050a7881aff3f9 MerchantOffer merchantOffer = this.slots.getActiveOffer(); + // Paper start + io.papermc.paper.event.player.PlayerPurchaseEvent event = null; -+ if (this.merchant instanceof net.minecraft.world.entity.npc.AbstractVillager && this.merchant.getTradingPlayer().getBukkitEntity() instanceof org.bukkit.entity.Player) { -+ event = new io.papermc.paper.event.player.PlayerTradeEvent((org.bukkit.entity.Player) this.merchant.getTradingPlayer().getBukkitEntity(), (org.bukkit.entity.AbstractVillager) ((net.minecraft.world.entity.npc.AbstractVillager) this.merchant).getBukkitEntity(), merchantOffer.asBukkit(), true, true); -+ } else if (this.merchant instanceof org.bukkit.craftbukkit.inventory.CraftMerchantCustom.MinecraftMerchant && this.merchant.getTradingPlayer().getBukkitEntity() instanceof org.bukkit.entity.Player) { -+ event = new io.papermc.paper.event.player.PlayerPurchaseEvent((org.bukkit.entity.Player) this.merchant.getTradingPlayer().getBukkitEntity(), merchantOffer.asBukkit(), false, true); -+ } -+ if (event != null) { -+ if (!event.callEvent()) { -+ stack.setCount(0); -+ event.getPlayer().updateInventory(); ++ if (merchantOffer != null) { ++ if (this.merchant instanceof net.minecraft.world.entity.npc.AbstractVillager && this.merchant.getTradingPlayer().getBukkitEntity() instanceof org.bukkit.entity.Player) { ++ event = new io.papermc.paper.event.player.PlayerTradeEvent((org.bukkit.entity.Player) this.merchant.getTradingPlayer().getBukkitEntity(), (org.bukkit.entity.AbstractVillager) ((net.minecraft.world.entity.npc.AbstractVillager) this.merchant).getBukkitEntity(), merchantOffer.asBukkit(), true, true); ++ } else if (this.merchant instanceof org.bukkit.craftbukkit.inventory.CraftMerchantCustom.MinecraftMerchant && this.merchant.getTradingPlayer().getBukkitEntity() instanceof org.bukkit.entity.Player) { ++ event = new io.papermc.paper.event.player.PlayerPurchaseEvent((org.bukkit.entity.Player) this.merchant.getTradingPlayer().getBukkitEntity(), merchantOffer.asBukkit(), false, true); ++ } ++ if (event != null) { ++ if (!event.callEvent()) { ++ stack.setCount(0); ++ event.getPlayer().updateInventory(); ++ return; ++ } ++ merchantOffer = org.bukkit.craftbukkit.inventory.CraftMerchantRecipe.fromBukkit(event.getTrade()).toMinecraft(); ++ } else { ++ // TODO: Check this, I don't see how this would be hit and the logic below should be fired, warn for missing cases? + return; + } -+ merchantOffer = org.bukkit.craftbukkit.inventory.CraftMerchantRecipe.fromBukkit(event.getTrade()).toMinecraft(); ++ this.checkTakeAchievements(stack); + } -+ this.checkTakeAchievements(stack); + // Paper end if (merchantOffer != null) { ItemStack itemStack = this.slots.getItem(0); -- cgit v1.2.3