mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-08-06 06:25:27 +02:00
Add builder, switch to BiPredicate for impl
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user