1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-25 20:16:13 +01:00

Merge pull request #1533 from Teyir/master

[IMPROVE] Add -s argument and rand for /exp command
This commit is contained in:
Zrips 2023-01-06 13:59:56 +02:00 committed by GitHub
commit f145eee8df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 67 deletions

View File

@ -197,7 +197,7 @@
<artifactId>CMILib</artifactId> <artifactId>CMILib</artifactId>
<version>latest</version> <version>latest</version>
<scope>system</scope> <scope>system</scope>
<systemPath>${basedir}/libs/CMILib1.1.2.3.jar</systemPath> <systemPath>${basedir}/libs/CMILib1.2.3.3.jar</systemPath>
</dependency> </dependency>
<!-- WildStacker --> <!-- WildStacker -->
<dependency> <dependency>

View File

@ -9,86 +9,127 @@ import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobProgression; import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.JobsPlayer;
import java.util.Random;
public class exp implements Cmd { public class exp implements Cmd {
private enum Action { private enum Action {
Set, Add, Take Set, Add, Take
} }
@Override @Override
public boolean perform(Jobs plugin, CommandSender sender, String[] args) { public boolean perform(Jobs plugin, CommandSender sender, String[] args) {
if (args.length < 4) { if (args.length < 4) {
Jobs.getCommandManager().sendUsage(sender, "exp"); Jobs.getCommandManager().sendUsage(sender, "exp");
return true; return true;
} }
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]); JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]);
if (jPlayer == null) { if (jPlayer == null) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfoByPlayer", "%playername%", args[0])); sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfoByPlayer", "%playername%", args[0]));
return true; return true;
} }
Job job = Jobs.getJob(args[1]); Job job = Jobs.getJob(args[1]);
if (job == null) { if (job == null) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job")); sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job"));
return true; return true;
} }
Action action = Action.Add; Action action = Action.Add;
switch (args[2].toLowerCase()) { switch (args[2].toLowerCase()) {
case "add": case "add":
action = Action.Add; action = Action.Add;
break; break;
case "set": case "set":
action = Action.Set; action = Action.Set;
break; break;
case "take": case "take":
action = Action.Take; action = Action.Take;
break; break;
default: default:
break; break;
} }
double amount = 0.0; double amount = 0.0;
try {
amount = Double.parseDouble(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error"));
return true;
}
try { /* Add random argument, ex: rand_5-10 */
// check if player already has the job if (args[3].startsWith("rand_")) {
if (jPlayer.isInJob(job)) { String data = args[3].split("(?i)rand_")[1];
JobProgression prog = jPlayer.getJobProgression(job); String[] arr = data.split("-");
switch (action) { int amountMin = Integer.parseInt(arr[0]);
case Add: int amountMax = Integer.parseInt(arr[1]);
int oldLevel = prog.getLevel();
if (prog.addExperience(amount))
Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel);
break;
case Set:
prog.setExperience(amount);
break;
case Take:
prog.takeExperience(amount);
break;
default:
break;
}
Player player = jPlayer.getPlayer(); if (amountMin <= amountMax) {
if (player != null) amount = amountMin + new Random().nextDouble() * (amountMax - amountMin);
player.sendMessage(Jobs.getLanguage().getMessage("command.exp.output.target", "%jobname%", job.getDisplayName(), "%level%", prog.getLevelFormatted(), "%exp%", prog } else {
.getExperience())); amount = amountMax;
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success")); }
} else
sender.sendMessage(Jobs.getLanguage().getMessage("command.exp.error.nojob")); } else {
} catch (Exception e) { try {
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error")); amount = Double.parseDouble(args[3]);
} } catch (NumberFormatException e) {
return true; sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error"));
return true;
}
}
try {
// check if player already has the job
if (jPlayer.isInJob(job)) {
JobProgression prog = jPlayer.getJobProgression(job);
switch (action) {
case Add:
int oldLevel = prog.getLevel();
if (prog.addExperience(amount))
Jobs.getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel);
break;
case Set:
prog.setExperience(amount);
break;
case Take:
prog.takeExperience(amount);
break;
default:
break;
}
Player player = jPlayer.getPlayer();
boolean isSilent = false;
boolean isSilentAdmin = false;
if (player != null) {
for (String one : args) {
if (one.equalsIgnoreCase("-s")) {
isSilent = true;
break;
}
}
if (!isSilent) {
player.sendMessage(Jobs.getLanguage().getMessage("command.exp.output.target", "%jobname%", job.getDisplayName(), "%level%", prog.getLevelFormatted(), "%exp%", prog
.getExperience()));
}
for (String one : args) {
if (one.equalsIgnoreCase("-sa")) {
isSilentAdmin = true;
break;
}
}
if (!isSilentAdmin) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success"));
}
} else {
sender.sendMessage(Jobs.getLanguage().getMessage("general.give.output.notonline"));
}
} else {
sender.sendMessage(Jobs.getLanguage().getMessage("command.exp.error.nojob"));
}
} catch (Exception e) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error"));
}
return true;
} }
} }