/
SpeechContext.java
162 lines (141 loc) · 4.22 KB
/
SpeechContext.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
package net.citizensnpcs.api.ai.speech;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import net.citizensnpcs.api.ai.speech.event.NPCSpeechEvent;
import net.citizensnpcs.api.npc.NPC;
/**
* SpeechContext contains information about a {@link NPCSpeechEvent}, including the {@link Talkable} talker, recipients,
* and message.
*
*/
public class SpeechContext implements Iterable<Talkable> {
private String message;
private List<Talkable> recipients = Collections.emptyList();
private Talkable talker = null;
public SpeechContext() {
// Must set talker/message (and recipients, if any)
// with supplied methods.
}
public SpeechContext(NPC talker, String message) {
if (talker != null)
setTalker(talker.getEntity());
this.message = message;
}
public SpeechContext(NPC talker, String message, LivingEntity recipient) {
this(talker, message);
if (recipient != null) {
addRecipient(recipient);
}
}
public SpeechContext(String message) {
this.message = message;
}
public SpeechContext(String message, LivingEntity recipient) {
this.message = message;
if (recipient != null)
addRecipient(recipient);
}
/**
* Adds a direct {@link Talkable} recipient. The {@link VocalChord} should use this information to correctly direct
* the message. Note: depending on the VocalChord, this list may not be inclusive as to who gets the message.
*
* @param entity
* Talkable entity
* @return the speech context
*
*/
public SpeechContext addRecipient(Entity entity) {
if (recipients.isEmpty())
recipients = new ArrayList<Talkable>();
recipients.add(new TalkableEntity(entity));
return this;
}
@Deprecated
public SpeechContext addRecipient(LivingEntity entity) {
return addRecipient((Entity) entity);
}
/**
* Adds a list of {@link Talkable} recipients. The {@link VocalChord} should use this information to correctly
* direct the message. Note: depending on the VocalChord, this list may not be inclusive as to who gets the message.
*
* @param talkables
* Talkable entities
* @return the Tongue
*
*/
public SpeechContext addRecipients(List<Talkable> talkables) {
if (recipients.isEmpty())
recipients = new ArrayList<Talkable>();
recipients.addAll(talkables);
return this;
}
/**
* Gets the text message sent.
*
*/
public String getMessage() {
return message;
}
/**
* Gets the talker.
*
* @return NPC doing the talking
*
*/
public Talkable getTalker() {
return talker;
}
/**
* Checks if there are any recipients. If none, this {@link SpeechContext} is not targeted.
*
* @return true if recipients are specified.
*/
public boolean hasRecipients() {
return (recipients.isEmpty()) ? false : true;
}
/**
* Gets direct recipients, if any.
*
* @return recipients Iterator
*
*/
@Override
public Iterator<Talkable> iterator() {
final Iterator<Talkable> itr = recipients.iterator();
return itr;
}
/**
* Sets the text message sent. Overrides text set with the constructor.
*
* @param message
* The text to send.
*
*/
public void setMessage(String message) {
this.message = message;
}
/**
* Sets the talker.
*
* @param entity
* NPC doing the talking
*
*/
public void setTalker(Entity entity) {
this.talker = new TalkableEntity(entity);
}
@Deprecated
public void setTalker(LivingEntity entity) {
setTalker((Entity) entity);
}
/**
* @return number of recipients.
*/
public int size() {
return recipients.size();
}
}