Improve compass functionality and use permission, fixes #1106

This commit is contained in:
PikaMug 2020-03-04 02:32:24 -05:00
parent fc7ccbe654
commit ca9f71d16b
7 changed files with 104 additions and 35 deletions

View File

@ -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();
}

View File

@ -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<Integer, Quest> timers = new ConcurrentHashMap<Integer, Quest>();
protected ConcurrentHashMap<Quest, Integer> currentQuests = new ConcurrentHashMap<Quest, Integer>() {
@ -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<Integer, Quest> 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<String> 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("<quest>", ChatColor.GOLD + quest.getName() + ChatColor.YELLOW));
}
}
}
});
}
/**
* Check whether the Quester's inventory contains the specified item
*

View File

@ -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"));

View File

@ -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<Quester> 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()) {

View File

@ -18,5 +18,4 @@ show-titles: true
strict-player-movement: 0
top-limit: 150
translate-names: true
translate-subcommands: false
use-compass: true
translate-subcommands: false

View File

@ -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

View File

@ -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 <quest>."
compassReset: "Reset compass target."
timeZone: "Time zone"
timeDay: "Day"
timeDays: "Days"