diff options
Diffstat (limited to 'bukkit/src/main/java/com/leonardobishop')
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) { |
