-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
TalkableEntity.java
93 lines (80 loc) · 3.04 KB
/
TalkableEntity.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
package net.citizensnpcs.npc.ai.speech;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.ai.speech.Talkable;
import net.citizensnpcs.api.ai.speech.VocalChord;
import net.citizensnpcs.api.ai.speech.event.SpeechBystanderEvent;
import net.citizensnpcs.api.ai.speech.event.SpeechTargetedEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
public class TalkableEntity implements Talkable {
Entity entity;
public TalkableEntity(Entity entity) {
this.entity = entity;
}
public TalkableEntity(NPC npc) {
entity = npc.getEntity();
}
public TalkableEntity(Player player) {
entity = player;
}
/**
* Used to compare a LivingEntity to this TalkableEntity
*
* @return 0 if the Entities are the same, 1 if they are not, -1 if the object compared is not a valid LivingEntity
*/
@Override
public int compareTo(Object o) {
// If not living entity, return -1
if (!(o instanceof Entity)) {
return -1;
// If NPC and matches, return 0
} else if (CitizensAPI.getNPCRegistry().isNPC((Entity) o) && CitizensAPI.getNPCRegistry().isNPC(entity)
&& CitizensAPI.getNPCRegistry().getNPC((Entity) o).getUniqueId()
.equals(CitizensAPI.getNPCRegistry().getNPC(entity).getUniqueId())) {
return 0;
} else if (entity.equals(o)) {
return 0;
} else {
return 1;
}
}
@Override
public Entity getEntity() {
return entity;
}
@Override
public String getName() {
if (CitizensAPI.getNPCRegistry().isNPC(entity)) {
return CitizensAPI.getNPCRegistry().getNPC(entity).getName();
} else if (entity instanceof Player) {
return ((Player) entity).getName();
} else {
return entity.getType().name().replace("_", " ");
}
}
private void talk(String message) {
if (entity instanceof Player && !CitizensAPI.getNPCRegistry().isNPC(entity)) {
Messaging.send((Player) entity, message);
}
}
@Override
public void talkNear(SpeechContext context, String text, VocalChord vocalChord) {
SpeechBystanderEvent event = new SpeechBystanderEvent(this, context, text, vocalChord);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled())
return;
talk(event.getMessage());
}
@Override
public void talkTo(SpeechContext context, String text, VocalChord vocalChord) {
SpeechTargetedEvent event = new SpeechTargetedEvent(this, context, text, vocalChord);
Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled())
return;
talk(event.getMessage());
}
}