diff --git a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java index c99d4d05b..a91834f7f 100644 --- a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java @@ -2,7 +2,10 @@ package com.gmail.nossr50.commands.levelup; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; +import org.jetbrains.annotations.NotNull; +import java.util.Collection; +import java.util.LinkedList; import java.util.Set; import java.util.function.BiPredicate; @@ -29,7 +32,7 @@ public interface LevelUpCommand { class LevelUpCommandBuilder { private Set skillFilter = null; private Set levels = null; - private String commandStr = null; + private LinkedList commands = null; private BiPredicate predicate = null; private boolean logInfo; @@ -47,23 +50,40 @@ public interface LevelUpCommand { return this; } - public LevelUpCommandBuilder commandString(String commandStr) { - this.commandStr = commandStr; + public LevelUpCommandBuilder command(@NotNull String command) { + this.commands = new LinkedList<>(); + this.commands.add(command); return this; } - public LevelUpCommandBuilder withLevels(Set levels) { - this.levels = levels; + public LevelUpCommandBuilder commands(@NotNull Collection command) { + this.commands = new LinkedList<>(command); return this; } - public LevelUpCommandBuilder withSkillFilter(Set skillFilter) { + public LevelUpCommandBuilder withLevels(@NotNull Collection levels) { + requireNonNull(levels, "levels is null!"); + this.levels = Set.copyOf(levels); + return this; + } + + public LevelUpCommandBuilder withSkillFilter(@NotNull Set skillFilter) { + requireNonNull(skillFilter, "skillFilter is null!"); + if (skillFilter.isEmpty()) { + throw new IllegalArgumentException("skillFilter is empty"); + } this.skillFilter = skillFilter; return this; } + public LevelUpCommandBuilder withSkillFilter(@NotNull PrimarySkillType skill) { + requireNonNull(skill, "skill is null!"); + this.skillFilter = Set.of(skill); + return this; + } + public LevelUpCommand build() { - requireNonNull(commandStr, "commandStr is null"); + requireNonNull(commands, "commandStr is null"); if (predicate == null) { requireNonNull(levels, "levels is null"); @@ -73,10 +93,10 @@ public interface LevelUpCommand { } else { return skillFilter.contains(skill) && levels.contains(level); } - }, commandStr, logInfo); + }, commands, logInfo); } - return new LevelUpCommandImpl(predicate, commandStr, logInfo); + return new LevelUpCommandImpl(predicate, commands, logInfo); } } } diff --git a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java index e48f5a156..2d2aad559 100644 --- a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java +++ b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java @@ -7,6 +7,8 @@ import com.gmail.nossr50.util.LogUtils; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; +import java.util.Collection; +import java.util.LinkedList; import java.util.Objects; import java.util.Set; import java.util.function.BiPredicate; @@ -14,11 +16,18 @@ import java.util.function.BiPredicate; public class LevelUpCommandImpl implements LevelUpCommand { private final BiPredicate predicate; private final boolean logInfo; - private final @NotNull String commandStr; + private final @NotNull LinkedList commandStr; public LevelUpCommandImpl(@NotNull BiPredicate predicate, @NotNull String commandStr, boolean logInfo) { - this.commandStr = commandStr; this.predicate = predicate; + this.commandStr = new LinkedList<>(); + this.commandStr.add(commandStr); + this.logInfo = logInfo; + } + + public LevelUpCommandImpl(@NotNull BiPredicate predicate, @NotNull LinkedList commandStr, boolean logInfo) { + this.predicate = predicate; + this.commandStr = commandStr; this.logInfo = logInfo; } @@ -38,7 +47,13 @@ public class LevelUpCommandImpl implements LevelUpCommand { } public void executeCommand() { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commandStr); + // TODO: Change this to debug later + mcMMO.p.getLogger().info("Executing commands for level up: " + commandStr); + for (String command : commandStr) { + // TODO: Change this to debug later + mcMMO.p.getLogger().info("Executing command: " + command); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); + } } @Override diff --git a/src/main/java/com/gmail/nossr50/config/CommandOnLevelUpConfig.java b/src/main/java/com/gmail/nossr50/config/CommandOnLevelUpConfig.java index e3c7a9dfa..c3f9f2aa5 100644 --- a/src/main/java/com/gmail/nossr50/config/CommandOnLevelUpConfig.java +++ b/src/main/java/com/gmail/nossr50/config/CommandOnLevelUpConfig.java @@ -1,14 +1,27 @@ package com.gmail.nossr50.config; import com.gmail.nossr50.commands.levelup.LevelUpCommand; +import com.gmail.nossr50.datatypes.skills.PrimarySkillType; +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.util.LogUtils; import org.bukkit.configuration.ConfigurationSection; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; public class CommandOnLevelUpConfig extends BukkitConfig { public static final String LEVEL_UP_COMMANDS = "level_up_commands"; + public static final String LEVELS_SECTION = "levels"; + public static final String SKILLS_SECTION = "skills"; + public static final String CONDITION_SECTION = "condition"; + public static final String ENABLED = "enabled"; + public static final String COMMANDS = "commands"; public CommandOnLevelUpConfig(@NotNull File dataFolder) { super("commandonlevelup", dataFolder); @@ -17,10 +30,104 @@ public class CommandOnLevelUpConfig extends BukkitConfig { @Override protected void loadKeys() { final ConfigurationSection configurationSection = config.getConfigurationSection(LEVEL_UP_COMMANDS); - } + if (configurationSection == null) { + LogUtils.debug(mcMMO.p.getLogger(), "No commands found in the level up commands config file."); + return; + } - private LevelUpCommand buildCommand() { + for (String key : configurationSection.getKeys(false)) { + final ConfigurationSection commandSection = configurationSection.getConfigurationSection(key); + if (commandSection == null) { + mcMMO.p.getLogger().severe("Unable to load command section for key: " + key); + continue; + } + + LevelUpCommand levelUpCommand = buildCommand(commandSection); + if (levelUpCommand == null) { + mcMMO.p.getLogger().severe("Invalid command format for key: " + key); + continue; + } else { + mcMMO.p.getLogger().info("Command successfully loaded from config for key: " + key); + mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand); + } + } + } + + @Nullable + private LevelUpCommand buildCommand(final ConfigurationSection commandSection) { LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder(); + // check if command is enabled + if (!commandSection.getBoolean(ENABLED, true)) { + return null; + } + /* Condition Section */ + ConfigurationSection condition = commandSection.getConfigurationSection(CONDITION_SECTION); + if (condition == null) { + mcMMO.p.getLogger().severe("No condition section found for command named " + commandSection.getName()); + return null; + } + + // Skill Filter + // check if skills is string or configuration section + if (condition.contains(SKILLS_SECTION)) { + if (condition.isString(SKILLS_SECTION)) { + String skillName = condition.getString(SKILLS_SECTION); + if (skillName != null) { + PrimarySkillType primarySkillType = mcMMO.p.getSkillTools().matchSkill(skillName); + if (primarySkillType != null) { + builder.withSkillFilter(getSkillsFromFilter(new HashSet<>(Set.of(skillName)))); + } + } + } else { + ConfigurationSection skillsSection = condition.getConfigurationSection(SKILLS_SECTION); + if (skillsSection != null) { + Set skillNames = skillsSection.getKeys(false); + Set skillsFromFilter = getSkillsFromFilter(skillNames); + if (skillsFromFilter.isEmpty()) { + LogUtils.debug(mcMMO.p.getLogger(), "No valid skills found for command named " + + commandSection.getName() + "for condition section named " + skillsSection.getName()); + } else { + builder.withSkillFilter(skillsFromFilter); + } + } + } + } + + // for now only simple condition is supported + if (!condition.contains(LEVELS_SECTION)) { + mcMMO.p.getLogger().severe("No condition.levels section found for command named " + commandSection.getName()); + return null; + } + + Collection levels = condition.getIntegerList(LEVELS_SECTION); + if (levels.isEmpty()) { + mcMMO.p.getLogger().severe("No valid levels found in condition.levels for command named " + + commandSection.getName()); + return null; + } + builder.withLevels(levels); + + // commands + if (commandSection.isString(COMMANDS)) { + String command = commandSection.getString(COMMANDS); + if (command != null) { + builder.command(command); + } + } else { + List commands = commandSection.getStringList(COMMANDS); + if (commands.isEmpty()) { + mcMMO.p.getLogger().severe("No commands defined for command named " + + commandSection.getName()); + return null; + } else { + builder.commands(commands); + } + } + return builder.build(); } + + private Set getSkillsFromFilter(Set skillFilter) { + return mcMMO.p.getSkillTools().matchSkills(skillFilter); + } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/skills/PrimarySkillType.java b/src/main/java/com/gmail/nossr50/datatypes/skills/PrimarySkillType.java index 5b513c280..9fa39bd5d 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/skills/PrimarySkillType.java +++ b/src/main/java/com/gmail/nossr50/datatypes/skills/PrimarySkillType.java @@ -9,7 +9,9 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Set; public enum PrimarySkillType { ACROBATICS, @@ -140,7 +142,6 @@ public enum PrimarySkillType { /** * WARNING: Being removed in an upcoming update, you should be using mcMMO.getSkillTools() instead - * @return the max level of this skill * @see SkillTools#matchSkill(java.lang.String) * @deprecated this is being removed in an upcoming update, you should be using mcMMO.getSkillTools() instead */ @@ -149,6 +150,16 @@ public enum PrimarySkillType { return mcMMO.p.getSkillTools().matchSkill(skillName); } + /** + * WARNING: Being removed in an upcoming update, you should be using mcMMO.getSkillTools() instead + * @see SkillTools#matchSkill(java.lang.String) + * @deprecated this is being removed in an upcoming update, you should be using mcMMO.getSkillTools() instead + */ + @Deprecated + public static Set getSkills(Collection skillNames) { + return mcMMO.p.getSkillTools().matchSkills(skillNames); + } + /** * WARNING: Being removed in an upcoming update, you should be using mcMMO.getSkillTools() instead * @return the max level of this skill diff --git a/src/main/java/com/gmail/nossr50/util/skills/SkillTools.java b/src/main/java/com/gmail/nossr50/util/skills/SkillTools.java index 416926fe4..240bd6861 100644 --- a/src/main/java/com/gmail/nossr50/util/skills/SkillTools.java +++ b/src/main/java/com/gmail/nossr50/util/skills/SkillTools.java @@ -17,6 +17,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Tameable; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.VisibleForTesting; import java.util.*; @@ -236,14 +237,15 @@ public class SkillTools { /** * Matches a string of a skill to a skill - * This is NOT case sensitive + * This is NOT case-sensitive * First it checks the locale file and tries to match by the localized name of the skill * Then if nothing is found it checks against the hard coded "name" of the skill, which is just its name in English * * @param skillName target skill name * @return the matching PrimarySkillType if one is found, otherwise null */ - public PrimarySkillType matchSkill(String skillName) { + @Nullable + public PrimarySkillType matchSkill(@NotNull String skillName) { if (!pluginRef.getGeneralConfig().getLocale().equalsIgnoreCase("en_US")) { for (PrimarySkillType type : PrimarySkillType.values()) { if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(type.name()) + ".SkillName"))) { @@ -258,11 +260,29 @@ public class SkillTools { } } - if (!skillName.equalsIgnoreCase("all")) { - pluginRef.getLogger().warning("Invalid mcMMO skill (" + skillName + ")"); //TODO: Localize + return null; + } + + /** + * Matches an array of strings of skills to skills + * This is NOT case-sensitive + * First it checks the locale file and tries to match by the localized name of the skill + * Then if nothing is found it checks against the hard coded "name" of the skill, which is just its name in English + * + * @param skills target skill names + * @return the matching PrimarySkillType if one is found, otherwise null + */ + @NotNull + public Set matchSkills(@NotNull Collection skills) { + Set matchingSkills = new HashSet<>(); + for (String skillName : skills) { + PrimarySkillType primarySkillType = matchSkill(skillName); + if(primarySkillType != null) { + matchingSkills.add(primarySkillType); + } } - return null; + return matchingSkills; } /**