diff --git a/src/main/java/com/gamingmesh/jobs/PlayerManager.java b/src/main/java/com/gamingmesh/jobs/PlayerManager.java index cc101dd7..6026b58b 100644 --- a/src/main/java/com/gamingmesh/jobs/PlayerManager.java +++ b/src/main/java/com/gamingmesh/jobs/PlayerManager.java @@ -544,7 +544,25 @@ public class PlayerManager { * @param levels - number of levels to promote */ public void promoteJob(JobsPlayer jPlayer, Job job, int levels) { + promoteJob(jPlayer, job, levels, false); + } + + /** + * Promotes player in their job + * + * @param jPlayer {@link JobsPlayer} + * @param job - the job + * @param levels - number of levels to promote + */ + public void promoteJob(JobsPlayer jPlayer, Job job, int levels, boolean performCommands) { + + if (performCommands) { + JobProgression prog = jPlayer.getJobProgression(job); + performCommandOnLevelUp(jPlayer, prog, prog.getLevel(), prog.getLevel() + levels); + } + jPlayer.promoteJob(job, levels); + jPlayer.save(); Jobs.getSignUtil().updateAllSign(job); @@ -628,7 +646,7 @@ public class PlayerManager { if (prog.getLevel() < oldLevel) { String message = Jobs.getLanguage().getMessage("message.leveldown.message"); - message = message.replace("%jobname%", job.getJobDisplayName()); + message = message.replace("%jobname%", job.getDisplayName()); message = message.replace("%playername%", player != null ? plugin.getComplement().getDisplayName(player) : jPlayer.getName()); message = message.replace("%joblevel%", prog.getLevelFormatted()); message = message.replace("%lostLevel%", Integer.toString(oldLevel)); @@ -731,7 +749,7 @@ public class PlayerManager { String message = Jobs.getLanguage().getMessage("message.levelup." + (Jobs.getGCManager().isBroadcastingLevelups() ? "broadcast" : "nobroadcast")); - message = message.replace("%jobname%", job.getJobDisplayName()); + message = message.replace("%jobname%", job.getDisplayName()); if (levelUpEvent.getOldTitle() != null) message = message.replace("%titlename%", levelUpEvent.getOldTitle() @@ -772,7 +790,7 @@ public class PlayerManager { message = message.replace("%playername%", player != null ? plugin.getComplement().getDisplayName(player) : jPlayer.getName()); message = message.replace("%titlename%", levelUpEvent.getNewTitle() .getChatColor().toString() + levelUpEvent.getNewTitle().getName()); - message = message.replace("%jobname%", job.getJobDisplayName()); + message = message.replace("%jobname%", job.getDisplayName()); if (Jobs.getGCManager().isBroadcastingSkillups() || Jobs.getGCManager().TitleChangeActionBar || Jobs.getGCManager().TitleChangeChat) { for (String line : message.split("\n")) { @@ -831,23 +849,40 @@ public class PlayerManager { */ public void performCommandOnLevelUp(JobsPlayer jPlayer, JobProgression prog, int oldLevel) { int newLevel = oldLevel + 1; - Player player = plugin.getServer().getPlayer(jPlayer.getUniqueId()); + List commands = getCommandsOnLevelUp(jPlayer, prog, newLevel); + commands.stream().forEach(cmd -> plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), cmd)); + } + private static List getCommandsOnLevelUp(JobsPlayer jPlayer, JobProgression prog, int newLevel) { + List commands = new ArrayList(); for (JobCommands command : prog.getJob().getCommands()) { - if ((command.getLevelFrom() == 0 && command.getLevelUntil() == 0) || (newLevel >= command.getLevelFrom() - && newLevel <= command.getLevelUntil())) { + if ((command.getLevelFrom() == 0 && command.getLevelUntil() == 0) || (newLevel >= command.getLevelFrom() && newLevel <= command.getLevelUntil())) { for (String commandString : new ArrayList<>(command.getCommands())) { - if (player != null) - commandString = commandString.replace("[player]", player.getName()); - - commandString = commandString.replace("[oldlevel]", Integer.toString(oldLevel)); - commandString = commandString.replace("[newlevel]", Integer.toString(newLevel)); - commandString = commandString.replace("[jobname]", prog.getJob().getName()); - - plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), commandString); + commandString = commandString.replace("[player]", jPlayer.getName()) + .replace("[oldlevel]", Integer.toString(newLevel - 1)) + .replace("[newlevel]", Integer.toString(newLevel)) + .replace("[jobname]", prog.getJob().getName()); + commands.add(commandString); } } } + return commands; + } + + /** + * Performs command for each level + * + * @param jPlayer {@link JobsPlayer} + * @param job {@link Job} + * @param oldLevel + */ + public void performCommandOnLevelUp(JobsPlayer jPlayer, JobProgression prog, int oldLevel, int untilLevel) { + if (oldLevel > untilLevel) + return; + for (int newLevel = oldLevel + 1; newLevel <= untilLevel; newLevel++) { + List commands = getCommandsOnLevelUp(jPlayer, prog, newLevel); + commands.stream().forEach(cmd -> plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), cmd)); + } } /** diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/promote.java b/src/main/java/com/gamingmesh/jobs/commands/list/promote.java index a06a19fe..d588202b 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/promote.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/promote.java @@ -15,7 +15,7 @@ public class promote implements Cmd { if (args.length < 3) { Jobs.getCommandManager().sendUsage(sender, "promote"); return true; - } + } JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]); if (jPlayer == null) { @@ -28,26 +28,38 @@ public class promote implements Cmd { sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job")); return true; } + + // check if player already has the job + if (!jPlayer.isInJob(job)) + return false; + try { - // check if player already has the job - if (jPlayer.isInJob(job)) { - int levelsGained = -1; - try { - levelsGained = Integer.parseInt(args[2]); - } catch (NumberFormatException ex) { - return true; - } - Jobs.getPlayerManager().promoteJob(jPlayer, job, levelsGained); - - Player player = jPlayer.getPlayer(); - if (player != null) - player.sendMessage(Jobs.getLanguage().getMessage("command.promote.output.target", - "%jobname%", job.getDisplayName(), - "%levelsgained%", levelsGained)); - - sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success")); + int levelsGained = -1; + try { + levelsGained = Integer.parseInt(args[2]); + } catch (NumberFormatException ex) { + return false; } + + boolean commands = false; + for (String one : args) { + if (one.equalsIgnoreCase("-cmd")) { + commands = true; + continue; + } + } + + Jobs.getPlayerManager().promoteJob(jPlayer, job, levelsGained, commands); + + Player player = jPlayer.getPlayer(); + if (player != null) + player.sendMessage(Jobs.getLanguage().getMessage("command.promote.output.target", + "%jobname%", job.getDisplayName(), + "%levelsgained%", levelsGained)); + + sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success")); + } catch (Throwable e) { sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error")); } diff --git a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java index 89e11009..780f6ccf 100644 --- a/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/LanguageManager.java @@ -589,8 +589,8 @@ public class LanguageManager { c.get("command.transfer.output.target", "You have been transferred from %oldjobname% to %newjobname%."); c.get("command.promote.help.info", "Promote the player X levels in a job."); - c.get("command.promote.help.args", "[playername] [jobname] [levels]"); - Jobs.getGCManager().getCommandArgs().put("promote", Arrays.asList("[playername]", "[jobname]", "[levels]")); + c.get("command.promote.help.args", "[playername] [jobname] [levels] (-cmd)"); + Jobs.getGCManager().getCommandArgs().put("promote", Arrays.asList("[playername]", "[jobname]", "[levels]", "%%-cmd")); c.get("command.promote.output.target", "You have been promoted %levelsgained% levels in %jobname%."); c.get("command.exp.help.info", "Change the player exp for job.");