Add some string injection for commands

This commit is contained in:
nossr50 2023-08-20 14:28:53 -07:00
parent 899a37edbc
commit 02cacb9e94
6 changed files with 55 additions and 29 deletions

View File

@ -26,8 +26,11 @@ public interface LevelUpCommand {
/**
* Execute the command
* @param player the player
* @param primarySkillType the skill
* @param level the level of the skill
*/
void executeCommand();
void executeCommand(McMMOPlayer player, PrimarySkillType primarySkillType, int level);
class LevelUpCommandBuilder {
private Set<PrimarySkillType> skillFilter = null;

View File

@ -7,7 +7,6 @@ import com.gmail.nossr50.util.LogUtils;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Set;
@ -16,18 +15,18 @@ import java.util.function.BiPredicate;
public class LevelUpCommandImpl implements LevelUpCommand {
private final BiPredicate<PrimarySkillType, Integer> predicate;
private final boolean logInfo;
private final @NotNull LinkedList<String> commandStr;
private final @NotNull LinkedList<String> commands;
public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull String commandStr, boolean logInfo) {
public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull String command, boolean logInfo) {
this.predicate = predicate;
this.commandStr = new LinkedList<>();
this.commandStr.add(commandStr);
this.commands = new LinkedList<>();
this.commands.add(command);
this.logInfo = logInfo;
}
public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull LinkedList<String> commandStr, boolean logInfo) {
public LevelUpCommandImpl(@NotNull BiPredicate<PrimarySkillType, Integer> predicate, @NotNull LinkedList<String> commands, boolean logInfo) {
this.predicate = predicate;
this.commandStr = commandStr;
this.commands = commands;
this.logInfo = logInfo;
}
@ -37,23 +36,44 @@ public class LevelUpCommandImpl implements LevelUpCommand {
if (predicate.test(primarySkillType, i)) {
// execute command via server console in Bukkit
if(logInfo) {
mcMMO.p.getLogger().info("Executing command: " + commandStr);
mcMMO.p.getLogger().info("Executing command: " + commands);
} else {
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commandStr);
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commands);
}
executeCommand();
executeCommand(player, primarySkillType, i);
}
}
}
public void executeCommand() {
public void executeCommand(McMMOPlayer player, PrimarySkillType primarySkillType, int level) {
// TODO: Change this to debug later
mcMMO.p.getLogger().info("Executing commands for level up: " + commandStr);
for (String command : commandStr) {
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);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
String injectedCommand = injectedCommand(command, player, primarySkillType, 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, 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
@ -61,12 +81,12 @@ 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(predicate, that.predicate) && Objects.equals(commandStr, that.commandStr);
return logInfo == that.logInfo && Objects.equals(predicate, that.predicate) && Objects.equals(commands, that.commands);
}
@Override
public int hashCode() {
return Objects.hash(predicate, logInfo, commandStr);
return Objects.hash(predicate, logInfo, commands);
}
@Override
@ -74,7 +94,7 @@ public class LevelUpCommandImpl implements LevelUpCommand {
return "LevelUpCommandImpl{" +
"predicate=" + predicate +
", logInfo=" + logInfo +
", commandStr='" + commandStr + '\'' +
", commandStr='" + commands + '\'' +
'}';
}
}

View File

@ -24,7 +24,9 @@ public class CommandOnLevelUpConfig extends BukkitConfig {
public static final String COMMANDS = "commands";
public CommandOnLevelUpConfig(@NotNull File dataFolder) {
super("commandonlevelup", dataFolder);
super("levelupcommands.yml", dataFolder);
// TODO: loadKeys() should really get called in super
loadKeys();
}
@Override

View File

@ -167,6 +167,7 @@ public class mcMMO extends JavaPlugin {
skillTools = new SkillTools(this); //Load after general config
//Init configs
levelUpCommandManager = new LevelUpCommandManager(this);
advancedConfig = new AdvancedConfig(getDataFolder());
commandOnLevelUpConfig = new CommandOnLevelUpConfig(getDataFolder());

View File

@ -1,6 +1,6 @@
level_up_commands:
unique_id_here:
enabled: false
enabled: true
condition:
skills:
- 'Swords'
@ -12,12 +12,11 @@ level_up_commands:
run_command_as: 'CONSOLE'
log_level: 'INFO'
other_unique_id_here:
enabled: false
enabled: true
condition:
complex_condition:
source: 'player.getLevel() % 2 == 0'
levels: [1, 2, 3, 4, 5]
commands:
- "say %player% reached an even level!"
- "say this command should execute for all skills, %player%!"
- "say Isn't that fun?"
run_command_as: 'CONSOLE'
log_level: 'DEBUG'

View File

@ -2,6 +2,7 @@ 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.skills.PrimarySkillType;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
import com.gmail.nossr50.mcMMO;
@ -46,7 +47,7 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
verify(levelUpCommandManager).apply(any(), any(), any());
verify(levelUpCommand).process(any(), any(), any());
// THEN the command should have executed
verify(levelUpCommand, times(levelsGained)).executeCommand();
verify(levelUpCommand, times(levelsGained)).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
}
@Test
@ -67,7 +68,7 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
verify(levelUpCommandManager).apply(any(), any(), any());
verify(levelUpCommand).process(any(), any(), any());
// THEN the command should have executed
verify(levelUpCommand).executeCommand();
verify(levelUpCommand).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
}
@Test
@ -89,12 +90,12 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
verify(levelUpCommandManager).apply(any(), any(), any());
verify(levelUpCommand).process(any(), any(), any());
// THEN the command should not be run
verify(levelUpCommand, never()).executeCommand();
verify(levelUpCommand, never()).executeCommand(any(McMMOPlayer.class), any(PrimarySkillType.class), anyInt());
}
private LevelUpCommand buildLevelUpCommand(String commandStr, Set<Integer> levels, Set<PrimarySkillType> skillFilter) {
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
builder.commandString(commandStr)
builder.command(commandStr)
.withLevels(levels)
.withLogInfo(true);
if (skillFilter != null) {
@ -106,7 +107,7 @@ class LevelUpCommandTest extends MMOTestEnvironmentBasic {
private LevelUpCommand buildLevelUpCommand(String commandStr, BiPredicate<PrimarySkillType, Integer> predicate) {
LevelUpCommand.LevelUpCommandBuilder builder = new LevelUpCommand.LevelUpCommandBuilder();
builder.commandString(commandStr)
builder.command(commandStr)
.withPredicate(predicate)
.withLogInfo(true);
return Mockito.spy(builder.build());