diff options
| author | Krakenied <Krakenied1@gmail.com> | 2023-07-04 07:27:32 +0200 |
|---|---|---|
| committer | Krakenied <Krakenied1@gmail.com> | 2023-07-04 07:27:32 +0200 |
| commit | 4c9a7d83a19828e64ea7f90ddf69f9212bb7a7d9 (patch) | |
| tree | 7c02baca3607b58f4059312332b4c16c41ae225e /bukkit/src/main/java | |
| parent | f2b6f3c29f25bb7c5222f27c298928ffac7f3700 (diff) | |
Add exact match option to inventory and citizens deliver task types
Diffstat (limited to 'bukkit/src/main/java')
4 files changed, 85 insertions, 55 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java index 6250f345..93373377 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/InventoryTaskType.java @@ -41,6 +41,7 @@ public final class InventoryTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "item")); super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "item")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "data")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "exact-match")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "remove-items-when-complete")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "allow-partial-completion")); } @@ -110,7 +111,6 @@ public final class InventoryTaskType extends BukkitTaskType { super.debug("Inventory check triggered", quest.getId(), task.getId(), player.getUniqueId()); - int itemsNeeded = (int) task.getConfigValue("amount"); boolean remove = TaskUtils.getConfigBoolean(task, "remove-items-when-complete"); boolean allowPartial = TaskUtils.getConfigBoolean(task, "allow-partial-completion"); @@ -121,14 +121,15 @@ public final class InventoryTaskType extends BukkitTaskType { qi = fetchedItem; } - int progress = TaskUtils.getIntegerTaskProgress(taskProgress); - - int total; - int[] amountPerSlot = TaskUtils.getAmountsPerSlot(player, qi); + boolean exactMatch = TaskUtils.getConfigBoolean(task, "exact-match", true); + int[] amountPerSlot = TaskUtils.getAmountsPerSlot(player, qi, exactMatch); super.debug("Player has " + amountPerSlot[36] + " of the required item", quest.getId(), task.getId(), player.getUniqueId()); + int amount = (int) task.getConfigValue("amount"); + if (allowPartial) { - total = Math.min(amountPerSlot[36], itemsNeeded - progress); + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + int total = Math.min(amountPerSlot[36], amount - progress); if (total == 0) { continue; @@ -137,26 +138,27 @@ public final class InventoryTaskType extends BukkitTaskType { // We must ALWAYS remove items if partial completion is allowed // https://github.com/LMBishop/Quests/issues/375 TaskUtils.removeItemsInSlots(player, amountPerSlot, total); - super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Removing " + total + " items from inventory", quest.getId(), task.getId(), player.getUniqueId()); progress += total; taskProgress.setProgress(progress); - super.debug("Updating task progress (now " + (progress) + ")", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Updating task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (progress >= itemsNeeded) { + if (progress >= amount) { taskProgress.setCompleted(true); super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); } } else { - total = Math.min(amountPerSlot[36], itemsNeeded); + int progress = Math.min(amountPerSlot[36], amount); + taskProgress.setProgress(progress); + super.debug("Updating task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setProgress(total); - if (total >= itemsNeeded) { + if (progress >= amount) { taskProgress.setCompleted(true); super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); if (remove) { - TaskUtils.removeItemsInSlots(player, amountPerSlot, total); + TaskUtils.removeItemsInSlots(player, amountPerSlot, progress); super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java index fd118c6f..5bccf409 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensDeliverTaskType.java @@ -39,6 +39,7 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "item")); super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "item")); super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "data")); + super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "exact-match")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "remove-items-when-complete")); super.addConfigValidator(TaskUtils.useBooleanConfigValidator(this, "allow-partial-completion")); } @@ -49,20 +50,29 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onNPCClick(NPCRightClickEvent event) { - Bukkit.getScheduler().runTaskLater(plugin, () -> checkInventory(event.getClicker(), event.getNPC()), 1L); + public void onNPCRightClick(NPCRightClickEvent event) { + checkInventory(event.getClicker(), event.getNPC(), 1L); + } + + @SuppressWarnings("SameParameterValue") + private void checkInventory(Player player, NPC npc, long delay) { + if (player.hasMetadata("NPC")) return; + Bukkit.getScheduler().runTaskLater(plugin, () -> checkInventory(player, npc), delay); } @SuppressWarnings("deprecation") private void checkInventory(Player player, NPC npc) { - if (player == null || !player.isOnline()) { + if (!player.isOnline()) { return; } + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); if (qPlayer == null) { return; } + String npcName = null; + for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { Quest quest = pendingTask.quest(); Task task = pendingTask.task(); @@ -70,21 +80,26 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { super.debug("Player clicked NPC", quest.getId(), task.getId(), player.getUniqueId()); - if (task.getConfigValue("npc-id") != null) { - if (!task.getConfigValue("npc-id").equals(npc.getId())) { - super.debug("NPC id ('" + npc.getId() + "') does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + Integer configNPCId = (Integer) task.getConfigValue("npc-id"); + if (configNPCId != null) { + if (npc.getId() != configNPCId) { + super.debug("NPC id " + npc.getId() + " does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } - } else if (task.getConfigValue("npc-name") != null) { - String npcName = Chat.legacyStrip(Chat.legacyColor(npc.getName())); - super.debug("NPC name is required, current name = '" + npcName + "'", quest.getId(), task.getId(), player.getUniqueId()); - if (!Chat.legacyStrip(Chat.legacyColor(String.valueOf(task.getConfigValue("npc-name")))) - .equals(npcName)) { - super.debug("NPC name does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); - continue; + } else { + String configNPCName = (String) task.getConfigValue("npc-name"); + if (configNPCName != null) { + if (npcName == null) { + npcName = Chat.legacyStrip(Chat.legacyColor(npc.getName())); + } + + if (!configNPCName.equals(npcName)) { + super.debug("NPC name " + npcName + " does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } } } - int itemsNeeded = (int) task.getConfigValue("amount"); + boolean remove = TaskUtils.getConfigBoolean(task, "remove-items-when-complete"); boolean allowPartial = TaskUtils.getConfigBoolean(task, "allow-partial-completion"); @@ -95,14 +110,15 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { qi = fetchedItem; } - int progress = TaskUtils.getIntegerTaskProgress(taskProgress); - - int total; - int[] amountPerSlot = TaskUtils.getAmountsPerSlot(player, qi); + boolean exactMatch = TaskUtils.getConfigBoolean(task, "exact-match", true); + int[] amountPerSlot = TaskUtils.getAmountsPerSlot(player, qi, exactMatch); super.debug("Player has " + amountPerSlot[36] + " of the required item", quest.getId(), task.getId(), player.getUniqueId()); + int amount = (int) task.getConfigValue("amount"); + if (allowPartial) { - total = Math.min(amountPerSlot[36], itemsNeeded - progress); + int progress = TaskUtils.getIntegerTaskProgress(taskProgress); + int total = Math.min(amountPerSlot[36], amount - progress); if (total == 0) { continue; @@ -111,26 +127,27 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { // We must ALWAYS remove items if partial completion is allowed // https://github.com/LMBishop/Quests/issues/375 TaskUtils.removeItemsInSlots(player, amountPerSlot, total); - super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Removing " + total + " items from inventory", quest.getId(), task.getId(), player.getUniqueId()); progress += total; taskProgress.setProgress(progress); - super.debug("Updating task progress (now " + (progress) + ")", quest.getId(), task.getId(), player.getUniqueId()); + super.debug("Updating task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - if (progress >= itemsNeeded) { + if (progress >= amount) { taskProgress.setCompleted(true); super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); } } else { - total = Math.min(amountPerSlot[36], itemsNeeded); + int progress = Math.min(amountPerSlot[36], amount); + taskProgress.setProgress(progress); + super.debug("Updating task progress (now " + progress + ")", quest.getId(), task.getId(), player.getUniqueId()); - taskProgress.setProgress(total); - if (total >= itemsNeeded) { + if (progress >= amount) { taskProgress.setCompleted(true); super.debug("Marking task as complete", quest.getId(), task.getId(), player.getUniqueId()); if (remove) { - TaskUtils.removeItemsInSlots(player, amountPerSlot, total); + TaskUtils.removeItemsInSlots(player, amountPerSlot, progress); super.debug("Removing items from inventory", quest.getId(), task.getId(), player.getUniqueId()); } } diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java index 99275f9f..63bfd3dd 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/dependent/CitizensInteractTaskType.java @@ -10,6 +10,7 @@ import com.leonardobishop.quests.common.player.questprogressfile.TaskProgress; import com.leonardobishop.quests.common.quest.Quest; import com.leonardobishop.quests.common.quest.Task; import net.citizensnpcs.api.event.NPCRightClickEvent; +import net.citizensnpcs.api.npc.NPC; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -31,13 +32,19 @@ public final class CitizensInteractTaskType extends BukkitTaskType { } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onNPCClick(NPCRightClickEvent event) { - QPlayer qPlayer = plugin.getPlayerManager().getPlayer(event.getClicker().getUniqueId()); + public void onNPCRightClick(NPCRightClickEvent event) { + Player player = event.getClicker(); + if (player.hasMetadata("NPC")) { + return; + } + + QPlayer qPlayer = plugin.getPlayerManager().getPlayer(player.getUniqueId()); if (qPlayer == null) { return; } - Player player = event.getClicker(); + NPC npc = event.getNPC(); + String npcName = null; for (TaskUtils.PendingTask pendingTask : TaskUtils.getApplicableTasks(player, qPlayer, this)) { Quest quest = pendingTask.quest(); @@ -46,18 +53,23 @@ public final class CitizensInteractTaskType extends BukkitTaskType { super.debug("Player clicked NPC", quest.getId(), task.getId(), player.getUniqueId()); - if (task.getConfigValue("npc-id") != null) { - if (!task.getConfigValue("npc-id").equals(event.getNPC().getId())) { - super.debug("NPC id ('" + event.getNPC().getId() + "') does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + Integer configNPCId = (Integer) task.getConfigValue("npc-id"); + if (configNPCId != null) { + if (npc.getId() != configNPCId) { + super.debug("NPC id " + npc.getId() + " does not match required id, continuing...", quest.getId(), task.getId(), player.getUniqueId()); continue; } - } else if (task.getConfigValue("npc-name") != null) { - String npcName = Chat.legacyStrip(Chat.legacyColor(event.getNPC().getName())); - super.debug("NPC name is required, current name = '" + npcName + "'", quest.getId(), task.getId(), player.getUniqueId()); - if (!Chat.legacyStrip(Chat.legacyColor(String.valueOf(task.getConfigValue("npc-name")))) - .equals(npcName)) { - super.debug("NPC name does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); - continue; + } else { + String configNPCName = (String) task.getConfigValue("npc-name"); + if (configNPCName != null) { + if (npcName == null) { + npcName = Chat.legacyStrip(Chat.legacyColor(npc.getName())); + } + + if (!configNPCName.equals(npcName)) { + super.debug("NPC name " + npcName + " does not match required name, continuing...", quest.getId(), task.getId(), player.getUniqueId()); + continue; + } } } 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 9b7f0246..1f46e4f1 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 @@ -312,13 +312,12 @@ public class TaskUtils { return false; } - public static int[] getAmountsPerSlot(Player player, QuestItem qi) { + public static int[] getAmountsPerSlot(Player player, QuestItem qi, boolean exactMatch) { int[] slotToAmount = new int[37]; // idx 36 = total for (int i = 0; i < 36; i++) { ItemStack slot = player.getInventory().getItem(i); - if (slot == null || !qi.compareItemStack(slot, true)) - continue; + if (slot == null || !qi.compareItemStack(slot, exactMatch)) continue; slotToAmount[36] = slotToAmount[36] + slot.getAmount(); slotToAmount[i] = slot.getAmount(); } |
