aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0177-Add-ThrownEggHatchEvent.patch
blob: 8636b6d6df59c3009e7df4b378303a7df94b60a7 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Sun, 9 Feb 2020 00:19:08 -0600
Subject: [PATCH] Add ThrownEggHatchEvent

Adds a new event similar to PlayerEggThrowEvent, but without the Player requirement
(dispensers can throw eggs to hatch them, too).

diff --git a/src/main/java/com/destroystokyo/paper/event/entity/ThrownEggHatchEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/ThrownEggHatchEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..092bab9be36acc0f04c7ea5b3510879169a0a125
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/entity/ThrownEggHatchEvent.java
@@ -0,0 +1,120 @@
+package com.destroystokyo.paper.event.entity;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.entity.Egg;
+import org.bukkit.entity.EntityType;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a thrown egg might hatch.
+ * <p>
+ * This event fires for all thrown eggs that may hatch, players, dispensers, etc.
+ */
+public class ThrownEggHatchEvent extends Event {
+
+    private static final HandlerList HANDLER_LIST = new HandlerList();
+
+    private final Egg egg;
+    private boolean hatching;
+    private byte numHatches;
+    private EntityType hatchType;
+
+    @ApiStatus.Internal
+    public ThrownEggHatchEvent(@NotNull final Egg egg, final boolean hatching, final byte numHatches, @NotNull final EntityType hatchingType) {
+        this.egg = egg;
+        this.hatching = hatching;
+        this.numHatches = numHatches;
+        this.hatchType = hatchingType;
+    }
+
+    /**
+     * Gets the egg involved in this event.
+     *
+     * @return the egg involved in this event
+     */
+    @NotNull
+    public Egg getEgg() {
+        return this.egg;
+    }
+
+    /**
+     * Gets whether the egg is hatching or not. Will be what the server
+     * would've done without interaction.
+     *
+     * @return boolean Whether the egg is going to hatch or not
+     */
+    public boolean isHatching() {
+        return this.hatching;
+    }
+
+    /**
+     * Sets whether the egg will hatch or not.
+     *
+     * @param hatching {@code true} if you want the egg to hatch, {@code false} if you want it
+     *     not to
+     */
+    public void setHatching(boolean hatching) {
+        this.hatching = hatching;
+    }
+
+    /**
+     * Get the type of the mob being hatched ({@link EntityType#CHICKEN} by default)
+     *
+     * @return The type of the mob being hatched by the egg
+     */
+    @NotNull
+    public EntityType getHatchingType() {
+        return this.hatchType;
+    }
+
+    /**
+     * Change the type of mob being hatched by the egg
+     *
+     * @param hatchType The type of the mob being hatched by the egg
+     */
+    public void setHatchingType(@NotNull EntityType hatchType) {
+        Preconditions.checkArgument(hatchType.isSpawnable(), "Can't spawn that entity type from an egg!");
+        this.hatchType = hatchType;
+    }
+
+    /**
+     * Get the number of mob hatches from the egg. By default the number will
+     * be the number the server would've done
+     * <ul>
+     *  <li>7/8 chance of being 0
+     *  <li>31/256 ~= 1/8 chance to be 1
+     *  <li>1/256 chance to be 4
+     * </ul>
+     *
+     * @return The number of mobs going to be hatched by the egg
+     */
+    public byte getNumHatches() {
+        return this.numHatches;
+    }
+
+    /**
+     * Change the number of mobs coming out of the hatched egg
+     * <p>
+     * The boolean hatching will override this number. I.e. If hatching is
+     * {@code false}, this number will not matter
+     *
+     * @param numHatches The number of mobs coming out of the egg
+     */
+    public void setNumHatches(byte numHatches) {
+        this.numHatches = numHatches;
+    }
+
+    @NotNull
+    @Override
+    public HandlerList getHandlers() {
+        return HANDLER_LIST;
+    }
+
+    @NotNull
+    public static HandlerList getHandlerList() {
+        return HANDLER_LIST;
+    }
+}