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 89cb4e1bb..e8ca656bd 100644 --- a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java @@ -5,6 +5,21 @@ import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import java.util.Set; +/** + * Represents a command to be executed on a level up + */ public interface LevelUpCommand { - void apply(McMMOPlayer player, PrimarySkillType primarySkillType, Set levelsGained); + /** + * Process the command + * + * @param player the player + * @param primarySkillType the skill type + * @param levelsGained the levels gained + */ + void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set levelsGained); + + /** + * Execute the command + */ + void executeCommand(); } 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 11bdae089..f0b132a0e 100644 --- a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java +++ b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandImpl.java @@ -26,7 +26,7 @@ public class LevelUpCommandImpl implements LevelUpCommand { } @Override - public void apply(McMMOPlayer player, PrimarySkillType primarySkillType, Set levelsGained) { + public void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set levelsGained) { if(!skills.contains(primarySkillType)) { return; } @@ -39,11 +39,15 @@ public class LevelUpCommandImpl implements LevelUpCommand { } else { LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commandStr); } - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commandStr); + executeCommand(); } } } + public void executeCommand() { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commandStr); + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandManager.java b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandManager.java index 212cf90e7..aaecc3fc3 100644 --- a/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandManager.java +++ b/src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommandManager.java @@ -28,7 +28,7 @@ public class LevelUpCommandManager { } for (LevelUpCommand command : commands) { - command.apply(mmoPlayer, primarySkillType, levelsGained); + command.process(mmoPlayer, primarySkillType, levelsGained); } } diff --git a/src/main/java/com/gmail/nossr50/listeners/SelfListener.java b/src/main/java/com/gmail/nossr50/listeners/SelfListener.java index 0e2d063f6..75089bb96 100644 --- a/src/main/java/com/gmail/nossr50/listeners/SelfListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/SelfListener.java @@ -62,9 +62,9 @@ public class SelfListener implements Listener { } final Set levelsAchieved = new LinkedHashSet<>(); - for(int i = 0; i < event.getLevelsGained(); i++) + for(int i = 1; i <= event.getLevelsGained(); i++) { - levelsAchieved.add(event.getSkillLevel()); + levelsAchieved.add(event.getSkillLevel() + i); } plugin.getLevelUpCommandManager().apply(mcMMOPlayer, skill, levelsAchieved); } diff --git a/src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java b/src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java index 66b872183..6430ce435 100644 --- a/src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java +++ b/src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java @@ -2,21 +2,15 @@ package com.gmail.nossr50.commands.levelup; import com.gmail.nossr50.MMOTestEnvironmentBasic; import com.gmail.nossr50.datatypes.experience.XPGainReason; -import com.gmail.nossr50.datatypes.player.McMMOPlayer; -import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; -import com.gmail.nossr50.listeners.SelfListener; import com.gmail.nossr50.mcMMO; -import org.bukkit.entity.Player; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.MockedStatic; import org.mockito.Mockito; import java.util.Set; -import java.util.UUID; import java.util.function.Predicate; import static org.mockito.ArgumentMatchers.any; @@ -35,8 +29,8 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic { } @Test - void levelInMiningShouldRunCommand() { - // validate command manager has zero registered commands + void levelInMiningShouldRunCommandFiveTimes() { + // GIVEN level up command for Mining should always execute for Mining level up assert mcMMO.p.getLevelUpCommandManager().isEmpty(); final PrimarySkillType skillType = PrimarySkillType.MINING; final Predicate predicate = (i) -> true; @@ -47,16 +41,66 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic { true)); mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand); - // GIVEN level up command that should always execute for Mining is registered with command manager + int levelsGained = 5; + // WHEN player gains 5 levels in mining + McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE); + selfListener.onPlayerLevelUp(event); + + // THEN the command should be checked for execution + verify(levelUpCommandManager).apply(any(), any(), any()); + verify(levelUpCommand).process(any(), any(), any()); + // THEN the command should have executed + verify(levelUpCommand, times(levelsGained)).executeCommand(); + } + + @Test + void levelInMiningShouldRunCommandAtLeastOnce() { + // GIVEN level up command for Mining should always execute for Mining level up + assert mcMMO.p.getLevelUpCommandManager().isEmpty(); + final PrimarySkillType skillType = PrimarySkillType.MINING; + final Predicate predicate = (i) -> true; + final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl( + predicate, + "say hello", + Set.of(skillType), + true)); + mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand); + + int levelsGained = 1; + // WHEN player gains 5 levels in mining + McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE); + selfListener.onPlayerLevelUp(event); + + // THEN the command should be checked for execution + verify(levelUpCommandManager).apply(any(), any(), any()); + verify(levelUpCommand).process(any(), any(), any()); + // THEN the command should have executed + verify(levelUpCommand).executeCommand(); + } + + @Test + void levelInMiningShouldNotRunCommand() { + // GIVEN level up command for Woodcutting should not execute for Mining level up + assert mcMMO.p.getLevelUpCommandManager().isEmpty(); + final PrimarySkillType skillType = PrimarySkillType.WOODCUTTING; + final Predicate predicate = (i) -> true; + final LevelUpCommand levelUpCommand = spy(new LevelUpCommandImpl( + predicate, + "say hello", + Set.of(skillType), + true)); + mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand); + int levelsGained = 5; // WHEN player gains 5 levels in mining McMMOPlayerLevelUpEvent event = new McMMOPlayerLevelUpEvent(player, PrimarySkillType.MINING, levelsGained, XPGainReason.PVE); selfListener.onPlayerLevelUp(event); - // THEN the command should be run - // check the mockito spy for level up command manager for executing the command - Mockito.verify(levelUpCommandManager).apply(any(), any(), any()); - Mockito.verify(levelUpCommand).apply(any(), any(), any()); + // THEN the command should be checked for execution + verify(levelUpCommandManager).apply(any(), any(), any()); + verify(levelUpCommand).process(any(), any(), any()); + // THEN the command should not be run + verify(levelUpCommand, never()).executeCommand(); } } \ No newline at end of file