Skip to content

Commit

Permalink
Implement projectile launching task type
Browse files Browse the repository at this point in the history
  • Loading branch information
Krakenied authored and LMBishop committed Dec 9, 2023
1 parent 5facd15 commit 1b44d06
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Expand Up @@ -80,6 +80,7 @@
import com.leonardobishop.quests.bukkit.tasktype.type.PlayerkillingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.PlaytimeTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.PositionTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.ProjectilelaunchingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.ReplenishingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.ShearingTaskType;
import com.leonardobishop.quests.bukkit.tasktype.type.SmeltingTaskType;
Expand Down Expand Up @@ -406,6 +407,7 @@ public void onEnable() {
taskTypeManager.registerTaskType(new PlayerkillingTaskType(this));
taskTypeManager.registerTaskType(new PlaytimeTaskType(this));
taskTypeManager.registerTaskType(new PositionTaskType(this));
taskTypeManager.registerTaskType(new ProjectilelaunchingTaskType(this));
taskTypeManager.registerTaskType(new ShearingTaskType(this));
taskTypeManager.registerTaskType(new SmeltingTaskType(this));
taskTypeManager.registerTaskType(new TamingTaskType(this));
Expand Down
@@ -0,0 +1,72 @@
package com.leonardobishop.quests.bukkit.tasktype.type;

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.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.projectiles.ProjectileSource;

public final class ProjectilelaunchingTaskType extends BukkitTaskType {

private final BukkitQuestsPlugin plugin;

public ProjectilelaunchingTaskType(BukkitQuestsPlugin plugin) {
super("projectilelaunching", TaskUtils.TASK_ATTRIBUTION_STRING, "Launch a certain amount of projectiles.");
this.plugin = plugin;

super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useEntityListConfigValidator(this, "projectile", "projectiles"));
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onProjectileLaunch(ProjectileLaunchEvent event) {
Projectile projectile = event.getEntity();

ProjectileSource shooter = projectile.getShooter();
if (!(shooter instanceof Player player)) {
return;
}

if (player.hasMetadata("NPC")) {
return;
}

QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
if (qPlayer == null) {
return;
}

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 launched a projectile, current entity is " + projectile.getType(), quest.getId(), task.getId(), player.getUniqueId());
if (!TaskUtils.matchEntity(this, pendingTask, projectile, player.getUniqueId(), "projectile", "projectiles")) {
super.debug("Continuing...", quest.getId(), task.getId(), player.getUniqueId());
continue;
}

int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress);
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, taskProgress, amount);
}
}
}

0 comments on commit 1b44d06

Please sign in to comment.