mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-08-03 21:15:28 +02:00
(WIP) reworking level up commands to have multiple requirements
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.gmail.nossr50.commands.levelup;
|
||||
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.util.LogUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.VisibleForTesting;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class LevelUpCommand implements CommandsOnLevel {
|
||||
private final @Nullable List<BiPredicate<PrimarySkillType, Integer>> conditions;
|
||||
private final @Nullable Predicate<Integer> powerLevelCondition;
|
||||
private final boolean logInfo;
|
||||
private final @NotNull LinkedList<String> commands;
|
||||
|
||||
public LevelUpCommand(@Nullable List<BiPredicate<PrimarySkillType, Integer>> conditions,
|
||||
@Nullable Predicate<Integer> powerLevelCondition,
|
||||
@NotNull LinkedList<String> commands, boolean logInfo) {
|
||||
this.conditions = conditions;
|
||||
this.powerLevelCondition = powerLevelCondition;
|
||||
if (conditions == null && powerLevelCondition == null)
|
||||
throw new IllegalArgumentException("At least one condition must be set");
|
||||
this.commands = commands;
|
||||
this.logInfo = logInfo;
|
||||
}
|
||||
|
||||
public void process(@NotNull McMMOPlayer player, @NotNull PrimarySkillType primarySkillType, @NotNull Set<Integer> levelsGained,
|
||||
@NotNull Set<Integer> powerLevelsGained) {
|
||||
// each predicate has to pass at least once
|
||||
// we check the predicates against all levels gained to see if they pass at least once
|
||||
// if all predicates pass at least once, we execute the command
|
||||
boolean allConditionsPass = (conditions == null) || conditions.stream().allMatch(predicate -> levelsGained.stream().anyMatch(level -> predicate.test(primarySkillType, level)));
|
||||
// we also check the power level predicate to see if it passes at least once, if this predicate is null, we mark it as passed
|
||||
boolean powerLevelConditionPass = (powerLevelCondition == null) || powerLevelsGained.stream().anyMatch(powerLevelCondition);
|
||||
if (allConditionsPass && powerLevelConditionPass) {
|
||||
executeCommand(player);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void executeCommand(@NotNull McMMOPlayer player) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing level up commands: " + commands);
|
||||
for (String command : commands) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + command);
|
||||
String injectedCommand = injectedCommand(command, player);
|
||||
if (!injectedCommand.equalsIgnoreCase(command)) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), ("Command has been injected with new values: " + injectedCommand));
|
||||
}
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), injectedCommand);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
String injectedCommand(String command, McMMOPlayer player) {
|
||||
// TODO: unit tests
|
||||
StringBuilder commandBuilder = new StringBuilder(command);
|
||||
|
||||
// Replace %player% with player name
|
||||
replaceAll(commandBuilder, "{@player}", player.getPlayer().getName());
|
||||
|
||||
// Replace each skill level
|
||||
for (PrimarySkillType primarySkillType : PrimarySkillType.values()) {
|
||||
if (primarySkillType == PrimarySkillType.SMELTING || primarySkillType == PrimarySkillType.SALVAGE) {
|
||||
continue;
|
||||
}
|
||||
replaceAll(commandBuilder, "{@" + primarySkillType.name().toLowerCase() + "_level}",
|
||||
String.valueOf(player.getSkillLevel(primarySkillType)));
|
||||
}
|
||||
|
||||
// Replace power level
|
||||
replaceAll(commandBuilder, "{@power_level}", String.valueOf(player.getPowerLevel()));
|
||||
|
||||
return commandBuilder.toString();
|
||||
}
|
||||
|
||||
private void replaceAll(StringBuilder builder, String from, String to) {
|
||||
int index = builder.indexOf(from);
|
||||
while (index != -1) {
|
||||
builder.replace(index, index + from.length(), to);
|
||||
index = builder.indexOf(from, index + to.length());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LevelUpCommand that = (LevelUpCommand) o;
|
||||
return logInfo == that.logInfo && Objects.equals(conditions, that.conditions) && Objects.equals(commands, that.commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(conditions, logInfo, commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SkillLevelUpCommand{" +
|
||||
"conditions=" + conditions +
|
||||
", logInfo=" + logInfo +
|
||||
", commands=" + commands +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
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.List;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class LevelUpCommandBuilder {
|
||||
private LinkedList<String> commands = null;
|
||||
private List<BiPredicate<PrimarySkillType, Integer>> conditions = null;
|
||||
private Predicate<Integer> powerLevelCondition = null;
|
||||
private boolean logInfo;
|
||||
|
||||
public LevelUpCommandBuilder() {
|
||||
this.logInfo = false;
|
||||
}
|
||||
|
||||
public LevelUpCommandBuilder withPredicate(BiPredicate<PrimarySkillType, Integer> condition) {
|
||||
if (this.conditions == null) {
|
||||
this.conditions = new LinkedList<>();
|
||||
}
|
||||
|
||||
conditions.add(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LevelUpCommandBuilder withPowerLevelCondition(Predicate<Integer> powerLevelCondition) {
|
||||
if (this.powerLevelCondition != null) {
|
||||
throw new IllegalStateException("power level condition already set");
|
||||
}
|
||||
|
||||
this.powerLevelCondition = powerLevelCondition;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LevelUpCommandBuilder withConditions(
|
||||
@NotNull Collection<BiPredicate<PrimarySkillType, Integer>> conditions) {
|
||||
if (this.conditions == null) {
|
||||
this.conditions = new LinkedList<>();
|
||||
} else {
|
||||
throw new IllegalStateException("conditions already set");
|
||||
}
|
||||
|
||||
this.conditions.addAll(conditions);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LevelUpCommandBuilder withLogInfo(boolean logInfo) {
|
||||
this.logInfo = logInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public LevelUpCommandBuilder command(@NotNull String command) {
|
||||
this.commands = new LinkedList<>();
|
||||
this.commands.add(command);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LevelUpCommandBuilder commands(@NotNull Collection<String> command) {
|
||||
this.commands = new LinkedList<>(command);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LevelUpCommand build() {
|
||||
if (conditions == null && powerLevelCondition == null) {
|
||||
throw new IllegalStateException("no conditions found for level up command");
|
||||
}
|
||||
requireNonNull(commands, "no commands found for level up command");
|
||||
|
||||
return new LevelUpCommand(conditions, powerLevelCondition, commands, logInfo);
|
||||
}
|
||||
}
|
@@ -16,26 +16,18 @@ import static java.util.Objects.requireNonNull;
|
||||
* Manages commands to be executed on level up
|
||||
*/
|
||||
public class LevelUpCommandManager {
|
||||
private final @NotNull Set<SkillLevelUpCommand> skillLevelCommands;
|
||||
private final @NotNull Set<PowerLevelUpCommand> powerLevelUpCommands;
|
||||
private final @NotNull Set<LevelUpCommand> levelUpCommands;
|
||||
private final @NotNull mcMMO plugin;
|
||||
|
||||
public LevelUpCommandManager(@NotNull mcMMO plugin) {
|
||||
this.plugin = requireNonNull(plugin, "plugin cannot be null");
|
||||
this.skillLevelCommands = new HashSet<>();
|
||||
this.powerLevelUpCommands = new HashSet<>();
|
||||
this.levelUpCommands = new HashSet<>();
|
||||
}
|
||||
|
||||
public void registerCommand(@NotNull SkillLevelUpCommand skillLevelUpCommand) {
|
||||
requireNonNull(skillLevelUpCommand, "skillLevelUpCommand cannot be null");
|
||||
skillLevelCommands.add(skillLevelUpCommand);
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Registered level up command - SkillLevelUpCommand: " + skillLevelUpCommand);
|
||||
}
|
||||
|
||||
public void registerCommand(@NotNull PowerLevelUpCommand powerLevelUpCommand) {
|
||||
requireNonNull(powerLevelUpCommand, "powerLevelUpCommand cannot be null");
|
||||
powerLevelUpCommands.add(powerLevelUpCommand);
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Registered level up command - PowerLevelUpCommand: " + powerLevelUpCommand);
|
||||
public void registerCommand(@NotNull LevelUpCommand levelUpCommand) {
|
||||
requireNonNull(levelUpCommand, "skillLevelUpCommand cannot be null");
|
||||
levelUpCommands.add(levelUpCommand);
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Registered level up command - SkillLevelUpCommand: " + levelUpCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,32 +37,19 @@ public class LevelUpCommandManager {
|
||||
* @param primarySkillType the skill type
|
||||
* @param levelsGained the levels gained
|
||||
*/
|
||||
public void applySkillLevelUp(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
|
||||
public void applySkillLevelUp(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType,
|
||||
Set<Integer> levelsGained, Set<Integer> powerLevelsGained) {
|
||||
if (!mmoPlayer.getPlayer().isOnline()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (SkillLevelUpCommand command : skillLevelCommands) {
|
||||
command.process(mmoPlayer, primarySkillType, levelsGained);
|
||||
for (LevelUpCommand command : levelUpCommands) {
|
||||
command.process(mmoPlayer, primarySkillType, levelsGained, powerLevelsGained);
|
||||
}
|
||||
}
|
||||
|
||||
public void applyPowerLevelUp(@NotNull McMMOPlayer mmoPlayer, Set<Integer> levelsGained) {
|
||||
if (!mmoPlayer.getPlayer().isOnline()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (PowerLevelUpCommand command : powerLevelUpCommands) {
|
||||
command.process(mmoPlayer, levelsGained);
|
||||
}
|
||||
}
|
||||
|
||||
public @NotNull Set<SkillLevelUpCommand> getSkillLevelCommands() {
|
||||
return skillLevelCommands;
|
||||
}
|
||||
|
||||
public @NotNull Set<PowerLevelUpCommand> getPowerLevelUpCommands() {
|
||||
return powerLevelUpCommands;
|
||||
public @NotNull Set<LevelUpCommand> getLevelUpCommands() {
|
||||
return levelUpCommands;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,8 +57,7 @@ public class LevelUpCommandManager {
|
||||
*/
|
||||
public void clear() {
|
||||
mcMMO.p.getLogger().info("Clearing registered commands on level up");
|
||||
skillLevelCommands.clear();
|
||||
powerLevelUpCommands.clear();
|
||||
levelUpCommands.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,19 +65,18 @@ public class LevelUpCommandManager {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
LevelUpCommandManager that = (LevelUpCommandManager) o;
|
||||
return Objects.equals(skillLevelCommands, that.skillLevelCommands) && Objects.equals(powerLevelUpCommands, that.powerLevelUpCommands) && Objects.equals(plugin, that.plugin);
|
||||
return Objects.equals(levelUpCommands, that.levelUpCommands) && Objects.equals(plugin, that.plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(skillLevelCommands, powerLevelUpCommands, plugin);
|
||||
return Objects.hash(levelUpCommands, plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LevelUpCommandManager{" +
|
||||
"skillLevelCommands=" + skillLevelCommands +
|
||||
", powerLevelUpCommands=" + powerLevelUpCommands +
|
||||
"levelUpCommands=" + levelUpCommands +
|
||||
", plugin=" + plugin +
|
||||
'}';
|
||||
}
|
||||
|
@@ -1,96 +0,0 @@
|
||||
package com.gmail.nossr50.commands.levelup;
|
||||
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.util.LogUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class PowerLevelUpCommand implements CommandsOnLevel {
|
||||
private final Predicate<Integer> predicate;
|
||||
private final boolean logInfo;
|
||||
private final @NotNull LinkedList<String> commands;
|
||||
|
||||
public PowerLevelUpCommand(@NotNull Predicate<Integer> predicate, @NotNull String command, boolean logInfo) {
|
||||
this.predicate = predicate;
|
||||
this.commands = new LinkedList<>();
|
||||
this.commands.add(command);
|
||||
this.logInfo = logInfo;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommand(@NotNull Predicate<Integer> predicate, @NotNull LinkedList<String> commands, boolean logInfo) {
|
||||
this.predicate = predicate;
|
||||
this.commands = commands;
|
||||
this.logInfo = logInfo;
|
||||
}
|
||||
|
||||
public void process(McMMOPlayer player, Set<Integer> levelsGained) {
|
||||
for (int i : levelsGained) {
|
||||
if (predicate.test(i)) {
|
||||
// execute command via server console in Bukkit
|
||||
if(logInfo) {
|
||||
mcMMO.p.getLogger().info("Executing command: " + commands);
|
||||
} else {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commands);
|
||||
}
|
||||
executeCommand(player, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void executeCommand(McMMOPlayer player, int level) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing commands for level up: " + commands);
|
||||
for (String command : commands) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + command);
|
||||
String injectedCommand = injectedCommand(command, player, level);
|
||||
if (!injectedCommand.equalsIgnoreCase(command)) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), ("Command has been injected with new values: " + injectedCommand));
|
||||
}
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), injectedCommand);
|
||||
}
|
||||
}
|
||||
|
||||
private String injectedCommand(String command, McMMOPlayer player, int level) {
|
||||
// replace %player% with player name, %skill% with skill name, and %level% with level
|
||||
command = safeReplace(command, "%player%", player.getPlayer().getName());
|
||||
command = safeReplace(command, "%power_level%", "power level");
|
||||
command = safeReplace(command, "%skill%", "power level");
|
||||
command = safeReplace(command, "%level%", String.valueOf(level));
|
||||
return command;
|
||||
}
|
||||
|
||||
private String safeReplace(String targetStr, String toReplace, String replacement) {
|
||||
if (replacement == null) {
|
||||
return targetStr;
|
||||
}
|
||||
|
||||
return targetStr.replace(toReplace, replacement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PowerLevelUpCommand that = (PowerLevelUpCommand) o;
|
||||
return logInfo == that.logInfo && Objects.equals(predicate, that.predicate) && Objects.equals(commands, that.commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(predicate, logInfo, commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PowerLevelUpCommand{" +
|
||||
"predicate=" + predicate +
|
||||
", logInfo=" + logInfo +
|
||||
", commands=" + commands +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
package com.gmail.nossr50.commands.levelup;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class PowerLevelUpCommandBuilder {
|
||||
private Set<Integer> levels = null;
|
||||
private LinkedList<String> commands = null;
|
||||
private Predicate<Integer> predicate = null;
|
||||
private boolean logInfo;
|
||||
|
||||
public PowerLevelUpCommandBuilder() {
|
||||
this.logInfo = false;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommandBuilder withPredicate(Predicate<Integer> predicate) {
|
||||
this.predicate = predicate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommandBuilder withLogInfo(boolean logInfo) {
|
||||
this.logInfo = logInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommandBuilder command(@NotNull String command) {
|
||||
this.commands = new LinkedList<>();
|
||||
this.commands.add(command);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommandBuilder commands(@NotNull Collection<String> command) {
|
||||
this.commands = new LinkedList<>(command);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommandBuilder withLevels(@NotNull Collection<Integer> levels) {
|
||||
requireNonNull(levels, "levels is null!");
|
||||
this.levels = Set.copyOf(levels);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PowerLevelUpCommand build() {
|
||||
requireNonNull(commands, "commandStr is null");
|
||||
if (predicate == null) {
|
||||
requireNonNull(levels, "levels is null");
|
||||
|
||||
return new PowerLevelUpCommand((level) -> levels.contains(level), commands, logInfo);
|
||||
}
|
||||
|
||||
return new PowerLevelUpCommand(predicate, commands, logInfo);
|
||||
}
|
||||
}
|
@@ -1,96 +0,0 @@
|
||||
package com.gmail.nossr50.commands.levelup;
|
||||
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.util.LogUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
public class SkillLevelUpCommand implements CommandsOnLevel {
|
||||
private final BiPredicate<PrimarySkillType, Integer> predicate;
|
||||
private final boolean logInfo;
|
||||
private final @NotNull LinkedList<String> commands;
|
||||
|
||||
public SkillLevelUpCommand(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull String command, boolean logInfo) {
|
||||
this.predicate = predicate;
|
||||
this.commands = new LinkedList<>();
|
||||
this.commands.add(command);
|
||||
this.logInfo = logInfo;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommand(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull LinkedList<String> commands, boolean logInfo) {
|
||||
this.predicate = predicate;
|
||||
this.commands = commands;
|
||||
this.logInfo = logInfo;
|
||||
}
|
||||
|
||||
public void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
|
||||
for (int i : levelsGained) {
|
||||
if (predicate.test(primarySkillType, i)) {
|
||||
// execute command via server console in Bukkit
|
||||
if(logInfo) {
|
||||
mcMMO.p.getLogger().info("Executing command: " + commands);
|
||||
} else {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commands);
|
||||
}
|
||||
executeCommand(player, primarySkillType, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void executeCommand(McMMOPlayer player, PrimarySkillType primarySkillType, int level) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing commands for level up: " + commands);
|
||||
for (String command : commands) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + command);
|
||||
String injectedCommand = injectedCommand(command, player, primarySkillType, level);
|
||||
if (!injectedCommand.equalsIgnoreCase(command)) {
|
||||
LogUtils.debug(mcMMO.p.getLogger(), ("Command has been injected with new values: " + injectedCommand));
|
||||
}
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), injectedCommand);
|
||||
}
|
||||
}
|
||||
|
||||
private String injectedCommand(String command, McMMOPlayer player, PrimarySkillType primarySkillType, int level) {
|
||||
// replace %player% with player name, %skill% with skill name, and %level% with level
|
||||
command = safeReplace(command, "%player%", player.getPlayer().getName());
|
||||
command = safeReplace(command, "%skill%", primarySkillType.getName());
|
||||
command = safeReplace(command, "%level%", String.valueOf(level));
|
||||
return command;
|
||||
}
|
||||
|
||||
private String safeReplace(String targetStr, String toReplace, String replacement) {
|
||||
if (replacement == null) {
|
||||
return targetStr;
|
||||
}
|
||||
|
||||
return targetStr.replace(toReplace, replacement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
SkillLevelUpCommand that = (SkillLevelUpCommand) o;
|
||||
return logInfo == that.logInfo && Objects.equals(predicate, that.predicate) && Objects.equals(commands, that.commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(predicate, logInfo, commands);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LevelUpCommandImpl{" +
|
||||
"predicate=" + predicate +
|
||||
", logInfo=" + logInfo +
|
||||
", commandStr='" + commands + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -1,82 +0,0 @@
|
||||
package com.gmail.nossr50.commands.levelup;
|
||||
|
||||
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;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public class SkillLevelUpCommandBuilder {
|
||||
private Set<PrimarySkillType> skillFilter = null;
|
||||
private Set<Integer> levels = null;
|
||||
private LinkedList<String> commands = null;
|
||||
private BiPredicate<PrimarySkillType, Integer> predicate = null;
|
||||
private boolean logInfo;
|
||||
|
||||
public SkillLevelUpCommandBuilder() {
|
||||
this.logInfo = false;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommandBuilder withPredicate(BiPredicate<PrimarySkillType, Integer> predicate) {
|
||||
this.predicate = predicate;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommandBuilder withLogInfo(boolean logInfo) {
|
||||
this.logInfo = logInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommandBuilder command(@NotNull String command) {
|
||||
this.commands = new LinkedList<>();
|
||||
this.commands.add(command);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommandBuilder commands(@NotNull Collection<String> command) {
|
||||
this.commands = new LinkedList<>(command);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommandBuilder withLevels(@NotNull Collection<Integer> levels) {
|
||||
requireNonNull(levels, "levels is null!");
|
||||
this.levels = Set.copyOf(levels);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommandBuilder 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 SkillLevelUpCommandBuilder withSkillFilter(@NotNull PrimarySkillType skill) {
|
||||
requireNonNull(skill, "skill is null!");
|
||||
this.skillFilter = Set.of(skill);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SkillLevelUpCommand build() {
|
||||
requireNonNull(commands, "commandStr is null");
|
||||
if (predicate == null) {
|
||||
requireNonNull(levels, "levels is null");
|
||||
|
||||
return new SkillLevelUpCommand((skill, level) -> {
|
||||
if (skillFilter == null) {
|
||||
return levels.contains(level);
|
||||
} else {
|
||||
return skillFilter.contains(skill) && levels.contains(level);
|
||||
}
|
||||
}, commands, logInfo);
|
||||
}
|
||||
|
||||
return new SkillLevelUpCommand(predicate, commands, logInfo);
|
||||
}
|
||||
}
|
@@ -1,9 +1,7 @@
|
||||
package com.gmail.nossr50.config;
|
||||
|
||||
import com.gmail.nossr50.commands.levelup.PowerLevelUpCommand;
|
||||
import com.gmail.nossr50.commands.levelup.PowerLevelUpCommandBuilder;
|
||||
import com.gmail.nossr50.commands.levelup.SkillLevelUpCommand;
|
||||
import com.gmail.nossr50.commands.levelup.SkillLevelUpCommandBuilder;
|
||||
import com.gmail.nossr50.commands.levelup.LevelUpCommand;
|
||||
import com.gmail.nossr50.commands.levelup.LevelUpCommandBuilder;
|
||||
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.util.LogUtils;
|
||||
@@ -48,153 +46,89 @@ public class CommandOnLevelUpConfig extends BukkitConfig {
|
||||
continue;
|
||||
}
|
||||
|
||||
SkillLevelUpCommand skillLevelUpCommand = buildSkillLevelUpCommand(commandSection);
|
||||
PowerLevelUpCommand powerLevelUpCommand = buildPowerLevelUpCommand(commandSection);
|
||||
LevelUpCommand levelUpCommand = buildSkillLevelUpCommand(commandSection);
|
||||
|
||||
if (skillLevelUpCommand == null && powerLevelUpCommand == null) {
|
||||
if (levelUpCommand == null) {
|
||||
mcMMO.p.getLogger().severe("Invalid command format for key: " + key);
|
||||
} else {
|
||||
if(skillLevelUpCommand != null) {
|
||||
mcMMO.p.getLevelUpCommandManager().registerCommand(skillLevelUpCommand);
|
||||
mcMMO.p.getLogger().info("Skill Level up command successfully loaded from config for key: " + key);
|
||||
}
|
||||
if(powerLevelUpCommand != null) {
|
||||
mcMMO.p.getLevelUpCommandManager().registerCommand(powerLevelUpCommand);
|
||||
mcMMO.p.getLogger().info("Power Level up command successfully loaded from config for key: " + key);
|
||||
}
|
||||
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
|
||||
mcMMO.p.getLogger().info("Level up command successfully loaded from config for key: " + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private @Nullable SkillLevelUpCommand buildSkillLevelUpCommand(final ConfigurationSection commandSection) {
|
||||
SkillLevelUpCommandBuilder builder = new SkillLevelUpCommandBuilder();
|
||||
// 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 @Nullable PowerLevelUpCommand buildPowerLevelUpCommand(final ConfigurationSection commandSection) {
|
||||
PowerLevelUpCommandBuilder builder = new PowerLevelUpCommandBuilder();
|
||||
// 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;
|
||||
}
|
||||
|
||||
// No power level condition
|
||||
if (!condition.contains(POWER_LEVEL_SECTION)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// for now only simple condition is supported
|
||||
if (!condition.contains(LEVELS_SECTION)) {
|
||||
mcMMO.p.getLogger().severe("No condition.levels section found for power level 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 power level 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 power level 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);
|
||||
private @Nullable LevelUpCommand buildSkillLevelUpCommand(final ConfigurationSection commandSection) {
|
||||
// TODO: Rework
|
||||
return null;
|
||||
// LevelUpCommandBuilder builder = new 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();
|
||||
}
|
||||
}
|
||||
|
@@ -70,8 +70,7 @@ public class SelfListener implements Listener {
|
||||
powerLevelsAchieved.add(startingPowerLevel + (i + 1));
|
||||
}
|
||||
|
||||
plugin.getLevelUpCommandManager().applySkillLevelUp(mcMMOPlayer, skill, levelsAchieved);
|
||||
plugin.getLevelUpCommandManager().applyPowerLevelUp(mcMMOPlayer, powerLevelsAchieved);
|
||||
plugin.getLevelUpCommandManager().applySkillLevelUp(mcMMOPlayer, skill, levelsAchieved, powerLevelsAchieved);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
|
Reference in New Issue
Block a user