Skip to content

Commit ff19645

Browse files
GenSecretsmdcfeJRoy
authored
Add kits subfolder to allow for multiple kit files (EssentialsX#4407)
Co-authored-by: MD <[email protected]> Co-authored-by: Josh Roy <[email protected]>
1 parent 73575d2 commit ff19645

File tree

7 files changed

+87
-58
lines changed

7 files changed

+87
-58
lines changed

Essentials/src/main/java/com/earth2me/essentials/EssentialsUpgrade.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public void convertIgnoreList() {
438438

439439
public void convertKits() {
440440
final Kits kits = ess.getKits();
441-
final EssentialsConfiguration config = kits.getConfig();
441+
final EssentialsConfiguration config = kits.getRootConfig();
442442
if (doneFile.getBoolean("kitsyml", false)) {
443443
return;
444444
}

Essentials/src/main/java/com/earth2me/essentials/Kits.java

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,106 @@
44
import com.earth2me.essentials.config.EssentialsConfiguration;
55
import com.earth2me.essentials.utils.NumberUtil;
66
import org.spongepowered.configurate.CommentedConfigurationNode;
7-
import org.spongepowered.configurate.serialize.SerializationException;
87

98
import java.io.File;
109
import java.math.BigDecimal;
10+
import java.util.HashMap;
1111
import java.util.List;
1212
import java.util.Locale;
1313
import java.util.Map;
14+
import java.util.Set;
1415

1516
import static com.earth2me.essentials.I18n.capitalCase;
1617
import static com.earth2me.essentials.I18n.tl;
1718

1819
public class Kits implements IConf {
19-
private final EssentialsConfiguration config;
20-
private CommentedConfigurationNode kits;
20+
private final IEssentials ess;
21+
private final EssentialsConfiguration rootConfig;
22+
private final Map<String, EssentialsConfiguration> kitToConfigMap = new HashMap<>();
23+
private final Map<String, Map<String, Object>> kitMap = new HashMap<>();
2124

2225
public Kits(final IEssentials essentials) {
23-
config = new EssentialsConfiguration(new File(essentials.getDataFolder(), "kits.yml"), "/kits.yml");
26+
this.ess = essentials;
27+
this.rootConfig = new EssentialsConfiguration(new File(essentials.getDataFolder(), "kits.yml"), "/kits.yml");
2428

2529
reloadConfig();
2630
}
2731

2832
@Override
2933
public void reloadConfig() {
30-
config.load();
31-
kits = _getKits();
34+
rootConfig.load();
35+
parseKits();
3236
}
3337

3438
public File getFile() {
35-
return config.getFile();
39+
return rootConfig.getFile();
3640
}
3741

38-
private CommentedConfigurationNode _getKits() {
39-
final CommentedConfigurationNode section = config.getSection("kits");
40-
if (section != null) {
41-
final CommentedConfigurationNode newSection = config.newSection();
42-
for (final String kitItem : ConfigurateUtil.getKeys(section)) {
43-
final CommentedConfigurationNode kitSection = section.node(kitItem);
44-
if (kitSection.isMap()) {
45-
try {
46-
newSection.node(kitItem.toLowerCase(Locale.ENGLISH)).set(kitSection);
47-
} catch (SerializationException e) {
48-
e.printStackTrace();
42+
private void parseKit(final String kitName, final CommentedConfigurationNode kitSection, final EssentialsConfiguration parentConfig) {
43+
if (kitSection.isMap()) {
44+
final String effectiveKitName = kitName.toLowerCase(Locale.ENGLISH);
45+
kitToConfigMap.put(effectiveKitName, parentConfig);
46+
kitMap.put(effectiveKitName, ConfigurateUtil.getRawMap(kitSection));
47+
}
48+
}
49+
50+
private void parseKits() {
51+
kitToConfigMap.clear();
52+
kitMap.clear();
53+
54+
// Kits from kits.yml file
55+
final CommentedConfigurationNode fileKits = rootConfig.getSection("kits");
56+
if (fileKits != null) {
57+
for (final Map.Entry<String, CommentedConfigurationNode> kitEntry : ConfigurateUtil.getMap(fileKits).entrySet()) {
58+
parseKit(kitEntry.getKey(), kitEntry.getValue(), rootConfig);
59+
}
60+
}
61+
62+
// Kits from kits subdirectory
63+
final File kitsFolder = new File(this.ess.getDataFolder(), "kits");
64+
if (!kitsFolder.exists() || !kitsFolder.isDirectory()) {
65+
return;
66+
}
67+
68+
final File[] kitsFiles = kitsFolder.listFiles();
69+
70+
//noinspection ConstantConditions - will not be null, conditions checked above.
71+
for (final File kitFile : kitsFiles) {
72+
if (kitFile.getName().endsWith(".yml")) {
73+
final EssentialsConfiguration kitConfig = new EssentialsConfiguration(kitFile);
74+
kitConfig.load();
75+
final CommentedConfigurationNode kits = kitConfig.getSection("kits");
76+
if (kits != null) {
77+
for (final Map.Entry<String, CommentedConfigurationNode> kitEntry : ConfigurateUtil.getMap(kits).entrySet()) {
78+
parseKit(kitEntry.getKey(), kitEntry.getValue(), kitConfig);
4979
}
5080
}
5181
}
52-
return newSection;
5382
}
54-
return null;
5583
}
5684

57-
public EssentialsConfiguration getConfig() {
58-
return config;
85+
/**
86+
* Should be used for EssentialsUpgrade conversions <b>only</b>.
87+
*/
88+
public EssentialsConfiguration getRootConfig() {
89+
return rootConfig;
5990
}
6091

61-
public CommentedConfigurationNode getKits() {
62-
return kits;
92+
public Set<String> getKitKeys() {
93+
return kitMap.keySet();
6394
}
6495

65-
public Map<String, Object> getKit(String name) {
66-
name = name.replace('.', '_').replace('/', '_');
67-
if (getKits() != null) {
68-
final CommentedConfigurationNode kits = getKits();
69-
// Other parts of the codebase/3rd party plugins expect us to lowercase kit names here.
70-
// This isn't strictly needed for the future of Essentials, but for compatibility it's here.
71-
final CommentedConfigurationNode kitSection = kits.node(name.toLowerCase());
72-
if (!kitSection.virtual() && kitSection.isMap()) {
73-
return ConfigurateUtil.getRawMap(kitSection);
74-
}
96+
public Map<String, Object> getKit(final String name) {
97+
if (name != null) {
98+
return kitMap.get(name.replace('.', '_').replace('/', '_'));
7599
}
76-
77100
return null;
78101
}
79102

80103
// Tries to find an existing kit name that matches the given name, ignoring case. Returns null if no match.
81104
public String matchKit(final String name) {
82-
final CommentedConfigurationNode section = config.getSection("kits");
83-
if (section != null) {
84-
for (final String kitName : ConfigurateUtil.getKeys(section)) {
105+
if (name != null) {
106+
for (final String kitName : kitMap.keySet()) {
85107
if (kitName.equalsIgnoreCase(name)) {
86108
return kitName;
87109
}
@@ -90,25 +112,32 @@ public String matchKit(final String name) {
90112
return null;
91113
}
92114

93-
public void addKit(final String name, final List<String> lines, final long delay) {
115+
public void addKit(String name, final List<String> lines, final long delay) {
116+
name = name.replace('.', '_').replace('/', '_').toLowerCase(Locale.ENGLISH);
94117
// Will overwrite but w/e
95-
config.setProperty("kits." + name + ".delay", delay);
96-
config.setProperty("kits." + name + ".items", lines);
97-
kits = _getKits();
98-
config.save();
118+
rootConfig.setProperty("kits." + name + ".delay", delay);
119+
rootConfig.setProperty("kits." + name + ".items", lines);
120+
parseKits();
121+
rootConfig.save();
99122
}
100123

101-
public void removeKit(final String name) {
124+
public void removeKit(String name) {
125+
name = name.replace('.', '_').replace('/', '_').toLowerCase(Locale.ENGLISH);
126+
if (!kitToConfigMap.containsKey(name) || !kitMap.containsKey(name)) {
127+
return;
128+
}
129+
130+
final EssentialsConfiguration config = kitToConfigMap.get(name);
102131
config.removeProperty("kits." + name);
103-
kits = _getKits();
104-
config.save();
132+
133+
config.blockingSave();
134+
parseKits();
105135
}
106136

107137
public String listKits(final net.ess3.api.IEssentials ess, final User user) throws Exception {
108138
try {
109-
final CommentedConfigurationNode kits = config.getSection("kits");
110139
final StringBuilder list = new StringBuilder();
111-
for (final String kitItem : ConfigurateUtil.getKeys(kits)) {
140+
for (final String kitItem : kitMap.keySet()) {
112141
if (user == null) {
113142
list.append(" ").append(capitalCase(kitItem));
114143
} else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH))) {

Essentials/src/main/java/com/earth2me/essentials/commands/Commanddelkit.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.earth2me.essentials.CommandSource;
44
import com.earth2me.essentials.Kit;
5-
import com.earth2me.essentials.config.ConfigurateUtil;
65
import com.google.common.collect.Lists;
76
import org.bukkit.Server;
87

@@ -38,7 +37,7 @@ public void run(final Server server, final CommandSource sender, final String co
3837
@Override
3938
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
4039
if (args.length == 1) {
41-
return Lists.newArrayList(ConfigurateUtil.getKeys(ess.getKits().getKits()));
40+
return Lists.newArrayList(ess.getKits().getKitKeys());
4241
}
4342
return Collections.emptyList();
4443
}

Essentials/src/main/java/com/earth2me/essentials/commands/Commandkit.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.earth2me.essentials.CommandSource;
44
import com.earth2me.essentials.Kit;
55
import com.earth2me.essentials.User;
6-
import com.earth2me.essentials.config.ConfigurateUtil;
76
import com.earth2me.essentials.utils.StringUtil;
87
import org.bukkit.Server;
98

@@ -101,7 +100,7 @@ protected List<String> getTabCompleteOptions(final Server server, final User use
101100
if (args.length == 1) {
102101
final List<String> options = new ArrayList<>();
103102
// TODO: Move all of this to its own method
104-
for (final String kitName : ConfigurateUtil.getKeys(ess.getKits().getKits())) {
103+
for (final String kitName : ess.getKits().getKitKeys()) {
105104
if (!user.isAuthorized("essentials.kits." + kitName)) { // Only check perm, not time or money
106105
continue;
107106
}
@@ -118,7 +117,7 @@ protected List<String> getTabCompleteOptions(final Server server, final User use
118117
@Override
119118
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
120119
if (args.length == 1) {
121-
return new ArrayList<>(ConfigurateUtil.getKeys(ess.getKits().getKits())); // TODO: Move this to its own method
120+
return new ArrayList<>(ess.getKits().getKitKeys()); // TODO: Move this to its own method
122121
} else if (args.length == 2) {
123122
return getPlayers(server, sender);
124123
} else {

Essentials/src/main/java/com/earth2me/essentials/commands/Commandkitreset.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.earth2me.essentials.CommandSource;
44
import com.earth2me.essentials.User;
5-
import com.earth2me.essentials.config.ConfigurateUtil;
65
import org.bukkit.Server;
76

87
import java.util.ArrayList;
@@ -59,7 +58,7 @@ protected void run(Server server, CommandSource sender, String commandLabel, Str
5958
@Override
6059
protected List<String> getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) {
6160
if (args.length == 1) {
62-
return new ArrayList<>(ConfigurateUtil.getKeys(ess.getKits().getKits()));
61+
return new ArrayList<>(ess.getKits().getKitKeys());
6362
} else if (args.length == 2 && sender.isAuthorized("essentials.kitreset.others", ess)) {
6463
return getPlayers(server, sender);
6564
} else {

Essentials/src/main/java/com/earth2me/essentials/commands/Commandshowkit.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.earth2me.essentials.Kit;
44
import com.earth2me.essentials.User;
5-
import com.earth2me.essentials.config.ConfigurateUtil;
65
import org.bukkit.Server;
76

87
import java.util.ArrayList;
@@ -34,7 +33,7 @@ public void run(final Server server, final User user, final String commandLabel,
3433
@Override
3534
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
3635
if (args.length == 1) {
37-
return new ArrayList<>(ConfigurateUtil.getKeys(ess.getKits().getKits())); // TODO: Move this to its own method
36+
return new ArrayList<>(ess.getKits().getKitKeys()); // TODO: Move this to its own method
3837
} else {
3938
return Collections.emptyList();
4039
}

Essentials/src/main/resources/kits.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
# {PLAYER} will show the player's displayname instead of username.
1010
# 'delay' refers to the cooldown between how often you can use each kit, measured in seconds.
1111
# Set delay to -1 for a one time kit.
12+
#
13+
# In addition, you can also organize your kits into separate files under the `kits` subdirectory.
14+
# Essentials will treat all .yml files in the `kits` subdirectory as kits files, and will add any kits from those files along with the kits in `kits.yml`.
15+
# Any file in the `kits` subdirectory must be formatted in the same way as this file. This allows you to define multiple kits in each file.
1216
# For more information, visit http://wiki.ess3.net/wiki/Kits
1317
kits:
1418
tools:

0 commit comments

Comments
 (0)