From b7183db984be742267e2b6efb111e200a4cffa9a Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Thu, 22 Oct 2020 20:15:25 -0400 Subject: [PATCH] New stay within region condition, fixes #1083 --- .../main/java/me/blackvein/quests/Quest.java | 29 ++++- .../java/me/blackvein/quests/Quester.java | 19 ++- .../main/java/me/blackvein/quests/Quests.java | 23 ++++ .../quests/conditions/Condition.java | 17 +++ .../quests/conditions/ConditionFactory.java | 11 ++ .../convo/conditions/tasks/WorldPrompt.java | 109 +++++++++++++++++- .../convo/quests/main/QuestMainPrompt.java | 16 +-- .../java/me/blackvein/quests/util/CK.java | 1 + main/src/main/resources/strings.yml | 4 + 9 files changed, 211 insertions(+), 18 deletions(-) diff --git a/main/src/main/java/me/blackvein/quests/Quest.java b/main/src/main/java/me/blackvein/quests/Quest.java index af0532e39..795b77ca7 100644 --- a/main/src/main/java/me/blackvein/quests/Quest.java +++ b/main/src/main/java/me/blackvein/quests/Quest.java @@ -958,11 +958,36 @@ public class Quest { /** * Checks if quester is in WorldGuard region start * + * @deprecated Use {@link #isInRegion(Quester)} * @param quester The quester to check * @return true if quester is in region */ + @Deprecated public boolean isInRegion(final Quester quester) { - return isInRegion(quester.getPlayer()); + return isInRegionStart(quester); + } + + /** + * Checks if player is in WorldGuard region start + * + * @deprecated Use {@link #isInRegionStart(Player)} + * @param player The player to check + * @return true if player is in region + */ + @Deprecated + @SuppressWarnings("unused") + private boolean isInRegion(final Player player) { + return isInRegionStart(player); + } + + /** + * Checks if quester is in WorldGuard region start + * + * @param quester The quester to check + * @return true if quester is in region + */ + public boolean isInRegionStart(final Quester quester) { + return isInRegionStart(quester.getPlayer()); } /** @@ -971,7 +996,7 @@ public class Quest { * @param player The player to check * @return true if player is in region */ - private boolean isInRegion(final Player player) { + private boolean isInRegionStart(final Player player) { if (regionStart == null) { return false; } diff --git a/main/src/main/java/me/blackvein/quests/Quester.java b/main/src/main/java/me/blackvein/quests/Quester.java index 219016cec..7305561a3 100644 --- a/main/src/main/java/me/blackvein/quests/Quester.java +++ b/main/src/main/java/me/blackvein/quests/Quester.java @@ -627,6 +627,12 @@ public class Quester { msg += ChatColor.AQUA + "\n \u2515 " + MiscUtil.snakeCaseToUpperCamelCase(b); } p.sendMessage(ChatColor.YELLOW + msg); + } else if (!c.getRegionsWhileStayingWithin().isEmpty()) { + String msg = "- " + Lang.get("conditionEditorStayWithinRegion"); + for (final String r : c.getRegionsWhileStayingWithin()) { + msg += ChatColor.AQUA + "\n \u2515 " + r; + } + p.sendMessage(ChatColor.YELLOW + msg); } } } @@ -4126,7 +4132,7 @@ public class Quester { } return false; } else if (quest.getRegionStart() != null) { - if (!quest.isInRegion(this)) { + if (!quest.isInRegionStart(this)) { if (giveReason) { String msg = Lang.get(getPlayer(), "questInvalidLocation"); msg = msg.replace("", ChatColor.AQUA + quest.getName() + ChatColor.YELLOW); @@ -4170,4 +4176,15 @@ public class Quester { } return false; } + + public boolean isInRegion(final String regionID) { + if (getPlayer() == null) { + return false; + } + if (plugin.getDependencies().getWorldGuardApi() + .getApplicableRegionsIDs(getPlayer().getWorld(), getPlayer().getLocation()).contains(regionID)) { + return true; + } + return false; + } } diff --git a/main/src/main/java/me/blackvein/quests/Quests.java b/main/src/main/java/me/blackvein/quests/Quests.java index 8894ea1ad..915314116 100644 --- a/main/src/main/java/me/blackvein/quests/Quests.java +++ b/main/src/main/java/me/blackvein/quests/Quests.java @@ -3439,6 +3439,29 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener throw new ConditionFormatException("stay-within-biome is not a list of biomes", conditionKey); } } + if (data.contains(conditionKey + "stay-within-region")) { + if (ConfigUtil.checkList(data.getList(conditionKey + "stay-within-region"), String.class)) { + final LinkedList regions = new LinkedList(); + for (final String s : data.getStringList(conditionKey + "stay-within-region")) { + boolean exists = false; + for (final World world : getServer().getWorlds()) { + if (getDependencies().getWorldGuardApi().getRegionManager(world) != null) { + if (getDependencies().getWorldGuardApi().getRegionManager(world).hasRegion(s)) { + regions.add(s); + exists = true; + break; + } + } + } + if (!exists) { + throw new ConditionFormatException("region has invalid WorldGuard region name", conditionKey); + } + } + condition.setRegionsWhileStayingWithin(regions); + } else { + throw new ConditionFormatException("stay-within-region is not a list of regions", conditionKey); + } + } return condition; } diff --git a/main/src/main/java/me/blackvein/quests/conditions/Condition.java b/main/src/main/java/me/blackvein/quests/conditions/Condition.java index 038efa5a8..449c5705f 100644 --- a/main/src/main/java/me/blackvein/quests/conditions/Condition.java +++ b/main/src/main/java/me/blackvein/quests/conditions/Condition.java @@ -33,6 +33,7 @@ public class Condition { private LinkedList itemsWhileHoldingMainHand = new LinkedList(); private LinkedList worldsWhileStayingWithin = new LinkedList(); private LinkedList biomesWhileStayingWithin = new LinkedList(); + private LinkedList regionsWhileStayingWithin = new LinkedList(); public Condition(final Quests plugin) { this.plugin = plugin; @@ -93,6 +94,14 @@ public class Condition { public void setBiomesWhileStayingWithin(final LinkedList biomesWhileStayingWithin) { this.biomesWhileStayingWithin = biomesWhileStayingWithin; } + + public LinkedList getRegionsWhileStayingWithin() { + return regionsWhileStayingWithin; + } + + public void setRegionsWhileStayingWithin(final LinkedList biomesWhileStayingWithin) { + this.regionsWhileStayingWithin = biomesWhileStayingWithin; + } @SuppressWarnings("deprecation") public boolean check(final Quester quester, final Quest quest) { @@ -144,6 +153,14 @@ public class Condition { + MiscUtil.getProperBiome(b)); } } + } else if (!regionsWhileStayingWithin.isEmpty()) { + for (final String r : regionsWhileStayingWithin) { + if (quester.isInRegion(r)) { + return true; + } else if (plugin.getSettings().getConsoleLogging() > 2) { + plugin.getLogger().info("DEBUG: Condition region mismatch for " + player.getName() + ": " + r); + } + } } return false; } diff --git a/main/src/main/java/me/blackvein/quests/conditions/ConditionFactory.java b/main/src/main/java/me/blackvein/quests/conditions/ConditionFactory.java index 537b9ce12..bf1b0213c 100644 --- a/main/src/main/java/me/blackvein/quests/conditions/ConditionFactory.java +++ b/main/src/main/java/me/blackvein/quests/conditions/ConditionFactory.java @@ -109,6 +109,12 @@ public class ConditionFactory implements ConversationAbandonedListener { biomes.addAll(condition.getBiomesWhileStayingWithin()); context.setSessionData(CK.C_WHILE_WITHIN_BIOME, biomes); } + if (condition.getRegionsWhileStayingWithin() != null + && condition.getRegionsWhileStayingWithin().isEmpty() == false) { + final LinkedList regions = new LinkedList(); + regions.addAll(condition.getRegionsWhileStayingWithin()); + context.setSessionData(CK.C_WHILE_WITHIN_REGION, regions); + } } public void clearData(final ConversationContext context) { @@ -120,6 +126,7 @@ public class ConditionFactory implements ConversationAbandonedListener { context.setSessionData(CK.C_WHILE_HOLDING_MAIN_HAND, null); context.setSessionData(CK.C_WHILE_WITHIN_WORLD, null); context.setSessionData(CK.C_WHILE_WITHIN_BIOME, null); + context.setSessionData(CK.C_WHILE_WITHIN_REGION, null); } public void deleteCondition(final ConversationContext context) { @@ -220,6 +227,10 @@ public class ConditionFactory implements ConversationAbandonedListener { section.set("stay-within-biome", context.getSessionData(CK.C_WHILE_WITHIN_BIOME)); } + if (context.getSessionData(CK.C_WHILE_WITHIN_REGION) != null) { + section.set("stay-within-region", + context.getSessionData(CK.C_WHILE_WITHIN_REGION)); + } try { data.save(conditionsFile); } catch (final IOException e) { diff --git a/main/src/main/java/me/blackvein/quests/convo/conditions/tasks/WorldPrompt.java b/main/src/main/java/me/blackvein/quests/convo/conditions/tasks/WorldPrompt.java index 7f023a42b..981b6f65f 100644 --- a/main/src/main/java/me/blackvein/quests/convo/conditions/tasks/WorldPrompt.java +++ b/main/src/main/java/me/blackvein/quests/convo/conditions/tasks/WorldPrompt.java @@ -24,22 +24,29 @@ import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.Prompt; import org.bukkit.entity.Player; +import com.sk89q.worldguard.protection.managers.RegionManager; + +import me.blackvein.quests.Quests; import me.blackvein.quests.convo.conditions.main.ConditionMainPrompt; import me.blackvein.quests.convo.quests.QuestsEditorNumericPrompt; import me.blackvein.quests.convo.quests.QuestsEditorStringPrompt; import me.blackvein.quests.events.editor.quests.QuestsEditorPostOpenNumericPromptEvent; import me.blackvein.quests.events.editor.quests.QuestsEditorPostOpenStringPromptEvent; +import me.blackvein.quests.reflect.worldguard.WorldGuardAPI; import me.blackvein.quests.util.CK; import me.blackvein.quests.util.Lang; import me.blackvein.quests.util.MiscUtil; public class WorldPrompt extends QuestsEditorNumericPrompt { + private final Quests plugin; + public WorldPrompt(final ConversationContext context) { super(context); + this.plugin = (Quests)context.getPlugin(); } - private final int size = 3; + private final int size = 4; @Override public int getSize() { @@ -56,8 +63,9 @@ public class WorldPrompt extends QuestsEditorNumericPrompt { switch (number) { case 1: case 2: - return ChatColor.BLUE; case 3: + return ChatColor.BLUE; + case 4: return ChatColor.GREEN; default: return null; @@ -72,6 +80,12 @@ public class WorldPrompt extends QuestsEditorNumericPrompt { case 2: return ChatColor.YELLOW + Lang.get("conditionEditorStayWithinBiome"); case 3: + if (plugin.getDependencies().getWorldGuardApi() != null) { + return ChatColor.YELLOW + Lang.get("conditionEditorStayWithinRegion"); + } else { + return ChatColor.GRAY + Lang.get("conditionEditorStayWithinRegion"); + } + case 4: return ChatColor.GREEN + Lang.get("done"); default: return null; @@ -103,6 +117,20 @@ public class WorldPrompt extends QuestsEditorNumericPrompt { return text; } case 3: + if (plugin.getDependencies().getWorldGuardApi() != null) { + if (context.getSessionData(CK.C_WHILE_WITHIN_REGION) == null) { + return ChatColor.GRAY + "(" + Lang.get("noneSet") + ")"; + } else { + String text = "\n"; + for (final String s: (List) context.getSessionData(CK.C_WHILE_WITHIN_REGION)) { + text += ChatColor.GRAY + " - " + ChatColor.BLUE + s + "\n"; + } + return text; + } + } else { + return ChatColor.GRAY + "(" + Lang.get("notInstalled") + ")"; + } + case 4: return ""; default: return null; @@ -130,6 +158,8 @@ public class WorldPrompt extends QuestsEditorNumericPrompt { case 2: return new BiomesPrompt(context); case 3: + return new RegionsPrompt(context); + case 4: try { return new ConditionMainPrompt(context); } catch (final Exception e) { @@ -245,4 +275,79 @@ public class WorldPrompt extends QuestsEditorNumericPrompt { return new WorldPrompt(context); } } + + public class RegionsPrompt extends QuestsEditorStringPrompt { + + public RegionsPrompt(final ConversationContext context) { + super(context); + } + + @Override + public String getTitle(final ConversationContext context) { + return Lang.get("conditionEditorRegionsTitle"); + } + + @Override + public String getQueryText(final ConversationContext context) { + return Lang.get("conditionEditorRegionsPrompt"); + } + + @Override + public String getPromptText(final ConversationContext context) { + final QuestsEditorPostOpenStringPromptEvent event = new QuestsEditorPostOpenStringPromptEvent(context, this); + context.getPlugin().getServer().getPluginManager().callEvent(event); + + String regions = ChatColor.LIGHT_PURPLE + getTitle(context) + "\n"; + boolean any = false; + for (final World world : plugin.getServer().getWorlds()) { + final WorldGuardAPI api = plugin.getDependencies().getWorldGuardApi(); + final RegionManager rm = api.getRegionManager(world); + for (final String region : rm.getRegions().keySet()) { + any = true; + regions += ChatColor.GREEN + region + ", "; + } + } + if (any) { + regions = regions.substring(0, regions.length() - 2); + regions += "\n\n"; + } else { + regions += ChatColor.GRAY + "(" + Lang.get("none") + ")\n\n"; + } + return regions + ChatColor.YELLOW + getQueryText(context); + } + + @Override + public Prompt acceptInput(final ConversationContext context, final String input) { + final Player player = (Player) context.getForWhom(); + if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { + final LinkedList regions = new LinkedList(); + for (final String r : input.split(" ")) { + boolean found = false; + for (final World world : plugin.getServer().getWorlds()) { + final WorldGuardAPI api = plugin.getDependencies().getWorldGuardApi(); + final RegionManager rm = api.getRegionManager(world); + for (final String region : rm.getRegions().keySet()) { + if (region.equalsIgnoreCase(r)) { + regions.add(region); + found = true; + break; + } + } + + if (found) { + break; + } + } + if (found = false) { + String error = Lang.get("questWGInvalidRegion"); + error = error.replace("", ChatColor.RED + r + ChatColor.YELLOW); + player.sendMessage(ChatColor.YELLOW + error); + return new RegionsPrompt(context); + } + } + context.setSessionData(CK.C_WHILE_WITHIN_REGION, regions); + } + return new WorldPrompt(context); + } + } } diff --git a/main/src/main/java/me/blackvein/quests/convo/quests/main/QuestMainPrompt.java b/main/src/main/java/me/blackvein/quests/convo/quests/main/QuestMainPrompt.java index 2999a35e7..bc0049a06 100644 --- a/main/src/main/java/me/blackvein/quests/convo/quests/main/QuestMainPrompt.java +++ b/main/src/main/java/me/blackvein/quests/convo/quests/main/QuestMainPrompt.java @@ -124,9 +124,7 @@ public class QuestMainPrompt extends QuestsEditorNumericPrompt { case 3: return ChatColor.YELLOW + Lang.get("questEditorFinishMessage"); case 4: - if (context.getSessionData(CK.Q_START_NPC) == null && plugin.getDependencies().getCitizens() != null) { - return ChatColor.YELLOW + Lang.get("questEditorNPCStart"); - } else if (plugin.getDependencies().getCitizens() != null) { + if (context.getSessionData(CK.Q_START_NPC) == null || plugin.getDependencies().getCitizens() != null) { return ChatColor.YELLOW + Lang.get("questEditorNPCStart"); } else { return ChatColor.GRAY + Lang.get("questEditorNPCStart"); @@ -135,11 +133,7 @@ public class QuestMainPrompt extends QuestsEditorNumericPrompt { return ChatColor.YELLOW + Lang.get("questEditorBlockStart"); case 6: if (plugin.getDependencies().getWorldGuardApi() != null) { - if (context.getSessionData(CK.Q_REGION) == null) { - return ChatColor.YELLOW + Lang.get("questWGSetRegion"); - } else { - return ChatColor.YELLOW + Lang.get("questWGSetRegion"); - } + return ChatColor.YELLOW + Lang.get("questWGSetRegion"); } else { return ChatColor.GRAY + Lang.get("questWGSetRegion"); } @@ -147,11 +141,7 @@ public class QuestMainPrompt extends QuestsEditorNumericPrompt { return ChatColor.YELLOW + Lang.get("questEditorInitialEvent"); case 8: if (plugin.getDependencies().getCitizens() != null) { - if (context.getSessionData(CK.Q_GUIDISPLAY) == null) { - return ChatColor.YELLOW + Lang.get("questEditorSetGUI"); - } else { - return ChatColor.YELLOW + Lang.get("questEditorSetGUI"); - } + return ChatColor.YELLOW + Lang.get("questEditorSetGUI"); } else { return ChatColor.GRAY + Lang.get("questEditorSetGUI"); } diff --git a/main/src/main/java/me/blackvein/quests/util/CK.java b/main/src/main/java/me/blackvein/quests/util/CK.java index 5c4200f6a..eae3328e9 100644 --- a/main/src/main/java/me/blackvein/quests/util/CK.java +++ b/main/src/main/java/me/blackvein/quests/util/CK.java @@ -186,4 +186,5 @@ public class CK { public static final String C_WHILE_HOLDING_MAIN_HAND = "conHoldingMainHand"; public static final String C_WHILE_WITHIN_WORLD = "conWithinWorld"; public static final String C_WHILE_WITHIN_BIOME = "conWithinBiome"; + public static final String C_WHILE_WITHIN_REGION = "conWithinRegion"; } diff --git a/main/src/main/resources/strings.yml b/main/src/main/resources/strings.yml index 905eeec00..8432340d7 100644 --- a/main/src/main/resources/strings.yml +++ b/main/src/main/resources/strings.yml @@ -423,6 +423,10 @@ conditionEditorBiomesTitle: "- Biomes -" conditionEditorBiomesPrompt: "Enter biome names, , " conditionEditorStayWithinBiome: "Stay within biome" conditionEditorInvalidBiome: "is not a valid biome name!" +conditionEditorRegionsTitle: "- Regions -" +conditionEditorRegionsPrompt: "Enter region names, , " +conditionEditorStayWithinRegion: "Stay within region" +conditionEditorInvalidRegion: "is not a valid region name!" reqSetMoney: "Set money requirement" reqSetQuestPoints: "Set requirement" reqSetItem: "Set item requirements"