Skip to content

Commit

Permalink
Add spawn reason option to mob killing task type
Browse files Browse the repository at this point in the history
Closes #460
  • Loading branch information
Krakenied authored and LMBishop committed Feb 22, 2024
1 parent 03be381 commit 80e8af6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;

Expand All @@ -34,6 +35,7 @@ public MobkillingTaskType(BukkitQuestsPlugin plugin) {
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"));
Expand Down Expand Up @@ -96,8 +98,10 @@ private void handle(Player player, LivingEntity entity, int eventAmount) {
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();
Expand All @@ -123,6 +127,11 @@ private void handle(Player player, LivingEntity entity, int eventAmount) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,38 @@ public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull Pending
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;

Expand Down Expand Up @@ -785,6 +817,54 @@ public static TaskType.ConfigValidator useEntityListConfigValidator(TaskType typ
};
}

/**
* 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.
Expand Down

0 comments on commit 80e8af6

Please sign in to comment.