aboutsummaryrefslogtreecommitdiffstats
path: root/bukkit/src
diff options
context:
space:
mode:
authorLMBishop <13875753+LMBishop@users.noreply.github.com>2022-07-06 19:20:31 +0100
committerLMBishop <13875753+LMBishop@users.noreply.github.com>2022-07-06 19:20:31 +0100
commit34f5f420d6741569e705455b07b4980c7e0fb114 (patch)
treec93778b0940ab847f72b829f3562f1764fca8d2f /bukkit/src
parentc9a47f73f58312f5f7af4c8c3faaaac2c0d4ef0b (diff)
Add specific enchantment & level to enchanting task (closes #408)
Diffstat (limited to 'bukkit/src')
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java46
-rw-r--r--bukkit/src/main/java/com/leonardobishop/quests/bukkit/util/TaskUtils.java49
2 files changed, 94 insertions, 1 deletions
diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java
index a9def444..9f48e9d6 100644
--- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java
+++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/tasktype/type/EnchantingTaskType.java
@@ -7,11 +7,16 @@ 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.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.enchantment.EnchantItemEvent;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
public final class EnchantingTaskType extends BukkitTaskType {
private final BukkitQuestsPlugin plugin;
@@ -22,6 +27,9 @@ public final class EnchantingTaskType extends BukkitTaskType {
super.addConfigValidator(TaskUtils.useRequiredConfigValidator(this, "amount"));
super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "amount"));
+ super.addConfigValidator(TaskUtils.useItemStackConfigValidator(this, "item"));
+ super.addConfigValidator(TaskUtils.useEnchantmentListConfigValidator(this, "enchantment"));
+ super.addConfigValidator(TaskUtils.useIntegerConfigValidator(this, "min-level"));
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
@@ -42,6 +50,44 @@ public final class EnchantingTaskType extends BukkitTaskType {
super.debug("Player enchanted item", quest.getId(), task.getId(), player.getUniqueId());
+ boolean hasEnchantment = true;
+
+ if (task.hasConfigKey("enchantment")) {
+ hasEnchantment = false;
+ List<String> enchantments = TaskUtils.getConfigStringList(task, "enchantment");
+ for (String enchantment : enchantments) {
+ Enchantment enchantmentObject = Enchantment.getByName(enchantment);
+ if (enchantmentObject == null) {
+ super.debug("Enchantment '" + enchantment + "' does not exist, skipping...", quest.getId(), task.getId(), player.getUniqueId());
+ continue;
+ }
+ if (e.getEnchantsToAdd().containsKey(enchantmentObject)) {
+ super.debug("Enchantments to add contains enchantment '" + enchantment + "'", quest.getId(), task.getId(), player.getUniqueId());
+ if (task.hasConfigKey("min-level")) {
+ int level = (int) task.getConfigValue("min-level");
+ super.debug("Minimum level of " + level + " is specified", quest.getId(), task.getId(), player.getUniqueId());
+ if (e.getEnchantsToAdd().get(enchantmentObject) >= level) {
+ hasEnchantment = true;
+ super.debug("Item has minimum required level", quest.getId(), task.getId(), player.getUniqueId());
+ break;
+ } else {
+ super.debug("Item does not have minimum level (level = " + e.getEnchantsToAdd().get(enchantmentObject) + ")", quest.getId(), task.getId(), player.getUniqueId());
+ }
+ } else {
+ hasEnchantment = true;
+ break;
+ }
+ } else {
+ super.debug("Enchantments to add does not contains enchantment '" + enchantment + "'", quest.getId(), task.getId(), player.getUniqueId());
+ }
+ }
+ }
+
+ if (!hasEnchantment) {
+ super.debug("Applied enchantments does not contain any in required enchantments, skipping...", quest.getId(), task.getId(), player.getUniqueId());
+ continue;
+ }
+
int enchantsNeeded = (int) task.getConfigValue("amount");
int progress = TaskUtils.incrementIntegerTaskProgress(taskProgress);
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 642b58be..21aacd95 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
@@ -15,6 +15,7 @@ import com.leonardobishop.quests.common.tasktype.TaskType;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
+import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@@ -481,6 +482,52 @@ public class TaskUtils {
/**
* Returns a config validator which checks if at least one value in the given
+ * paths is a valid list of enchantments.
+ * <p>
+ * The list of entities is expected to be in the format of:
+ * <pre>key: "ENCHANTMENT"</pre>
+ * where ENCHANTMENT is the name of an entity. Alternatively, the list
+ * of entities can be in the format of:
+ * <pre>key:
+ * - "ENCHANTMENT"
+ * - "..."</pre>
+ * </p>
+ *
+ * @param paths a list of valid paths for task
+ * @return config validator
+ */
+ public static TaskType.ConfigValidator useEnchantmentListConfigValidator(TaskType type, String... paths) {
+ return (config, problems) -> {
+ for (String path : paths) {
+ Object configObject = config.get(path);
+
+ List<String> checkEnchantments = new ArrayList<>();
+ if (configObject instanceof List<?> configList) {
+ for (Object object : configList) {
+ checkEnchantments.add(String.valueOf(object));
+ }
+ } else {
+ if (configObject == null) {
+ continue;
+ }
+ checkEnchantments.add(String.valueOf(configObject));
+ }
+
+ for (String enchantment : checkEnchantments) {
+ if (Enchantment.getByName(enchantment) == null) {
+ problems.add(new ConfigProblem(ConfigProblem.ConfigProblemType.WARNING,
+ ConfigProblemDescriptions.UNKNOWN_ENCHANTMENT.getDescription(enchantment),
+ ConfigProblemDescriptions.UNKNOWN_ENCHANTMENT.getExtendedDescription(enchantment),
+ path));
+ }
+ }
+ break;
+ }
+ };
+ }
+
+ /**
+ * Returns a config validator which checks if at least one value in the given
* paths is a value in the list of accepted values.
*
* @param acceptedValues a list of accepted values
@@ -503,7 +550,7 @@ public class TaskUtils {
extendedDescription += "<br> - " + value;
}
problems.add(new ConfigProblem(ConfigProblem.ConfigProblemType.WARNING,
- ConfigProblemDescriptions.NOT_ACCEPTED_VALUE.getDescription(String.valueOf(configObject), type.getType(), acceptedValues.toString()),
+ ConfigProblemDescriptions.NOT_ACCEPTED_VALUE.getDescription(String.valueOf(configObject), type.getType()),
extendedDescription,
path));
}