/
ChatbotTrait.java
60 lines (49 loc) · 1.69 KB
/
ChatbotTrait.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
package net.aufdemrand.denizen.npc.traits;
import java.io.File;
import net.citizensnpcs.api.ai.speech.SpeechContext;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import org.alicebot.ab.*;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
public class ChatbotTrait extends Trait {
// <--[language]
// @name ChatBot Trait
// @group NPC Traits
// @description
// A fully functional NPC AI Chat bot.
//
// Requires <@link url http://ci.citizensnpcs.co/job/AliceBot/> (ALICE Bot)
// In your /plugins/Denizen/lib folder.
//
// -->
public ChatbotTrait() {
super("chatbot");
}
static String path = Bukkit.getPluginManager().getPlugin("Denizen")
.getDataFolder() + File.separator;
@Persist("botname")
private String botname;
private Bot bot;
private Chat chatSession;
@Override
public void load(DataKey key) throws NPCLoadException {
setBot(key.getString("botname"));
}
public void chatTo(Entity entity, String input) {
SpeechContext context = new SpeechContext(response(input));
context.addRecipient(entity);
context.setTalker(getNPC().getEntity());
npc.getDefaultSpeechController().speak(context, "chat");
}
public String response(String request) {
return chatSession.multisentenceRespond(request);
}
public void setBot(String newname) {
botname = newname;
bot = new Bot(botname, path);
chatSession = new Chat(bot);
}
}