/
Dialogue.java
163 lines (132 loc) · 4.2 KB
/
Dialogue.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
package com.nthbyte.dialogue;
import com.nthbyte.dialogue.action.Action;
import com.nthbyte.dialogue.action.context.ActionContext;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Object that represents dialogue between the plugin and a player.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.5.0
*/
public class Dialogue {
/**
* The sequence that you have to type in order to quit the dialogue.
*/
private String[] escapeSequences;
private List<Prompt> prompts;
/**
* Actions that are ran when the dialogue ends.
*/
private LinkedHashMap<Action.BasePromptAction, ActionContext> endActions;
private int currentIndexPrompt = 0;
/**
* Repeats the prompt if the input was invalid.
*/
private boolean repeatPrompt;
/**
* How much time the user has to complete the dialogue (in seconds).
*/
private int timeLimit;
private Dialogue(){}
private Dialogue(Dialogue.Builder builder) {
this.prompts = builder.prompts;
this.endActions = builder.endActions;
this.escapeSequences = builder.escapeSequences;
this.repeatPrompt = builder.repeatPrompt;
this.timeLimit = builder.timeLimit;
}
/**
* Gets the current prompt this dialogue is on.
*
* @return The current prompt.
*/
public Prompt getCurrentPrompt() {
return prompts.get(currentIndexPrompt);
}
/**
* Whether the dialogue has more prompts.
*
* @return If the dialogue has more prompts.
*/
public boolean hasMorePrompts() {
return currentIndexPrompt != prompts.size() - 1;
}
/**
* Prompts the next prompt to the player.
*
* @param player The player we are prompting.
*/
public void nextPrompt(JavaPlugin plugin, Player player) {
currentIndexPrompt++;
getCurrentPrompt().prompt(plugin, this, player);
}
public Map<Action.BasePromptAction, ActionContext> getEndActions() {
return endActions;
}
public String[] getEscapeSequences() {
return escapeSequences;
}
public int getCurrentIndexPrompt() {
return currentIndexPrompt;
}
public boolean shouldRepeatPrompt() {
return repeatPrompt;
}
public int getTimeLimit() {
return timeLimit;
}
public static class Builder {
private boolean repeatPrompt = true;
private String[] escapeSequences = new String[]{""};
private List<Prompt> prompts = new ArrayList<>();
private LinkedHashMap<Action.BasePromptAction, ActionContext> endActions = new LinkedHashMap<>();
private int timeLimit;
public Builder() {}
public Builder addPrompt(Prompt.Builder prompt) {
this.prompts.add(prompt.build());
return this;
}
public Builder setTimeLimit(int timeLimit){
this.timeLimit = timeLimit;
return this;
}
public Builder setEscapeSequences(String... escapeSequences) {
this.escapeSequences = escapeSequences;
return this;
}
public Builder setRepeatPrompt(boolean repeatPrompt) {
this.repeatPrompt = repeatPrompt;
return this;
}
/**
* Using a default action.
*
* @param defaultAction A default action.
* @param context The endActionContext for the action.
* @return The builder.
* @see Action
*/
public <U extends ActionContext> Builder addEndAction(Action.DefaultAction<U> defaultAction, U context) {
this.endActions.put(defaultAction, context);
return this;
}
/**
* Defines your own end action.
*
* @param action Your action.
* @return The builder.
*/
public <T extends ActionContext> Builder addEndAction(Action.EndAction<T> action) {
this.endActions.put(action, null);
return this;
}
public Dialogue build() {
return new Dialogue(this);
}
}
}