aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0194-Add-Mob-Goal-API.patch
blob: 1d8c7585d751719be507be9a5df297d676a72b13 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MiniDigger <admin@benndorf.dev>
Date: Fri, 3 Jan 2020 16:24:46 +0100
Subject: [PATCH] Add Mob Goal API


diff --git a/src/main/java/com/destroystokyo/paper/entity/ai/Goal.java b/src/main/java/com/destroystokyo/paper/entity/ai/Goal.java
new file mode 100644
index 0000000000000000000000000000000000000000..c57c5416c88e2070a082403ab0dda9d7f08d2a57
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/ai/Goal.java
@@ -0,0 +1,66 @@
+package com.destroystokyo.paper.entity.ai;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.EnumSet;
+
+import org.bukkit.entity.Mob;
+
+/**
+ * Represents an AI goal of an entity
+ */
+public interface Goal<T extends Mob> {
+
+    /**
+     * Checks if this goal should be activated
+     *
+     * @return if this goal should be activated
+     */
+    boolean shouldActivate();
+
+    /**
+     * Checks if this goal should stay active, defaults to {@link Goal#shouldActivate()}
+     *
+     * @return if this goal should stay active
+     */
+    default boolean shouldStayActive() {
+        return shouldActivate();
+    }
+
+    /**
+     * Called when this goal gets activated
+     */
+    default void start() {
+    }
+
+    /**
+     * Called when this goal gets stopped
+     */
+    default void stop() {
+    }
+
+    /**
+     * Called each tick the goal is activated
+     */
+    default void tick() {
+    }
+
+    /**
+     * A unique key that identifies this type of goal. Plugins should use their own namespace, not the minecraft
+     * namespace. Additionally, this key also specifies to what mobs this goal can be applied to
+     *
+     * @return the goal key
+     */
+    @NotNull
+    GoalKey<T> getKey();
+
+    /**
+     * Returns a list of all applicable flags for this goal.<br>
+     *
+     * This method is only called on construction.
+     *
+     * @return the subtypes.
+     */
+    @NotNull
+    EnumSet<GoalType> getTypes();
+}
diff --git a/src/main/java/com/destroystokyo/paper/entity/ai/GoalKey.java b/src/main/java/com/destroystokyo/paper/entity/ai/GoalKey.java
new file mode 100644
index 0000000000000000000000000000000000000000..9cd98c6fcfa3eb439d9013ef76ef4661175a0e5a
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/ai/GoalKey.java
@@ -0,0 +1,64 @@
+package com.destroystokyo.paper.entity.ai;
+
+import com.google.common.base.Objects;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.StringJoiner;
+
+import org.bukkit.NamespacedKey;
+import org.bukkit.entity.Mob;
+
+/**
+ *
+ * Used to identify a Goal. Consists of a {@link NamespacedKey} and the type of mob the goal can be applied to
+ *
+ * @param <T> the type of mob the goal can be applied to
+ */
+public class GoalKey<T extends Mob> {
+
+    private final Class<T> entityClass;
+    private final NamespacedKey namespacedKey;
+
+    private GoalKey(@NotNull Class<T> entityClass, @NotNull NamespacedKey namespacedKey) {
+        this.entityClass = entityClass;
+        this.namespacedKey = namespacedKey;
+    }
+
+    @NotNull
+    public Class<T> getEntityClass() {
+        return entityClass;
+    }
+
+    @NotNull
+    public NamespacedKey getNamespacedKey() {
+        return namespacedKey;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        GoalKey<?> goalKey = (GoalKey<?>) o;
+        return Objects.equal(entityClass, goalKey.entityClass) &&
+               Objects.equal(namespacedKey, goalKey.namespacedKey);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(entityClass, namespacedKey);
+    }
+
+    @Override
+    public String toString() {
+        return new StringJoiner(", ", GoalKey.class.getSimpleName() + "[", "]")
+            .add("entityClass=" + entityClass)
+            .add("namespacedKey=" + namespacedKey)
+            .toString();
+    }
+
+    @NotNull
+    public static <A extends Mob> GoalKey<A> of(@NotNull Class<A> entityClass, @NotNull NamespacedKey namespacedKey) {
+        return new GoalKey<>(entityClass, namespacedKey);
+    }
+}
diff --git a/src/main/java/com/destroystokyo/paper/entity/ai/GoalType.java b/src/main/java/com/destroystokyo/paper/entity/ai/GoalType.java
new file mode 100644
index 0000000000000000000000000000000000000000..7024c8f484d2460abf3abfe65a29771d814105ec
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/ai/GoalType.java
@@ -0,0 +1,17 @@
+package com.destroystokyo.paper.entity.ai;
+
+/**
+ * Represents the subtype of a goal. Used by minecraft to disable certain types of goals if needed.
+ */
+public enum GoalType {
+
+    MOVE,
+    LOOK,
+    JUMP,
+    TARGET,
+    /**
+     * Used to map vanilla goals, that are a behavior goal but don't have a type set...
+     */
+    UNKNOWN_BEHAVIOR,
+
+}
diff --git a/src/main/java/com/destroystokyo/paper/entity/ai/MobGoals.java b/src/main/java/com/destroystokyo/paper/entity/ai/MobGoals.java
new file mode 100644
index 0000000000000000000000000000000000000000..e21f7574763dd4f13794f91bbef192ef66a8f5e9
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/entity/ai/MobGoals.java
@@ -0,0 +1,50 @@
+package com.destroystokyo.paper.entity.ai;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.Collection;
+
+import org.bukkit.entity.Mob;
+
+/**
+ * Represents a part of the "brain" of a mob. It tracks all tasks (running or not), allows adding and removing goals
+ */
+public interface MobGoals {
+
+    <T extends Mob> void addGoal(@NotNull T mob, int priority, @NotNull Goal<T> goal);
+
+    <T extends Mob> void removeGoal(@NotNull T mob, @NotNull Goal<T> goal);
+
+    <T extends Mob> void removeAllGoals(@NotNull T mob);
+
+    <T extends Mob> void removeAllGoals(@NotNull T mob, @NotNull GoalType type);
+
+    <T extends Mob> void removeGoal(@NotNull T mob, @NotNull GoalKey<T> key);
+
+    <T extends Mob> boolean hasGoal(@NotNull T mob, @NotNull GoalKey<T> key);
+
+    @Nullable
+    <T extends Mob> Goal<T> getGoal(@NotNull T mob, @NotNull GoalKey<T> key);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getGoals(@NotNull T mob, @NotNull GoalKey<T> key);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getAllGoals(@NotNull T mob);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getAllGoals(@NotNull T mob, @NotNull GoalType type);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getAllGoalsWithout(@NotNull T mob, @NotNull GoalType type);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getRunningGoals(@NotNull T mob);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getRunningGoals(@NotNull T mob, @NotNull GoalType type);
+
+    @NotNull
+    <T extends Mob> Collection<Goal<T>> getRunningGoalsWithout(@NotNull T mob, @NotNull GoalType type);
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index b608a6dc26bfc6d08f1e31107fed8ef1aaf90e1d..79db7b5c25a7c824b107a5c79f40c61983d2757c 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -2542,6 +2542,16 @@ public final class Bukkit {
     public static boolean isStopping() {
         return server.isStopping();
     }
+
+    /**
+     * Returns the {@link com.destroystokyo.paper.entity.ai.MobGoals} manager
+     *
+     * @return the mob goals manager
+     */
+    @NotNull
+    public static com.destroystokyo.paper.entity.ai.MobGoals getMobGoals() {
+        return server.getMobGoals();
+    }
     // Paper end
 
     @NotNull
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 1f4ebdaf9cfd14de9ff2d0cea7d110fe7630dc9a..711de014b7cf69459526e6c20c94f29ee4db11c4 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -2215,5 +2215,13 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
      * @return true if server is in the process of being shutdown
      */
     boolean isStopping();
+
+    /**
+     * Returns the {@link com.destroystokyo.paper.entity.ai.MobGoals} manager
+     *
+     * @return the mob goals manager
+     */
+    @NotNull
+    com.destroystokyo.paper.entity.ai.MobGoals getMobGoals();
     // Paper end
 }