Skip to content

Commit

Permalink
Move applicable tasks to TaskUtils and add debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
LMBishop committed Jul 6, 2022
1 parent 196410e commit cd4dfb9
Show file tree
Hide file tree
Showing 50 changed files with 1,295 additions and 1,642 deletions.
Expand Up @@ -348,7 +348,7 @@ public void onEnable() {
if (Bukkit.getPluginManager().isPluginEnabled("MythicMobs")) {
String mythicMobsVersion = Bukkit.getPluginManager().getPlugin("MythicMobs").getDescription().getVersion();
if (mythicMobsVersion.startsWith("4") || mythicMobsVersion.startsWith("5")) {
taskTypeManager.registerTaskType(new MythicMobsKillingType(this, mythicMobsVersion));
taskTypeManager.registerTaskType(new MythicMobsKillingTaskType(this, mythicMobsVersion));
}
}
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
Expand Down Expand Up @@ -378,7 +378,7 @@ public void onEnable() {
}
if (Bukkit.getPluginManager().isPluginEnabled("Votifier")) {
// not tested
taskTypeManager.registerTaskType(new NuVotifierVoteType(this));
taskTypeManager.registerTaskType(new NuVotifierVoteTaskType(this));
}

taskTypeManager.closeRegistrations();
Expand Down
Expand Up @@ -58,6 +58,11 @@ public void handle(CommandSender sender, String[] args) {
QPlayerPreferences preferences = qPlayer.getPlayerPreferences();
QPlayerPreferences.DebugType currentDebugType = preferences.getDebug(questId);
if (currentDebugType == null) {
if (args.length < 5) {
sender.sendMessage(ChatColor.RED + "You must specify a debug type.");
return;
}

String debugType = args[4];
QPlayerPreferences.DebugType debugTypeEnum;

Expand All @@ -70,7 +75,7 @@ public void handle(CommandSender sender, String[] args) {

preferences.setDebug(questId, debugTypeEnum);
sender.sendMessage(ChatColor.GREEN + "Debugging enabled for quest '" + questId + "'.");
sender.sendMessage(ChatColor.GRAY + "You will now see debug logs for quest '" + quest + "' for " +
sender.sendMessage(ChatColor.GRAY + "You will now see debug logs for quest '" + quest.getId() + "' for " +
(debugTypeEnum == QPlayerPreferences.DebugType.SELF ? "yourself" : "everybody on the server") +
". This may generate a lot of spam.");
sender.sendMessage(ChatColor.DARK_GRAY + "Use '/quests admin debug " + questId + "' to disable.");
Expand All @@ -89,7 +94,7 @@ public List<String> tabComplete(CommandSender sender, String[] args) {
if (args.length == 4) {
return TabHelper.tabCompleteQuests(args[3]);
} else if (args.length == 5) {
return TabHelper.matchTabComplete(args[2], Arrays.asList("self", "all"));
return TabHelper.matchTabComplete(args[4], Arrays.asList("self", "all"));
}
return Collections.emptyList();
}
Expand Down
Expand Up @@ -18,8 +18,8 @@ public BukkitTaskType(@NotNull String type) {
super(type);
}

public final void debug(@NotNull String message, String questId, @NotNull UUID player) {
taskTypeManager.sendDebug(message, super.getType(), questId, player);
public final void debug(@NotNull String message, String questId, String taskId, @NotNull UUID player) {
taskTypeManager.sendDebug(message, super.getType(), questId, taskId, player);
}

}
Expand Up @@ -6,6 +6,7 @@
import com.leonardobishop.quests.common.tasktype.TaskType;
import com.leonardobishop.quests.common.tasktype.TaskTypeManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -37,21 +38,24 @@ public boolean registerTaskType(@NotNull TaskType taskType) {
return false;
}

public void sendDebug(@NotNull String message, @NotNull String taskType, @NotNull String questId, @NotNull UUID associatedPlayer) {
public void sendDebug(@NotNull String message, @NotNull String taskType, @NotNull String questId, @NotNull String taskId, @NotNull UUID associatedPlayer) {
for (QPlayer qPlayer : plugin.getPlayerManager().getQPlayers()) {
QPlayerPreferences.DebugType debugType = qPlayer.getPlayerPreferences().getDebug(questId);
Player player = Bukkit.getPlayer(qPlayer.getPlayerUUID());
Player otherPlayer = Bukkit.getPlayer(associatedPlayer);
String associatedName = otherPlayer == null ? associatedPlayer.toString() : otherPlayer.getName();

String chatMessage = "[" + taskType + ": " + associatedName + " on '" + questId + "'] " + message;
String chatHeader = ChatColor.GRAY + "[" + associatedName + " - " + questId + "/" + taskId + " - type '" + taskType + "']";
if (player != null && debugType != null) {
switch (debugType) {
case ALL -> player.sendMessage(chatMessage);
case ALL -> {
player.sendMessage(chatHeader);
player.sendMessage(message);
}
case SELF -> {
if (player.getUniqueId().equals(associatedPlayer)) {
player.sendMessage(chatMessage);
}
player.sendMessage(chatHeader);
player.sendMessage(message); }
}
}
}
Expand Down
Expand Up @@ -5,7 +5,6 @@
import com.leonardobishop.quests.bukkit.util.TaskUtils;
import com.leonardobishop.quests.common.config.ConfigProblem;
import com.leonardobishop.quests.common.player.QPlayer;
import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress;
import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress;
import com.leonardobishop.quests.common.quest.Quest;
import com.leonardobishop.quests.common.quest.Task;
Expand Down Expand Up @@ -52,41 +51,27 @@ public void onBreed(CreatureSpawnEvent e) {
}
// Check if there is a player in the list, otherwise: return.
for (Entity current : entList) {
if (current instanceof Player && !current.hasMetadata("NPC")) {
Player player = (Player) current;
if (current instanceof Player player && !current.hasMetadata("NPC")) {
QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId());
if (qPlayer == null) {
continue;
}

for (Quest quest : super.getRegisteredQuests()) {
if (qPlayer.hasStartedQuest(quest)) {
QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest);
for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) {
Quest quest = pendingTask.quest();
Task task = pendingTask.task();
TaskProgress taskProgress = pendingTask.taskProgress();

for (Task task : quest.getTasksOfType(super.getType())) {
if (!TaskUtils.validateWorld(player, task)) continue;
super.debug("Player detected near bred animal", quest.getId(), task.getId(), player.getUniqueId());

TaskProgress taskProgress = questProgress.getTaskProgress(task.getId());
int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress);
super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId());

if (taskProgress.isCompleted()) {
continue;
}
int breedingNeeded = (int) task.getConfigValue("amount");

int breedingNeeded = (int) task.getConfigValue("amount");
int breedingProgress;

if (taskProgress.getProgress() == null) {
breedingProgress = 0;
} else {
breedingProgress = (int) taskProgress.getProgress();
}

taskProgress.setProgress(breedingProgress + 1);

if (((int) taskProgress.getProgress()) >= breedingNeeded) {
taskProgress.setCompleted(true);
}
}
if (progress >= breedingNeeded) {
super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId());
taskProgress.setCompleted(true);
}
}
}
Expand Down
Expand Up @@ -5,7 +5,6 @@
import com.leonardobishop.quests.bukkit.util.TaskUtils;
import com.leonardobishop.quests.common.config.ConfigProblem;
import com.leonardobishop.quests.common.player.QPlayer;
import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress;
import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress;
import com.leonardobishop.quests.common.quest.Quest;
import com.leonardobishop.quests.common.quest.Task;
Expand All @@ -18,7 +17,6 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -68,38 +66,29 @@ public void onBlockPlace(BrewEvent event) {
return;
}

for (Quest quest : super.getRegisteredQuests()) {
if (qPlayer.hasStartedQuest(quest)) {
QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest);
for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) {
Quest quest = pendingTask.quest();
Task task = pendingTask.task();
TaskProgress taskProgress = pendingTask.taskProgress();

for (Task task : quest.getTasksOfType(super.getType())) {
if (!TaskUtils.validateWorld(player, task)) continue;
super.debug("Player brewed potion", quest.getId(), task.getId(), player.getUniqueId());

TaskProgress taskProgress = questProgress.getTaskProgress(task.getId());
int progress = 0;

if (taskProgress.isCompleted()) {
continue;
}

int potionsNeeded = (int) task.getConfigValue("amount");

int progress;
if (taskProgress.getProgress() == null) {
progress = 0;
} else {
progress = (int) taskProgress.getProgress();
}

ItemStack potion1 = event.getContents().getItem(0);
ItemStack potion2 = event.getContents().getItem(1);
ItemStack potion3 = event.getContents().getItem(2);
for (int i = 0; i < 3; i++) {
if (event.getContents().getItem(i) != null) {
progress = TaskUtils.incrementIntegerTaskProgress(taskProgress);
super.debug("Incrementing task progress for brewed potion in slot " + i + " (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId());
} else {
super.debug("Slot " + i + " does not have a brewed potion", quest.getId(), task.getId(), player.getUniqueId());
}
}

taskProgress.setProgress(progress + (potion1 == null ? 0 : 1) + (potion2 == null ? 0 : 1) + (potion3 == null ? 0 : 1));
int potionsNeeded = (int) task.getConfigValue("amount");

if (((int) taskProgress.getProgress()) >= potionsNeeded) {
taskProgress.setCompleted(true);
}
}
if (progress >= potionsNeeded) {
super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId());
taskProgress.setCompleted(true);
}
}
}
Expand Down
Expand Up @@ -6,7 +6,6 @@
import com.leonardobishop.quests.common.config.ConfigProblem;
import com.leonardobishop.quests.common.config.ConfigProblemDescriptions;
import com.leonardobishop.quests.common.player.QPlayer;
import com.leonardobishop.quests.common.player.questprogressfile.QuestProgress;
import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress;
import com.leonardobishop.quests.common.quest.Quest;
import com.leonardobishop.quests.common.quest.Task;
Expand Down Expand Up @@ -52,41 +51,28 @@ public void onBucket(Player player, Material bucket, BukkitQuestsPlugin plugin)

if (qPlayer == null) return;

for (Quest quest : super.getRegisteredQuests()) {
if (qPlayer.hasStartedQuest(quest)) {
QuestProgress questProgress = qPlayer.getQuestProgressFile().getQuestProgress(quest);
for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player.getPlayer(), qPlayer, this, TaskUtils.TaskConstraint.WORLD)) {
Quest quest = pendingTask.quest();
Task task = pendingTask.task();
TaskProgress taskProgress = pendingTask.taskProgress();

for (Task task : quest.getTasksOfType(super.getType())) {
if (!TaskUtils.validateWorld(player, task)) continue;
int amount = (int) task.getConfigValue("amount");
Object configBucket = task.getConfigValue("bucket");
Material material = Material.getMaterial((String) configBucket);

TaskProgress taskProgress = questProgress.getTaskProgress(task.getId());
super.debug("Player used bucket of type " + bucket, quest.getId(), task.getId(), player.getUniqueId());

if (taskProgress.isCompleted()) {
continue;
}

int amount = (int) task.getConfigValue("amount");
Object configBucket = task.getConfigValue("bucket");
Material material = Material.getMaterial((String) configBucket);

if (bucket != material) {
continue;
}

int progress;
if (taskProgress.getProgress() == null) {
progress = 0;
} else {
progress = (int) taskProgress.getProgress();
}
if (bucket != material) {
super.debug("Player bucket does not match required bucket '" + material + "', continuing...", quest.getId(), task.getId(), player.getUniqueId());
continue;
}

taskProgress.setProgress(progress + 1);
int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress);
super.debug("Incrementing task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId());

if ((int) taskProgress.getProgress() >= amount) {
taskProgress.setProgress(amount);
taskProgress.setCompleted(true);
}
}
if ((int) taskProgress.getProgress() >= amount) {
super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId());
taskProgress.setCompleted(true);
}
}
}
Expand Down

0 comments on commit cd4dfb9

Please sign in to comment.