Better support for quest names with color codes

This commit is contained in:
PikaMug 2019-06-11 13:09:12 -04:00
parent 946779b17e
commit 2360f68eda
5 changed files with 118 additions and 112 deletions

View File

@ -316,23 +316,10 @@ public class QuestFactory implements ConversationAbandonedListener {
@Override @Override
public Prompt acceptInput(ConversationContext context, String input) { public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) {
for (Quest q : plugin.getQuests()) { Quest q = plugin.getQuest(input);
if (q.getName().equalsIgnoreCase(input)) { if (q != null) {
loadQuest(context, q); loadQuest(context, q);
return new CreateMenuPrompt(); return new CreateMenuPrompt();
}
}
for (Quest q : plugin.getQuests()) {
if (q.getName().toLowerCase().startsWith(input.toLowerCase())) {
loadQuest(context, q);
return new CreateMenuPrompt();
}
}
for (Quest q : plugin.getQuests()) {
if (q.getName().toLowerCase().contains(input.toLowerCase())) {
loadQuest(context, q);
return new CreateMenuPrompt();
}
} }
return new SelectEditPrompt(); return new SelectEditPrompt();
} else { } else {
@ -533,10 +520,10 @@ public class QuestFactory implements ConversationAbandonedListener {
@Override @Override
public String getPromptText(ConversationContext context) { public String getPromptText(ConversationContext context) {
String text = ChatColor.DARK_GREEN + Lang.get("eventTitle") + "\n"; String text = ChatColor.DARK_GREEN + Lang.get("eventTitle") + "\n";
if (plugin.getEvents().isEmpty()) { if (plugin.getActions().isEmpty()) {
text += ChatColor.RED + "- " + Lang.get("none"); text += ChatColor.RED + "- " + Lang.get("none");
} else { } else {
for (Action e : plugin.getEvents()) { for (Action e : plugin.getActions()) {
text += ChatColor.GREEN + "- " + e.getName() + "\n"; text += ChatColor.GREEN + "- " + e.getName() + "\n";
} }
} }
@ -547,20 +534,13 @@ public class QuestFactory implements ConversationAbandonedListener {
public Prompt acceptInput(ConversationContext context, String input) { public Prompt acceptInput(ConversationContext context, String input) {
Player player = (Player) context.getForWhom(); Player player = (Player) context.getForWhom();
if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false && input.equalsIgnoreCase(Lang.get("cmdClear")) == false) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false && input.equalsIgnoreCase(Lang.get("cmdClear")) == false) {
Action found = null; Action a = plugin.getAction(input);
for (Action e : plugin.getEvents()) { if (a != null) {
if (e.getName().equalsIgnoreCase(input)) { context.setSessionData(CK.Q_INITIAL_EVENT, a.getName());
found = e;
break;
}
}
if (found == null) {
player.sendMessage(ChatColor.RED + input + ChatColor.YELLOW + " " + Lang.get("questEditorInvalidEventName"));
return new InitialEventPrompt();
} else {
context.setSessionData(CK.Q_INITIAL_EVENT, found.getName());
return new CreateMenuPrompt(); return new CreateMenuPrompt();
} }
player.sendMessage(ChatColor.RED + input + ChatColor.YELLOW + " " + Lang.get("questEditorInvalidEventName"));
return new InitialEventPrompt();
} else if (input.equalsIgnoreCase(Lang.get("cmdClear"))) { } else if (input.equalsIgnoreCase(Lang.get("cmdClear"))) {
context.setSessionData(CK.Q_INITIAL_EVENT, null); context.setSessionData(CK.Q_INITIAL_EVENT, null);
player.sendMessage(ChatColor.YELLOW + Lang.get("questEditorEventCleared")); player.sendMessage(ChatColor.YELLOW + Lang.get("questEditorEventCleared"));

View File

@ -277,10 +277,24 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
return quests; return quests;
} }
public LinkedList<Action> getActions() {
return events;
}
public void setActions(LinkedList<Action> actions) {
this.events = actions;
}
/**
* @deprecated Use getActions()
*/
public LinkedList<Action> getEvents() { public LinkedList<Action> getEvents() {
return events; return events;
} }
/**
* @deprecated Use setActions()
*/
public void setEvents(LinkedList<Action> events) { public void setEvents(LinkedList<Action> events) {
this.events = events; this.events = events;
} }
@ -3202,10 +3216,19 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
* @return Quest or null if not found * @return Quest or null if not found
*/ */
public Quest getQuest(String name) { public Quest getQuest(String name) {
for (Quest q : quests) { LinkedList<Quest> qs = quests;
if (q.getName().equalsIgnoreCase(name)) { for (Quest q : qs) {
if (q.getName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', name))) {
return q; return q;
} else if (q.getName().toLowerCase().startsWith(name.toLowerCase())) { }
}
for (Quest q : qs) {
if (q.getName().toLowerCase().startsWith(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) {
return q;
}
}
for (Quest q : qs) {
if (q.getName().toLowerCase().contains(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) {
return q; return q;
} }
} }
@ -3219,11 +3242,20 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
* @return Action or null if not found * @return Action or null if not found
*/ */
public Action getAction(String name) { public Action getAction(String name) {
for (Action e : events) { LinkedList<Action> as = events;
if (e.getName().equalsIgnoreCase(name)){ for (Action a : as) {
return e; if (a.getName().equalsIgnoreCase(ChatColor.translateAlternateColorCodes('&', name))) {
} else if (e.getName().toLowerCase().startsWith(name.toLowerCase())) { return a;
return e; }
}
for (Action a : as) {
if (a.getName().toLowerCase().startsWith(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) {
return a;
}
}
for (Action a : as) {
if (a.getName().toLowerCase().contains(ChatColor.translateAlternateColorCodes('&', name).toLowerCase())) {
return a;
} }
} }
return null; return null;

View File

@ -167,7 +167,7 @@ public class ActionFactory implements ConversationAbandonedListener {
} }
} else if (input.equalsIgnoreCase("2")) { } else if (input.equalsIgnoreCase("2")) {
if (player.hasPermission("quests.editor.actions.edit") || player.hasPermission("quests.editor.events.edit")) { if (player.hasPermission("quests.editor.actions.edit") || player.hasPermission("quests.editor.events.edit")) {
if (plugin.getEvents().isEmpty()) { if (plugin.getActions().isEmpty()) {
((Player) context.getForWhom()).sendMessage(ChatColor.YELLOW + Lang.get("eventEditorNoneToEdit")); ((Player) context.getForWhom()).sendMessage(ChatColor.YELLOW + Lang.get("eventEditorNoneToEdit"));
return new MenuPrompt(); return new MenuPrompt();
} else { } else {
@ -179,7 +179,7 @@ public class ActionFactory implements ConversationAbandonedListener {
} }
} else if (input.equalsIgnoreCase("3")) { } else if (input.equalsIgnoreCase("3")) {
if (player.hasPermission("quests.editor.actions.delete") || player.hasPermission("quests.editor.events.delete")) { if (player.hasPermission("quests.editor.actions.delete") || player.hasPermission("quests.editor.events.delete")) {
if (plugin.getEvents().isEmpty()) { if (plugin.getActions().isEmpty()) {
((Player) context.getForWhom()).sendMessage(ChatColor.YELLOW + Lang.get("eventEditorNoneToDelete")); ((Player) context.getForWhom()).sendMessage(ChatColor.YELLOW + Lang.get("eventEditorNoneToDelete"));
return new MenuPrompt(); return new MenuPrompt();
} else { } else {
@ -329,8 +329,8 @@ public class ActionFactory implements ConversationAbandonedListener {
@Override @Override
public String getPromptText(ConversationContext context) { public String getPromptText(ConversationContext context) {
String text = ChatColor.GOLD + "- " + Lang.get("eventEditorEdit") + " -\n"; String text = ChatColor.GOLD + "- " + Lang.get("eventEditorEdit") + " -\n";
for (Action evt : plugin.getEvents()) { for (Action a : plugin.getActions()) {
text += ChatColor.AQUA + evt.getName() + ChatColor.YELLOW + ", "; text += ChatColor.AQUA + a.getName() + ChatColor.YELLOW + ", ";
} }
text = text.substring(0, text.length() - 2) + "\n"; text = text.substring(0, text.length() - 2) + "\n";
text += ChatColor.YELLOW + Lang.get("eventEditorEnterEventName"); text += ChatColor.YELLOW + Lang.get("eventEditorEnterEventName");
@ -340,13 +340,12 @@ public class ActionFactory implements ConversationAbandonedListener {
@Override @Override
public Prompt acceptInput(ConversationContext context, String input) { public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) {
for (Action evt : plugin.getEvents()) { Action a = plugin.getAction(input);
if (evt.getName().toLowerCase().startsWith(input.toLowerCase())) { if (a != null) {
context.setSessionData(CK.E_OLD_EVENT, evt.getName()); context.setSessionData(CK.E_OLD_EVENT, a.getName());
context.setSessionData(CK.E_NAME, evt.getName()); context.setSessionData(CK.E_NAME, a.getName());
loadData(evt, context); loadData(a, context);
return new CreateMenuPrompt(); return new CreateMenuPrompt();
}
} }
((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorNotFound")); ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorNotFound"));
return new SelectEditPrompt(); return new SelectEditPrompt();
@ -361,8 +360,8 @@ public class ActionFactory implements ConversationAbandonedListener {
@Override @Override
public String getPromptText(ConversationContext context) { public String getPromptText(ConversationContext context) {
String text = ChatColor.GOLD + "- " + Lang.get("eventEditorDelete") + " -\n"; String text = ChatColor.GOLD + "- " + Lang.get("eventEditorDelete") + " -\n";
for (Action evt : plugin.getEvents()) { for (Action a : plugin.getActions()) {
text += ChatColor.AQUA + evt.getName() + ChatColor.YELLOW + ","; text += ChatColor.AQUA + a.getName() + ChatColor.YELLOW + ",";
} }
text = text.substring(0, text.length() - 1) + "\n"; text = text.substring(0, text.length() - 1) + "\n";
text += ChatColor.YELLOW + Lang.get("eventEditorEnterEventName"); text += ChatColor.YELLOW + Lang.get("eventEditorEnterEventName");
@ -373,27 +372,26 @@ public class ActionFactory implements ConversationAbandonedListener {
public Prompt acceptInput(ConversationContext context, String input) { public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) {
LinkedList<String> used = new LinkedList<String>(); LinkedList<String> used = new LinkedList<String>();
for (Action evt : plugin.getEvents()) { Action a = plugin.getAction(input);
if (evt.getName().equalsIgnoreCase(input)) { if (a != null) {
for (Quest quest : plugin.getQuests()) { for (Quest quest : plugin.getQuests()) {
for (Stage stage : quest.getStages()) { for (Stage stage : quest.getStages()) {
if (stage.getFinishEvent() != null && stage.getFinishEvent().getName().equalsIgnoreCase(evt.getName())) { if (stage.getFinishEvent() != null && stage.getFinishEvent().getName().equalsIgnoreCase(a.getName())) {
used.add(quest.getName()); used.add(quest.getName());
break; break;
}
} }
} }
if (used.isEmpty()) { }
context.setSessionData(CK.ED_EVENT_DELETE, evt.getName()); if (used.isEmpty()) {
return new DeletePrompt(); context.setSessionData(CK.ED_EVENT_DELETE, a.getName());
} else { return new DeletePrompt();
((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorEventInUse") + " \"" + ChatColor.DARK_PURPLE + evt.getName() + ChatColor.RED + "\":"); } else {
for (String s : used) { ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorEventInUse") + " \"" + ChatColor.DARK_PURPLE + a.getName() + ChatColor.RED + "\":");
((Player) context.getForWhom()).sendMessage(ChatColor.RED + "- " + ChatColor.DARK_RED + s); for (String s : used) {
} ((Player) context.getForWhom()).sendMessage(ChatColor.RED + "- " + ChatColor.DARK_RED + s);
((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorMustModifyQuests"));
return new SelectDeletePrompt();
} }
((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorMustModifyQuests"));
return new SelectDeletePrompt();
} }
} }
((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorNotFound")); ((Player) context.getForWhom()).sendMessage(ChatColor.RED + Lang.get("eventEditorNotFound"));
@ -913,9 +911,9 @@ public class ActionFactory implements ConversationAbandonedListener {
} }
if (((String) context.getSessionData(CK.E_OLD_EVENT)).isEmpty() == false) { if (((String) context.getSessionData(CK.E_OLD_EVENT)).isEmpty() == false) {
data.set(key + "." + (String) context.getSessionData(CK.E_OLD_EVENT), null); data.set(key + "." + (String) context.getSessionData(CK.E_OLD_EVENT), null);
LinkedList<Action> temp = plugin.getEvents(); LinkedList<Action> temp = plugin.getActions();
temp.remove(plugin.getAction((String) context.getSessionData(CK.E_OLD_EVENT))); temp.remove(plugin.getAction((String) context.getSessionData(CK.E_OLD_EVENT)));
plugin.setEvents(temp); plugin.setActions(temp);
} }
ConfigurationSection section = data.createSection(key + "." + (String) context.getSessionData(CK.E_NAME)); ConfigurationSection section = data.createSection(key + "." + (String) context.getSessionData(CK.E_NAME));
names.remove((String) context.getSessionData(CK.E_NAME)); names.remove((String) context.getSessionData(CK.E_NAME));
@ -1067,7 +1065,7 @@ public class ActionFactory implements ConversationAbandonedListener {
@Override @Override
public Prompt acceptInput(ConversationContext context, String input) { public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) {
for (Action e : plugin.getEvents()) { for (Action e : plugin.getActions()) {
if (e.getName().equalsIgnoreCase(input)) { if (e.getName().equalsIgnoreCase(input)) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists")); context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists"));
return new ActionNamePrompt(); return new ActionNamePrompt();
@ -1142,7 +1140,7 @@ public class ActionFactory implements ConversationAbandonedListener {
@Override @Override
public Prompt acceptInput(ConversationContext context, String input) { public Prompt acceptInput(ConversationContext context, String input) {
if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) { if (input.equalsIgnoreCase(Lang.get("cmdCancel")) == false) {
for (Action e : plugin.getEvents()) { for (Action e : plugin.getActions()) {
if (e.getName().equalsIgnoreCase(input)) { if (e.getName().equalsIgnoreCase(input)) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists")); context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists"));
return new SetNamePrompt(); return new SetNamePrompt();

View File

@ -619,17 +619,17 @@ public class CmdExecutor implements CommandExecutor {
} }
Quester quester = plugin.getQuester(player.getUniqueId()); Quester quester = plugin.getQuester(player.getUniqueId());
if (quester.getCurrentQuests().isEmpty() == false) { if (quester.getCurrentQuests().isEmpty() == false) {
Quest q = plugin.getQuest(MiscUtil.concatArgArray(args, 1, args.length - 1, ' ')); Quest quest = plugin.getQuest(concatArgArray(args, 1, args.length - 1, ' '));
if (q != null) { if (quest != null) {
if (q.getOptions().getAllowQuitting()) { if (quest.getOptions().getAllowQuitting()) {
QuestQuitEvent event = new QuestQuitEvent(q, quester); QuestQuitEvent event = new QuestQuitEvent(quest, quester);
plugin.getServer().getPluginManager().callEvent(event); plugin.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
return; return;
} }
quester.hardQuit(q); quester.hardQuit(quest);
String msg = Lang.get("questQuit"); String msg = Lang.get("questQuit");
msg = msg.replace("<quest>", ChatColor.DARK_PURPLE + q.getName() + ChatColor.YELLOW); msg = msg.replace("<quest>", ChatColor.DARK_PURPLE + quest.getName() + ChatColor.YELLOW);
player.sendMessage(ChatColor.YELLOW + msg); player.sendMessage(ChatColor.YELLOW + msg);
quester.saveData(); quester.saveData();
quester.loadData(); quester.loadData();
@ -654,32 +654,7 @@ public class CmdExecutor implements CommandExecutor {
if (args.length == 1) { if (args.length == 1) {
player.sendMessage(ChatColor.YELLOW + Lang.get(player, "COMMAND_TAKE_USAGE")); player.sendMessage(ChatColor.YELLOW + Lang.get(player, "COMMAND_TAKE_USAGE"));
} else { } else {
String name = null; Quest questToFind = plugin.getQuest(concatArgArray(args, 1, args.length - 1, ' '));
if (args.length == 2) {
name = args[1].toLowerCase();
} else {
boolean first = true;
int lastIndex = (args.length - 1);
int index = 0;
for (String s : args) {
if (index != 0) {
if (first) {
first = false;
if (args.length > 2) {
name = s.toLowerCase() + " ";
} else {
name = s.toLowerCase();
}
} else if (index == lastIndex) {
name = name + s.toLowerCase();
} else {
name = name + s.toLowerCase() + " ";
}
}
index++;
}
}
Quest questToFind = plugin.getQuest(name);
if (questToFind != null) { if (questToFind != null) {
final Quest q = questToFind; final Quest q = questToFind;
final Quester quester = plugin.getQuester(player.getUniqueId()); final Quester quester = plugin.getQuester(player.getUniqueId());
@ -1168,7 +1143,7 @@ public class CmdExecutor implements CommandExecutor {
msg = msg.replace("<player>", target.getName()); msg = msg.replace("<player>", target.getName());
cs.sendMessage(ChatColor.YELLOW + msg); cs.sendMessage(ChatColor.YELLOW + msg);
} else { } else {
Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' '));
if (quest == null) { if (quest == null) {
cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); cs.sendMessage(ChatColor.RED + Lang.get("questNotFound"));
return; return;
@ -1228,7 +1203,7 @@ public class CmdExecutor implements CommandExecutor {
msg = msg.replace("<player>", target.getName()); msg = msg.replace("<player>", target.getName());
cs.sendMessage(ChatColor.YELLOW + msg); cs.sendMessage(ChatColor.YELLOW + msg);
} else { } else {
Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' '));
if (quest == null) { if (quest == null) {
cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); cs.sendMessage(ChatColor.RED + Lang.get("questNotFound"));
return; return;
@ -1266,7 +1241,7 @@ public class CmdExecutor implements CommandExecutor {
msg = msg.replace("<player>", target.getName()); msg = msg.replace("<player>", target.getName());
cs.sendMessage(ChatColor.YELLOW + msg); cs.sendMessage(ChatColor.YELLOW + msg);
} else { } else {
Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' '));
if (quest == null) { if (quest == null) {
cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); cs.sendMessage(ChatColor.RED + Lang.get("questNotFound"));
return; return;
@ -1307,7 +1282,7 @@ public class CmdExecutor implements CommandExecutor {
msg = msg.replace("<player>", target.getName()); msg = msg.replace("<player>", target.getName());
cs.sendMessage(ChatColor.YELLOW + msg); cs.sendMessage(ChatColor.YELLOW + msg);
} else { } else {
Quest quest = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); Quest quest = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' '));
if (quest == null) { if (quest == null) {
cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); cs.sendMessage(ChatColor.RED + Lang.get("questNotFound"));
return; return;
@ -1392,7 +1367,7 @@ public class CmdExecutor implements CommandExecutor {
cs.sendMessage(ChatColor.YELLOW + Lang.get("playerNotFound")); cs.sendMessage(ChatColor.YELLOW + Lang.get("playerNotFound"));
return; return;
} }
Quest toRemove = plugin.getQuest(MiscUtil.concatArgArray(args, 2, args.length - 1, ' ')); Quest toRemove = plugin.getQuest(concatArgArray(args, 2, args.length - 1, ' '));
if (toRemove == null) { if (toRemove == null) {
cs.sendMessage(ChatColor.RED + Lang.get("questNotFound")); cs.sendMessage(ChatColor.RED + Lang.get("questNotFound"));
return; return;
@ -1503,4 +1478,22 @@ public class CmdExecutor implements CommandExecutor {
} }
return sortedMap; return sortedMap;
} }
/**
* Used to get quest names that contain spaces from command input
*
* @param args an array of Strings
* @param startingIndex the index to start combining at
* @param endingIndex the index to stop combining at
* @param delimiter the character for which the array was split
* @return a String or null
*/
private static String concatArgArray(String[] args, int startingIndex, int endingIndex, char delimiter) {
String s = "";
for (int i = startingIndex; i <= endingIndex; i++) {
s += args[i] + delimiter;
}
s = s.substring(0, s.length());
return s.trim().equals("") ? null : s.trim();
}
} }

View File

@ -134,7 +134,10 @@ public class MiscUtil {
public static String getDyeString(DyeColor dc) { public static String getDyeString(DyeColor dc) {
return Lang.get("COLOR_" + dc.name()); return Lang.get("COLOR_" + dc.name());
} }
/**
* @deprecated Will be removed in a future version of Quests
*/
public static String concatArgArray(String[] args, int startingIndex, int endingIndex, char delimiter) { public static String concatArgArray(String[] args, int startingIndex, int endingIndex, char delimiter) {
String s = ""; String s = "";
for (int i = startingIndex; i <= endingIndex; i++) { for (int i = startingIndex; i <= endingIndex; i++) {