Fix text setting loading to allow empty text

This commit is contained in:
fullwall 2024-08-18 22:03:35 +08:00
parent 2caa1b360a
commit 8ee9ecb5ed

View File

@ -4,9 +4,13 @@ import java.io.File;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.bukkit.configuration.ConfigurationSection;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
@ -152,15 +156,27 @@ public class Settings {
"npc.pathfinding.default-stuck-action", "none"), "npc.pathfinding.default-stuck-action", "none"),
DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false), DEFAULT_TALK_CLOSE("npc.default.talk-close.enabled", false),
DEFAULT_TALK_CLOSE_RANGE("Default talk close range in blocks", "npc.default.talk-close.range", 5), DEFAULT_TALK_CLOSE_RANGE("Default talk close range in blocks", "npc.default.talk-close.range", 5),
DEFAULT_TEXT("npc.default.talk-close.text.0", "Hi, I'm <npc>!") { DEFAULT_TEXT("npc.default.talk-close.text", "Hi, I'm <npc>!") {
@SuppressWarnings("unchecked")
@Override @Override
public void loadFromKey(DataKey root) { public void loadFromKey(DataKey root) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (DataKey key : root.getRelative("npc.default.talk-close.text").getSubKeys()) { Object raw = root.getRaw(path);
if (raw instanceof ConfigurationSection || raw instanceof Map) {
for (DataKey key : root.getRelative(path).getSubKeys()) {
list.add(key.getString("")); list.add(key.getString(""));
} }
} else if (raw instanceof Collection) {
list.addAll((Collection<? extends String>) raw);
}
value = list; value = list;
} }
@Override
protected void setAtKey(DataKey root) {
root.setRaw(path, Lists.newArrayList(value));
setComments(root);
}
}, },
DEFAULT_TEXT_DELAY_MAX("Default maximum delay when talking to players", DEFAULT_TEXT_DELAY_MAX("Default maximum delay when talking to players",
"npc.text.default-random-text-delay-max", "10s"), "npc.text.default-random-text-delay-max", "10s"),
@ -283,7 +299,7 @@ public class Settings {
private String comments; private String comments;
private Duration duration; private Duration duration;
private String migrate; private String migrate;
private final String path; protected final String path;
protected Object value; protected Object value;
Setting(String path, Object value) { Setting(String path, Object value) {
@ -367,8 +383,9 @@ public class Settings {
setComments(root); setComments(root);
} }
private void setComments(DataKey root) { protected void setComments(DataKey root) {
if (SUPPORTS_SET_COMMENTS && root.keyExists(path)) { if (!SUPPORTS_SET_COMMENTS || !root.keyExists(path))
return;
try { try {
((MemoryDataKey) root).getSection("").setComments(path, ((MemoryDataKey) root).getSection("").setComments(path,
comments == null ? null : Arrays.asList(comments.split("<br>"))); comments == null ? null : Arrays.asList(comments.split("<br>")));
@ -377,7 +394,6 @@ public class Settings {
} }
} }
} }
}
private static boolean SUPPORTS_SET_COMMENTS = true; private static boolean SUPPORTS_SET_COMMENTS = true;
} }