PaperMC/patches/api/0183-Add-ThrownEggHatchEvent.patch

135 lines
4.1 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
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
2024-02-01 10:15:57 +01:00
index 0000000000000000000000000000000000000000..092bab9be36acc0f04c7ea5b3510879169a0a125
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/entity/ThrownEggHatchEvent.java
2024-02-01 10:15:57 +01:00
@@ -0,0 +1,120 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.entity;
+
2024-02-01 10:15:57 +01:00
+import com.google.common.base.Preconditions;
2021-06-11 14:02:28 +02:00
+import org.bukkit.entity.Egg;
+import org.bukkit.entity.EntityType;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2021-06-11 14:02:28 +02:00
+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 {
2024-02-01 10:15:57 +01:00
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ private final Egg egg;
+ private boolean hatching;
+ private byte numHatches;
2024-02-01 10:15:57 +01:00
+ private EntityType hatchType;
2021-06-11 14:02:28 +02:00
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2021-06-11 14:02:28 +02:00
+ 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() {
2024-02-01 10:15:57 +01:00
+ return this.egg;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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() {
2024-02-01 10:15:57 +01:00
+ return this.hatching;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets whether the egg will hatch or not.
+ *
2024-02-01 10:15:57 +01:00
+ * @param hatching {@code true} if you want the egg to hatch, {@code false} if you want it
2021-06-11 14:02:28 +02:00
+ * not to
+ */
+ public void setHatching(boolean hatching) {
+ this.hatching = hatching;
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Get the type of the mob being hatched ({@link EntityType#CHICKEN} by default)
2021-06-11 14:02:28 +02:00
+ *
+ * @return The type of the mob being hatched by the egg
+ */
+ @NotNull
+ public EntityType getHatchingType() {
2024-02-01 10:15:57 +01:00
+ return this.hatchType;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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) {
2024-02-01 10:15:57 +01:00
+ Preconditions.checkArgument(hatchType.isSpawnable(), "Can't spawn that entity type from an egg!");
2021-06-11 14:02:28 +02:00
+ 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>
2024-02-01 10:15:57 +01:00
+ * <li>7/8 chance of being 0
+ * <li>31/256 ~= 1/8 chance to be 1
+ * <li>1/256 chance to be 4
2021-06-11 14:02:28 +02:00
+ * </ul>
+ *
+ * @return The number of mobs going to be hatched by the egg
+ */
+ public byte getNumHatches() {
2024-02-01 10:15:57 +01:00
+ return this.numHatches;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Change the number of mobs coming out of the hatched egg
+ * <p>
2024-02-01 10:15:57 +01:00
+ * The boolean hatching will override this number. I.e. If hatching is
+ * {@code false}, this number will not matter
2021-06-11 14:02:28 +02:00
+ *
+ * @param numHatches The number of mobs coming out of the egg
+ */
+ public void setNumHatches(byte numHatches) {
+ this.numHatches = numHatches;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}