LevelUpCommandImpl.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.gmail.nossr50.commands.levelup;
  2. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  3. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  4. import com.gmail.nossr50.mcMMO;
  5. import com.gmail.nossr50.util.LogUtils;
  6. import org.bukkit.Bukkit;
  7. import org.jetbrains.annotations.NotNull;
  8. import java.util.Objects;
  9. import java.util.Set;
  10. import java.util.function.Predicate;
  11. public class LevelUpCommandImpl implements LevelUpCommand {
  12. private final @NotNull Predicate<Integer> shouldApply;
  13. private final boolean logInfo;
  14. private final @NotNull String commandStr;
  15. private final @NotNull Set<PrimarySkillType> skills;
  16. public LevelUpCommandImpl(@NotNull Predicate<Integer> shouldApply, @NotNull String commandStr, @NotNull Set<PrimarySkillType> skills, boolean logInfo) {
  17. this.shouldApply = shouldApply;
  18. this.commandStr = commandStr;
  19. this.skills = skills;
  20. this.logInfo = logInfo;
  21. }
  22. @Override
  23. public void process(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
  24. if(!skills.contains(primarySkillType)) {
  25. return;
  26. }
  27. for (int i : levelsGained) {
  28. if (shouldApply.test(i)) {
  29. // execute command via server console in Bukkit
  30. if(logInfo) {
  31. mcMMO.p.getLogger().info("Executing command: " + commandStr);
  32. } else {
  33. LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commandStr);
  34. }
  35. executeCommand();
  36. }
  37. }
  38. }
  39. public void executeCommand() {
  40. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commandStr);
  41. }
  42. @Override
  43. public boolean equals(Object o) {
  44. if (this == o) return true;
  45. if (o == null || getClass() != o.getClass()) return false;
  46. LevelUpCommandImpl that = (LevelUpCommandImpl) o;
  47. return logInfo == that.logInfo && Objects.equals(shouldApply, that.shouldApply) && Objects.equals(commandStr, that.commandStr) && Objects.equals(skills, that.skills);
  48. }
  49. @Override
  50. public int hashCode() {
  51. return Objects.hash(shouldApply, logInfo, commandStr, skills);
  52. }
  53. @Override
  54. public String toString() {
  55. return "LevelUpCommandImpl{" +
  56. "shouldApply=" + shouldApply +
  57. ", logInfo=" + logInfo +
  58. ", commandStr='" + commandStr + '\'' +
  59. ", skills=" + skills +
  60. '}';
  61. }
  62. }