aboutsummaryrefslogtreecommitdiffstats
path: root/bukkit/src/main/java/com/leonardobishop
diff options
context:
space:
mode:
authorKrakenied <Krakenied1@gmail.com>2024-03-14 00:41:51 +0100
committerLeonardo Bishop <13875753+LMBishop@users.noreply.github.com>2024-03-16 00:19:16 +0000
commit1578f9430eb969bb3f919fa8004358da1fc30e84 (patch)
treeafc279287e195584594c9227d51a5a1c6f925b48 /bukkit/src/main/java/com/leonardobishop
parentfe0eeea7154973a86b7e086717efb2ba1db1fcc5 (diff)
Implement hatching task type
Diffstat (limited to 'bukkit/src/main/java/com/leonardobishop')
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/BukkitQuestsPlugin.java2
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/HatchingTaskType.java80
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java10
3 files changed, 90 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) {