/
SettingsQuestion.java
86 lines (66 loc) · 2.98 KB
/
SettingsQuestion.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
package net.bdew.wurm.customnpc.config;
import com.wurmonline.server.creatures.Communicator;
import com.wurmonline.server.creatures.Creature;
import com.wurmonline.server.questions.Question;
import net.bdew.wurm.customnpc.CustomAIData;
import net.bdew.wurm.customnpc.CustomNpcMod;
import org.gotti.wurmunlimited.modsupport.questions.ModQuestion;
import org.gotti.wurmunlimited.modsupport.questions.ModQuestions;
import java.util.Properties;
public class SettingsQuestion implements ModQuestion {
private final Creature responder, npc;
private final CustomAIData data;
public SettingsQuestion(Creature responder, Creature npc) {
this.responder = responder;
this.npc = npc;
data = (CustomAIData) npc.getCreatureAIData();
}
@Override
public void sendQuestion(Question question) {
final StringBuilder buf = new StringBuilder(ModQuestions.getBmlHeader(question));
buf.append("harray{label{text='Name:'};input{id='name'; text=\"").append(npc.getName()).append("\";maxchars='50'}}");
buf.append("text{text=''}");
buf.append("harray{label{text='Sex:'};");
buf.append("radio{group='sex';id='0';text='Male';").append(npc.getSex() == 0 ? "selected='true'" : "").append("}");
buf.append("radio{group='sex';id='1';text='Female';").append(npc.getSex() == 1 ? "selected='true'" : "").append("}");
buf.append("}");
buf.append("text{text=''}");
data.getConfig().getMovementScript().configBML(npc, buf);
buf.append("text{text=''}");
buf.append(ModQuestions.createAnswerButton2(question, "Save"));
question.getResponder().getCommunicator().sendBml(250, 400, true, true, buf.toString(), 200, 200, 200, question.getTitle());
}
@Override
public void answer(Question question, Properties answers) {
Communicator comm = responder.getCommunicator();
boolean changed = false;
String newName = answers.getProperty("name");
if (newName != null && newName.length() > 0 && !newName.equals(npc.getName())) {
try {
changed = true;
npc.setVisible(false);
npc.setName(newName);
data.getConfig().setName(newName);
data.configUpdated();
} catch (Exception e) {
CustomNpcMod.logException("Error setting name", e);
}
}
byte newSex = Byte.parseByte(answers.getProperty("sex"));
if (newSex != npc.getSex()) {
if (!changed) {
changed = true;
npc.setVisible(false);
}
npc.setSex(newSex);
}
if (changed) {
npc.setVisible(true);
}
data.getConfig().getMovementScript().processConfig(npc, answers);
data.configUpdated();
}
public static void send(Creature player, Creature npc) {
ModQuestions.createQuestion(player, "NPC Settings", "", -10, new SettingsQuestion(player, npc)).sendQuestion();
}
}