From c408fdd2697aaa6b7b899880e7f6afdea88836d5 Mon Sep 17 00:00:00 2001 From: LMBishop <13875753+LMBishop@users.noreply.github.com> Date: Mon, 18 Apr 2022 13:15:07 +0100 Subject: Add partial delivery option (closes #360) --- .../bukkit/tasktype/type/InventoryTaskType.java | 33 +++++++++++++++++----- .../type/dependent/CitizensDeliverTaskType.java | 31 ++++++++++++++++---- 2 files changed, 51 insertions(+), 13 deletions(-) (limited to 'bukkit/src/main/java') 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 22aec162..9d934a33 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 @@ -122,8 +122,9 @@ public final class InventoryTaskType extends BukkitTaskType { int amount = (int) task.getConfigValue("amount"); Object configBlock = task.getConfigValue("item"); Object configData = task.getConfigValue("data"); - Object remove = task.getConfigValue("remove-items-when-complete"); - + boolean remove = (boolean) task.getConfigValue("remove-items-when-complete", false); + boolean allowPartial = (boolean) task.getConfigValue("allow-partial-completion", false); + QuestItem qi; if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { if (configBlock instanceof ConfigurationSection) { @@ -144,14 +145,32 @@ public final class InventoryTaskType extends BukkitTaskType { fixedQuestItemCache.put(quest.getId(), task.getId(), qi); } + int progress; + if (taskProgress.getProgress() == null) { + progress = 0; + } else { + progress = (int) taskProgress.getProgress(); + } + + int deficit = amount - progress; + int[] amountPerSlot = getAmountsPerSlot(player, qi); - int total = Math.min(amountPerSlot[36], amount); - taskProgress.setProgress(total); + int total = Math.min(amountPerSlot[36], deficit); - if (total >= amount) { - taskProgress.setCompleted(true); + if (total == 0) { + continue; + } + + progress += total; + taskProgress.setProgress(progress); - if (remove != null && ((Boolean) remove)) removeItemsInSlots(player, amountPerSlot, total); + if (progress >= amount) { + taskProgress.setCompleted(true); + if (remove) { + removeItemsInSlots(player, amountPerSlot, total); + } + } else if (remove && allowPartial) { + removeItemsInSlots(player, amountPerSlot, total); } } } 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 256216b0..e876b5be 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 @@ -100,7 +100,8 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { int amount = (int) task.getConfigValue("amount"); Object configBlock = task.getConfigValue("item"); Object configData = task.getConfigValue("data"); - Object remove = task.getConfigValue("remove-items-when-complete"); + boolean remove = (boolean) task.getConfigValue("remove-items-when-complete", false); + boolean allowPartial = (boolean) task.getConfigValue("allow-partial-completion", false); QuestItem qi; if ((qi = fixedQuestItemCache.get(quest.getId(), task.getId())) == null) { @@ -122,14 +123,32 @@ public final class CitizensDeliverTaskType extends BukkitTaskType { fixedQuestItemCache.put(quest.getId(), task.getId(), qi); } + int progress; + if (taskProgress.getProgress() == null) { + progress = 0; + } else { + progress = (int) taskProgress.getProgress(); + } + + int deficit = amount - progress; + int[] amountPerSlot = getAmountsPerSlot(player, qi); - int total = Math.min(amountPerSlot[36], amount); - taskProgress.setProgress(total); + int total = Math.min(amountPerSlot[36], deficit); - if (total >= amount) { - taskProgress.setCompleted(true); + if (total == 0) { + continue; + } + + progress += total; + taskProgress.setProgress(progress); - if (remove != null && ((Boolean) remove)) removeItemsInSlots(player, amountPerSlot, total); + if (progress >= amount) { + taskProgress.setCompleted(true); + if (remove) { + removeItemsInSlots(player, amountPerSlot, total); + } + } else if (remove && allowPartial) { + removeItemsInSlots(player, amountPerSlot, total); } } } -- cgit v1.2.3-70-g09d2