mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-05 16:08:14 +01:00
Add kits subfolder to allow for multiple kit files (#4407)
Co-authored-by: MD <1917406+mdcfe@users.noreply.github.com> Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
parent
b3692d18b0
commit
ef8bcc8bc4
@ -438,7 +438,7 @@ public class EssentialsUpgrade {
|
|||||||
|
|
||||||
public void convertKits() {
|
public void convertKits() {
|
||||||
final Kits kits = ess.getKits();
|
final Kits kits = ess.getKits();
|
||||||
final EssentialsConfiguration config = kits.getConfig();
|
final EssentialsConfiguration config = kits.getRootConfig();
|
||||||
if (doneFile.getBoolean("kitsyml", false)) {
|
if (doneFile.getBoolean("kitsyml", false)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -4,84 +4,106 @@ import com.earth2me.essentials.config.ConfigurateUtil;
|
|||||||
import com.earth2me.essentials.config.EssentialsConfiguration;
|
import com.earth2me.essentials.config.EssentialsConfiguration;
|
||||||
import com.earth2me.essentials.utils.NumberUtil;
|
import com.earth2me.essentials.utils.NumberUtil;
|
||||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||||
import org.spongepowered.configurate.serialize.SerializationException;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static com.earth2me.essentials.I18n.capitalCase;
|
import static com.earth2me.essentials.I18n.capitalCase;
|
||||||
import static com.earth2me.essentials.I18n.tl;
|
import static com.earth2me.essentials.I18n.tl;
|
||||||
|
|
||||||
public class Kits implements IConf {
|
public class Kits implements IConf {
|
||||||
private final EssentialsConfiguration config;
|
private final IEssentials ess;
|
||||||
private CommentedConfigurationNode kits;
|
private final EssentialsConfiguration rootConfig;
|
||||||
|
private final Map<String, EssentialsConfiguration> kitToConfigMap = new HashMap<>();
|
||||||
|
private final Map<String, Map<String, Object>> kitMap = new HashMap<>();
|
||||||
|
|
||||||
public Kits(final IEssentials essentials) {
|
public Kits(final IEssentials essentials) {
|
||||||
config = new EssentialsConfiguration(new File(essentials.getDataFolder(), "kits.yml"), "/kits.yml");
|
this.ess = essentials;
|
||||||
|
this.rootConfig = new EssentialsConfiguration(new File(essentials.getDataFolder(), "kits.yml"), "/kits.yml");
|
||||||
|
|
||||||
reloadConfig();
|
reloadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reloadConfig() {
|
public void reloadConfig() {
|
||||||
config.load();
|
rootConfig.load();
|
||||||
kits = _getKits();
|
parseKits();
|
||||||
}
|
}
|
||||||
|
|
||||||
public File getFile() {
|
public File getFile() {
|
||||||
return config.getFile();
|
return rootConfig.getFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
private CommentedConfigurationNode _getKits() {
|
private void parseKit(final String kitName, final CommentedConfigurationNode kitSection, final EssentialsConfiguration parentConfig) {
|
||||||
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()) {
|
if (kitSection.isMap()) {
|
||||||
try {
|
final String effectiveKitName = kitName.toLowerCase(Locale.ENGLISH);
|
||||||
newSection.node(kitItem.toLowerCase(Locale.ENGLISH)).set(kitSection);
|
kitToConfigMap.put(effectiveKitName, parentConfig);
|
||||||
} catch (SerializationException e) {
|
kitMap.put(effectiveKitName, ConfigurateUtil.getRawMap(kitSection));
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newSection;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public EssentialsConfiguration getConfig() {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommentedConfigurationNode getKits() {
|
|
||||||
return kits;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Object> getKit(String name) {
|
|
||||||
name = name.replace('.', '_').replace('/', '_');
|
|
||||||
if (getKits() != null) {
|
|
||||||
final CommentedConfigurationNode kits = getKits();
|
|
||||||
// Other parts of the codebase/3rd party plugins expect us to lowercase kit names here.
|
|
||||||
// This isn't strictly needed for the future of Essentials, but for compatibility it's here.
|
|
||||||
final CommentedConfigurationNode kitSection = kits.node(name.toLowerCase());
|
|
||||||
if (!kitSection.virtual() && kitSection.isMap()) {
|
|
||||||
return ConfigurateUtil.getRawMap(kitSection);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void parseKits() {
|
||||||
|
kitToConfigMap.clear();
|
||||||
|
kitMap.clear();
|
||||||
|
|
||||||
|
// Kits from kits.yml file
|
||||||
|
final CommentedConfigurationNode fileKits = rootConfig.getSection("kits");
|
||||||
|
if (fileKits != null) {
|
||||||
|
for (final Map.Entry<String, CommentedConfigurationNode> kitEntry : ConfigurateUtil.getMap(fileKits).entrySet()) {
|
||||||
|
parseKit(kitEntry.getKey(), kitEntry.getValue(), rootConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kits from kits subdirectory
|
||||||
|
final File kitsFolder = new File(this.ess.getDataFolder(), "kits");
|
||||||
|
if (!kitsFolder.exists() || !kitsFolder.isDirectory()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Map.Entry<String, CommentedConfigurationNode> kitEntry : ConfigurateUtil.getMap(kits).entrySet()) {
|
||||||
|
parseKit(kitEntry.getKey(), kitEntry.getValue(), kitConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should be used for EssentialsUpgrade conversions <b>only</b>.
|
||||||
|
*/
|
||||||
|
public EssentialsConfiguration getRootConfig() {
|
||||||
|
return rootConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> getKitKeys() {
|
||||||
|
return kitMap.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getKit(final String name) {
|
||||||
|
if (name != null) {
|
||||||
|
return kitMap.get(name.replace('.', '_').replace('/', '_'));
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tries to find an existing kit name that matches the given name, ignoring case. Returns null if no match.
|
// Tries to find an existing kit name that matches the given name, ignoring case. Returns null if no match.
|
||||||
public String matchKit(final String name) {
|
public String matchKit(final String name) {
|
||||||
final CommentedConfigurationNode section = config.getSection("kits");
|
if (name != null) {
|
||||||
if (section != null) {
|
for (final String kitName : kitMap.keySet()) {
|
||||||
for (final String kitName : ConfigurateUtil.getKeys(section)) {
|
|
||||||
if (kitName.equalsIgnoreCase(name)) {
|
if (kitName.equalsIgnoreCase(name)) {
|
||||||
return kitName;
|
return kitName;
|
||||||
}
|
}
|
||||||
@ -90,25 +112,32 @@ public class Kits implements IConf {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addKit(final String name, final List<String> lines, final long delay) {
|
public void addKit(String name, final List<String> lines, final long delay) {
|
||||||
|
name = name.replace('.', '_').replace('/', '_').toLowerCase(Locale.ENGLISH);
|
||||||
// Will overwrite but w/e
|
// Will overwrite but w/e
|
||||||
config.setProperty("kits." + name + ".delay", delay);
|
rootConfig.setProperty("kits." + name + ".delay", delay);
|
||||||
config.setProperty("kits." + name + ".items", lines);
|
rootConfig.setProperty("kits." + name + ".items", lines);
|
||||||
kits = _getKits();
|
parseKits();
|
||||||
config.save();
|
rootConfig.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeKit(final String name) {
|
public void removeKit(String name) {
|
||||||
|
name = name.replace('.', '_').replace('/', '_').toLowerCase(Locale.ENGLISH);
|
||||||
|
if (!kitToConfigMap.containsKey(name) || !kitMap.containsKey(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final EssentialsConfiguration config = kitToConfigMap.get(name);
|
||||||
config.removeProperty("kits." + name);
|
config.removeProperty("kits." + name);
|
||||||
kits = _getKits();
|
|
||||||
config.save();
|
config.blockingSave();
|
||||||
|
parseKits();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String listKits(final net.ess3.api.IEssentials ess, final User user) throws Exception {
|
public String listKits(final net.ess3.api.IEssentials ess, final User user) throws Exception {
|
||||||
try {
|
try {
|
||||||
final CommentedConfigurationNode kits = config.getSection("kits");
|
|
||||||
final StringBuilder list = new StringBuilder();
|
final StringBuilder list = new StringBuilder();
|
||||||
for (final String kitItem : ConfigurateUtil.getKeys(kits)) {
|
for (final String kitItem : kitMap.keySet()) {
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
list.append(" ").append(capitalCase(kitItem));
|
list.append(" ").append(capitalCase(kitItem));
|
||||||
} else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH))) {
|
} else if (user.isAuthorized("essentials.kits." + kitItem.toLowerCase(Locale.ENGLISH))) {
|
||||||
|
@ -2,7 +2,6 @@ package com.earth2me.essentials.commands;
|
|||||||
|
|
||||||
import com.earth2me.essentials.CommandSource;
|
import com.earth2me.essentials.CommandSource;
|
||||||
import com.earth2me.essentials.Kit;
|
import com.earth2me.essentials.Kit;
|
||||||
import com.earth2me.essentials.config.ConfigurateUtil;
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
@ -38,7 +37,7 @@ public class Commanddelkit extends EssentialsCommand {
|
|||||||
@Override
|
@Override
|
||||||
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
return Lists.newArrayList(ConfigurateUtil.getKeys(ess.getKits().getKits()));
|
return Lists.newArrayList(ess.getKits().getKitKeys());
|
||||||
}
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package com.earth2me.essentials.commands;
|
|||||||
import com.earth2me.essentials.CommandSource;
|
import com.earth2me.essentials.CommandSource;
|
||||||
import com.earth2me.essentials.Kit;
|
import com.earth2me.essentials.Kit;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.config.ConfigurateUtil;
|
|
||||||
import com.earth2me.essentials.utils.StringUtil;
|
import com.earth2me.essentials.utils.StringUtil;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
@ -101,7 +100,7 @@ public class Commandkit extends EssentialsCommand {
|
|||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
final List<String> options = new ArrayList<>();
|
final List<String> options = new ArrayList<>();
|
||||||
// TODO: Move all of this to its own method
|
// TODO: Move all of this to its own method
|
||||||
for (final String kitName : ConfigurateUtil.getKeys(ess.getKits().getKits())) {
|
for (final String kitName : ess.getKits().getKitKeys()) {
|
||||||
if (!user.isAuthorized("essentials.kits." + kitName)) { // Only check perm, not time or money
|
if (!user.isAuthorized("essentials.kits." + kitName)) { // Only check perm, not time or money
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -118,7 +117,7 @@ public class Commandkit extends EssentialsCommand {
|
|||||||
@Override
|
@Override
|
||||||
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
return new ArrayList<>(ConfigurateUtil.getKeys(ess.getKits().getKits())); // TODO: Move this to its own method
|
return new ArrayList<>(ess.getKits().getKitKeys()); // TODO: Move this to its own method
|
||||||
} else if (args.length == 2) {
|
} else if (args.length == 2) {
|
||||||
return getPlayers(server, sender);
|
return getPlayers(server, sender);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,7 +2,6 @@ package com.earth2me.essentials.commands;
|
|||||||
|
|
||||||
import com.earth2me.essentials.CommandSource;
|
import com.earth2me.essentials.CommandSource;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.config.ConfigurateUtil;
|
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -59,7 +58,7 @@ public class Commandkitreset extends EssentialsCommand {
|
|||||||
@Override
|
@Override
|
||||||
protected List<String> getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) {
|
protected List<String> getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
return new ArrayList<>(ConfigurateUtil.getKeys(ess.getKits().getKits()));
|
return new ArrayList<>(ess.getKits().getKitKeys());
|
||||||
} else if (args.length == 2 && sender.isAuthorized("essentials.kitreset.others", ess)) {
|
} else if (args.length == 2 && sender.isAuthorized("essentials.kitreset.others", ess)) {
|
||||||
return getPlayers(server, sender);
|
return getPlayers(server, sender);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2,7 +2,6 @@ package com.earth2me.essentials.commands;
|
|||||||
|
|
||||||
import com.earth2me.essentials.Kit;
|
import com.earth2me.essentials.Kit;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.config.ConfigurateUtil;
|
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -34,7 +33,7 @@ public class Commandshowkit extends EssentialsCommand {
|
|||||||
@Override
|
@Override
|
||||||
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
|
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
|
||||||
if (args.length == 1) {
|
if (args.length == 1) {
|
||||||
return new ArrayList<>(ConfigurateUtil.getKeys(ess.getKits().getKits())); // TODO: Move this to its own method
|
return new ArrayList<>(ess.getKits().getKitKeys()); // TODO: Move this to its own method
|
||||||
} else {
|
} else {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,10 @@
|
|||||||
# {PLAYER} will show the player's displayname instead of username.
|
# {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.
|
# '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.
|
# Set delay to -1 for a one time kit.
|
||||||
|
#
|
||||||
|
# In addition, you can also organize your kits into separate files under the `kits` subdirectory.
|
||||||
|
# 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`.
|
||||||
|
# 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.
|
||||||
# For more information, visit http://wiki.ess3.net/wiki/Kits
|
# For more information, visit http://wiki.ess3.net/wiki/Kits
|
||||||
kits:
|
kits:
|
||||||
tools:
|
tools:
|
||||||
|
Loading…
Reference in New Issue
Block a user