aboutsummaryrefslogtreecommitdiffstats
path: root/bukkit/src
diff options
context:
space:
mode:
authorKrakenied <Krakenied1@gmail.com>2024-02-27 16:21:22 +0100
committerLeonardo Bishop <13875753+LMBishop@users.noreply.github.com>2024-03-09 16:14:21 +0000
commit587172c903124e8c6589453347bf632fb5b01673 (patch)
tree6007e0ca779f1055cb3c58977f44a387c8b19c46 /bukkit/src
parent7e2124cc2f8602b950d092c50ce6726f390b3287 (diff)
Apparently getEntitySpawnReason method is supported only on Paper + forks
Diffstat (limited to 'bukkit/src')
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java5
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingTaskType.java5
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java33
3 files changed, 32 insertions, 11 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java
index f0177b16..1cec5ffc 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MilkingTaskType.java
@@ -15,7 +15,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
-import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;
@@ -60,8 +59,6 @@ public final class MilkingTaskType extends BukkitTaskType {
return;
}
- CreatureSpawnEvent.SpawnReason spawnReason = entity.getEntitySpawnReason();
-
for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) {
Quest quest = pendingTask.quest();
Task task = pendingTask.task();
@@ -74,7 +71,7 @@ public final class MilkingTaskType extends BukkitTaskType {
continue;
}
- if (!TaskUtils.matchSpawnReason(this, pendingTask, spawnReason, player.getUniqueId())) {
+ if (!TaskUtils.matchSpawnReason(this, pendingTask, entity, player.getUniqueId())) {
super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId());
continue;
}
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingTaskType.java
index ca4074bb..81da75f7 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingTaskType.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingTaskType.java
@@ -19,7 +19,6 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
-import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.inventory.ItemStack;
@@ -98,8 +97,6 @@ public final class MobkillingTaskType extends BukkitTaskType {
return;
}
- CreatureSpawnEvent.SpawnReason spawnReason = entity.getEntitySpawnReason();
-
//noinspection deprecation
String customName = entity.getCustomName();
@@ -127,7 +124,7 @@ public final class MobkillingTaskType extends BukkitTaskType {
continue;
}
- if (!TaskUtils.matchSpawnReason(this, pendingTask, spawnReason, player.getUniqueId())) {
+ if (!TaskUtils.matchSpawnReason(this, pendingTask, entity, player.getUniqueId())) {
super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId());
continue;
}
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java
index 53635fa5..78728f89 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java
@@ -31,6 +31,8 @@ import org.bukkit.material.Colorable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -394,11 +396,21 @@ public class TaskUtils {
return false;
}
- public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull CreatureSpawnEvent.SpawnReason spawnReason, @NotNull UUID player) {
- return matchSpawnReason(type, pendingTask, spawnReason, player, "spawn-reason", "spawn-reasons");
+ private static Method getEntitySpawnReasonMethod;
+
+ static {
+ try {
+ getEntitySpawnReasonMethod = Entity.class.getMethod("getEntitySpawnReason");
+ } catch (NoSuchMethodException ignored) {
+ // server version cannot support the method (doesn't work on Spigot)
+ }
}
- public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull CreatureSpawnEvent.SpawnReason spawnReason, @NotNull UUID player, @NotNull String stringKey, @NotNull String listKey) {
+ public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Entity entity, @NotNull UUID player) {
+ return matchSpawnReason(type, pendingTask, entity, player, "spawn-reason", "spawn-reasons");
+ }
+
+ public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Entity entity, @NotNull UUID player, @NotNull String stringKey, @NotNull String listKey) {
Task task = pendingTask.task;
List<String> checkSpawnReasons = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey(stringKey) ? stringKey : listKey);
@@ -408,6 +420,21 @@ public class TaskUtils {
return false;
}
+ if (getEntitySpawnReasonMethod == null) {
+ type.debug("Spawn reason is specified but the server software doesn't have the method necessary to get it", pendingTask.quest.getId(), task.getId(), player);
+
+ // it is supported only on Paper so we simply ignore it
+ return true;
+ }
+
+ CreatureSpawnEvent.SpawnReason spawnReason;
+ try {
+ spawnReason = (CreatureSpawnEvent.SpawnReason) getEntitySpawnReasonMethod.invoke(entity);
+ } catch (IllegalAccessException | InvocationTargetException e) {
+ // it should never happen
+ return false;
+ }
+
CreatureSpawnEvent.SpawnReason reason;
for (String spawnReasonName : checkSpawnReasons) {