mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-07-19 22:04:44 +02:00
command on level up wip
This commit is contained in:
@ -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);
|
||||
}
|
@ -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 +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.gmail.nossr50.config;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class CommandOnLevelUpConfig extends BukkitConfig {
|
||||
|
||||
public CommandOnLevelUpConfig(@NotNull File dataFolder) {
|
||||
super("commandonlevelup", dataFolder);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadKeys() {
|
||||
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.gmail.nossr50.events.experience;
|
||||
|
||||
import com.gmail.nossr50.datatypes.experience.XPGainReason;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
@ -20,6 +20,11 @@ import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class SelfListener implements Listener {
|
||||
//Used in task scheduling and other things
|
||||
private final mcMMO plugin;
|
||||
@ -31,10 +36,10 @@ public class SelfListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerLevelUp(McMMOPlayerLevelUpEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
PrimarySkillType skill = event.getSkill();
|
||||
final Player player = event.getPlayer();
|
||||
final PrimarySkillType skill = event.getSkill();
|
||||
|
||||
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
|
||||
final McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
|
||||
|
||||
//TODO: Handle proper validation at the event level
|
||||
if(mcMMOPlayer == null || !mcMMOPlayer.getProfile().isLoaded())
|
||||
@ -55,6 +60,13 @@ public class SelfListener implements Listener {
|
||||
if(mcMMO.p.getGeneralConfig().getScoreboardsEnabled())
|
||||
ScoreboardManager.handleLevelUp(player, skill);
|
||||
}
|
||||
|
||||
final Set<Integer> levelsAchieved = new LinkedHashSet<>();
|
||||
for(int i = 0; i < event.getLevelsGained(); i++)
|
||||
{
|
||||
levelsAchieved.add(event.getSkillLevel());
|
||||
}
|
||||
plugin.getLevelUpCommandManager().apply(mcMMOPlayer, skill, levelsAchieved);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
|
@ -2,6 +2,7 @@ package com.gmail.nossr50;
|
||||
|
||||
import com.gmail.nossr50.chat.ChatManager;
|
||||
import com.gmail.nossr50.commands.CommandManager;
|
||||
import com.gmail.nossr50.commands.levelup.LevelUpCommandManager;
|
||||
import com.gmail.nossr50.config.*;
|
||||
import com.gmail.nossr50.config.experience.ExperienceConfig;
|
||||
import com.gmail.nossr50.config.mods.ArmorConfigManager;
|
||||
@ -88,6 +89,7 @@ public class mcMMO extends JavaPlugin {
|
||||
private static DatabaseManager databaseManager;
|
||||
private static FormulaManager formulaManager;
|
||||
private static UpgradeManager upgradeManager;
|
||||
private static LevelUpCommandManager levelUpCommandManager;
|
||||
private static MaterialMapStore materialMapStore;
|
||||
private static PlayerLevelUtils playerLevelUtils;
|
||||
private static SmeltingTracker smeltingTracker;
|
||||
@ -137,15 +139,8 @@ public class mcMMO extends JavaPlugin {
|
||||
|
||||
private GeneralConfig generalConfig;
|
||||
private AdvancedConfig advancedConfig;
|
||||
// private RepairConfig repairConfig;
|
||||
// private SalvageConfig salvageConfig;
|
||||
// private PersistentDataConfig persistentDataConfig;
|
||||
// private ChatConfig chatConfig;
|
||||
// private CoreSkillsConfig coreSkillsConfig;
|
||||
// private RankConfig rankConfig;
|
||||
// private TreasureConfig treasureConfig;
|
||||
// private FishingTreasureConfig fishingTreasureConfig;
|
||||
// private SoundConfig soundConfig;
|
||||
|
||||
private CommandOnLevelUpConfig commandOnLevelUpConfig;
|
||||
|
||||
public mcMMO() {
|
||||
p = this;
|
||||
@ -173,6 +168,7 @@ public class mcMMO extends JavaPlugin {
|
||||
|
||||
//Init configs
|
||||
advancedConfig = new AdvancedConfig(getDataFolder());
|
||||
commandOnLevelUpConfig = new CommandOnLevelUpConfig(getDataFolder());
|
||||
|
||||
//Store this value so other plugins can check it
|
||||
isRetroModeEnabled = generalConfig.getIsRetroMode();
|
||||
@ -770,4 +766,12 @@ public class mcMMO extends JavaPlugin {
|
||||
public @NotNull AdvancedConfig getAdvancedConfig() {
|
||||
return advancedConfig;
|
||||
}
|
||||
|
||||
public @NotNull CommandOnLevelUpConfig getCommandOnLevelUpConfig() {
|
||||
return commandOnLevelUpConfig;
|
||||
}
|
||||
|
||||
public @NotNull LevelUpCommandManager getLevelUpCommandManager() {
|
||||
return levelUpCommandManager;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user