From d0b0b36801515d406f87c34011a6ade206509a80 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Thu, 15 Dec 2022 00:14:44 -0800 Subject: [PATCH] Fix several issues with EntityBreedEvent Upstream did not account for different hands when storing the breed item for later use in the event. Also they only stored a reference to the stack, not a copy so if the stack changed after love mode was started, the breed item in the event also changed. Also in several places, the breed item was stored after it was decreased by one to consume the item. --- .../world/entity/animal/Animal.java.patch | 26 +++++++++++++++-- .../world/entity/animal/Panda.java.patch | 21 ++++++++++---- .../entity/animal/camel/Camel.java.patch | 9 +++++- .../animal/horse/AbstractHorse.java.patch | 28 +++++++++++++++---- .../entity/animal/horse/Llama.java.patch | 8 +++++- 5 files changed, 76 insertions(+), 16 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/world/entity/animal/Animal.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/animal/Animal.java.patch index 79a03f7409..f8c533216c 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/animal/Animal.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/animal/Animal.java.patch @@ -39,14 +39,34 @@ } @Override -@@ -188,10 +202,17 @@ +@@ -144,8 +158,9 @@ + int i = this.getAge(); + + if (!this.level().isClientSide && i == 0 && this.canFallInLove()) { ++ final ItemStack breedCopy = itemstack.copy(); // Paper - Fix EntityBreedEvent copying + this.usePlayerItem(player, hand, itemstack); +- this.setInLove(player); ++ this.setInLove(player, breedCopy); // Paper - Fix EntityBreedEvent copying + this.playEatingSound(); + return InteractionResult.SUCCESS_SERVER; + } +@@ -187,11 +202,26 @@ + return this.inLove <= 0; } ++ @Deprecated @io.papermc.paper.annotation.DoNotUse // Paper - Fix EntityBreedEvent copying public void setInLove(@Nullable Player player) { - this.inLove = 600; ++ // Paper start - Fix EntityBreedEvent copying ++ this.setInLove(player, null); ++ } ++ public void setInLove(@Nullable Player player, @Nullable ItemStack breedItemCopy) { ++ if (breedItemCopy != null) this.breedItem = breedItemCopy; ++ // Paper end - Fix EntityBreedEvent copying + // CraftBukkit start + EntityEnterLoveModeEvent entityEnterLoveModeEvent = CraftEventFactory.callEntityEnterLoveModeEvent(player, this, 600); + if (entityEnterLoveModeEvent.isCancelled()) { ++ this.breedItem = null; // Paper - Fix EntityBreedEvent copying; clear if cancelled + return; + } + this.inLove = entityEnterLoveModeEvent.getTicksInLove(); @@ -54,11 +74,11 @@ if (player != null) { this.loveCause = player.getUUID(); } -+ this.breedItem = player.getInventory().getSelected(); // CraftBukkit ++ // Paper - Fix EntityBreedEvent copying; set breed item in better place this.level().broadcastEntityEvent(this, (byte) 18); } -@@ -233,25 +254,48 @@ +@@ -233,25 +263,48 @@ if (entityageable != null) { entityageable.setBaby(true); entityageable.moveTo(this.getX(), this.getY(), this.getZ(), 0.0F, 0.0F); diff --git a/paper-server/patches/sources/net/minecraft/world/entity/animal/Panda.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/animal/Panda.java.patch index efe53ccccb..911c02e2c0 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/animal/Panda.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/animal/Panda.java.patch @@ -57,7 +57,18 @@ } } -@@ -657,7 +667,9 @@ +@@ -643,8 +653,9 @@ + this.usePlayerItem(player, hand, itemstack); + this.ageUp((int) ((float) (-this.getAge() / 20) * 0.1F), true); + } else if (!this.level().isClientSide && this.getAge() == 0 && this.canFallInLove()) { ++ final ItemStack breedCopy = itemstack.copy(); // Paper - Fix EntityBreedEvent copying + this.usePlayerItem(player, hand, itemstack); +- this.setInLove(player); ++ this.setInLove(player, breedCopy); // Paper - Fix EntityBreedEvent copying + } else { + Level world = this.level(); + +@@ -657,7 +668,9 @@ ItemStack itemstack1 = this.getItemBySlot(EquipmentSlot.MAINHAND); if (!itemstack1.isEmpty() && !player.hasInfiniteMaterials()) { @@ -67,7 +78,7 @@ } this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(itemstack.getItem(), 1)); -@@ -772,7 +784,7 @@ +@@ -772,7 +785,7 @@ } public static Panda.Gene byName(String name) { @@ -76,7 +87,7 @@ } public static Panda.Gene getRandom(RandomSource random) { -@@ -876,10 +888,10 @@ +@@ -876,10 +889,10 @@ private final Panda panda; public PandaAvoidGoal(Panda panda, Class fleeFromType, float distance, double slowSpeed, double fastSpeed) { @@ -90,7 +101,7 @@ this.panda = panda; } -@@ -935,7 +947,9 @@ +@@ -935,7 +948,9 @@ ItemStack itemstack = Panda.this.getItemBySlot(EquipmentSlot.MAINHAND); if (!itemstack.isEmpty()) { @@ -100,7 +111,7 @@ Panda.this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY); int i = Panda.this.isLazy() ? Panda.this.random.nextInt(50) + 10 : Panda.this.random.nextInt(150) + 10; -@@ -1116,7 +1130,7 @@ +@@ -1116,7 +1131,7 @@ @Override protected void alertOther(Mob mob, LivingEntity target) { if (mob instanceof Panda && mob.isAggressive()) { diff --git a/paper-server/patches/sources/net/minecraft/world/entity/animal/camel/Camel.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/animal/camel/Camel.java.patch index 58fdf79e56..e2b9543713 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/animal/camel/Camel.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/animal/camel/Camel.java.patch @@ -19,7 +19,7 @@ behaviorcontroller.tick(world, this); gameprofilerfiller.pop(); -@@ -386,7 +389,7 @@ +@@ -386,13 +389,13 @@ boolean flag = this.getHealth() < this.getMaxHealth(); if (flag) { @@ -28,6 +28,13 @@ } boolean flag1 = this.isTamed() && this.getAge() == 0 && this.canFallInLove(); + + if (flag1) { +- this.setInLove(player); ++ this.setInLove(player, item.copy()); // Paper - Fix EntityBreedEvent copying + } + + boolean flag2 = this.isBaby(); @@ -454,9 +457,15 @@ } diff --git a/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/AbstractHorse.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/AbstractHorse.java.patch index be0e10b03e..e975349f91 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/AbstractHorse.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/AbstractHorse.java.patch @@ -61,12 +61,12 @@ + @Override + public InventoryHolder getOwner() { + return (org.bukkit.entity.AbstractHorse) AbstractHorse.this.getBukkitEntity(); - } ++ } + + @Override + public Location getLocation() { + return AbstractHorse.this.getBukkitEntity().getLocation(); -+ } + } + // CraftBukkit end }; + public int maxDomestication = 100; // CraftBukkit - store max domestication value @@ -100,7 +100,22 @@ } @Override -@@ -541,7 +598,7 @@ +@@ -528,7 +585,7 @@ + b0 = 5; + if (!this.level().isClientSide && this.isTamed() && this.getAge() == 0 && !this.isInLove()) { + flag = true; +- this.setInLove(player); ++ this.setInLove(player, item.copy()); // Paper - Fix EntityBreedEvent copying + } + } else if (item.is(Items.GOLDEN_APPLE) || item.is(Items.ENCHANTED_GOLDEN_APPLE)) { + f = 10.0F; +@@ -536,12 +593,12 @@ + b0 = 10; + if (!this.level().isClientSide && this.isTamed() && this.getAge() == 0 && !this.isInLove()) { + flag = true; +- this.setInLove(player); ++ this.setInLove(player, item.copy()); // Paper - Fix EntityBreedEvent copying + } } if (this.getHealth() < this.getMaxHealth() && f > 0.0F) { @@ -155,14 +170,15 @@ if (!this.inventory.getItem(0).isEmpty()) { nbt.put("SaddleItem", this.inventory.getItem(0).save(this.registryAccess())); -@@ -910,6 +982,11 @@ +@@ -909,7 +981,12 @@ + if (uuid != null) { this.setOwnerUUID(uuid); - } ++ } + // CraftBukkit start + if (nbt.contains("Bukkit.MaxDomestication")) { + this.maxDomestication = nbt.getInt("Bukkit.MaxDomestication"); -+ } + } + // CraftBukkit end if (nbt.contains("SaddleItem", 10)) { diff --git a/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/Llama.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/Llama.java.patch index 10347a10c5..df44c55d01 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/Llama.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/animal/horse/Llama.java.patch @@ -25,7 +25,13 @@ private void setStrength(int strength) { this.entityData.set(Llama.DATA_STRENGTH_ID, Math.max(1, Math.min(5, strength))); } -@@ -176,7 +182,7 @@ +@@ -171,12 +177,12 @@ + f = 10.0F; + if (this.isTamed() && this.getAge() == 0 && this.canFallInLove()) { + flag = true; +- this.setInLove(player); ++ this.setInLove(player, item.copy()); // Paper - Fix EntityBreedEvent copying + } } if (this.getHealth() < this.getMaxHealth() && f > 0.0F) {