From b14ce47071dc408f676d7076ec2e0d3bc4794730 Mon Sep 17 00:00:00 2001 From: Rodney_Mc_Kay Date: Mon, 3 Feb 2020 20:10:01 +0100 Subject: Added support for BentoBox levels --- .../java/com/leonardobishop/quests/Quests.java | 3 + .../tasktypes/types/BentoBoxLevelTaskType.java | 89 ++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/main/java/com/leonardobishop/quests/quests/tasktypes/types/BentoBoxLevelTaskType.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/leonardobishop/quests/Quests.java b/src/main/java/com/leonardobishop/quests/Quests.java index 3fb67354..c8064f95 100644 --- a/src/main/java/com/leonardobishop/quests/Quests.java +++ b/src/main/java/com/leonardobishop/quests/Quests.java @@ -146,6 +146,9 @@ public class Quests extends JavaPlugin { if (Bukkit.getPluginManager().isPluginEnabled("ASkyBlock")) { taskTypeManager.registerTaskType(new ASkyBlockLevelType()); } + if (Bukkit.getPluginManager().isPluginEnabled("BentoBox")) { + BentoBoxLevelTaskType.register(taskTypeManager); + } if (Bukkit.getPluginManager().isPluginEnabled("uSkyBlock")) { taskTypeManager.registerTaskType(new uSkyBlockLevelType()); } diff --git a/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/BentoBoxLevelTaskType.java b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/BentoBoxLevelTaskType.java new file mode 100644 index 00000000..1bea0d3e --- /dev/null +++ b/src/main/java/com/leonardobishop/quests/quests/tasktypes/types/BentoBoxLevelTaskType.java @@ -0,0 +1,89 @@ +package com.leonardobishop.quests.quests.tasktypes.types; + +import com.leonardobishop.quests.api.QuestsAPI; +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; +import com.leonardobishop.quests.quests.tasktypes.TaskTypeManager; +import org.bukkit.event.EventHandler; +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.events.BentoBoxEvent; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; + +public class BentoBoxLevelTaskType extends TaskType { + + private List creatorConfigValues = new ArrayList<>(); + private Field levelField = null; + + public BentoBoxLevelTaskType() { + super("bentobox_level", "Rodney_Mc_Kay", "Reach a certain island level in the level addon for BentoBox."); + this.creatorConfigValues.add(new ConfigValue("level", true, "Minimum island level needed.")); + } + + @Override + public List getCreatorConfigValues() { + return creatorConfigValues; + } + + public static void register(TaskTypeManager manager) { + if (BentoBox.getInstance().getAddonsManager().getAddonByName("Level").isPresent()) { + manager.registerTaskType(new BentoBoxLevelTaskType()); + } + } + + @EventHandler + public void onBentoBoxIslandLevelCalculated(BentoBoxEvent event) { + if ("IslandLevelCalculatedEvent".equalsIgnoreCase(event.getEventName())) { + QPlayer qPlayer = QuestsAPI.getPlayerManager().getPlayer((UUID) event.getKeyValues().get("targetPlayer")); + if (qPlayer == null) { + return; + } + + 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; + } + + long islandLevelNeeded = (long) (int) task.getConfigValue("level"); + + Object results = event.getKeyValues().get("results"); + + try { + if (levelField == null) { + levelField = results.getClass().getDeclaredField("level"); + levelField.setAccessible(true); + } + + AtomicLong level = (AtomicLong) levelField.get(results); + taskProgress.setProgress(level.get()); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + + if (((long) taskProgress.getProgress()) >= islandLevelNeeded) { + taskProgress.setCompleted(true); + } + } + } + } + } + } +} -- cgit v1.2.3-70-g09d2