Skip to content

Commit

Permalink
getValuesDeep now removes all configurationsections from output
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Dec 11, 2022
1 parent 87de4d4 commit bf4186e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/main/java/net/citizensnpcs/api/util/DataKey.java
@@ -1,5 +1,8 @@
package net.citizensnpcs.api.util;

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Map;

import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -132,6 +135,24 @@ public boolean keyExists() {

public abstract void removeKey(String key);

protected Map<String, Object> sectionToValues(ConfigurationSection section) {
if (section == null)
return Collections.emptyMap();
Map<String, Object> object = section.getValues(false);
Deque<Map<String, Object>> queue = new ArrayDeque<>();
queue.add(object);
while (!queue.isEmpty()) {
for (Map.Entry<String, Object> entry : queue.pollLast().entrySet()) {
if (entry.getValue() instanceof ConfigurationSection) {
Map<String, Object> values = ((ConfigurationSection) entry.getValue()).getValues(false);
entry.setValue(values);
queue.add(values);
}
}
}
return object;
}

public abstract void setBoolean(String key, boolean value);

public abstract void setDouble(String key, double value);
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/net/citizensnpcs/api/util/MemoryDataKey.java
Expand Up @@ -111,18 +111,12 @@ public DataKey apply(String input) {

@Override
public Map<String, Object> getValuesDeep() {
ConfigurationSection section = root.getConfigurationSection(path);
if (section == null)
return Collections.emptyMap();
return section.getValues(true);
return sectionToValues(root);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
return 31 + ((path == null) ? 0 : path.hashCode());
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/citizensnpcs/api/util/Messaging.java
Expand Up @@ -186,7 +186,9 @@ private static void sendMessageTo(CommandSender sender, String rawMessage, boole
rawMessage = Placeholders.replace(rawMessage, (Player) sender);
}
for (String message : CHAT_NEWLINE_SPLITTER.split(rawMessage)) {
message = prettify(message);
if (messageColor) {
message = prettify(message);
}
if (AUDIENCES != null) {
AUDIENCES.sender(sender).sendMessage(MINIMESSAGE.deserialize(convertLegacyCodes(message)));
} else {
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/net/citizensnpcs/api/util/NBTStorage.java
Expand Up @@ -292,7 +292,6 @@ public Iterable<DataKey> getSubKeys() {
return subKeys;
}

@Override
public Map<String, Object> getValuesDeep() {
Tag tag = findLastTag(path, false);
if (!(tag instanceof CompoundTag))
Expand All @@ -315,11 +314,7 @@ public Map<String, Object> getValuesDeep() {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
return 31 * (31 + getOuterType().hashCode()) + ((path == null) ? 0 : path.hashCode());
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/net/citizensnpcs/api/util/YamlStorage.java
Expand Up @@ -266,11 +266,9 @@ public Iterable<DataKey> getSubKeys() {
return res;
}

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getValuesDeep() {
ConfigurationSection subSection = config.getConfigurationSection(path);
return (Map<String, Object>) (subSection == null ? Collections.emptyMap() : subSection.getValues(true));
return sectionToValues(config.getConfigurationSection(path));
}

@Override
Expand Down

0 comments on commit bf4186e

Please sign in to comment.