From 4e5a9f1703eb4a94319e7c7f6629814b8bfb6f96 Mon Sep 17 00:00:00 2001 From: ToastedCoconut Date: Thu, 4 Jul 2019 15:36:50 +0200 Subject: Another 2 new tasks + fix for fishing (#45) * Two new tasks + a fix for fishing - Deal Damage task type - Enchanting task type - Fixes #37 --- .../quests/tasktypes/types/DealDamageTaskType.java | 78 ++++++++++++++++++++++ .../quests/tasktypes/types/EnchantingTaskType.java | 71 ++++++++++++++++++++ .../quests/tasktypes/types/FishingTaskType.java | 10 +++ 3 files changed, 159 insertions(+) create mode 100644 src/main/java/com/leonardobishop/quests/quests/tasktypes/types/DealDamageTaskType.java create mode 100644 src/main/java/com/leonardobishop/quests/quests/tasktypes/types/EnchantingTaskType.java (limited to 'src') diff --git a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/DealDamageTaskType.java b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/DealDamageTaskType.java new file mode 100644 index 00000000..e46d9bf6 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/DealDamageTaskType.java @@ -0,0 +1,78 @@ +package com.leonardobishop.quests.quests.tasktypes.types; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.entity.EntityDamageByEntityEvent; + +import com.leonardobishop.quests.Quests; +import com.leonardobishop.quests.player.QPlayer; +import com.leonardobishop.quests.player.questprogressfile.QuestProgress; +import com.leonardobishop.quests.player.questprogressfile.QuestProgressFile; +import com.leonardobishop.quests.player.questprogressfile.TaskProgress; +import com.leonardobishop.quests.quests.Quest; +import com.leonardobishop.quests.quests.Task; +import com.leonardobishop.quests.quests.tasktypes.ConfigValue; +import com.leonardobishop.quests.quests.tasktypes.TaskType; + +public final class DealDamageTaskType extends TaskType { + + private List creatorConfigValues = new ArrayList<>(); + + public DealDamageTaskType() { + super("dealdamage", "toasted", "Deal a certain amount of damage."); + this.creatorConfigValues.add(new ConfigValue("amount", true, "Amount of damage you need to deal")); + } + + @Override + public List getCreatorConfigValues() { + return creatorConfigValues; + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onDamage(EntityDamageByEntityEvent e) { + + if (!(e.getDamager() instanceof Player)) { + return; + } + + Player player = (Player) e.getDamager(); + double damage = e.getDamage(); + + QPlayer qPlayer = Quests.getPlayerManager().getPlayer(player.getUniqueId()); + QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile(); + + for (Quest quest : super.getRegisteredQuests()) { + if (questProgressFile.hasStartedQuest(quest)) { + QuestProgress questProgress = questProgressFile.getQuestProgress(quest); + + for (Task task : quest.getTasksOfType(super.getType())) { + TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + + if (taskProgress.isCompleted()) { + continue; + } + + Double progressDamage; + int damageNeeded = (int) task.getConfigValue("amount"); + Double damageNeededDouble = (double) damageNeeded; + + if (taskProgress.getProgress() == null) { + progressDamage = 0.0; + } else { + progressDamage = (double) taskProgress.getProgress(); + } + + taskProgress.setProgress(progressDamage + damage); + + if (((double) taskProgress.getProgress()) >= damageNeededDouble) { + taskProgress.setCompleted(true); + } + } + } + } + } +} diff --git a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/EnchantingTaskType.java b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/EnchantingTaskType.java new file mode 100644 index 00000000..014f6896 --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/EnchantingTaskType.java @@ -0,0 +1,71 @@ +package com.leonardobishop.quests.quests.tasktypes.types; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.enchantment.EnchantItemEvent; + +import com.leonardobishop.quests.Quests; +import com.leonardobishop.quests.player.QPlayer; +import com.leonardobishop.quests.player.questprogressfile.QuestProgress; +import com.leonardobishop.quests.player.questprogressfile.QuestProgressFile; +import com.leonardobishop.quests.player.questprogressfile.TaskProgress; +import com.leonardobishop.quests.quests.Quest; +import com.leonardobishop.quests.quests.Task; +import com.leonardobishop.quests.quests.tasktypes.ConfigValue; +import com.leonardobishop.quests.quests.tasktypes.TaskType; + +public final class EnchantingTaskType extends TaskType { + + private List creatorConfigValues = new ArrayList<>(); + + public EnchantingTaskType() { + super("enchanting", "toasted", "Enchant a certain amount of items."); + this.creatorConfigValues.add(new ConfigValue("amount", true, "Amount of items you need to enchant.")); + } + + @Override + public List getCreatorConfigValues() { + return creatorConfigValues; + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onEnchant(EnchantItemEvent e) { + Player player = e.getEnchanter(); + + QPlayer qPlayer = Quests.getPlayerManager().getPlayer(player.getUniqueId()); + QuestProgressFile questProgressFile = qPlayer.getQuestProgressFile(); + + for (Quest quest : super.getRegisteredQuests()) { + if (questProgressFile.hasStartedQuest(quest)) { + QuestProgress questProgress = questProgressFile.getQuestProgress(quest); + + for (Task task : quest.getTasksOfType(super.getType())) { + TaskProgress taskProgress = questProgress.getTaskProgress(task.getId()); + + if (taskProgress.isCompleted()) { + continue; + } + + int enchantsNeeded = (int) task.getConfigValue("amount"); + + int progressEnchant; + if (taskProgress.getProgress() == null) { + progressEnchant = 0; + } else { + progressEnchant = (int) taskProgress.getProgress(); + } + + taskProgress.setProgress(progressEnchant + 1); + + if (((int) taskProgress.getProgress()) >= enchantsNeeded) { + taskProgress.setCompleted(true); + } + } + } + } + } +} diff --git a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/FishingTaskType.java b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/FishingTaskType.java index 28f78f05..22ac3444 100644 --- a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/FishingTaskType.java +++ b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/FishingTaskType.java @@ -9,6 +9,9 @@ import com.leonardobishop.quests.quests.Quest; import com.leonardobishop.quests.quests.Task; import com.leonardobishop.quests.quests.tasktypes.ConfigValue; import com.leonardobishop.quests.quests.tasktypes.TaskType; + +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -36,6 +39,13 @@ public final class FishingTaskType extends TaskType { if (event.getState() == PlayerFishEvent.State.BITE) { return; } + + Location hookLocation = event.getHook().getLocation().add(0, -1, 0); + if (!hookLocation.getBlock().getType().equals(Material.STATIONARY_WATER) | + hookLocation.getBlock().getType().equals(Material.WATER)) { + return; + } + Player player = event.getPlayer(); QPlayer qPlayer = Quests.getPlayerManager().getPlayer(player.getUniqueId()); -- cgit v1.2.3-70-g09d2