1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +01:00

extra variable as -cmd for promote commands

This commit is contained in:
Zrips 2022-01-19 14:49:55 +02:00
parent 10b5e8a438
commit 0811283201
3 changed files with 81 additions and 34 deletions

View File

@ -544,7 +544,25 @@ public class PlayerManager {
* @param levels - number of levels to promote * @param levels - number of levels to promote
*/ */
public void promoteJob(JobsPlayer jPlayer, Job job, int levels) { 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.promoteJob(job, levels);
jPlayer.save(); jPlayer.save();
Jobs.getSignUtil().updateAllSign(job); Jobs.getSignUtil().updateAllSign(job);
@ -628,7 +646,7 @@ public class PlayerManager {
if (prog.getLevel() < oldLevel) { if (prog.getLevel() < oldLevel) {
String message = Jobs.getLanguage().getMessage("message.leveldown.message"); 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("%playername%", player != null ? plugin.getComplement().getDisplayName(player) : jPlayer.getName());
message = message.replace("%joblevel%", prog.getLevelFormatted()); message = message.replace("%joblevel%", prog.getLevelFormatted());
message = message.replace("%lostLevel%", Integer.toString(oldLevel)); message = message.replace("%lostLevel%", Integer.toString(oldLevel));
@ -731,7 +749,7 @@ public class PlayerManager {
String message = Jobs.getLanguage().getMessage("message.levelup." + (Jobs.getGCManager().isBroadcastingLevelups() String message = Jobs.getLanguage().getMessage("message.levelup." + (Jobs.getGCManager().isBroadcastingLevelups()
? "broadcast" : "nobroadcast")); ? "broadcast" : "nobroadcast"));
message = message.replace("%jobname%", job.getJobDisplayName()); message = message.replace("%jobname%", job.getDisplayName());
if (levelUpEvent.getOldTitle() != null) if (levelUpEvent.getOldTitle() != null)
message = message.replace("%titlename%", levelUpEvent.getOldTitle() 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("%playername%", player != null ? plugin.getComplement().getDisplayName(player) : jPlayer.getName());
message = message.replace("%titlename%", levelUpEvent.getNewTitle() message = message.replace("%titlename%", levelUpEvent.getNewTitle()
.getChatColor().toString() + levelUpEvent.getNewTitle().getName()); .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) { if (Jobs.getGCManager().isBroadcastingSkillups() || Jobs.getGCManager().TitleChangeActionBar || Jobs.getGCManager().TitleChangeChat) {
for (String line : message.split("\n")) { for (String line : message.split("\n")) {
@ -831,23 +849,40 @@ public class PlayerManager {
*/ */
public void performCommandOnLevelUp(JobsPlayer jPlayer, JobProgression prog, int oldLevel) { public void performCommandOnLevelUp(JobsPlayer jPlayer, JobProgression prog, int oldLevel) {
int newLevel = oldLevel + 1; int newLevel = oldLevel + 1;
Player player = plugin.getServer().getPlayer(jPlayer.getUniqueId()); List<String> commands = getCommandsOnLevelUp(jPlayer, prog, newLevel);
commands.stream().forEach(cmd -> plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), cmd));
}
private static List<String> getCommandsOnLevelUp(JobsPlayer jPlayer, JobProgression prog, int newLevel) {
List<String> commands = new ArrayList<String>();
for (JobCommands command : prog.getJob().getCommands()) { for (JobCommands command : prog.getJob().getCommands()) {
if ((command.getLevelFrom() == 0 && command.getLevelUntil() == 0) || (newLevel >= command.getLevelFrom() if ((command.getLevelFrom() == 0 && command.getLevelUntil() == 0) || (newLevel >= command.getLevelFrom() && newLevel <= command.getLevelUntil())) {
&& newLevel <= command.getLevelUntil())) {
for (String commandString : new ArrayList<>(command.getCommands())) { for (String commandString : new ArrayList<>(command.getCommands())) {
if (player != null) commandString = commandString.replace("[player]", jPlayer.getName())
commandString = commandString.replace("[player]", player.getName()); .replace("[oldlevel]", Integer.toString(newLevel - 1))
.replace("[newlevel]", Integer.toString(newLevel))
commandString = commandString.replace("[oldlevel]", Integer.toString(oldLevel)); .replace("[jobname]", prog.getJob().getName());
commandString = commandString.replace("[newlevel]", Integer.toString(newLevel)); commands.add(commandString);
commandString = commandString.replace("[jobname]", prog.getJob().getName());
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), 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<String> commands = getCommandsOnLevelUp(jPlayer, prog, newLevel);
commands.stream().forEach(cmd -> plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), cmd));
}
} }
/** /**

View File

@ -15,7 +15,7 @@ public class promote implements Cmd {
if (args.length < 3) { if (args.length < 3) {
Jobs.getCommandManager().sendUsage(sender, "promote"); Jobs.getCommandManager().sendUsage(sender, "promote");
return true; return true;
} }
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]); JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]);
if (jPlayer == null) { if (jPlayer == null) {
@ -28,26 +28,38 @@ public class promote implements Cmd {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job")); sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job"));
return true; return true;
} }
// check if player already has the job
if (!jPlayer.isInJob(job))
return false;
try { 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); int levelsGained = -1;
try {
Player player = jPlayer.getPlayer(); levelsGained = Integer.parseInt(args[2]);
if (player != null) } catch (NumberFormatException ex) {
player.sendMessage(Jobs.getLanguage().getMessage("command.promote.output.target", return false;
"%jobname%", job.getDisplayName(),
"%levelsgained%", levelsGained));
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success"));
} }
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) { } catch (Throwable e) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error")); sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error"));
} }

View File

@ -589,8 +589,8 @@ public class LanguageManager {
c.get("command.transfer.output.target", "You have been transferred from %oldjobname% to %newjobname%."); 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.info", "Promote the player X levels in a job.");
c.get("command.promote.help.args", "[playername] [jobname] [levels]"); c.get("command.promote.help.args", "[playername] [jobname] [levels] (-cmd)");
Jobs.getGCManager().getCommandArgs().put("promote", Arrays.asList("[playername]", "[jobname]", "[levels]")); 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.promote.output.target", "You have been promoted %levelsgained% levels in %jobname%.");
c.get("command.exp.help.info", "Change the player exp for job."); c.get("command.exp.help.info", "Change the player exp for job.");