Add builder, switch to BiPredicate for impl

This commit is contained in:
nossr50 2023-07-30 16:23:41 -07:00
parent eb8c5bf0e9
commit 8ee282beec
9 changed files with 177 additions and 44 deletions

View File

@ -4,6 +4,9 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
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
@ -22,4 +25,58 @@ public interface LevelUpCommand {
* Execute the command
*/
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);
}
}
}

View File

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

View File

@ -6,8 +6,12 @@ import com.gmail.nossr50.mcMMO;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* Manages commands to be executed on level up
*/
public class LevelUpCommandManager {
private final @NotNull Set<LevelUpCommand> commands;
private final @NotNull mcMMO plugin;
@ -17,11 +21,23 @@ public class LevelUpCommandManager {
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) {
if (!mmoPlayer.getPlayer().isOnline()) {
return;
@ -32,12 +48,39 @@ public class LevelUpCommandManager {
}
}
/**
* Clear all registered commands
*/
public void clear() {
mcMMO.p.getLogger().info("Clearing registered commands on level up");
commands.clear();
}
/**
* @return true if there are no registered commands
*/
public boolean 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 +
'}';
}
}

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.BiPredicate;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ -32,13 +32,9 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
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<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);
int levelsGained = 5;
@ -57,13 +53,9 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
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<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);
int levelsGained = 1;
@ -82,13 +74,9 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
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<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);
@ -103,4 +91,23 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
// THEN the command should not be run
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());
}
}

View File

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

View File

@ -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'