Add power level commands to commands on level up

This commit is contained in:
nossr50
2024-01-28 16:57:26 -08:00
parent c5ec99ab51
commit 847095aff4
12 changed files with 537 additions and 248 deletions

View File

@@ -0,0 +1,4 @@
package com.gmail.nossr50.commands.levelup;
public interface CommandsOnLevel {
}

View File

@@ -1,105 +0,0 @@
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.Set;
import java.util.function.BiPredicate;
import static java.util.Objects.requireNonNull;
/**
* Represents a command to be executed on a level up
*/
public interface LevelUpCommand {
/**
* Process the command
*
* @param player the player
* @param primarySkillType the skill type
* @param levelsGained the levels gained
*/
void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained);
/**
* Execute the command
* @param player the player
* @param primarySkillType the skill
* @param level the level of the skill
*/
void executeCommand(McMMOPlayer player, PrimarySkillType primarySkillType, int level);
class LevelUpCommandBuilder {
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 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 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 LevelUpCommandBuilder withLevels(@NotNull Collection<Integer> levels) {
requireNonNull(levels, "levels is null!");
this.levels = Set.copyOf(levels);
return this;
}
public LevelUpCommandBuilder 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 LevelUpCommandBuilder withSkillFilter(@NotNull PrimarySkillType skill) {
requireNonNull(skill, "skill is null!");
this.skillFilter = Set.of(skill);
return this;
}
public LevelUpCommand build() {
requireNonNull(commands, "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);
}
}, commands, logInfo);
}
return new LevelUpCommandImpl(predicate, commands, logInfo);
}
}
}

View File

@@ -9,26 +9,32 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import static java.util.Objects.requireNonNull;
/**
* Manages commands to be executed on level up
*/
public class LevelUpCommandManager {
private final @NotNull Set<LevelUpCommand> commands;
private final @NotNull Set<SkillLevelUpCommand> skillLevelCommands;
private final @NotNull Set<PowerLevelUpCommand> powerLevelUpCommands;
private final @NotNull mcMMO plugin;
public LevelUpCommandManager(@NotNull mcMMO plugin) {
this.plugin = plugin;
this.commands = new HashSet<>();
this.plugin = requireNonNull(plugin, "plugin cannot be null");
this.skillLevelCommands = new HashSet<>();
this.powerLevelUpCommands = new HashSet<>();
}
/**
* 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);
public void registerCommand(@NotNull SkillLevelUpCommand skillLevelUpCommand) {
requireNonNull(skillLevelUpCommand, "skillLevelUpCommand cannot be null");
skillLevelCommands.add(skillLevelUpCommand);
mcMMO.p.getLogger().info("Registered level up command - SkillLevelUpCommand: " + skillLevelUpCommand);
}
public void registerCommand(@NotNull PowerLevelUpCommand powerLevelUpCommand) {
requireNonNull(powerLevelUpCommand, "powerLevelUpCommand cannot be null");
powerLevelUpCommands.add(powerLevelUpCommand);
mcMMO.p.getLogger().info("Registered level up command - PowerLevelUpCommand: " + powerLevelUpCommand);
}
/**
@@ -38,29 +44,41 @@ public class LevelUpCommandManager {
* @param primarySkillType the skill type
* @param levelsGained the levels gained
*/
public void apply(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
public void applySkillLevelUp(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
if (!mmoPlayer.getPlayer().isOnline()) {
return;
}
for (LevelUpCommand command : commands) {
for (SkillLevelUpCommand command : skillLevelCommands) {
command.process(mmoPlayer, primarySkillType, levelsGained);
}
}
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;
}
/**
* 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();
skillLevelCommands.clear();
powerLevelUpCommands.clear();
}
@Override
@@ -68,18 +86,19 @@ public class LevelUpCommandManager {
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);
return Objects.equals(skillLevelCommands, that.skillLevelCommands) && Objects.equals(powerLevelUpCommands, that.powerLevelUpCommands) && Objects.equals(plugin, that.plugin);
}
@Override
public int hashCode() {
return Objects.hash(commands, plugin);
return Objects.hash(skillLevelCommands, powerLevelUpCommands, plugin);
}
@Override
public String toString() {
return "LevelUpCommandManager{" +
"commands=" + commands +
"skillLevelCommands=" + skillLevelCommands +
", powerLevelUpCommands=" + powerLevelUpCommands +
", plugin=" + plugin +
'}';
}

View File

@@ -0,0 +1,98 @@
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) {
// TODO: Change this to debug later
mcMMO.p.getLogger().info("Executing commands for level up: " + commands);
for (String command : commands) {
// TODO: Change this to debug later
mcMMO.p.getLogger().info("Executing command: " + command);
String injectedCommand = injectedCommand(command, player, level);
// TODO: Remove verbose logging later
if (!injectedCommand.equalsIgnoreCase(command)) {
mcMMO.p.getLogger().info(("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, "%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 +
'}';
}
}

View File

@@ -0,0 +1,59 @@
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);
}
}

View File

@@ -12,25 +12,24 @@ import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
public class LevelUpCommandImpl implements LevelUpCommand {
public class SkillLevelUpCommand implements CommandsOnLevel {
private final BiPredicate<PrimarySkillType, Integer> predicate;
private final boolean logInfo;
private final @NotNull LinkedList<String> commands;
public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull String command, boolean logInfo) {
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 LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull LinkedList<String> commands, boolean logInfo) {
public SkillLevelUpCommand(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull LinkedList<String> commands, boolean logInfo) {
this.predicate = predicate;
this.commands = commands;
this.logInfo = logInfo;
}
@Override
public void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
for (int i : levelsGained) {
if (predicate.test(primarySkillType, i)) {
@@ -80,7 +79,7 @@ public class LevelUpCommandImpl implements LevelUpCommand {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LevelUpCommandImpl that = (LevelUpCommandImpl) o;
SkillLevelUpCommand that = (SkillLevelUpCommand) o;
return logInfo == that.logInfo && Objects.equals(predicate, that.predicate) && Objects.equals(commands, that.commands);
}

View File

@@ -0,0 +1,82 @@
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);
}
}

View File

@@ -1,6 +1,9 @@
package com.gmail.nossr50.config;
import com.gmail.nossr50.commands.levelup.LevelUpCommand;
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.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.LogUtils;
@@ -22,6 +25,7 @@ public class CommandOnLevelUpConfig extends BukkitConfig {
public static final String CONDITION_SECTION = "condition";
public static final String ENABLED = "enabled";
public static final String COMMANDS = "commands";
public static final String POWER_LEVEL_SECTION = "power_level";
public CommandOnLevelUpConfig(@NotNull File dataFolder) {
super("levelupcommands.yml", dataFolder);
@@ -44,20 +48,26 @@ public class CommandOnLevelUpConfig extends BukkitConfig {
continue;
}
LevelUpCommand levelUpCommand = buildCommand(commandSection);
if (levelUpCommand == null) {
SkillLevelUpCommand skillLevelUpCommand = buildSkillLevelUpCommand(commandSection);
PowerLevelUpCommand powerLevelUpCommand = buildPowerLevelUpCommand(commandSection);
if (skillLevelUpCommand == null && powerLevelUpCommand == null) {
mcMMO.p.getLogger().severe("Invalid command format for key: " + key);
continue;
} else {
mcMMO.p.getLogger().info("Command successfully loaded from config for key: " + key);
mcMMO.p.getLevelUpCommandManager().registerCommand(levelUpCommand);
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);
}
}
}
}
@Nullable
private LevelUpCommand buildCommand(final ConfigurationSection commandSection) {
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
private @NotNull SkillLevelUpCommand buildSkillLevelUpCommand(final ConfigurationSection commandSection) {
SkillLevelUpCommandBuilder builder = new SkillLevelUpCommandBuilder();
// check if command is enabled
if (!commandSection.getBoolean(ENABLED, true)) {
return null;
@@ -129,6 +139,60 @@ public class CommandOnLevelUpConfig extends BukkitConfig {
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);
}

View File

@@ -54,7 +54,6 @@ import com.gmail.nossr50.util.sounds.SoundManager;
import com.gmail.nossr50.util.sounds.SoundType;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.block.Block;

View File

@@ -60,11 +60,18 @@ public class SelfListener implements Listener {
}
final Set<Integer> levelsAchieved = new LinkedHashSet<>();
final Set<Integer> powerLevelsAchieved = new LinkedHashSet<>();
int startingLevel = event.getSkillLevel() - event.getLevelsGained();
int startingPowerLevel = mcMMOPlayer.getPowerLevel() - event.getLevelsGained();
for (int i = 0; i < event.getLevelsGained(); i++) {
levelsAchieved.add(startingLevel + (i + 1));
}
plugin.getLevelUpCommandManager().apply(mcMMOPlayer, skill, levelsAchieved);
for (int i = 0; i < event.getLevelsGained(); i++) {
powerLevelsAchieved.add(startingPowerLevel + (i + 1));
}
plugin.getLevelUpCommandManager().applySkillLevelUp(mcMMOPlayer, skill, levelsAchieved);
plugin.getLevelUpCommandManager().applyPowerLevelUp(mcMMOPlayer, powerLevelsAchieved);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)