-
-
Notifications
You must be signed in to change notification settings - Fork 875
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add power level commands to commands on level up
Showing
12 changed files
with
537 additions
and
248 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
src/main/java/com/gmail/nossr50/commands/levelup/CommandsOnLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.gmail.nossr50.commands.levelup; | ||
|
||
public interface CommandsOnLevel { | ||
} |
105 changes: 0 additions & 105 deletions
105
src/main/java/com/gmail/nossr50/commands/levelup/LevelUpCommand.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/com/gmail/nossr50/commands/levelup/PowerLevelUpCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + | ||
'}'; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/gmail/nossr50/commands/levelup/PowerLevelUpCommandBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/com/gmail/nossr50/commands/levelup/SkillLevelUpCommandBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 138 additions & 75 deletions
213
src/test/java/com/gmail/nossr50/commands/levelup/LevelUpCommandTest.java
Large diffs are not rendered by default.
Oops, something went wrong.