Added default text in config file. This addresses CITIZENS-28.

This commit is contained in:
aPunch 2012-03-15 03:30:40 -05:00
parent bc01012701
commit a37716ebeb
2 changed files with 26 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package net.citizensnpcs;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Storage;
@ -8,7 +10,7 @@ import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.util.Messaging;
public class Settings {
private final Storage config;
private static Storage config;
public Settings(File folder) {
config = new YamlStorage(folder + File.separator + "config.yml", "Citizens Configuration");
@ -40,6 +42,7 @@ public class Settings {
DEFAULT_LOOK_CLOSE("npc.default.look-close", false),
DEFAULT_RANDOM_TALKER("npc.default.random-talker", true),
DEFAULT_TALK_CLOSE("npc.default.talk-close", false),
DEFAULT_TEXT("npc.default.text.0", "Hi, I'm <npc>!"),
QUICK_SELECT("npc.selection.quick-select", false),
SELECTION_ITEM("npc.selection.item", "280"),
SELECTION_MESSAGE("npc.selection.message", "<b>You selected <a><npc><b>!"),
@ -56,6 +59,13 @@ public class Settings {
this.value = value;
}
public List<String> asList() {
List<String> list = new ArrayList<String>();
for (DataKey key : config.getKey("npc.default.text").getIntegerSubKeys())
list.add(key.getString(""));
return list;
}
public boolean asBoolean() {
return (Boolean) value;
}

View File

@ -47,6 +47,8 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba
public void load(DataKey key) throws NPCLoadException {
for (DataKey sub : key.getIntegerSubKeys())
text.add(sub.getString(""));
if (text.isEmpty())
populateDefaultText();
if (key.keyExists("talk-close"))
talkClose = key.getBoolean("talk-close");
@ -94,6 +96,12 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba
}
}
@Override
public void onNPCSpawn() {
if (text.isEmpty())
populateDefaultText();
}
@Override
public void conversationAbandoned(ConversationAbandonedEvent event) {
Bukkit.dispatchCommand((Player) event.getContext().getForWhom(), "npc text");
@ -105,8 +113,8 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba
public Editor getEditor(final Player player) {
final Conversation conversation = new ConversationFactory(plugin).addConversationAbandonedListener(this)
.withLocalEcho(false).withEscapeSequence("/npc text").withModality(false)
.withFirstPrompt(new StartPrompt(this)).buildConversation(player);
.withLocalEcho(false).withEscapeSequence("/npc text").withModality(false).withFirstPrompt(
new StartPrompt(this)).buildConversation(player);
return new Editor() {
@Override
@ -171,6 +179,11 @@ public class Text extends Trait implements Runnable, Toggleable, ConversationAba
return randomTalker;
}
private void populateDefaultText() {
for (String line : Setting.DEFAULT_TEXT.asList())
text.add(line);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();