aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0935-Add-EntityFertilizeEggEvent.patch
blob: 1d6a8aeb2c39443ded702017026c6d15e9c6ce1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
Date: Fri, 24 Jun 2022 12:39:34 +0200
Subject: [PATCH] Add EntityFertilizeEggEvent


diff --git a/src/main/java/net/minecraft/world/entity/animal/Turtle.java b/src/main/java/net/minecraft/world/entity/animal/Turtle.java
index c48bf4ca76f70d878378fc43c8270de5c3332824..098ae9d8fa3e7cad8473a877decba771f6bd1b36 100644
--- a/src/main/java/net/minecraft/world/entity/animal/Turtle.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Turtle.java
@@ -441,6 +441,10 @@ public class Turtle extends Animal {
             if (entityplayer == null && this.partner.getLoveCause() != null) {
                 entityplayer = this.partner.getLoveCause();
             }
+            // Paper start - Add EntityFertilizeEggEvent event
+            io.papermc.paper.event.entity.EntityFertilizeEggEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityFertilizeEggEvent(this.animal, this.partner);
+            if (event.isCancelled()) return;
+            // Paper end - Add EntityFertilizeEggEvent event
 
             if (entityplayer != null) {
                 entityplayer.awardStat(Stats.ANIMALS_BRED);
@@ -455,7 +459,7 @@ public class Turtle extends Animal {
             RandomSource randomsource = this.animal.getRandom();
 
             if (this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT)) {
-                this.level.addFreshEntity(new ExperienceOrb(this.level, this.animal.getX(), this.animal.getY(), this.animal.getZ(), randomsource.nextInt(7) + 1, org.bukkit.entity.ExperienceOrb.SpawnReason.BREED, entityplayer)); // Paper;
+                if(event.getExperience() > 0) this.level.addFreshEntity(new ExperienceOrb(this.level, this.animal.getX(), this.animal.getY(), this.animal.getZ(), event.getExperience(), org.bukkit.entity.ExperienceOrb.SpawnReason.BREED, entityplayer)); // Paper - Add EntityFertilizeEggEvent event
             }
 
         }
diff --git a/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java b/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
index 22eb0a8fc35baa04b34265b62aa29a71f3cc7343..203691417e208b9e023e5f8c3b76993db2747ba8 100644
--- a/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
+++ b/src/main/java/net/minecraft/world/entity/animal/frog/Frog.java
@@ -242,7 +242,12 @@ public class Frog extends Animal implements VariantHolder<FrogVariant> {
 
     @Override
     public void spawnChildFromBreeding(ServerLevel world, Animal other) {
-        this.finalizeSpawnChildFromBreeding(world, other, (AgeableMob)null);
+        // Paper start - Add EntityFertilizeEggEvent event
+        final io.papermc.paper.event.entity.EntityFertilizeEggEvent result = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityFertilizeEggEvent(this, other);
+        if (result.isCancelled()) return;
+
+        this.finalizeSpawnChildFromBreeding(world, other, (AgeableMob)null, result.getExperience()); // Paper - use craftbukkit call that takes experience amount
+        // Paper end - Add EntityFertilizeEggEvent event
         this.getBrain().setMemory(MemoryModuleType.IS_PREGNANT, Unit.INSTANCE);
     }
 
diff --git a/src/main/java/net/minecraft/world/entity/animal/sniffer/Sniffer.java b/src/main/java/net/minecraft/world/entity/animal/sniffer/Sniffer.java
index c0f19138c6a00ce6ae837c972ae4af522ddd2895..a6d98f64910c816a5c11867d12698f5cd63c751a 100644
--- a/src/main/java/net/minecraft/world/entity/animal/sniffer/Sniffer.java
+++ b/src/main/java/net/minecraft/world/entity/animal/sniffer/Sniffer.java
@@ -350,11 +350,16 @@ public class Sniffer extends Animal {
 
     @Override
     public void spawnChildFromBreeding(ServerLevel world, Animal other) {
+        // Paper start - Add EntityFertilizeEggEvent event
+        final io.papermc.paper.event.entity.EntityFertilizeEggEvent result = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityFertilizeEggEvent(this, other);
+        if (result.isCancelled()) return;
+        // Paper end - Add EntityFertilizeEggEvent event
+
         ItemStack itemstack = new ItemStack(Items.SNIFFER_EGG);
         ItemEntity entityitem = new ItemEntity(world, this.position().x(), this.position().y(), this.position().z(), itemstack);
 
         entityitem.setDefaultPickUpDelay();
-        this.finalizeSpawnChildFromBreeding(world, other, (AgeableMob) null);
+        this.finalizeSpawnChildFromBreeding(world, other, (AgeableMob) null, result.getExperience()); // Paper - Add EntityFertilizeEggEvent event
         this.playSound(SoundEvents.SNIFFER_EGG_PLOP, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 0.5F);
         world.addFreshEntity(entityitem);
     }
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 22ebf307641f9aa7231df8607bb859785be60f7a..5cc8d5403058638ddecbed08059ecd96257f9dcb 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1983,4 +1983,29 @@ public class CraftEventFactory {
         return event.callEvent();
     }
     // Paper end
+
+    // Paper start - add EntityFertilizeEggEvent
+    /**
+     * Calls the io.papermc.paper.event.entity.EntityFertilizeEggEvent.
+     * If the event is cancelled, this method also resets the love on both the {@code breeding} and {@code other} entity.
+     *
+     * @param breeding the entity on which #spawnChildFromBreeding was called.
+     * @param other    the partner of the entity.
+     * @return the event after it was called. The instance may be used to retrieve the experience of the event.
+     */
+    public static io.papermc.paper.event.entity.EntityFertilizeEggEvent callEntityFertilizeEggEvent(net.minecraft.world.entity.animal.Animal breeding,
+                                                                                                    net.minecraft.world.entity.animal.Animal other) {
+        net.minecraft.server.level.ServerPlayer serverPlayer = breeding.getLoveCause();
+        if (serverPlayer == null) serverPlayer = other.getLoveCause();
+        final int experience = breeding.getRandom().nextInt(7) + 1; // From Animal#spawnChildFromBreeding(ServerLevel, Animal)
+
+        final io.papermc.paper.event.entity.EntityFertilizeEggEvent event = new io.papermc.paper.event.entity.EntityFertilizeEggEvent((org.bukkit.entity.LivingEntity) breeding.getBukkitEntity(), (org.bukkit.entity.LivingEntity) other.getBukkitEntity(), serverPlayer == null ? null : serverPlayer.getBukkitEntity(), breeding.breedItem == null ? null : org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(breeding.breedItem).clone(), experience);
+        if (!event.callEvent()) {
+            breeding.resetLove();
+            other.resetLove(); // stop the pathfinding to avoid infinite loop
+        }
+
+        return event;
+    }
+    // Paper end - add EntityFertilizeEggEvent
 }