diff options
4 files changed, 129 insertions, 2 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java index f88cbda1..a1111120 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java @@ -76,6 +76,7 @@ import com.leonardobishop.quests.bukkit.tasktype.type.EnchantingTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.ExpEarnTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.FarmingTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.FishingTaskType; +import com.leonardobishop.quests.bukkit.tasktype.type.HatchingTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.InteractTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.InventoryTaskType; import com.leonardobishop.quests.bukkit.tasktype.type.MilkingTaskType; @@ -440,6 +441,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests { taskTypeManager.registerTaskType(() -> new BrewingTaskType(this), () -> CompatUtils.classWithMethodExists("org.bukkit.event.inventory.BrewEvent", "getResults")); taskTypeManager.registerTaskType(() -> new CompostingTaskType(this), () -> CompatUtils.classExists("io.papermc.paper.event.entity.EntityCompostItemEvent")); taskTypeManager.registerTaskType(() -> new FarmingTaskType(this), () -> CompatUtils.classExists("org.bukkit.block.data.Ageable")); + taskTypeManager.registerTaskType(() -> new HatchingTaskType(this), () -> CompatUtils.classExists("com.destroystokyo.paper.event.entity.ThrownEggHatchEvent")); taskTypeManager.registerTaskType(() -> new ReplenishingTaskType(this), () -> CompatUtils.classExists("com.destroystokyo.paper.loottable.LootableInventoryReplenishEvent")); taskTypeManager.registerTaskType(() -> new SmithingTaskType(this), () -> CompatUtils.classExists("org.bukkit.event.inventory.SmithItemEvent")); diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/HatchingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/HatchingTaskType.java new file mode 100644 index 00000000..7752cbbf --- /dev/null +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/HatchingTaskType.java @@ -0,0 +1,80 @@ +package com.leonardobishop.quests.bukkit.tasktype.type; + +import com.destroystokyo.paper.event.entity.ThrownEggHatchEvent; +import com.leonardobishop.quests.bukkit.BukkitQuestsPlugin; +import com.leonardobishop.quests.bukkit.tasktype.BukkitTaskType; +import com.leonardobishop.quests.bukkit.util.TaskUtils; +import com.leonardobishop.quests.bukkit.util.constraint.TaskConstraintSet; +import com.leonardobishop.quests.common.player.QPlayer; +import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; +import com.leonardobishop.quests.common.quest.Quest; +import com.leonardobishop.quests.common.quest.Task; +import org.bukkit.entity.Egg; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.projectiles.ProjectileSource; + +public final class HatchingTaskType extends BukkitTaskType { + + private final BukkitQuestsPlugin plugin; + + public HatchingTaskType(BukkitQuestsPlugin plugin) { + super("hatching", TaskUtils.TASK_ATTRIBUTION_STRING, "Hatch a set amount of entities of certain types."); + this.plugin = plugin; + + super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount")); + super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount")); + super.addConfigValidator(TaskUtils.useEntityListConfigValidator(this, "mob", "mobs")); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onThrownEggHatch(ThrownEggHatchEvent event) { + int numHatches = event.getNumHatches(); + if (numHatches == 0) { + return; + } + + ProjectileSource shooter = event.getEgg().getShooter(); + if (!(shooter instanceof Player player)) { + return; + } + + if (player.hasMetadata("NPC")) { + return; + } + + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); + if (qPlayer == null) { + return; + } + + EntityType hatchingType = event.getHatchingType(); + + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this, TaskConstraintSet.ALL)) { + Quest quest = pendingTask.quest(); + Task task = pendingTask.task(); + TaskProgress taskProgress = pendingTask.taskProgress(); + + super.debug("Player hatched " + hatchingType, quest.getId(), task.getId(), player.getUniqueId()); + + if (!TaskUtils.matchEntity(this, pendingTask, hatchingType, player.getUniqueId())) { + super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } + + int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress, numHatches); + super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); + + int amount = (int) task.getConfigValue("amount"); + + if (progress >= amount) { + super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); + taskProgress.setCompleted(true); + } + + TaskUtils.sendTrackAdvancement(player, quest, task, pendingTask, amount); + } + } +} 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 d4caf566..064601b9 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 @@ -390,6 +390,14 @@ public class TaskUtils { } public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull Entity entity, @NotNull UUID player, @NotNull String stringKey, @NotNull String listKey) { + return matchEntity(type, pendingTask, entity.getType(), player, stringKey, listKey); + } + + public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull EntityType entityType, @NotNull UUID player) { + return matchEntity(type, pendingTask, entityType, player, "mob", "mobs"); + } + + public static boolean matchEntity(@NotNull BukkitTaskType type, @NotNull PendingTask pendingTask, @NotNull EntityType entityType, @NotNull UUID player, @NotNull String stringKey, @NotNull String listKey) { Task task = pendingTask.task; List<String> checkMobs = TaskUtils.getConfigStringList(task, task.getConfigValues().containsKey(stringKey) ? stringKey : listKey); @@ -399,8 +407,6 @@ public class TaskUtils { return false; } - EntityType entityType = entity.getType(); - EntityType mob; for (String mobName : checkMobs) { diff --git a/docs/task-types/hatching-(task-type).md b/docs/task-types/hatching-(task-type).md new file mode 100644 index 00000000..3c895cd1 --- /dev/null +++ b/docs/task-types/hatching-(task-type).md @@ -0,0 +1,39 @@ +--- +title: hatching +parent: Built-in task types +grand_parent: Task types +--- + +# hatching (task type) + +Not released yet (dev builds) +{: .label .label-green } + +Minecraft 1.15+ required +{: .label .label-purple } + +Paper required +{: .label .label-yellow } + +Hatch a set amount of entities of certain types. + +## Options + +| Key | Description | Type | Required | Default | Notes | +|----------|-------------------------------------------------|----------------------------------|----------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `amount` | The number of entities to hatch. | Integer | Yes | \- | \- | +| `mob` | The specific entity type(s) to hatch. | Entity type, or list of entities | No | \- | Not specifying this field will allow all entity types to count towards the task. Please see [this list](https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html) for entity types. | +| `worlds` | Worlds which should count towards the progress. | List of world names | No | \- | \- | + +## Examples + +Hatch a chicken (by throwing an egg): + +``` yaml +hatching: + type: "hatching" + amount: 1 # amount of entities to be hatched + mob: CHICKEN # (OPTIONAL) types of mobs + worlds: # (OPTIONAL) restrict to certain worlds + - "world" +``` |
