Skip to content

Commit 21e8a67

Browse files
committed
Added placeholders support for section mapping.
1 parent c97c1bf commit 21e8a67

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

src/main/java/me/zort/configurationlib/configuration/SectionNode.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.primitives.Primitives;
44
import me.zort.configurationlib.annotation.NodeName;
55
import me.zort.configurationlib.util.NodeTypeToken;
6+
import me.zort.configurationlib.util.Placeholders;
67

78
import java.lang.reflect.Field;
89
import java.lang.reflect.InvocationTargetException;
@@ -56,18 +57,26 @@ public void set(Object from) {
5657
}
5758

5859
public <T> T map(Class<T> typeClass) {
60+
return map(typeClass, new Placeholders());
61+
}
62+
63+
public <T> T map(Class<T> typeClass, Placeholders placeholders) {
5964
try {
6065
if(Primitives.isWrapperType(Primitives.wrap(typeClass))) {
6166
// We cannot map sections to primitive types since
6267
// sections are not leaf nodes.
6368
return null;
6469
}
65-
return map(typeClass.getDeclaredConstructor().newInstance());
70+
return map(typeClass.getDeclaredConstructor().newInstance(), placeholders);
6671
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
6772
throw new RuntimeException(e);
6873
}
6974
}
7075

76+
public <T> T map(T obj) {
77+
return map(obj, new Placeholders());
78+
}
79+
7180
/**
7281
* Tries top map this section to provided object. This method assigns values to the fields
7382
* according to the node types and rules specified by child classes.
@@ -76,7 +85,7 @@ public <T> T map(Class<T> typeClass) {
7685
* @return The mapped object.
7786
* @param <T> The type of the object to map to.
7887
*/
79-
public <T> T map(T obj) {
88+
public <T> T map(T obj, Placeholders placeholders) {
8089
Class<?> typeClass = obj.getClass();
8190
if(Primitives.isWrapperType(Primitives.wrap(typeClass))) {
8291
return null;
@@ -89,7 +98,7 @@ public <T> T map(T obj) {
8998
continue;
9099
}
91100
field.setAccessible(true);
92-
Object value = buildValue(field, node);
101+
Object value = buildValue(field, node, placeholders);
93102
if(value != null) {
94103
// Null values are skipped.
95104
try {
@@ -119,7 +128,7 @@ public <T> T map(T obj) {
119128
* @param node Node to build value from.
120129
* @return Value to assign to field.
121130
*/
122-
public Object buildValue(Field field, Node<L> node) {
131+
public Object buildValue(Field field, Node<L> node, Placeholders placeholders) {
123132
Object value = null;
124133
if(node instanceof SimpleNode && isPrimitive(field.getClass())) {
125134
value = ((SimpleNode<L>) node).get();

src/main/java/me/zort/configurationlib/configuration/bukkit/BukkitSectionNode.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.zort.configurationlib.configuration.SectionNode;
66
import me.zort.configurationlib.util.Colorizer;
77
import me.zort.configurationlib.util.ItemValidator;
8+
import me.zort.configurationlib.util.Placeholders;
89
import org.apache.commons.lang.ArrayUtils;
910
import org.bukkit.Bukkit;
1011
import org.bukkit.Material;
@@ -44,16 +45,16 @@ public void putSelf(ConfigurationSection location) {
4445
}
4546

4647
/**
47-
* @see SectionNode#buildValue(Field, Node)
48+
* @see SectionNode#buildValue(Field, Node, Placeholders)
4849
*/
4950
@Override
50-
public Object buildValue(Field field, Node<ConfigurationSection> node) {
51+
public Object buildValue(Field field, Node<ConfigurationSection> node, Placeholders placeholders) {
5152
// I'm specifying new field types for mapped objects.
5253
if(field.getType().equals(ItemStack.class) && node instanceof BukkitSectionNode) {
53-
return ((BukkitSectionNode) node).getAsItem();
54+
return ((BukkitSectionNode) node).getAsItem(placeholders);
5455
}
5556
// TODO: Add support for other types.
56-
return super.buildValue(field, node);
57+
return super.buildValue(field, node, placeholders);
5758
}
5859

5960
@Nullable
@@ -78,6 +79,11 @@ public boolean has(String path) {
7879

7980
@Nullable
8081
public ItemStack getAsItem() {
82+
return getAsItem(new Placeholders());
83+
}
84+
85+
@Nullable
86+
public ItemStack getAsItem(Placeholders placeholders) {
8187
if(!ItemValidator.validate(section)) {
8288
return null;
8389
}
@@ -103,8 +109,12 @@ public ItemStack getAsItem() {
103109
meta = item.getItemMeta();
104110
}
105111
if(meta != null) {
106-
if(section.contains("name")) meta.setDisplayName(Colorizer.colorize(section.getString("display-name")));
107-
if(section.contains("lore")) meta.setLore(Colorizer.colorize(section.getStringList("lore")));
112+
if(section.contains("name")) meta.setDisplayName(
113+
placeholders.replace(Colorizer.colorize(section.getString("display-name")))
114+
);
115+
if(section.contains("lore")) meta.setLore(
116+
placeholders.replace(Colorizer.colorize(section.getStringList("lore")))
117+
);
108118
if(section.contains("enchanted") && section.getBoolean("enchanted")) {
109119
meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
110120
meta.addItemFlags(ItemFlag.values());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package me.zort.configurationlib.util;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class Placeholders extends HashMap<String, Object> {
9+
10+
public static Placeholders of(String key, Object value) {
11+
return new Placeholders().and(key, value);
12+
}
13+
14+
public String replace(String s) {
15+
for(String placeholder : keySet()) {
16+
s = s.replaceAll(placeholder, getOrDefault(placeholder, "").toString());
17+
}
18+
return s;
19+
}
20+
21+
public List<String> replace(Iterable<String> iter) {
22+
List<String> result = new ArrayList<>();
23+
for(String s : iter) {
24+
result.add(replace(s));
25+
}
26+
return result;
27+
}
28+
29+
public Placeholders and(String placeholder, Object value) {
30+
put(placeholder, value);
31+
return this;
32+
}
33+
34+
public Placeholders and(Map<String, Object> placeholders) {
35+
this.putAll(placeholders);
36+
return this;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)