diff options
| author | Krakenied <Krakenied1@gmail.com> | 2024-02-20 09:42:17 +0100 |
|---|---|---|
| committer | Leonardo Bishop <13875753+LMBishop@users.noreply.github.com> | 2024-02-22 16:35:06 +0000 |
| commit | 80e8af636ef4c25e28635ea3b7f1d5c9716c7a7c (patch) | |
| tree | 1bb940703f1ea7b5eb4efb4ac2ec1499fdf09997 /bukkit/src | |
| parent | 03be381bc8a7ada87c5f1d1114861914e32432ce (diff) | |
Add spawn reason option to mob killing task type
Closes https://github.com/LMBishop/Quests/issues/460
Diffstat (limited to 'bukkit/src')
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/MobkillingTaskType.java | 11 | ||||
| -rw-r--r-- | bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java | 80 |
2 files changed, 90 insertions, 1 deletions
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 19265f92..ca4074bb 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,6 +19,7 @@ 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; @@ -34,6 +35,7 @@ public final class MobkillingTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount")); super.addConfigValidator(TaskUtils.useEntityListConfigValidator(this, "mob", "mobs")); + super.addConfigValidator(TaskUtils.useSpawnReasonListConfigValidator(this, "spawn-reason", "spawn-reasons")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "hostile")); super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "item")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "data")); @@ -96,8 +98,10 @@ public final class MobkillingTaskType extends BukkitTaskType { return; } + CreatureSpawnEvent.SpawnReason spawnReason = entity.getEntitySpawnReason(); + //noinspection deprecation - final String customName = entity.getCustomName(); + String customName = entity.getCustomName(); for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { Quest quest = pendingTask.quest(); @@ -123,6 +127,11 @@ public final class MobkillingTaskType extends BukkitTaskType { continue; } + if (!TaskUtils.matchSpawnReason(this, pendingTask, spawnReason, player.getUniqueId())) { + super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + if (!TaskUtils.matchString(this, pendingTask, customName, player.getUniqueId(), "name", "names", true, false)) { 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 c6508319..53635fa5 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 @@ -394,6 +394,38 @@ 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"); + } + + public static boolean matchSpawnReason(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull CreatureSpawnEvent.SpawnReason spawnReason, @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); + if (checkSpawnReasons == null) { + return true; + } else if (checkSpawnReasons.isEmpty()) { + return false; + } + + CreatureSpawnEvent.SpawnReason reason; + + for (String spawnReasonName : checkSpawnReasons) { + reason = CreatureSpawnEvent.SpawnReason.valueOf(spawnReasonName); + + type.debug("Checking against spawn reason " + reason, pendingTask.quest.getId(), task.getId(), player); + + if (reason == spawnReason) { + type.debug("Spawn reason match", pendingTask.quest.getId(), task.getId(), player); + return true; + } else { + type.debug("Spawn reason mismatch", pendingTask.quest.getId(), task.getId(), player); + } + } + + return false; + } + public static boolean matchString(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @Nullable String string, @NotNull UUID player, final @NotNull String stringKey, final @NotNull String listKey, boolean legacyColor, boolean ignoreCase) { Task task = pendingTask.task; @@ -787,6 +819,54 @@ public class TaskUtils { /** * Returns a config validator which checks if at least one value in the given + * paths is a valid list of spawn reasons. + * <p> + * The list of entities is expected to be in the format of: + * <pre>key: "SPAWN_REASON"</pre> + * where SPAWN_REASON is the name of an entity. Alternatively, the list + * of entities can be in the format of: + * <pre>key: + * - "SPAWN_REASON" + * - "..."</pre> + * </p> + * + * @param paths a list of valid paths for task + * @return config validator + */ + public static TaskType.ConfigValidator useSpawnReasonListConfigValidator(TaskType type, String... paths) { + return (config, problems) -> { + for (String path : paths) { + Object configObject = config.get(path); + + List<String> checkSpawnReasons = new ArrayList<>(); + if (configObject instanceof List<?> configList) { + for (Object object : configList) { + checkSpawnReasons.add(String.valueOf(object)); + } + } else { + if (configObject == null) { + continue; + } + checkSpawnReasons.add(String.valueOf(configObject)); + } + + for (String spawnReason : checkSpawnReasons) { + try { + CreatureSpawnEvent.SpawnReason.valueOf(spawnReason); + } catch (IllegalArgumentException ex) { + problems.add(new ConfigProblem(ConfigProblem.ConfigProblemType.WARNING, + ConfigProblemDescriptions.UNKNOWN_SPAWN_REASON.getDescription(spawnReason), + ConfigProblemDescriptions.UNKNOWN_SPAWN_REASON.getExtendedDescription(spawnReason), + path)); + } + } + break; + } + }; + } + + /** + * Returns a config validator which checks if at least one value in the given * paths is a valid list of enchantments. * <p> * The list of entities is expected to be in the format of: |
