command on level up wip

This commit is contained in:
nossr50
2023-07-22 10:14:35 -07:00
parent 24a57fab3d
commit 027b79639b
10 changed files with 483 additions and 12 deletions

View File

@@ -0,0 +1,10 @@
package com.gmail.nossr50.commands.levelup;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import java.util.Set;
public interface LevelUpCommand {
void apply(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained);
}

View File

@@ -0,0 +1,69 @@
package com.gmail.nossr50.commands.levelup;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.LogUtils;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
public class LevelUpCommandImpl implements LevelUpCommand {
private final @NotNull Predicate<Integer> shouldApply;
private final boolean logInfo;
private final @NotNull String commandStr;
private final @NotNull Set<PrimarySkillType> skills;
public LevelUpCommandImpl(@NotNull Predicate<Integer> shouldApply, @NotNull String commandStr, @NotNull Set<PrimarySkillType> skills, boolean logInfo) {
this.shouldApply = shouldApply;
this.commandStr = commandStr;
this.skills = skills;
this.logInfo = logInfo;
}
@Override
public void apply(McMMOPlayer player, PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
if(!skills.contains(primarySkillType)) {
return;
}
for (int i : levelsGained) {
if (shouldApply.test(i)) {
// execute command via server console in Bukkit
if(logInfo) {
mcMMO.p.getLogger().info("Executing command: " + commandStr);
} else {
LogUtils.debug(mcMMO.p.getLogger(), "Executing command: " + commandStr);
}
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commandStr);
}
}
}
@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(shouldApply, that.shouldApply) && Objects.equals(commandStr, that.commandStr) && Objects.equals(skills, that.skills);
}
@Override
public int hashCode() {
return Objects.hash(shouldApply, logInfo, commandStr, skills);
}
@Override
public String toString() {
return "LevelUpCommandImpl{" +
"shouldApply=" + shouldApply +
", logInfo=" + logInfo +
", commandStr='" + commandStr + '\'' +
", skills=" + skills +
'}';
}
}

View File

@@ -0,0 +1,43 @@
package com.gmail.nossr50.commands.levelup;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
import org.jetbrains.annotations.NotNull;
import java.util.HashSet;
import java.util.Set;
public class LevelUpCommandManager {
private final @NotNull Set<LevelUpCommand> commands;
private final @NotNull mcMMO plugin;
public LevelUpCommandManager(@NotNull mcMMO plugin) {
this.plugin = plugin;
this.commands = new HashSet<>();
}
public void registerCommand(@NotNull LevelUpCommand command) {
commands.add(command);
mcMMO.p.getLogger().info("Registered command on level up: " + command);
}
public void apply(@NotNull McMMOPlayer mmoPlayer, @NotNull PrimarySkillType primarySkillType, Set<Integer> levelsGained) {
if (!mmoPlayer.getPlayer().isOnline()) {
return;
}
for (LevelUpCommand command : commands) {
command.apply(mmoPlayer, primarySkillType, levelsGained);
}
}
public void clear() {
mcMMO.p.getLogger().info("Clearing registered commands on level up");
commands.clear();
}
public boolean isEmpty() {
return commands.isEmpty();
}
}