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

exp command cleanup

This commit is contained in:
Zrips 2023-01-06 14:30:50 +02:00
parent 6715fa7bee
commit 2dc25b232d
4 changed files with 156 additions and 143 deletions

View File

@ -197,7 +197,7 @@
<artifactId>CMILib</artifactId>
<version>latest</version>
<scope>system</scope>
<systemPath>${basedir}/libs/CMILib1.2.3.3.jar</systemPath>
<systemPath>${basedir}/libs/CMILib1.2.4.1.jar</systemPath>
</dependency>
<!-- WildStacker -->
<dependency>
@ -286,7 +286,7 @@
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<!-- <outputDirectory>D:\MC\Server 1.19\plugins\</outputDirectory> -->
<outputDirectory>D:\MC\Server 1.19\plugins\</outputDirectory>
</configuration>
</plugin>
</plugins>

View File

@ -28,6 +28,7 @@ import net.Zrips.CMILib.ActionBar.CMIActionBar;
import net.Zrips.CMILib.Container.CMIArray;
import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.Locale.LC;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages;
import net.Zrips.CMILib.RawMessages.RawMessage;
@ -51,6 +52,7 @@ public class JobsCommands implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
CMIDebug.d("jobs commands");
if (sender instanceof Player && !Jobs.getGCManager().canPerformActionInWorld(((Player) sender).getWorld())
&& !sender.hasPermission("jobs.disabledworld.commands")) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.worldisdisabled"));

View File

@ -1,5 +1,7 @@
package com.gamingmesh.jobs.commands.list;
import java.util.Random;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -8,8 +10,7 @@ import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsPlayer;
import java.util.Random;
import com.gamingmesh.jobs.i18n.Language;
public class exp implements Cmd {
@ -17,43 +18,58 @@ public class exp implements Cmd {
Set, Add, Take
}
Random rand = new Random();
@Override
public boolean perform(Jobs plugin, CommandSender sender, String[] args) {
if (args.length < 4) {
Jobs.getCommandManager().sendUsage(sender, "exp");
return true;
}
boolean silent = false;
boolean silentAdmin = false;
for (String one : args) {
if (one.equalsIgnoreCase("-s")) {
silent = true;
continue;
}
if (one.equalsIgnoreCase("-sa")) {
silentAdmin = true;
}
}
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(args[0]);
if (jPlayer == null) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.noinfoByPlayer", "%playername%", args[0]));
Language.sendMessage(sender, "general.error.noinfoByPlayer", "%playername%", args[0]);
return true;
}
Job job = Jobs.getJob(args[1]);
if (job == null) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.error.job"));
Language.sendMessage(sender, "general.error.job");
return true;
}
Action action = Action.Add;
switch (args[2].toLowerCase()) {
case "add":
action = Action.Add;
break;
case "set":
action = Action.Set;
break;
case "take":
action = Action.Take;
break;
default:
break;
case "add":
action = Action.Add;
break;
case "set":
action = Action.Set;
break;
case "take":
action = Action.Take;
break;
default:
break;
}
double amount = 0.0;
/* Add random argument, ex: rand_5-10 */
if (args[3].startsWith("rand_")) {
String data = args[3].split("(?i)rand_")[1];
@ -62,8 +78,8 @@ public class exp implements Cmd {
int amountMin = Integer.parseInt(arr[0]);
int amountMax = Integer.parseInt(arr[1]);
if (amountMin <= amountMax) {
amount = amountMin + new Random().nextDouble() * (amountMax - amountMin);
if (amountMin < amountMax) {
amount = amountMin + rand.nextInt(amountMax - amountMin);
} else {
amount = amountMax;
}
@ -77,58 +93,48 @@ public class exp implements Cmd {
}
}
if (!jPlayer.isInJob(job)) {
Language.sendMessage(sender, "command.exp.error.nojob");
return true;
}
try {
// check if player already has the job
if (jPlayer.isInJob(job)) {
JobProgression prog = jPlayer.getJobProgression(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"));
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();
if (player == null) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.give.output.notonline"));
return true;
}
if (!silent)
player.sendMessage(Jobs.getLanguage().getMessage("command.exp.output.target", "%jobname%", job.getDisplayName(), "%level%", prog.getLevelFormatted(), "%exp%", prog
.getExperience()));
if (!silentAdmin)
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.success"));
} catch (Exception e) {
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error"));
if (!silentAdmin)
sender.sendMessage(Jobs.getLanguage().getMessage("general.admin.error"));
e.printStackTrace();
}
return true;
}

View File

@ -24,6 +24,7 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import com.gamingmesh.jobs.Jobs;
@ -41,11 +42,15 @@ public class Language {
* Reloads the config
*/
public void reload() {
String ls = Jobs.getGCManager().localeString.toLowerCase();
customlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_" + ls + ".yml").getConfig();
enlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_en.yml").getConfig();
if (customlocale == null)
customlocale = enlocale;
String ls = Jobs.getGCManager().localeString.toLowerCase();
customlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_" + ls + ".yml").getConfig();
enlocale = new YmlMaker(Jobs.getFolder(), "locale/messages_en.yml").getConfig();
if (customlocale == null)
customlocale = enlocale;
}
public static void sendMessage(CommandSender sender, String key, Object... variables) {
sender.sendMessage(Jobs.getLanguage().getMessage(key, variables));
}
/**
@ -54,57 +59,57 @@ public class Language {
* @return the message
*/
public String getMessage(String key) {
return getMessage(key, "");
return getMessage(key, "");
}
public String getMessage(String key, Object... variables) {
String missing = "MLF " + key;
String msg = "";
try {
if (!customlocale.contains(key))
msg = enlocale.isString(key) ? CMIChatColor.translate(enlocale.getString(key)) : missing;
else
msg = customlocale.isString(key) ? CMIChatColor.translate(customlocale.getString(key)) : missing;
} catch (Exception e) {
Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key);
Jobs.consoleMsg(e.getLocalizedMessage());
return "";
}
String missing = "MLF " + key;
String msg = "";
try {
if (!customlocale.contains(key))
msg = enlocale.isString(key) ? CMIChatColor.translate(enlocale.getString(key)) : missing;
else
msg = customlocale.isString(key) ? CMIChatColor.translate(customlocale.getString(key)) : missing;
} catch (Exception e) {
Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key);
Jobs.consoleMsg(e.getLocalizedMessage());
return "";
}
if (msg.isEmpty() || msg.equals(missing)) {
msg = "";
try {
if (msg.isEmpty() || msg.equals(missing)) {
msg = "";
try {
List<String> ls = null;
List<String> ls = null;
if (customlocale.isList(key))
ls = colorsArray(customlocale.getStringList(key), true);
else if (enlocale.isList(key)) {
ls = enlocale.getStringList(key);
ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing);
}
if (customlocale.isList(key))
ls = colorsArray(customlocale.getStringList(key), true);
else if (enlocale.isList(key)) {
ls = enlocale.getStringList(key);
ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing);
}
if (ls != null)
for (String one : ls) {
if (!msg.isEmpty())
msg += "\n";
msg += one;
}
} catch (Exception e) {
Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key);
Jobs.consoleMsg(e.getLocalizedMessage());
return "";
}
}
if (ls != null)
for (String one : ls) {
if (!msg.isEmpty())
msg += "\n";
msg += one;
}
} catch (Exception e) {
Jobs.consoleMsg("&e[Jobs] &2Can't read language file for: " + key);
Jobs.consoleMsg(e.getLocalizedMessage());
return "";
}
}
if (variables != null && variables.length > 0)
for (int i = 0; i < variables.length; i++) {
if (variables.length >= i + 2)
msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1]));
i++;
}
if (variables != null && variables.length > 0)
for (int i = 0; i < variables.length; i++) {
if (variables.length >= i + 2)
msg = msg.replace(String.valueOf(variables[i]), String.valueOf(variables[i + 1]));
i++;
}
return msg;
return msg;
}
/**
@ -113,47 +118,47 @@ public class Language {
* @return the message
*/
public List<String> getMessageList(String key, Object... variables) {
String missing = "MLF " + key + " ";
String missing = "MLF " + key + " ";
List<String> ls;
if (customlocale.isList(key))
ls = colorsArray(customlocale.getStringList(key), true);
else {
ls = enlocale.getStringList(key);
ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing);
}
List<String> ls;
if (customlocale.isList(key))
ls = colorsArray(customlocale.getStringList(key), true);
else {
ls = enlocale.getStringList(key);
ls = !ls.isEmpty() ? colorsArray(ls, true) : Arrays.asList(missing);
}
if (variables != null && variables.length > 0)
for (int i = 0; i < ls.size(); i++) {
String msg = ls.get(i);
for (int y = 0; y < variables.length; y += 2) {
msg = msg.replace(String.valueOf(variables[y]), String.valueOf(variables[y + 1]));
}
if (variables != null && variables.length > 0)
for (int i = 0; i < ls.size(); i++) {
String msg = ls.get(i);
for (int y = 0; y < variables.length; y += 2) {
msg = msg.replace(String.valueOf(variables[y]), String.valueOf(variables[y + 1]));
}
ls.set(i, CMIChatColor.translate(filterNewLine(msg)));
}
ls.set(i, CMIChatColor.translate(filterNewLine(msg)));
}
return ls;
return ls;
}
public String filterNewLine(String msg) {
Matcher match = patern.matcher(msg);
while (match.find()) {
msg = msg.replace(match.group(0), "\n");
}
return msg;
Matcher match = patern.matcher(msg);
while (match.find()) {
msg = msg.replace(match.group(0), "\n");
}
return msg;
}
public List<String> colorsArray(List<String> text, boolean colorize) {
List<String> temp = new ArrayList<>();
List<String> temp = new ArrayList<>();
for (String part : text) {
if (colorize)
part = CMIChatColor.translate(part);
for (String part : text) {
if (colorize)
part = CMIChatColor.translate(part);
temp.add(part);
}
temp.add(part);
}
return temp;
return temp;
}
}