浏览代码

Read and register commands from config

nossr50 1 年之前
父节点
当前提交
753834e1fc

+ 29 - 9
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<PrimarySkillType> skillFilter = null;
         private Set<Integer> levels = null;
-        private String commandStr = null;
+        private LinkedList<String> commands = null;
         private BiPredicate<PrimarySkillType, Integer> 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<Integer> levels) {
-            this.levels = levels;
+        public LevelUpCommandBuilder commands(@NotNull Collection<String> command) {
+            this.commands = new LinkedList<>(command);
             return this;
         }
 
-        public LevelUpCommandBuilder withSkillFilter(Set<PrimarySkillType> skillFilter) {
+        public LevelUpCommandBuilder withLevels(@NotNull Collection<Integer> levels) {
+            requireNonNull(levels, "levels is null!");
+            this.levels = Set.copyOf(levels);
+            return this;
+        }
+
+        public LevelUpCommandBuilder withSkillFilter(@NotNull Set<PrimarySkillType> 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);
         }
     }
 }

+ 18 - 3
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<PrimarySkillType, Integer> predicate;
     private final boolean logInfo;
-    private final @NotNull String commandStr;
+    private final @NotNull LinkedList<String> commandStr;
 
     public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> 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<PrimarySkillType, Integer> predicate, @NotNull LinkedList<String> 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

+ 109 - 2
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<String> skillNames = skillsSection.getKeys(false);
+                    Set<PrimarySkillType> 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<Integer> 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<String> 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<PrimarySkillType> getSkillsFromFilter(Set<String> skillFilter) {
+        return mcMMO.p.getSkillTools().matchSkills(skillFilter);
+    }
 }

+ 12 - 1
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<PrimarySkillType> getSkills(Collection<String> 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

+ 25 - 5
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<PrimarySkillType> matchSkills(@NotNull Collection<String> skills) {
+        Set<PrimarySkillType> matchingSkills = new HashSet<>();
+        for (String skillName : skills) {
+            PrimarySkillType primarySkillType = matchSkill(skillName);
+            if(primarySkillType != null) {
+                matchingSkills.add(primarySkillType);
+            }
         }
 
-        return null;
+        return matchingSkills;
     }
 
     /**