-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
NpcOfferQuestPrompt.java
245 lines (225 loc) · 10.5 KB
/
NpcOfferQuestPrompt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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.misc;
import me.blackvein.quests.Quest;
import me.blackvein.quests.Quests;
import me.blackvein.quests.events.misc.MiscPostNpcOfferQuestEvent;
import me.blackvein.quests.player.IQuester;
import me.blackvein.quests.quests.IQuest;
import me.blackvein.quests.util.Lang;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.text.MessageFormat;
import java.util.Comparator;
import java.util.LinkedList;
public class NpcOfferQuestPrompt extends MiscStringPrompt {
private ConversationContext context;
private final Quests plugin;
public NpcOfferQuestPrompt(final Quests plugin) {
super(null);
this.plugin = plugin;
}
public NpcOfferQuestPrompt(final ConversationContext context) {
super(context);
this.plugin = (Quests)context.getPlugin();
}
@Override
public ConversationContext getConversationContext() {
return context;
}
private int size = 3;
@Override
public int getSize() {
return size;
}
@Override
public String getTitle(final ConversationContext context) {
final String npc = (String) context.getSessionData("npc");
return Lang.get("questNPCListTitle").replace("<npc>", npc != null ? npc : "NPC");
}
@SuppressWarnings("unchecked")
public ChatColor getNumberColor(final ConversationContext context, final int number) {
final LinkedList<IQuest> quests = (LinkedList<IQuest>) context.getSessionData("npcQuests");
if (plugin != null) {
final IQuester quester = plugin.getQuester(((Player) context.getForWhom()).getUniqueId());
if (quests != null && number > 0) {
if (number < (quests.size() + 1)) {
final IQuest quest = quests.get(number - 1);
if (quester.getCompletedQuestsTemp().contains(quest)) {
return ChatColor.GREEN;
} else {
return ChatColor.GOLD;
}
} else if (number == (quests.size() + 1)) {
//return ChatColor.RED;
return ChatColor.GOLD;
}
}
}
return null;
}
@SuppressWarnings("unchecked")
public String getSelectionText(final ConversationContext context, final int number) {
final LinkedList<IQuest> quests = (LinkedList<IQuest>) context.getSessionData("npcQuests");
if (plugin != null) {
final IQuester quester = plugin.getQuester(((Player) context.getForWhom()).getUniqueId());
if (quests != null && number > 0) {
if (number < (quests.size() + 1)) {
final IQuest quest = quests.get(number - 1);
if (quester.getCompletedQuestsTemp().contains(quest)) {
return ChatColor.GREEN + "" + ChatColor.ITALIC + quest.getName();
} else {
return ChatColor.YELLOW + "" + ChatColor.ITALIC + quest.getName();
}
} else if (number == (quests.size() + 1)) {
return ChatColor.GRAY + Lang.get("cancel");
}
}
}
return null;
}
@SuppressWarnings("unchecked")
public String getAdditionalText(final ConversationContext context, final int number) {
final LinkedList<IQuest> quests = (LinkedList<IQuest>) context.getSessionData("npcQuests");
if (plugin != null) {
final IQuester quester = plugin.getQuester(((Player) context.getForWhom()).getUniqueId());
if (quests != null && number > 0) {
if (number < (quests.size() + 1)) {
final IQuest quest = quests.get(number - 1);
if (quester.getCompletedQuestsTemp().contains(quest)) {
return ChatColor.GREEN + "" + Lang.get("redoCompleted");
}
}
}
}
return "";
}
public String getQueryText(final ConversationContext context) {
return Lang.get("enterAnOption");
}
@SuppressWarnings("unchecked")
@Override
public @NotNull String getPromptText(final ConversationContext context) {
this.context = context;
final LinkedList<Quest> quests = (LinkedList<Quest>) context.getSessionData("npcQuests");
final String npc = (String) context.getSessionData("npc");
if (plugin == null || quests == null || npc == null) {
return ChatColor.YELLOW + Lang.get("itemCreateCriticalError");
}
quests.sort(Comparator.comparing(Quest::getName));
final MiscPostNpcOfferQuestEvent event = new MiscPostNpcOfferQuestEvent(context, this);
plugin.getServer().getPluginManager().callEvent(event);
if (!(context.getForWhom() instanceof Player) || !plugin.getSettings().canClickablePrompts()) {
final StringBuilder text = new StringBuilder(ChatColor.WHITE + getTitle(context));
size = quests.size();
for (int i = 1; i <= size + 1; i++) {
text.append("\n").append(getNumberColor(context, i)).append(ChatColor.BOLD).append(i).append(". ")
.append(ChatColor.RESET).append(getSelectionText(context, i)).append(" ")
.append(getAdditionalText(context, i));
}
text.append("\n").append(ChatColor.WHITE).append(getQueryText(context));
return text.toString();
}
final TextComponent component = new TextComponent(getTitle(context));
component.setColor(net.md_5.bungee.api.ChatColor.WHITE);
size = quests.size();
final TextComponent line = new TextComponent("");
for (int i = 1; i <= size + 1; i++) {
final TextComponent choice = new TextComponent("\n" + getNumberColor(context, i) + ChatColor.BOLD + i + ". "
+ ChatColor.RESET + getSelectionText(context, i));
choice.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/quests choice " + i));
if (plugin.getSettings().canShowQuestReqs() && i <= size) {
choice.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(quests.get(i - 1).getDescription()).create()));
}
line.addExtra(choice);
line.addExtra(getAdditionalText(context, i));
}
component.addExtra(line);
component.addExtra("\n" + ChatColor.WHITE + getQueryText(context));
((Player)context.getForWhom()).spigot().sendMessage(component);
return "";
}
@SuppressWarnings("unchecked")
@Override
public Prompt acceptInput(final ConversationContext context, final String input) {
final LinkedList<IQuest> quests = (LinkedList<IQuest>) context.getSessionData("npcQuests");
if (plugin == null || quests == null) {
return Prompt.END_OF_CONVERSATION;
}
final IQuester quester = plugin.getQuester(((Player) context.getForWhom()).getUniqueId());
int numInput = -1;
try {
numInput = Integer.parseInt(input);
} catch (final NumberFormatException e) {
// Continue
}
if (input.equalsIgnoreCase(Lang.get("cancel")) || numInput == (quests.size() + 1)) {
context.getForWhom().sendRawMessage(ChatColor.YELLOW + Lang.get("cancelled"));
return Prompt.END_OF_CONVERSATION;
} else {
IQuest q = null;
for (final IQuest quest : quests) {
if (quest.getName().equalsIgnoreCase(input)) {
q = quest;
break;
}
}
if (q == null) {
for (final IQuest quest : quests) {
if (numInput == (quests.indexOf(quest) + 1)) {
q = quest;
break;
}
}
}
if (q == null) {
for (final IQuest quest : quests) {
if (quest.getName().toLowerCase().contains(input.toLowerCase())) {
q = quest;
break;
}
}
}
if (q == null) {
context.getForWhom().sendRawMessage(ChatColor.RED + Lang.get("invalidOption"));
return new NpcOfferQuestPrompt(context);
} else {
final Player player = quester.getPlayer();
if (quester.canAcceptOffer(q, true)) {
quester.setQuestIdToTake(q.getId());
for (final String msg : extracted(plugin, quester).split("<br>")) {
player.sendMessage(msg);
}
if (!plugin.getSettings().canConfirmAccept()) {
quester.takeQuest(q, false);
} else {
plugin.getConversationFactory().buildConversation(player).begin();
}
}
return Prompt.END_OF_CONVERSATION;
}
}
}
private String extracted(final Quests plugin, final IQuester quester) {
final IQuest quest = plugin.getQuestByIdTemp(quester.getQuestIdToTake());
return MessageFormat.format("{0}- {1}{2}{3} -\n\n{4}{5}\n", ChatColor.GOLD, ChatColor.DARK_PURPLE,
quest.getName(), ChatColor.GOLD, ChatColor.RESET, quest.getDescription());
}
}