diff --git a/main/src/main/java/me/blackvein/quests/Quest.java b/main/src/main/java/me/blackvein/quests/Quest.java index 0ab99c1b7..56558f3b0 100644 --- a/main/src/main/java/me/blackvein/quests/Quest.java +++ b/main/src/main/java/me/blackvein/quests/Quest.java @@ -182,7 +182,7 @@ public class Quest { this, quester.getPlayer())); } } - if (plugin.getSettings().canUseCompass()) { + if (quester.getPlayer().hasPermission("quests.compass")) { quester.resetCompass(); quester.findCompassTarget(); } @@ -277,31 +277,31 @@ public class Quest { * Method may be called as often as needed. * * @param quester The online quester to have their compass updated - * @param nextStage The stage to process for targets + * @param stage The stage to process for targets * @return true if successful */ - public boolean updateCompass(Quester quester, Stage nextStage) { - if (!plugin.getSettings().canUseCompass()) { - return false; - } + public boolean updateCompass(Quester quester, Stage stage) { if (quester == null) { return false; } - if (nextStage == null) { + if (stage == null) { return false; } if (!quester.getOfflinePlayer().isOnline()) { return false; } + if (!quester.getPlayer().hasPermission("quests.compass")) { + return false; + } Location targetLocation = null; - if (nextStage.citizensToInteract != null && nextStage.citizensToInteract.size() > 0) { - targetLocation = plugin.getDependencies().getNPCLocation(nextStage.citizensToInteract.getFirst()); - } else if (nextStage.citizensToKill != null && nextStage.citizensToKill.size() > 0) { - targetLocation = plugin.getDependencies().getNPCLocation(nextStage.citizensToKill.getFirst()); - } else if (nextStage.locationsToReach != null && nextStage.locationsToReach.size() > 0) { - targetLocation = nextStage.locationsToReach.getFirst(); - } else if (nextStage.itemDeliveryTargets != null && nextStage.itemDeliveryTargets.size() > 0) { - NPC npc = plugin.getDependencies().getCitizens().getNPCRegistry().getById(nextStage.itemDeliveryTargets + if (stage.citizensToInteract != null && stage.citizensToInteract.size() > 0) { + targetLocation = plugin.getDependencies().getNPCLocation(stage.citizensToInteract.getFirst()); + } else if (stage.citizensToKill != null && stage.citizensToKill.size() > 0) { + targetLocation = plugin.getDependencies().getNPCLocation(stage.citizensToKill.getFirst()); + } else if (stage.locationsToReach != null && stage.locationsToReach.size() > 0) { + targetLocation = stage.locationsToReach.getFirst(); + } else if (stage.itemDeliveryTargets != null && stage.itemDeliveryTargets.size() > 0) { + NPC npc = plugin.getDependencies().getCitizens().getNPCRegistry().getById(stage.itemDeliveryTargets .getFirst()); targetLocation = npc.getStoredLocation(); } diff --git a/main/src/main/java/me/blackvein/quests/Quester.java b/main/src/main/java/me/blackvein/quests/Quester.java index c0c03e089..696002dae 100644 --- a/main/src/main/java/me/blackvein/quests/Quester.java +++ b/main/src/main/java/me/blackvein/quests/Quester.java @@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.stream.Collectors; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.DyeColor; import org.bukkit.Location; @@ -76,6 +77,7 @@ public class Quester { private UUID id; protected String questToTake; protected int questPoints = 0; + private String compassTargetQuestId; protected ConcurrentHashMap timers = new ConcurrentHashMap(); protected ConcurrentHashMap currentQuests = new ConcurrentHashMap() { @@ -254,6 +256,24 @@ public class Quester { public void setQuestPoints(int questPoints) { this.questPoints = questPoints; } + + /** + * Get compass target quest. Returns null if not set + * + * @return Quest or null + */ + public Quest getCompassTarget() { + return compassTargetQuestId != null ? plugin.getQuestById(compassTargetQuestId) : null; + } + + /** + * Set compass target quest. Does not update in-game + * + * @param quest The target quest + */ + public void setCompassTarget(Quest quest) { + compassTargetQuestId = quest.getId(); + } public ConcurrentHashMap getTimers() { return timers; @@ -3535,8 +3555,9 @@ public class Quester { * Will set to Quester's spawn location if bed spawn does not exist */ public void resetCompass() { - if (!plugin.getSettings().canUseCompass()) + if (!getPlayer().hasPermission("quests.compass")) { return; + } Player player = getPlayer(); if (player == null) return; @@ -3544,25 +3565,64 @@ public class Quester { if (defaultLocation == null) { defaultLocation = player.getWorld().getSpawnLocation(); } + compassTargetQuestId = null; player.setCompassTarget(defaultLocation); } /** - * Gets first stage target from current quests, then updates compass accordingly + * Update compass target to current stage of first available current quest, if possible */ public void findCompassTarget() { - if (!plugin.getSettings().canUseCompass()) - return; - Player player = getPlayer(); - if (player == null) + if (!getPlayer().hasPermission("quests.compass")) { return; + } for (Quest quest : currentQuests.keySet()) { Stage stage = getCurrentStage(quest); - if (stage != null && quest.updateCompass(this, stage)) + if (stage != null && quest.updateCompass(this, stage)) { break; + } } } + /** + * Update compass target to current stage of next available current quest, if possible + * + * @param notify Whether to notify this quester of result + */ + public void findNextCompassTarget(boolean notify) { + if (!getPlayer().hasPermission("quests.compass")) { + return; + } + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + LinkedList list = currentQuests.keySet().stream() + .sorted(Comparator.comparing(Quest::getName)).map(Quest::getId) + .collect(Collectors.toCollection(LinkedList::new)); + int index = 0; + if (compassTargetQuestId != null) { + if (!list.contains(compassTargetQuestId) && notify) { + return; + } + index = list.indexOf(compassTargetQuestId) + 1; + if (index >= list.size()) { + index = 0; + } + } + final Quest quest = plugin.getQuestById(list.get(index)); + compassTargetQuestId = quest.getId(); + final Stage stage = getCurrentStage(quest); + if (stage != null) { + quest.updateCompass(Quester.this, stage); + if (notify && getPlayer().isOnline()) { + getPlayer().sendMessage(ChatColor.YELLOW + Lang.get(getPlayer(), "compassSet") + .replace("", ChatColor.GOLD + quest.getName() + ChatColor.YELLOW)); + } + } + } + }); + } + /** * Check whether the Quester's inventory contains the specified item * diff --git a/main/src/main/java/me/blackvein/quests/Settings.java b/main/src/main/java/me/blackvein/quests/Settings.java index a096e61ca..6b6d2c636 100644 --- a/main/src/main/java/me/blackvein/quests/Settings.java +++ b/main/src/main/java/me/blackvein/quests/Settings.java @@ -37,7 +37,6 @@ public class Settings { private int topLimit = 150; private boolean translateNames = false; private boolean translateSubCommands = false; - private boolean useCompass = true; private boolean useGPS = true; public Settings(Quests plugin) { @@ -146,12 +145,6 @@ public class Settings { public void setTranslateSubCommands(boolean translateSubCommands) { this.translateSubCommands = translateSubCommands; } - public boolean canUseCompass() { - return useCompass; - } - public void setUseCompass(boolean useCompass) { - this.useCompass = useCompass; - } public boolean canUseGPS() { return useGPS; } @@ -184,7 +177,6 @@ public class Settings { topLimit = config.getInt("top-limit", 150); translateNames = config.getBoolean("translate-names", true); translateSubCommands = config.getBoolean("translate-subcommands", false); - useCompass = config.getBoolean("use-compass", true); useGPS = config.getBoolean("use-gps-plugin", true); try { config.save(new File(plugin.getDataFolder(), "config.yml")); diff --git a/main/src/main/java/me/blackvein/quests/listeners/PlayerListener.java b/main/src/main/java/me/blackvein/quests/listeners/PlayerListener.java index ea06b45f8..71e160887 100644 --- a/main/src/main/java/me/blackvein/quests/listeners/PlayerListener.java +++ b/main/src/main/java/me/blackvein/quests/listeners/PlayerListener.java @@ -183,9 +183,9 @@ public class PlayerListener implements Listener { } } if (plugin.checkQuester(evt.getPlayer().getUniqueId()) == false) { + final Quester quester = plugin.getQuester(evt.getPlayer().getUniqueId()); + final Player player = evt.getPlayer(); if (evt.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { - final Quester quester = plugin.getQuester(evt.getPlayer().getUniqueId()); - final Player player = evt.getPlayer(); boolean hasObjective = false; if (evt.isCancelled() == false) { final ItemStack blockItemStack = new ItemStack(evt.getClickedBlock().getType(), 1, evt @@ -347,6 +347,19 @@ public class PlayerListener implements Listener { } } } + if (evt.getItem() != null && evt.getItem().getType().equals(Material.COMPASS)) { + if (!player.hasPermission("quests.compass")) { + return; + } + if (evt.getAction().equals(Action.LEFT_CLICK_AIR) + || evt.getAction().equals(Action.LEFT_CLICK_BLOCK)) { + quester.resetCompass(); + player.sendMessage(ChatColor.YELLOW + Lang.get(player, "compassReset")); + } else if (evt.getAction().equals(Action.RIGHT_CLICK_AIR) + || evt.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { + quester.findNextCompassTarget(true); + } + } } } } @@ -935,7 +948,7 @@ public class PlayerListener implements Listener { LinkedList temp = plugin.getQuesters(); temp.add(quester); plugin.setQuesters(temp); - if (plugin.getSettings().canUseCompass()) { + if (evt.getPlayer().hasPermission("quests.compass")) { quester.resetCompass(); } for (String s : quester.getCompletedQuests()) { diff --git a/main/src/main/resources/config.yml b/main/src/main/resources/config.yml index 6f2b369f8..732f21438 100644 --- a/main/src/main/resources/config.yml +++ b/main/src/main/resources/config.yml @@ -18,5 +18,4 @@ show-titles: true strict-player-movement: 0 top-limit: 150 translate-names: true -translate-subcommands: false -use-compass: true \ No newline at end of file +translate-subcommands: false \ No newline at end of file diff --git a/main/src/main/resources/plugin.yml b/main/src/main/resources/plugin.yml index 4107e01b5..d34661edc 100644 --- a/main/src/main/resources/plugin.yml +++ b/main/src/main/resources/plugin.yml @@ -38,6 +38,9 @@ permissions: quests.journal: description: Toggle the Quest Journal default: true + quests.compass: + description: Use a Compass to target quests + default: true quests.admin: description: Display administrator help default: op diff --git a/main/src/main/resources/strings.yml b/main/src/main/resources/strings.yml index 61a8b8fc2..e3f745fed 100644 --- a/main/src/main/resources/strings.yml +++ b/main/src/main/resources/strings.yml @@ -702,6 +702,8 @@ journalAlreadyHave: "You already have your Quest Journal out." journalNoRoom: "You have no room in your inventory for your Quest Journal!" journalNoQuests: "You have no accepted quests!" journalDenied: "You cannot do that with your Quest Journal." +compassSet: "Set compass target to quest ." +compassReset: "Reset compass target." timeZone: "Time zone" timeDay: "Day" timeDays: "Days"