Quests/main/src/main/java/me/blackvein/quests/convo/actions/menu/ActionMenuPrompt.java

405 lines
17 KiB
Java

/*
* Copyright (c) 2014 PikaMug and contributors. All rights reserved.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package me.blackvein.quests.convo.actions.menu;
import me.blackvein.quests.Quest;
import me.blackvein.quests.Quests;
import me.blackvein.quests.Stage;
import me.blackvein.quests.actions.Action;
import me.blackvein.quests.convo.QuestsNumericPrompt;
import me.blackvein.quests.convo.actions.ActionsEditorNumericPrompt;
import me.blackvein.quests.convo.actions.ActionsEditorStringPrompt;
import me.blackvein.quests.convo.actions.main.ActionMainPrompt;
import me.blackvein.quests.events.editor.actions.ActionsEditorPostOpenNumericPromptEvent;
import me.blackvein.quests.events.editor.actions.ActionsEditorPostOpenStringPromptEvent;
import me.blackvein.quests.util.CK;
import me.blackvein.quests.util.Lang;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class ActionMenuPrompt extends ActionsEditorNumericPrompt {
private final Quests plugin;
public ActionMenuPrompt(final ConversationContext context) {
super(context);
this.plugin = (Quests)context.getPlugin();
}
private final int size = 4;
@Override
public int getSize() {
return size;
}
@Override
public String getTitle(final ConversationContext context) {
final String title = Lang.get("eventEditorTitle");
return title + (plugin.hasLimitedAccess(context.getForWhom()) ? ChatColor.RED + " (" + Lang.get("trialMode")
+ ")" : "");
}
@Override
public ChatColor getNumberColor(final ConversationContext context, final int number) {
switch (number) {
case 1:
case 2:
case 3:
return ChatColor.BLUE;
case 4:
return ChatColor.RED;
default:
return null;
}
}
@Override
public String getSelectionText(final ConversationContext context, final int number) {
switch (number) {
case 1:
return ChatColor.YELLOW + Lang.get("eventEditorCreate");
case 2:
return ChatColor.YELLOW + Lang.get("eventEditorEdit");
case 3:
return ChatColor.YELLOW + Lang.get("eventEditorDelete");
case 4:
return ChatColor.RED + Lang.get("exit");
default:
return null;
}
}
@Override
public String getAdditionalText(final ConversationContext context, final int number) {
return null;
}
@Override
public @NotNull String getBasicPromptText(final @NotNull ConversationContext context) {
final ActionsEditorPostOpenNumericPromptEvent event
= new ActionsEditorPostOpenNumericPromptEvent(context, this);
plugin.getServer().getPluginManager().callEvent(event);
final StringBuilder text = new StringBuilder(ChatColor.GOLD + getTitle(context));
for (int i = 1; i <= size; i++) {
text.append("\n").append(getNumberColor(context, i)).append(ChatColor.BOLD).append(i).append(ChatColor.RESET).append(" - ").append(getSelectionText(context, i));
}
return text.toString();
}
@Override
protected Prompt acceptValidatedInput(final ConversationContext context, final Number input) {
final CommandSender cs = (CommandSender) context.getForWhom();
switch (input.intValue()) {
case 1:
if (cs.hasPermission("quests.editor.actions.create")
|| cs.hasPermission("quests.editor.events.create")) {
context.setSessionData(CK.E_OLD_EVENT, "");
return new ActionSelectCreatePrompt(context);
} else {
cs.sendMessage(ChatColor.RED + Lang.get("noPermission"));
return new ActionMenuPrompt(context);
}
case 2:
if (cs.hasPermission("quests.editor.actions.edit")
|| cs.hasPermission("quests.editor.events.edit")) {
if (plugin.getLoadedActions().isEmpty()) {
context.getForWhom().sendRawMessage(ChatColor.YELLOW
+ Lang.get("eventEditorNoneToEdit"));
return new ActionMenuPrompt(context);
} else {
return new ActionSelectEditPrompt(context);
}
} else {
cs.sendMessage(ChatColor.RED + Lang.get("noPermission"));
return new ActionMenuPrompt(context);
}
case 3:
if (cs.hasPermission("quests.editor.actions.delete")
|| cs.hasPermission("quests.editor.events.delete")) {
if (plugin.getLoadedActions().isEmpty()) {
context.getForWhom().sendRawMessage(ChatColor.YELLOW
+ Lang.get("eventEditorNoneToDelete"));
return new ActionMenuPrompt(context);
} else {
return new ActionSelectDeletePrompt(context);
}
} else {
cs.sendMessage(ChatColor.RED + Lang.get("noPermission"));
return new ActionMenuPrompt(context);
}
case 4:
context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("exited"));
return Prompt.END_OF_CONVERSATION;
default:
return new ActionMenuPrompt(context);
}
}
public class ActionSelectCreatePrompt extends ActionsEditorStringPrompt {
public ActionSelectCreatePrompt(final ConversationContext context) {
super(context);
}
@Override
public String getTitle(final ConversationContext context) {
return Lang.get("eventCreateTitle");
}
@Override
public String getQueryText(final ConversationContext context) {
return Lang.get("eventEditorEnterEventName");
}
@Override
public @NotNull String getPromptText(final @NotNull ConversationContext context) {
final ActionsEditorPostOpenStringPromptEvent event
= new ActionsEditorPostOpenStringPromptEvent(context, this);
plugin.getServer().getPluginManager().callEvent(event);
return ChatColor.GOLD + getTitle(context) + "\n" + ChatColor.YELLOW + getQueryText(context);
}
@Override
public Prompt acceptInput(final @NotNull ConversationContext context, String input) {
if (input == null) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput"));
return new ActionSelectCreatePrompt(context);
}
input = input.trim();
if (!input.equalsIgnoreCase(Lang.get("cmdCancel"))) {
for (final Action a : plugin.getLoadedActions()) {
if (a.getName().equalsIgnoreCase(input)) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorExists"));
return new ActionSelectCreatePrompt(context);
}
}
final List<String> actionNames = plugin.getActionFactory().getNamesOfActionsBeingEdited();
if (actionNames.contains(input)) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("questEditorBeingEdited"));
return new ActionSelectCreatePrompt(context);
}
if (input.contains(".") || input.contains(",")) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("questEditorInvalidQuestName"));
return new ActionSelectCreatePrompt(context);
}
if (input.equals("")) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("itemCreateInvalidInput"));
return new ActionSelectCreatePrompt(context);
}
context.setSessionData(CK.E_NAME, input);
actionNames.add(input);
plugin.getActionFactory().setNamesOfActionsBeingEdited(actionNames);
return new ActionMainPrompt(context);
} else {
return new ActionMenuPrompt(context);
}
}
}
public class ActionSelectEditPrompt extends ActionsEditorStringPrompt {
public ActionSelectEditPrompt(final ConversationContext context) {
super(context);
}
@Override
public String getTitle(final ConversationContext context) {
return Lang.get("eventEditTitle");
}
@Override
public String getQueryText(final ConversationContext context) {
return Lang.get("eventEditorEnterEventName");
}
@Override
public @NotNull String getPromptText(final @NotNull ConversationContext context) {
final ActionsEditorPostOpenStringPromptEvent event
= new ActionsEditorPostOpenStringPromptEvent(context, this);
plugin.getServer().getPluginManager().callEvent(event);
final List<String> names = plugin.getLoadedActions().stream().map(Action::getName).collect(Collectors.toList());
return sendClickableMenu(getTitle(context), names, getQueryText(context), context.getForWhom());
}
@Override
public Prompt acceptInput(final @NotNull ConversationContext context, final String input) {
if (input == null) {
return null;
}
if (!input.equalsIgnoreCase(Lang.get("cmdCancel"))) {
final Action a = plugin.getAction(input);
if (a != null) {
context.setSessionData(CK.E_OLD_EVENT, a.getName());
context.setSessionData(CK.E_NAME, a.getName());
plugin.getActionFactory().loadData(a, context);
return new ActionMainPrompt(context);
}
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorNotFound"));
return new ActionSelectEditPrompt(context);
} else {
return new ActionMenuPrompt(context);
}
}
}
public class ActionSelectDeletePrompt extends ActionsEditorStringPrompt {
public ActionSelectDeletePrompt(final ConversationContext context) {
super(context);
}
@Override
public String getTitle(final ConversationContext context) {
return Lang.get("eventDeleteTitle");
}
@Override
public String getQueryText(final ConversationContext context) {
return Lang.get("eventEditorEnterEventName");
}
@Override
public @NotNull String getPromptText(final @NotNull ConversationContext context) {
final ActionsEditorPostOpenStringPromptEvent event
= new ActionsEditorPostOpenStringPromptEvent(context, this);
plugin.getServer().getPluginManager().callEvent(event);
final List<String> names = plugin.getLoadedActions().stream().map(Action::getName).collect(Collectors.toList());
return sendClickableMenu(getTitle(context), names, getQueryText(context), context.getForWhom());
}
@Override
public Prompt acceptInput(final @NotNull ConversationContext context, final String input) {
if (input == null) {
return null;
}
if (!input.equalsIgnoreCase(Lang.get("cmdCancel"))) {
final LinkedList<String> used = new LinkedList<String>();
final Action a = plugin.getAction(input);
if (a != null) {
for (final Quest quest : plugin.getLoadedQuests()) {
for (final Stage stage : quest.getStages()) {
if (stage.getFinishAction() != null
&& stage.getFinishAction().getName().equalsIgnoreCase(a.getName())) {
used.add(quest.getName());
break;
}
}
}
if (used.isEmpty()) {
context.setSessionData(CK.ED_EVENT_DELETE, a.getName());
return new ActionConfirmDeletePrompt(context);
} else {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorEventInUse")
+ " \"" + ChatColor.DARK_PURPLE + a.getName() + ChatColor.RED + "\":");
for (final String s : used) {
context.getForWhom().sendRawMessage(ChatColor.RED + "- " + ChatColor.DARK_RED + s);
}
context.getForWhom().sendRawMessage(ChatColor.RED
+ Lang.get("eventEditorMustModifyQuests"));
return new ActionSelectDeletePrompt(context);
}
}
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("eventEditorNotFound"));
return new ActionSelectDeletePrompt(context);
} else {
return new ActionMenuPrompt(context);
}
}
}
public class ActionConfirmDeletePrompt extends ActionsEditorStringPrompt {
public ActionConfirmDeletePrompt(final ConversationContext context) {
super (context);
}
private final int size = 2;
public int getSize() {
return size;
}
@Override
public String getTitle(final ConversationContext context) {
return null;
}
public ChatColor getNumberColor(final ConversationContext context, final int number) {
switch (number) {
case 1:
return ChatColor.GREEN;
case 2:
return ChatColor.RED;
default:
return null;
}
}
public String getSelectionText(final ConversationContext context, final int number) {
switch (number) {
case 1:
return ChatColor.GREEN + Lang.get("yesWord");
case 2:
return ChatColor.RED + Lang.get("noWord");
default:
return null;
}
}
@Override
public String getQueryText(final ConversationContext context) {
return Lang.get("confirmDelete");
}
@Override
public @NotNull String getPromptText(final @NotNull ConversationContext context) {
final ActionsEditorPostOpenStringPromptEvent event
= new ActionsEditorPostOpenStringPromptEvent(context, this);
plugin.getServer().getPluginManager().callEvent(event);
final StringBuilder text = new StringBuilder(ChatColor.RED + getQueryText(context) + " (" + ChatColor.YELLOW
+ context.getSessionData(CK.ED_EVENT_DELETE) + ChatColor.RED + ")\n");
for (int i = 1; i <= size; i++) {
text.append("\n").append(getNumberColor(context, i)).append(ChatColor.BOLD).append(i)
.append(ChatColor.RESET).append(" - ").append(getSelectionText(context, i));
}
return QuestsNumericPrompt.sendClickableSelection(text.toString(), context.getForWhom());
}
@Override
public Prompt acceptInput(final @NotNull ConversationContext context, final String input) {
if (input == null) {
return null;
}
if (input.equalsIgnoreCase("1") || input.equalsIgnoreCase(Lang.get("yesWord"))) {
plugin.getActionFactory().deleteAction(context);
return Prompt.END_OF_CONVERSATION;
} else if (input.equalsIgnoreCase("2") || input.equalsIgnoreCase(Lang.get("noWord"))) {
return new ActionMenuPrompt(context);
} else {
return new ActionConfirmDeletePrompt(context);
}
}
}
}