Fix trader merchants NPE when interacting with a merchant with non-active trades

This commit is contained in:
Shane Freeder 2021-12-05 03:00:10 +00:00
parent df42b6f60c
commit 55c371884e

View file

@ -180,20 +180,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
MerchantOffer merchantOffer = this.slots.getActiveOffer(); MerchantOffer merchantOffer = this.slots.getActiveOffer();
+ // Paper start + // Paper start
+ io.papermc.paper.event.player.PlayerPurchaseEvent event = null; + 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) { + if (merchantOffer != null) {
+ 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); + if (this.merchant instanceof net.minecraft.world.entity.npc.AbstractVillager && this.merchant.getTradingPlayer().getBukkitEntity() instanceof org.bukkit.entity.Player) {
+ } 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.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);
+ event = new io.papermc.paper.event.player.PlayerPurchaseEvent((org.bukkit.entity.Player) this.merchant.getTradingPlayer().getBukkitEntity(), merchantOffer.asBukkit(), false, 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()) { + if (event != null) {
+ stack.setCount(0); + if (!event.callEvent()) {
+ event.getPlayer().updateInventory(); + 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; + return;
+ } + }
+ merchantOffer = org.bukkit.craftbukkit.inventory.CraftMerchantRecipe.fromBukkit(event.getTrade()).toMinecraft(); + this.checkTakeAchievements(stack);
+ } + }
+ this.checkTakeAchievements(stack);
+ // Paper end + // Paper end
if (merchantOffer != null) { if (merchantOffer != null) {
ItemStack itemStack = this.slots.getItem(0); ItemStack itemStack = this.slots.getItem(0);