mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-08-04 05:25:28 +02:00
Add some string injection for commands
This commit is contained in:
@@ -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;
|
||||
|
@@ -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,36 +36,57 @@ 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
|
||||
public boolean equals(Object o) {
|
||||
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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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());
|
||||
|
||||
|
Reference in New Issue
Block a user