2
0

LevelUpCommandImpl.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 apply(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. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commandStr);
  36. }
  37. }
  38. }
  39. @Override
  40. public boolean equals(Object o) {
  41. if (this == o) return true;
  42. if (o == null || getClass() != o.getClass()) return false;
  43. LevelUpCommandImpl that = (LevelUpCommandImpl) o;
  44. return logInfo == that.logInfo && Objects.equals(shouldApply, that.shouldApply) && Objects.equals(commandStr, that.commandStr) && Objects.equals(skills, that.skills);
  45. }
  46. @Override
  47. public int hashCode() {
  48. return Objects.hash(shouldApply, logInfo, commandStr, skills);
  49. }
  50. @Override
  51. public String toString() {
  52. return "LevelUpCommandImpl{" +
  53. "shouldApply=" + shouldApply +
  54. ", logInfo=" + logInfo +
  55. ", commandStr='" + commandStr + '\'' +
  56. ", skills=" + skills +
  57. '}';
  58. }
  59. }