Add Llama caravan events

This commit is contained in:
David Scandurra 2023-10-30 03:43:00 +01:00
parent 1865625d95
commit 443a912dff
2 changed files with 286 additions and 0 deletions

View File

@ -0,0 +1,176 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: David Scandurra <david.scandurra@check24.de>
Date: Mon, 30 Oct 2023 03:35:10 +0100
Subject: [PATCH] Add Llama caravan events
diff --git a/src/main/java/io/papermc/paper/event/entity/LlamaJoinCaravanEvent.java b/src/main/java/io/papermc/paper/event/entity/LlamaJoinCaravanEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..3e5b95757be63261eba893a34caeb59afe3e575c
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/entity/LlamaJoinCaravanEvent.java
@@ -0,0 +1,86 @@
+package io.papermc.paper.event.entity;
+
+import org.bukkit.entity.Llama;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.entity.EntityEvent;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called just before a {@link Llama} joins a caravan.
+ */
+public class LlamaJoinCaravanEvent extends EntityEvent implements Cancellable {
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+ @NotNull
+ private final Llama appendTo;
+ @NotNull
+ private final Reason reason;
+ private boolean cancelled;
+
+ @ApiStatus.Internal
+ public LlamaJoinCaravanEvent(final @NotNull Llama what, final @NotNull Llama appendTo, final @NotNull Reason reason) {
+ super(what);
+ this.appendTo = appendTo;
+ this.reason = reason;
+ }
+
+ /**
+ * The {@link Llama} that tries to join a caravan.
+ */
+ @Override
+ public @NotNull Llama getEntity() {
+ return (Llama) super.getEntity();
+ }
+
+ /**
+ * The LLama {@link LlamaJoinCaravanEvent#getEntity()} is appended to.
+ * @return The LLama new {@link Llama#getCaravanHead()} of {@link LlamaJoinCaravanEvent#getEntity()}
+ */
+ public @NotNull Llama getAppendTo() {
+ return appendTo;
+ }
+
+ /**
+ * @return The reason for {@link LlamaJoinCaravanEvent#getEntity()} to join the caravan
+ */
+ public Reason getReason() {
+ return reason;
+ }
+
+ public enum Reason {
+ /**
+ * A LLama AI goal commanded the join.
+ */
+ AI_GOAL,
+ /**
+ * A plugin invoking {@link Llama#joinCaravan(Llama)}.
+ */
+ PLUGIN,
+ /**
+ * Undefined reason.
+ */
+ UNKNOWN
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/event/entity/LlamaLeaveCaravanEvent.java b/src/main/java/io/papermc/paper/event/entity/LlamaLeaveCaravanEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..3f99c0ae3e72e75b8609317e4739cf8b3d0a0995
--- /dev/null
+++ b/src/main/java/io/papermc/paper/event/entity/LlamaLeaveCaravanEvent.java
@@ -0,0 +1,72 @@
+package io.papermc.paper.event.entity;
+
+import org.bukkit.entity.Llama;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.entity.EntityEvent;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called just before a {@link Llama} leaves a caravan.
+ */
+public class LlamaLeaveCaravanEvent extends EntityEvent implements Cancellable {
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+ @NotNull
+ private final Reason reason;
+ private boolean cancelled;
+
+ @ApiStatus.Internal
+ public LlamaLeaveCaravanEvent(final @NotNull Llama what, final @NotNull Reason reason) {
+ super(what);
+ this.reason = reason;
+ }
+
+ @Override
+ public @NotNull Llama getEntity() {
+ return (Llama) super.getEntity();
+ }
+
+ /**
+ * @return The reason for {@link LlamaJoinCaravanEvent#getEntity()} to leave the caravan
+ */
+ public @NotNull Reason getReason() {
+ return reason;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ public enum Reason {
+ /**
+ * Lost the head of this caravan. Either because it died or it got out of range.
+ */
+ LOST_HEAD,
+ /**
+ * A plugin invoking {@link Llama#leaveCaravan()}.
+ */
+ PLUGIN,
+ /**
+ * Undefined reason.
+ */
+ UNKNOWN
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
+ }
+}

View File

@ -0,0 +1,110 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: David Scandurra <david.scandurra@check24.de>
Date: Mon, 30 Oct 2023 03:41:35 +0100
Subject: [PATCH] Add Llama caravan events
diff --git a/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java b/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
index 21725aee29e9120d1c7e1e19f91c21a73a28844f..778a4037f87a8cfe9be0c9ff9db12323cccbe0c2 100644
--- a/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
+++ b/src/main/java/net/minecraft/world/entity/ai/goal/LlamaFollowCaravanGoal.java
@@ -61,7 +61,7 @@ public class LlamaFollowCaravanGoal extends Goal {
} else if (!llama.isLeashed() && !this.firstIsLeashed(llama, 1)) {
return false;
} else {
- this.llama.joinCaravan(llama);
+ this.llama.joinCaravan(llama, io.papermc.paper.event.entity.LlamaJoinCaravanEvent.Reason.AI_GOAL); // Paper - add reason
return true;
}
} else {
@@ -97,7 +97,7 @@ public class LlamaFollowCaravanGoal extends Goal {
@Override
public void stop() {
- this.llama.leaveCaravan();
+ this.llama.leaveCaravan(io.papermc.paper.event.entity.LlamaLeaveCaravanEvent.Reason.LOST_HEAD); // Paper - add reason
this.speedModifier = 2.1D;
}
diff --git a/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java b/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java
index 5f61c97478f005aaaaad1b027118079db7275cf7..72bfb0f59c07af274f8cd58b617da3a038bf3019 100644
--- a/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java
+++ b/src/main/java/net/minecraft/world/entity/animal/horse/Llama.java
@@ -437,19 +437,58 @@ public class Llama extends AbstractChestedHorse implements VariantHolder<Llama.V
}
}
+ // Paper start - Add Llama caravan events
+ /**
+ * @deprecated Use {@link Llama#leaveCaravan(io.papermc.paper.event.entity.LlamaLeaveCaravanEvent.Reason)}
+ */
+ @Deprecated
+ @io.papermc.paper.annotation.DoNotUse
public void leaveCaravan() {
+ leaveCaravan(io.papermc.paper.event.entity.LlamaLeaveCaravanEvent.Reason.UNKNOWN);
+ }
+ // Paper end - Add Llama caravan events
+
+ public void leaveCaravan(io.papermc.paper.event.entity.LlamaLeaveCaravanEvent.Reason reason) { // Paper - add reason
if (this.caravanHead != null) {
+ // Paper start - Add Llama caravan events
+ if (!new io.papermc.paper.event.entity.LlamaLeaveCaravanEvent(getBukkitEntity(), reason).callEvent()) {
+ return;
+ }
+ // Paper end - Add Llama caravan events
this.caravanHead.caravanTail = null;
}
this.caravanHead = null;
}
+ // Paper start - Add Llama caravan events
+ /**
+ * @deprecated Use {@link Llama#joinCaravan(Llama, io.papermc.paper.event.entity.LlamaJoinCaravanEvent.Reason)}
+ */
+ @Deprecated
+ @io.papermc.paper.annotation.DoNotUse
public void joinCaravan(Llama llama) {
+ joinCaravan(llama, io.papermc.paper.event.entity.LlamaJoinCaravanEvent.Reason.UNKNOWN);
+ }
+ // Paper end - Add Llama caravan events
+
+ public void joinCaravan(Llama llama, io.papermc.paper.event.entity.LlamaJoinCaravanEvent.Reason reason) { // Paper - add reason
+ // Paper start - Add Llama caravan events
+ if (!new io.papermc.paper.event.entity.LlamaJoinCaravanEvent(getBukkitEntity(), llama.getBukkitEntity(), reason).callEvent()) {
+ return;
+ }
+ // Paper end - Add Llama caravan events
this.caravanHead = llama;
this.caravanHead.caravanTail = this;
}
+ // Paper start
+ @Override
+ public org.bukkit.craftbukkit.entity.CraftLlama getBukkitEntity() {
+ return (org.bukkit.craftbukkit.entity.CraftLlama) super.getBukkitEntity();
+ }
+ // Paper end
+
public boolean hasCaravanTail() {
return this.caravanTail != null;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java
index 0ad16ee7b33582d214dab41eeee378d52c8e38ed..5e211b4f5d22b892a942fe3b7919e15b7a4de177 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLlama.java
@@ -67,12 +67,12 @@ public class CraftLlama extends CraftChestedHorse implements Llama, com.destroys
@Override
public void joinCaravan(@org.jetbrains.annotations.NotNull Llama llama) {
- this.getHandle().joinCaravan(((CraftLlama) llama).getHandle());
+ this.getHandle().joinCaravan(((CraftLlama) llama).getHandle(), io.papermc.paper.event.entity.LlamaJoinCaravanEvent.Reason.PLUGIN); // Paper - add reason
}
@Override
public void leaveCaravan() {
- this.getHandle().leaveCaravan();
+ this.getHandle().leaveCaravan(io.papermc.paper.event.entity.LlamaLeaveCaravanEvent.Reason.PLUGIN); // Paper - add reason
}
@Override