Browse Source

Add builder, switch to BiPredicate for impl

nossr50 1 year ago
parent
commit
8ee282beec

+ 57 - 0
src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java

@@ -4,6 +4,9 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
 import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
 import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
 
 
 import java.util.Set;
 import java.util.Set;
+import java.util.function.BiPredicate;
+
+import static java.util.Objects.requireNonNull;
 
 
 /**
 /**
  * Represents a command to be executed on a level up
  * Represents a command to be executed on a level up
@@ -22,4 +25,58 @@ public interface LevelUpCommand {
      * Execute the command
      * Execute the command
      */
      */
     void executeCommand();
     void executeCommand();
+
+    class LevelUpCommandBuilder {
+        private Set<PrimarySkillType> skillFilter = null;
+        private Set<Integer> levels = null;
+        private String commandStr = null;
+        private BiPredicate<PrimarySkillType, Integer> predicate = null;
+        private boolean logInfo;
+
+        public LevelUpCommandBuilder() {
+            this.logInfo = false;
+        }
+
+        public LevelUpCommandBuilder withPredicate(BiPredicate<PrimarySkillType, Integer> predicate) {
+            this.predicate = predicate;
+            return this;
+        }
+
+        public LevelUpCommandBuilder withLogInfo(boolean logInfo) {
+            this.logInfo = logInfo;
+            return this;
+        }
+
+        public LevelUpCommandBuilder commandString(String commandStr) {
+            this.commandStr = commandStr;
+            return this;
+        }
+
+        public LevelUpCommandBuilder withLevels(Set<Integer> levels) {
+            this.levels = levels;
+            return this;
+        }
+
+        public LevelUpCommandBuilder withSkillFilter(Set<PrimarySkillType> skillFilter) {
+            this.skillFilter = skillFilter;
+            return this;
+        }
+
+        public LevelUpCommand build() {
+            requireNonNull(commandStr, "commandStr is null");
+            if (predicate == null) {
+                requireNonNull(levels, "levels is null");
+
+                return new LevelUpCommandImpl((skill, level) -> {
+                    if (skillFilter == null) {
+                        return levels.contains(level);
+                    } else {
+                        return skillFilter.contains(skill) && levels.contains(level);
+                    }
+                }, commandStr, logInfo);
+            }
+
+            return new LevelUpCommandImpl(predicate, commandStr, logInfo);
+        }
+    }
 }
 }

+ 8 - 16
src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java

@@ -9,30 +9,23 @@ import org.jetbrains.annotations.NotNull;
 
 
 import java.util.Objects;
 import java.util.Objects;
 import java.util.Set;
 import java.util.Set;
-import java.util.function.Predicate;
+import java.util.function.BiPredicate;
 
 
 public class LevelUpCommandImpl implements LevelUpCommand {
 public class LevelUpCommandImpl implements LevelUpCommand {
-    private final @NotNull Predicate<Integer> shouldApply;
+    private final BiPredicate<PrimarySkillType, Integer> predicate;
     private final boolean logInfo;
     private final boolean logInfo;
     private final @NotNull String commandStr;
     private final @NotNull String commandStr;
 
 
-    private final @NotNull Set<PrimarySkillType> skills;
-
-    public LevelUpCommandImpl(@NotNull Predicate<Integer> shouldApply, @NotNull String commandStr, @NotNull Set<PrimarySkillType> skills, boolean logInfo) {
-        this.shouldApply = shouldApply;
+    public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull String commandStr, boolean logInfo) {
         this.commandStr = commandStr;
         this.commandStr = commandStr;
-        this.skills = skills;
+        this.predicate = predicate;
         this.logInfo = logInfo;
         this.logInfo = logInfo;
     }
     }
 
 
     @Override
     @Override
     public void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
     public void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
-        if(!skills.contains(primarySkillType)) {
-            return;
-        }
-
         for (int i : levelsGained) {
         for (int i : levelsGained) {
-            if (shouldApply.test(i)) {
+            if (predicate.test(primarySkillType, i)) {
                 // execute command via server console in Bukkit
                 // execute command via server console in Bukkit
                 if(logInfo) {
                 if(logInfo) {
                     mcMMO.p.getLogger().info("Executing command: " + commandStr);
                     mcMMO.p.getLogger().info("Executing command: " + commandStr);
@@ -53,21 +46,20 @@ public class LevelUpCommandImpl implements LevelUpCommand {
         if (this == o) return true;
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         if (o == null || getClass() != o.getClass()) return false;
         LevelUpCommandImpl that = (LevelUpCommandImpl) o;
         LevelUpCommandImpl that = (LevelUpCommandImpl) o;
-        return logInfo == that.logInfo && Objects.equals(shouldApply, that.shouldApply) && Objects.equals(commandStr, that.commandStr) && Objects.equals(skills, that.skills);
+        return logInfo == that.logInfo && Objects.equals(predicate, that.predicate) && Objects.equals(commandStr, that.commandStr);
     }
     }
 
 
     @Override
     @Override
     public int hashCode() {
     public int hashCode() {
-        return Objects.hash(shouldApply, logInfo, commandStr, skills);
+        return Objects.hash(predicate, logInfo, commandStr);
     }
     }
 
 
     @Override
     @Override
     public String toString() {
     public String toString() {
         return "LevelUpCommandImpl{" +
         return "LevelUpCommandImpl{" +
-                "shouldApply=" + shouldApply +
+                "predicate=" + predicate +
                 ", logInfo=" + logInfo +
                 ", logInfo=" + logInfo +
                 ", commandStr='" + commandStr + '\'' +
                 ", commandStr='" + commandStr + '\'' +
-                ", skills=" + skills +
                 '}';
                 '}';
     }
     }
 }
 }

+ 46 - 3
src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandManager.java

@@ -6,8 +6,12 @@ import com.gmail.nossr50.mcMMO;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.NotNull;
 
 
 import java.util.HashSet;
 import java.util.HashSet;
+import java.util.Objects;
 import java.util.Set;
 import java.util.Set;
 
 
+/**
+ * Manages commands to be executed on level up
+ */
 public class LevelUpCommandManager {
 public class LevelUpCommandManager {
     private final @NotNull Set<LevelUpCommand> commands;
     private final @NotNull Set<LevelUpCommand> commands;
     private final @NotNull mcMMO plugin;
     private final @NotNull mcMMO plugin;
@@ -17,11 +21,23 @@ public class LevelUpCommandManager {
         this.commands = new HashSet<>();
         this.commands = new HashSet<>();
     }
     }
 
 
-    public void registerCommand(@NotNull LevelUpCommand command) {
-        commands.add(command);
-        mcMMO.p.getLogger().info("Registered command on level up: " + command);
+    /**
+     * Register a level up command to be executed on level up
+     *
+     * @param levelUpCommand the levelUpCommand
+     */
+    public void registerCommand(@NotNull LevelUpCommand levelUpCommand) {
+        commands.add(levelUpCommand);
+        mcMMO.p.getLogger().info("Registered levelUpCommand on level up: " + levelUpCommand);
     }
     }
 
 
+    /**
+     * Apply the level up commands to the player
+     *
+     * @param mmoPlayer         the player
+     * @param primarySkillType  the skill type
+     * @param levelsGained      the levels gained
+     */
     public void apply(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
     public void apply(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
         if (!mmoPlayer.getPlayer().isOnline()) {
         if (!mmoPlayer.getPlayer().isOnline()) {
             return;
             return;
@@ -32,12 +48,39 @@ public class LevelUpCommandManager {
         }
         }
     }
     }
 
 
+    /**
+     * Clear all registered commands
+     */
     public void clear() {
     public void clear() {
         mcMMO.p.getLogger().info("Clearing registered commands on level up");
         mcMMO.p.getLogger().info("Clearing registered commands on level up");
         commands.clear();
         commands.clear();
     }
     }
 
 
+    /**
+     * @return true if there are no registered commands
+     */
     public boolean isEmpty() {
     public boolean isEmpty() {
         return commands.isEmpty();
         return commands.isEmpty();
     }
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        LevelUpCommandManager that = (LevelUpCommandManager) o;
+        return Objects.equals(commands, that.commands) && Objects.equals(plugin, that.plugin);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(commands, plugin);
+    }
+
+    @Override
+    public String toString() {
+        return "LevelUpCommandManager{" +
+                "commands=" + commands +
+                ", plugin=" + plugin +
+                '}';
+    }
 }
 }

+ 9 - 0
src/main/java/com/gmail/nossr50/config/CommandOnLevelUpConfig.java

@@ -1,17 +1,26 @@
 package com.gmail.nossr50.config;
 package com.gmail.nossr50.config;
 
 
+import com.gmail.nossr50.commands.levelup.LevelUpCommand;
+import org.bukkit.configuration.ConfigurationSection;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.NotNull;
 
 
 import java.io.File;
 import java.io.File;
 
 
 public class CommandOnLevelUpConfig extends BukkitConfig {
 public class CommandOnLevelUpConfig extends BukkitConfig {
 
 
+    public static final String LEVEL_UP_COMMANDS = "level_up_commands";
+
     public CommandOnLevelUpConfig(@NotNull File dataFolder) {
     public CommandOnLevelUpConfig(@NotNull File dataFolder) {
         super("commandonlevelup", dataFolder);
         super("commandonlevelup", dataFolder);
     }
     }
 
 
     @Override
     @Override
     protected void loadKeys() {
     protected void loadKeys() {
+        final ConfigurationSection configurationSection = config.getConfigurationSection(LEVEL_UP_COMMANDS);
+     }
 
 
+    private LevelUpCommand buildCommand() {
+        LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
+        return builder.build();
     }
     }
 }
 }

+ 0 - 1
src/main/java/com/gmail/nossr50/events/experience/McMMOPlayerLevelUpEvent.java

@@ -1,7 +1,6 @@
 package com.gmail.nossr50.events.experience;
 package com.gmail.nossr50.events.experience;
 
 
 import com.gmail.nossr50.datatypes.experience.XPGainReason;
 import com.gmail.nossr50.datatypes.experience.XPGainReason;
-import com.gmail.nossr50.datatypes.player.McMMOPlayer;
 import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
 import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
 import org.bukkit.entity.Player;
 import org.bukkit.entity.Player;
 import org.bukkit.event.HandlerList;
 import org.bukkit.event.HandlerList;

+ 0 - 2
src/main/java/com/gmail/nossr50/listeners/SelfListener.java

@@ -20,9 +20,7 @@ import org.bukkit.event.EventHandler;
 import org.bukkit.event.EventPriority;
 import org.bukkit.event.EventPriority;
 import org.bukkit.event.Listener;
 import org.bukkit.event.Listener;
 
 
-import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedHashSet;
-import java.util.List;
 import java.util.Set;
 import java.util.Set;
 
 
 public class SelfListener implements Listener {
 public class SelfListener implements Listener {

+ 29 - 22
src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java

@@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 import org.mockito.Mockito;
 
 
 import java.util.Set;
 import java.util.Set;
-import java.util.function.Predicate;
+import java.util.function.BiPredicate;
 
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.*;
 import static org.mockito.Mockito.*;
@@ -32,13 +32,9 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
     void levelInMiningShouldRunCommandFiveTimes() {
     void levelInMiningShouldRunCommandFiveTimes() {
         // GIVEN level up command for Mining should always execute for Mining level up
         // GIVEN level up command for Mining should always execute for Mining level up
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
-        final PrimarySkillType skillType = PrimarySkillType.MINING;
-        final Predicate<Integer> predicate = (i) -> true;
-        final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl(
-                predicate,
-                "say hello",
-                Set.of(skillType),
-                true));
+        final String commandStr = "say hello";
+        final LevelUpCommand levelUpCommand
+                = buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
 
 
         int levelsGained = 5;
         int levelsGained = 5;
@@ -57,13 +53,9 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
     void levelInMiningShouldRunCommandAtLeastOnce() {
     void levelInMiningShouldRunCommandAtLeastOnce() {
         // GIVEN level up command for Mining should always execute for Mining level up
         // GIVEN level up command for Mining should always execute for Mining level up
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
-        final PrimarySkillType skillType = PrimarySkillType.MINING;
-        final Predicate<Integer> predicate = (i) -> true;
-        final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl(
-                predicate,
-                "say hello",
-                Set.of(skillType),
-                true));
+        final String commandStr = "say hello";
+        final LevelUpCommand levelUpCommand
+                = buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.MINING);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
 
 
         int levelsGained = 1;
         int levelsGained = 1;
@@ -82,13 +74,9 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
     void levelInMiningShouldNotRunCommand() {
     void levelInMiningShouldNotRunCommand() {
         // GIVEN level up command for Woodcutting should not execute for Mining level up
         // GIVEN level up command for Woodcutting should not execute for Mining level up
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
         assert mcMMO.p.getLevelUpCommandManager().isEmpty();
-        final PrimarySkillType skillType = PrimarySkillType.WOODCUTTING;
-        final Predicate<Integer> predicate = (i) -> true;
-        final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl(
-                predicate,
-                "say hello",
-                Set.of(skillType),
-                true));
+        final String commandStr = "say hello";
+        final LevelUpCommand levelUpCommand
+                = buildLevelUpCommand(commandStr, (skill, ignored) -> skill == PrimarySkillType.WOODCUTTING);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
         mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
 
 
 
 
@@ -103,4 +91,23 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
         // THEN the command should not be run
         // THEN the command should not be run
         verify(levelUpCommand, never()).executeCommand();
         verify(levelUpCommand, never()).executeCommand();
     }
     }
+
+    private LevelUpCommand buildLevelUpCommand(String commandStr, Set<Integer> levels, Set<PrimarySkillType> skillFilter) {
+        LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
+        builder.commandString(commandStr);
+        builder.withLevels(levels);
+        if (skillFilter != null) {
+            builder.withSkillFilter(skillFilter);
+        }
+        builder.withLogInfo(true);
+        return Mockito.spy(builder.build());
+    }
+
+    private LevelUpCommand buildLevelUpCommand(String commandStr, BiPredicate<PrimarySkillType, Integer> predicate) {
+        LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
+        builder.commandString(commandStr);
+        builder.withPredicate(predicate);
+        builder.withLogInfo(true);
+        return Mockito.spy(builder.build());
+    }
 }
 }

+ 5 - 0
src/test/java/com/gmail/nossr50/config/CommandOnLevelUpConfigTest.java

@@ -0,0 +1,5 @@
+package com.gmail.nossr50.config;
+
+class CommandOnLevelUpConfigTest {
+
+}

+ 23 - 0
src/test/resources/commandonlevelup.yml

@@ -0,0 +1,23 @@
+level_up_commands:
+    unique_id_here:
+        enabled: false
+        condition:
+            skills:
+                - 'Swords'
+                - 'Axes'
+            levels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+        commands:
+            - "say %player% reached level %level%!"
+            - "say Isn't that nice?"
+        run_command_as: 'CONSOLE'
+        log_level: 'INFO'
+    other_unique_id_here:
+        enabled: false
+        condition:
+            complex_condition:
+                source: 'player.getLevel() % 2 == 0'
+        commands:
+            - "say %player% reached an even level!"
+            - "say Isn't that fun?"
+        run_command_as: 'CONSOLE'
+        log_level: 'DEBUG'