Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions Essentials/src/main/java/com/earth2me/essentials/Kits.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import static com.earth2me.essentials.I18n.tl;

public class Kits implements IConf {
private final IEssentials ess;
private final EssentialsConfiguration config;
private CommentedConfigurationNode kits;

public Kits(final IEssentials essentials) {
config = new EssentialsConfiguration(new File(essentials.getDataFolder(), "kits.yml"), "/kits.yml");
this.ess = essentials;
this.config = new EssentialsConfiguration(new File(essentials.getDataFolder(), "kits.yml"), "/kits.yml");

reloadConfig();
}
Expand All @@ -31,23 +33,49 @@ public void reloadConfig() {
kits = _getKits();
}

private void addKitSectionToNode(final String kitName, final CommentedConfigurationNode kitSection, final CommentedConfigurationNode destination) {
if (kitSection.isMap()) {
try {
destination.node(kitName.toLowerCase(Locale.ENGLISH)).set(kitSection);
} catch (SerializationException e) {
e.printStackTrace();
}
}
}

private CommentedConfigurationNode _getKits() {
final CommentedConfigurationNode section = config.getSection("kits");
if (section != null) {
final CommentedConfigurationNode newSection = config.newSection();
for (final String kitItem : ConfigurateUtil.getKeys(section)) {
final CommentedConfigurationNode kitSection = section.node(kitItem);
if (kitSection.isMap()) {
try {
newSection.node(kitItem.toLowerCase(Locale.ENGLISH)).set(kitSection);
} catch (SerializationException e) {
e.printStackTrace();
final CommentedConfigurationNode newSection = config.newSection();

// Kits from kits.yml file
final CommentedConfigurationNode fileKits = config.getSection("kits");
if (fileKits != null) {
for (final String kitItem : ConfigurateUtil.getKeys(fileKits)) {
addKitSectionToNode(kitItem, fileKits.node(kitItem), newSection);
}
}

// Kits from kits subdirectory
final File kitsFolder = new File(this.ess.getDataFolder(), "kits");
if (!kitsFolder.exists() || !kitsFolder.isDirectory()) {
return newSection;
}

final File[] kitsFiles = kitsFolder.listFiles();

//noinspection ConstantConditions - will not be null, conditions checked above.
for (final File kitFile : kitsFiles) {
if (kitFile.getName().endsWith(".yml")) {
final EssentialsConfiguration kitConfig = new EssentialsConfiguration(kitFile);
kitConfig.load();
final CommentedConfigurationNode kits = kitConfig.getSection("kits");
if (kits != null) {
for (final String kitItem : ConfigurateUtil.getKeys(kits)) {
addKitSectionToNode(kitItem, kits.node(kitItem), newSection);
}
}
}
return newSection;
}
return null;
return newSection;
}

public EssentialsConfiguration getConfig() {
Expand Down
4 changes: 4 additions & 0 deletions Essentials/src/main/resources/kits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
# {PLAYER} will show the player's displayname instead of username.
# 'delay' refers to the cooldown between how often you can use each kit, measured in seconds.
# Set delay to -1 for a one time kit.
#
# In addition, you can also define kits in multiple different files for sake of organization.
# Essentials will treat all .yml files in the `kits` subdirectory as kits files and will add them in addition to kits in this file as well.
# Any file in the `kits` subdirectory will need to be formatted in the same way as this file and thus support multiple kits per file.
# For more information, visit http://wiki.ess3.net/wiki/Kits
kits:
tools:
Expand Down