Better Time utils for formatting the cooldown.

This commit is contained in:
Mateus 2019-09-27 23:02:07 +01:00
parent d8667de780
commit 49dedc82fe
18 changed files with 445 additions and 450 deletions

View File

@ -39,7 +39,7 @@ import me.mattstudios.citizenscmd.schedulers.CooldownScheduler;
import me.mattstudios.citizenscmd.schedulers.UpdateScheduler;
import me.mattstudios.citizenscmd.updater.SpigotUpdater;
import me.mattstudios.citizenscmd.utility.DisplayFormat;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.base.CommandManager;
import net.milkbowl.vault.economy.Economy;
import org.bstats.bukkit.Metrics;
@ -54,9 +54,10 @@ import java.util.HashMap;
import java.util.Objects;
import java.util.stream.Stream;
import static me.mattstudios.citizenscmd.utility.Util.HEADER;
import static me.mattstudios.citizenscmd.utility.Util.TAG;
import static me.mattstudios.citizenscmd.utility.Util.disablePlugin;
import static me.mattstudios.citizenscmd.utility.Util.upCheck;
import static me.mattstudios.citizenscmd.utility.Util.setUpMetrics;
import static me.mattstudios.utils.MessageUtils.color;
import static me.mattstudios.utils.MessageUtils.info;
import static me.mattstudios.utils.YamlUtils.copyDefaults;
@ -97,33 +98,7 @@ public final class CitizensCMD extends JavaPlugin {
commandManager = new CommandManager(this);
Metrics metrics = new Metrics(this);
metrics.addCustomChart(new Metrics.SimplePie("lang", () -> {
switch (Objects.requireNonNull(getConfig().getString("lang", "en")).toLowerCase()) {
case "en":
return "English";
case "bg":
return "Bulgarian";
case "fr":
return "French";
case "no":
return "Norwegian";
case "pt":
return "Portuguese";
case "Ro":
return "Romanian";
case "ch":
return "Chinese";
default:
return "Other";
}
}));
setUpMetrics(metrics, getConfig());
info(color(TAG + "&3Citizens&cCMD &8&o" + getDescription().getVersion() + " &8By &3Mateus Moreira &c@LichtHund"));
@ -138,13 +113,15 @@ public final class CitizensCMD extends JavaPlugin {
registerCommands();
registerEvents();
info(color(TAG + lang.getMessage(Messages.USING_LANGUAGE)));
if (hasPAPI()) {
info(color(TAG + lang.getMessage(Path.PAPI_AVAILABLE)));
info(color(TAG + lang.getMessage(Messages.PAPI_AVAILABLE)));
papi = true;
}
if (setupEconomy()) {
info(color(TAG + lang.getUncoloredMessage(Path.VAULT_AVAILABLE)));
info(color(TAG + lang.getUncoloredMessage(Messages.VAULT_AVAILABLE)));
}
waitingList = new HashMap<>();
@ -154,9 +131,11 @@ public final class CitizensCMD extends JavaPlugin {
case "short":
displayFormat = DisplayFormat.SHORT;
break;
case "full":
displayFormat = DisplayFormat.FULL;
break;
default:
displayFormat = DisplayFormat.MEDIUM;
break;
@ -165,7 +144,7 @@ public final class CitizensCMD extends JavaPlugin {
displayFormat = DisplayFormat.MEDIUM;
}
if (upCheck(this)) {
if (getConfig().getBoolean("check-updates")) {
SpigotUpdater updater = new SpigotUpdater(this, 30224);
try {
// If there's an update, tell the user that they can update
@ -177,8 +156,7 @@ public final class CitizensCMD extends JavaPlugin {
}
} catch (Exception e) {
// If it can't check for an update, tell the user and throw an error.
info("Could not check for updates! Stacktrace:");
e.printStackTrace();
info("Could not check for updates!");
}
}
@ -210,6 +188,27 @@ public final class CitizensCMD extends JavaPlugin {
commandManager.getCompletionHandler().register("#click", input -> Arrays.asList("left", "right"));
commandManager.getCompletionHandler().register("#set", input -> Arrays.asList("set", "remove"));
commandManager.getMessageHandler().register("cmd.no.permission", (sender, arg) -> {
sender.sendMessage(color(HEADER));
sender.sendMessage(lang.getMessage(Messages.NO_PERMISSION));
});
commandManager.getMessageHandler().register("cmd.no.console", (sender, arg) -> {
sender.sendMessage(color(HEADER));
sender.sendMessage(lang.getMessage(Messages.CONSOLE_NOT_ALLOWED));
});
commandManager.getMessageHandler().register("cmd.no.exists", (sender, arg) -> {
sender.sendMessage(color(HEADER));
sender.sendMessage(lang.getMessage(Messages.WRONG_USAGE));
});
commandManager.getMessageHandler().register("cmd.wrong.usage", (sender, arg) -> {
sender.sendMessage(color(HEADER));
sender.sendMessage(lang.getMessage(Messages.WRONG_USAGE));
});
commandManager.getMessageHandler().register("arg.must.be.number", (sender, arg) -> {
sender.sendMessage(color(HEADER));
sender.sendMessage(lang.getMessage(Messages.INVALID_NUMBER));
});
Stream.of(
new AddCommand(this),
new HelpCommand(this),

View File

@ -1,7 +1,7 @@
package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Completion;
import me.mattstudios.mf.annotations.Permission;
@ -81,7 +81,7 @@ public class AddCommand extends CommandBase {
if (hasDelayError) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NPC_ADD_DELAY_FAIL));
player.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADD_DELAY_FAIL));
return;
}

View File

@ -2,7 +2,7 @@ package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.EnumTypes;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Completion;
import me.mattstudios.mf.annotations.Permission;
@ -40,17 +40,20 @@ public class EditCommand extends CommandBase {
case "cmd":
type = EnumTypes.EditType.CMD;
break;
case "perm":
if (arguments.length > 1) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_PERMISSION));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_PERMISSION));
return;
}
type = EnumTypes.EditType.PERM;
break;
default:
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ARGUMENTS));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_ARGUMENTS));
return;
}
@ -60,13 +63,13 @@ public class EditCommand extends CommandBase {
if (leftCommandSize == 0) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
player.sendMessage(plugin.getLang().getMessage(Messages.NO_COMMANDS));
return;
}
if (id < 1 || id > leftCommandSize) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_ID_NUMBER));
return;
}
@ -78,13 +81,13 @@ public class EditCommand extends CommandBase {
if (rightCommandSize == 0) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
player.sendMessage(plugin.getLang().getMessage(Messages.NO_COMMANDS));
return;
}
if (id < 1 || id > rightCommandSize) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_ID_NUMBER));
return;
}
click = EnumTypes.ClickType.RIGHT;
@ -92,7 +95,7 @@ public class EditCommand extends CommandBase {
default:
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_CLICK_TYPE));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_CLICK_TYPE));
return;
}

View File

@ -1,7 +1,7 @@
package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Alias;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Default;
@ -27,16 +27,16 @@ public class HelpCommand extends CommandBase {
@Permission("citizenscmd.npcmd")
public void help(Player player) {
JSONMessage.create(color(HEADER)).send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.HELP_VERSION) + " &c&o" + plugin.getDescription().getVersion())).send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.HELP_INFO))).send(player);
JSONMessage.create(color("&3/npcmd &cadd &b<console &b| &bmessage &b| &bplayer | &bpermission &b| &bserver &b| &bsound &b> &6<command> &d[-l]")).suggestCommand("/npcmd add ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_ADD) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oadd &b&ossentials.heal &6&oheal")).send(player);
JSONMessage.create(color("&3/npcmd &ccooldown &6<time>")).suggestCommand("/npcmd cooldown ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_COOLDOWN) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&ocooldown &6&o15")).send(player);
JSONMessage.create(color("&3/npcmd &cprice &6<price>")).suggestCommand("/npcmd price ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_PRICE) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oprice &6&o250")).send(player);
JSONMessage.create(color("&3/npcmd &clist")).suggestCommand("/npcmd list").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_LIST) + "\n&8" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&olist")).send(player);
JSONMessage.create(color("&3/npcmd &cedit &b<cmd | perm> &b<left | right> &6<id> &6<new command | new permission>")).suggestCommand("/npcmd edit ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_EDIT) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oedit &b&ocmd &b&oright &6&o1 fly")).send(player);
JSONMessage.create(color("&3/npcmd &cremove &b<left | right> &6<id>")).suggestCommand("/npcmd remove ").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_REMOVE) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oremove &b&oright &6&o1")).send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Messages.HELP_VERSION) + " &c&o" + plugin.getDescription().getVersion())).send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Messages.HELP_INFO))).send(player);
JSONMessage.create(color("&3/npcmd &cadd &b<console &b| &bmessage &b| &bplayer | &bpermission &b| &bserver &b| &bsound &b> &6<command> &d[-l]")).suggestCommand("/npcmd add ").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_ADD) + "\n" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oadd &b&ossentials.heal &6&oheal")).send(player);
JSONMessage.create(color("&3/npcmd &ccooldown &6<time>")).suggestCommand("/npcmd cooldown ").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_COOLDOWN) + "\n" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&ocooldown &6&o15")).send(player);
JSONMessage.create(color("&3/npcmd &cprice &6<price>")).suggestCommand("/npcmd price ").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_PRICE) + "\n" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oprice &6&o250")).send(player);
JSONMessage.create(color("&3/npcmd &clist")).suggestCommand("/npcmd list").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_LIST) + "\n&8" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&olist")).send(player);
JSONMessage.create(color("&3/npcmd &cedit &b<cmd | perm> &b<left | right> &6<id> &6<new command | new permission>")).suggestCommand("/npcmd edit ").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_EDIT) + "\n" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oedit &b&ocmd &b&oright &6&o1 fly")).send(player);
JSONMessage.create(color("&3/npcmd &cremove &b<left | right> &6<id>")).suggestCommand("/npcmd remove ").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_REMOVE) + "\n" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oremove &b&oright &6&o1")).send(player);
JSONMessage.create(color("&3/npcmd &cpermission &b<set | remove> &6<custom.permission>")).suggestCommand("/npcmd permission ").send(player);
JSONMessage.create(color("&3/npcmd &creload")).suggestCommand("/npcmd reload").tooltip(color(plugin.getLang().getUncoloredMessage(Path.HELP_DESCRIPTION_RELOAD) + "\n" + plugin.getLang().getUncoloredMessage(Path.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oreload")).send(player);
JSONMessage.create(color("&3/npcmd &creload")).suggestCommand("/npcmd reload").tooltip(color(plugin.getLang().getUncoloredMessage(Messages.HELP_DESCRIPTION_RELOAD) + "\n" + plugin.getLang().getUncoloredMessage(Messages.HELP_EXAMPLE) + "\n&3&o/npcmd &c&oreload")).send(player);
}
}

View File

@ -2,7 +2,7 @@ package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.EnumTypes;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Permission;
import me.mattstudios.mf.annotations.SubCommand;
@ -39,23 +39,23 @@ public class ListCommand extends CommandBase {
List<String> rightCommands = plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT) != null ? plugin.getDataHandler().getClickCommandsData(npc, EnumTypes.ClickType.RIGHT) : new ArrayList<>();
player.sendMessage(color(HEADER));
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.LIST_COOLDOWN) + plugin.getDataHandler().getNPCCooldown(npc))).tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).suggestCommand("/npcmd cooldown ").send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.LIST_PRICE) + plugin.getDataHandler().getPrice(npc))).tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).suggestCommand("/npcmd price ").send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Messages.LIST_COOLDOWN) + plugin.getDataHandler().getNPCCooldown(npc))).tooltip(plugin.getLang().getMessage(Messages.LIST_TOOLTIP)).suggestCommand("/npcmd cooldown ").send(player);
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Messages.LIST_PRICE) + plugin.getDataHandler().getPrice(npc))).tooltip(plugin.getLang().getMessage(Messages.LIST_TOOLTIP)).suggestCommand("/npcmd price ").send(player);
player.sendMessage("");
player.sendMessage(plugin.getLang().getMessage(Path.LIST_COUNT_RIGHT).replace("{count}", String.valueOf(rightCommands.size())));
player.sendMessage(plugin.getLang().getMessage(Messages.LIST_COUNT_RIGHT).replace("{count}", String.valueOf(rightCommands.size())));
int rightCount = 1;
for (String command : rightCommands) {
JSONMessage.create(color("&c" + rightCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd right " + rightCount + " ").tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).send(player);
JSONMessage.create(color("&c" + rightCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd right " + rightCount + " ").tooltip(plugin.getLang().getMessage(Messages.LIST_TOOLTIP)).send(player);
rightCount++;
}
player.sendMessage("");
player.sendMessage(plugin.getLang().getMessage(Path.LIST_COUNT_LEFT).replace("{count}", String.valueOf(leftCommands.size())));
player.sendMessage(plugin.getLang().getMessage(Messages.LIST_COUNT_LEFT).replace("{count}", String.valueOf(leftCommands.size())));
int leftCount = 1;
for (String command : leftCommands) {
JSONMessage.create(color("&c" + leftCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd left " + leftCount + " ").tooltip(plugin.getLang().getMessage(Path.LIST_TOOLTIP)).send(player);
JSONMessage.create(color("&c" + leftCount + " &7- &7" + command.replace("[", "&8[&c").replace("]", "&8]&b"))).suggestCommand("/npcmd edit cmd left " + leftCount + " ").tooltip(plugin.getLang().getMessage(Messages.LIST_TOOLTIP)).send(player);
leftCount++;
}
}

View File

@ -1,7 +1,7 @@
package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Completion;
import me.mattstudios.mf.annotations.Permission;
@ -40,7 +40,7 @@ public class PermissionCommand extends CommandBase {
default:
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.WRONG_USAGE));
player.sendMessage(plugin.getLang().getMessage(Messages.WRONG_USAGE));
}
}

View File

@ -2,7 +2,7 @@ package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.DisplayFormat;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Permission;
import me.mattstudios.mf.annotations.SubCommand;
@ -53,7 +53,7 @@ public class ReloadCommand extends CommandBase {
plugin.getCooldownHandler().reload();
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.RELOAD));
player.sendMessage(plugin.getLang().getMessage(Messages.RELOAD));
}
}

View File

@ -2,7 +2,7 @@ package me.mattstudios.citizenscmd.commands;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.EnumTypes;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Completion;
import me.mattstudios.mf.annotations.Permission;
@ -40,13 +40,13 @@ public class RemoveCommand extends CommandBase {
if (leftCommandSize == 0) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
player.sendMessage(plugin.getLang().getMessage(Messages.NO_COMMANDS));
return;
}
if (id < 1 || id > leftCommandSize) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_ID_NUMBER));
return;
}
@ -58,13 +58,13 @@ public class RemoveCommand extends CommandBase {
if (rightCommandSize == 0) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NO_COMMANDS));
player.sendMessage(plugin.getLang().getMessage(Messages.NO_COMMANDS));
return;
}
if (id < 0 || id > rightCommandSize) {
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_ID_NUMBER));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_ID_NUMBER));
return;
}
@ -73,7 +73,7 @@ public class RemoveCommand extends CommandBase {
default:
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.INVALID_CLICK_TYPE));
player.sendMessage(plugin.getLang().getMessage(Messages.INVALID_CLICK_TYPE));
return;
}

View File

@ -20,8 +20,8 @@ package me.mattstudios.citizenscmd.files;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.EnumTypes;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.citizenscmd.utility.Util;
import me.mattstudios.citizenscmd.utility.paths.Path;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -169,12 +169,12 @@ public class DataHandler {
}
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NPC_ADDED));
player.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADDED));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NPC_ADD_FAIL));
player.sendMessage(plugin.getLang().getMessage(Messages.NPC_ADD_FAIL));
}
}).start();
}
@ -245,12 +245,12 @@ public class DataHandler {
data.replace("npc-data.npc-" + npc + ".cooldown", cooldown);
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NPC_COOLDOWN_SET));
player.sendMessage(plugin.getLang().getMessage(Messages.NPC_COOLDOWN_SET));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NPC_COOLDOWN_SET_ERROR));
player.sendMessage(plugin.getLang().getMessage(Messages.NPC_COOLDOWN_SET_ERROR));
}
}).start();
}
@ -273,7 +273,7 @@ public class DataHandler {
data.replace("npc-data.npc-" + npc + ".price", price);
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NPC_PRICE_SET));
player.sendMessage(plugin.getLang().getMessage(Messages.NPC_PRICE_SET));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
@ -300,7 +300,7 @@ public class DataHandler {
data.replace("npc-data.npc-" + npc + ".permission", permission);
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.PERMISSION_SET));
player.sendMessage(plugin.getLang().getMessage(Messages.PERMISSION_SET));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
@ -327,7 +327,7 @@ public class DataHandler {
data.remove("npc-data.npc-" + npc + ".permission");
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.PERMISSION_REMOVED));
player.sendMessage(plugin.getLang().getMessage(Messages.PERMISSION_REMOVED));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
@ -441,7 +441,7 @@ public class DataHandler {
dataConfigurator.set(key, commands);
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.REMOVED_COMMAND));
player.sendMessage(plugin.getLang().getMessage(Messages.REMOVED_COMMAND));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {
@ -490,7 +490,7 @@ public class DataHandler {
dataConfigurator.set(key, commandsData);
player.sendMessage(color(Util.HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.EDITED_COMMAND).replace("{type}", typeText));
player.sendMessage(plugin.getLang().getMessage(Messages.EDITED_COMMAND).replace("{type}", typeText));
dataConfigurator.save(savesFile);
} catch (IOException | InvalidConfigurationException e) {

View File

@ -19,6 +19,7 @@
package me.mattstudios.citizenscmd.files;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.Messages;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -46,8 +47,6 @@ public class LangHandler {
messages = new HashMap<>();
cacheMessage();
System.out.println("Using " + lang);
}
/**
@ -60,7 +59,10 @@ public class LangHandler {
InputStream langStream = CitizensCMD.class.getClassLoader().getResourceAsStream("lang/" + lang + ".yml");
System.out.println("Stream: " + (langStream == null));
if (!langFile.exists()) {
System.out.println("no exist");
if (langStream == null) {
langFile.createNewFile();
copyDefaults(CitizensCMD.class.getClassLoader().getResourceAsStream("lang/en.yml"), langFile);
@ -68,9 +70,12 @@ public class LangHandler {
plugin.saveResource("lang/" + lang + ".yml", false);
}
} else {
System.out.println("exist");
if (langStream == null) {
System.out.println("nuru");
copyDefaults(CitizensCMD.class.getClassLoader().getResourceAsStream("lang/en.yml"), langFile);
} else {
System.out.println("noto nuru");
copyDefaults(langStream, langFile);
}
}
@ -94,8 +99,8 @@ public class LangHandler {
* @param path String with the path to the message
* @return Returns String with colored message from file
*/
public String getMessage(String path) {
return color(messages.get(path));
public String getMessage(Messages path) {
return color(messages.get(path.getPath()));
}
/**
@ -104,8 +109,8 @@ public class LangHandler {
* @param path String with the path to the message
* @return Returns String with message from file
*/
public String getUncoloredMessage(String path) {
return messages.get(path);
public String getUncoloredMessage(Messages path) {
return messages.get(path.getPath());
}
/**

View File

@ -21,9 +21,8 @@ package me.mattstudios.citizenscmd.listeners;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.schedulers.ConfirmScheduler;
import me.mattstudios.citizenscmd.utility.EnumTypes;
import me.mattstudios.citizenscmd.utility.TimeUtil;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.citizenscmd.utility.Util;
import me.mattstudios.citizenscmd.utility.paths.Path;
import net.citizensnpcs.api.event.NPCLeftClickEvent;
import net.citizensnpcs.api.event.NPCRemoveEvent;
import net.citizensnpcs.api.event.NPCRightClickEvent;
@ -34,6 +33,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import static me.mattstudios.citizenscmd.utility.Util.getFormattedTime;
public class NPCClickListener implements Listener {
private CitizensCMD plugin;
@ -59,10 +60,10 @@ public class NPCClickListener implements Listener {
if (plugin.getCooldownHandler().onCooldown(npc.getId(), player.getUniqueId().toString())) {
String cooldownMessage;
if (plugin.getDataHandler().getNPCCooldown(npc.getId()) == -1)
cooldownMessage = plugin.getLang().getMessage(Path.ONE_TIME_CLICK);
cooldownMessage = plugin.getLang().getMessage(Messages.ONE_TIME_CLICK);
else
cooldownMessage = plugin.getLang().getMessage(Path.ON_COOLDOWN);
player.sendMessage(cooldownMessage.replace("{time}", TimeUtil.getFormattedTime(plugin, plugin.getCooldownHandler().getTimeLeft(npc.getId(), player.getUniqueId().toString()), plugin.getDisplayFormat())));
cooldownMessage = plugin.getLang().getMessage(Messages.ON_COOLDOWN);
player.sendMessage(cooldownMessage.replace("{time}", getFormattedTime(plugin, plugin.getCooldownHandler().getTimeLeft(npc.getId(), player.getUniqueId().toString()), plugin.getDisplayFormat())));
return;
}
}
@ -76,7 +77,7 @@ public class NPCClickListener implements Listener {
if (CitizensCMD.getEconomy() != null) {
if (!plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc.getId())) {
String messageConfirm = plugin.getLang().getMessage(Path.PAY_CONFIRM);
String messageConfirm = plugin.getLang().getMessage(Messages.PAY_CONFIRM);
if (!plugin.isShift())
messageConfirm = messageConfirm.replace("{shift}", "");
else
@ -93,13 +94,13 @@ public class NPCClickListener implements Listener {
}
if (CitizensCMD.getEconomy().getBalance(player) < price) {
player.sendMessage(plugin.getLang().getMessage(Path.PAY_NO_MONEY));
player.sendMessage(plugin.getLang().getMessage(Messages.PAY_NO_MONEY));
return;
}
plugin.getWaitingList().remove(player.getUniqueId().toString() + "." + npc.getId());
CitizensCMD.getEconomy().withdrawPlayer(player, price);
player.sendMessage(plugin.getLang().getMessage(Path.PAY_COMPLETED).replace("{price}", String.valueOf(price)));
player.sendMessage(plugin.getLang().getMessage(Messages.PAY_COMPLETED).replace("{price}", String.valueOf(price)));
}
}
@ -128,10 +129,10 @@ public class NPCClickListener implements Listener {
if (plugin.getCooldownHandler().onCooldown(npc.getId(), player.getUniqueId().toString())) {
String cooldownMessage;
if (plugin.getDataHandler().getNPCCooldown(npc.getId()) == -1)
cooldownMessage = plugin.getLang().getMessage(Path.ONE_TIME_CLICK);
cooldownMessage = plugin.getLang().getMessage(Messages.ONE_TIME_CLICK);
else
cooldownMessage = plugin.getLang().getMessage(Path.ON_COOLDOWN);
player.sendMessage(cooldownMessage.replace("{time}", TimeUtil.getFormattedTime(plugin, plugin.getCooldownHandler().getTimeLeft(npc.getId(), player.getUniqueId().toString()), plugin.getDisplayFormat())));
cooldownMessage = plugin.getLang().getMessage(Messages.ON_COOLDOWN);
player.sendMessage(cooldownMessage.replace("{time}", getFormattedTime(plugin, plugin.getCooldownHandler().getTimeLeft(npc.getId(), player.getUniqueId().toString()), plugin.getDisplayFormat())));
return;
}
}
@ -145,7 +146,7 @@ public class NPCClickListener implements Listener {
if (CitizensCMD.getEconomy() != null) {
if (!plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc.getId())) {
String messageConfirm = plugin.getLang().getMessage(Path.PAY_CONFIRM);
String messageConfirm = plugin.getLang().getMessage(Messages.PAY_CONFIRM);
if (!plugin.isShift())
messageConfirm = messageConfirm.replace("{shift}", "");
else
@ -160,7 +161,7 @@ public class NPCClickListener implements Listener {
if (plugin.isShift() && !player.isSneaking()) return;
plugin.getWaitingList().remove(player.getUniqueId().toString() + "." + npc.getId());
player.sendMessage(plugin.getLang().getMessage(Path.PAY_CANCELED));
player.sendMessage(plugin.getLang().getMessage(Messages.PAY_CANCELED));
}
}

View File

@ -19,8 +19,8 @@
package me.mattstudios.citizenscmd.listeners;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.Messages;
import me.mattstudios.citizenscmd.utility.Util;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.rayzr522.jsonmessage.JSONMessage;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -41,8 +41,8 @@ public class UpdateEvent implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) {
if (plugin.isUpdateStatus() && event.getPlayer().hasPermission("citizenscmd.update")) {
JSONMessage.create(color(Util.HEADER)).send(event.getPlayer());
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.NEW_VERSION) + plugin.getNewVersion())).send(event.getPlayer());
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Path.DOWNLOAD_AT) + " spigotmc.org/resources/citizens-CMD.30224/")).openURL("https://spigotmc.org/resources/citizens-CMD.30224/").send(event.getPlayer());
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Messages.NEW_VERSION) + plugin.getNewVersion())).send(event.getPlayer());
JSONMessage.create(color(plugin.getLang().getUncoloredMessage(Messages.DOWNLOAD_AT) + " spigotmc.org/resources/citizens-CMD.30224/")).openURL("https://spigotmc.org/resources/citizens-CMD.30224/").send(event.getPlayer());
}
}

View File

@ -19,7 +19,7 @@
package me.mattstudios.citizenscmd.schedulers;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.paths.Path;
import me.mattstudios.citizenscmd.utility.Messages;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
@ -41,7 +41,7 @@ public class ConfirmScheduler extends BukkitRunnable {
@Override
public void run() {
if (plugin.getWaitingList().containsKey(player.getUniqueId().toString() + "." + npc)) {
player.sendMessage(plugin.getLang().getMessage(Path.PAY_CANCELED));
player.sendMessage(plugin.getLang().getMessage(Messages.PAY_CANCELED));
plugin.getWaitingList().remove(player.getUniqueId().toString() + "." + npc);
}
}

View File

@ -0,0 +1,97 @@
package me.mattstudios.citizenscmd.utility;
public enum Messages {
/**
* START UP
*/
USING_LANGUAGE("messages.start-up.using-language"),
STARTUP_NEW_VERSION("messages.start-up.new-version"),
PAPI_AVAILABLE("messages.start-up.papi-available"),
VAULT_AVAILABLE("messages.start-up.vault-available"),
/**
* COMMANDS
*/
NPC_ADDED("messages.commands.npc-add-command-added"),
NPC_ADD_FAIL("messages.commands.npc-add-command-failed"),
NPC_ADD_DELAY_FAIL("messages.commands.npc-add-command-delay-failed"),
NPC_COOLDOWN_SET("messages.commands.npc-cooldown-set"),
NPC_COOLDOWN_SET_ERROR("messages.commands.npc-cooldown-error"),
NPC_PRICE_SET("messages.commands.npc-price-set"),
LIST_COUNT_RIGHT("messages.commands.list-commands-counter-right"),
LIST_COUNT_LEFT("messages.commands.list-commands-counter-left"),
LIST_TOOLTIP("messages.commands.list-tooltip"),
LIST_COOLDOWN("messages.commands.list-cooldown"),
LIST_PRICE("messages.commands.list-price"),
RELOAD("messages.commands.reload-command"),
REMOVED_COMMAND("messages.commands.removed-command"),
EDITED_COMMAND("messages.commands.edit-command"),
PERMISSION_SET("messages.commands.set-permission"),
PERMISSION_REMOVED("messages.commands.remove-permission"),
/**
* WARNINGS
*/
NO_NPC("messages.warnings.no-npc-selected"),
INVALID_NUMBER("messages.warnings.invalid-number"),
INVALID_ID_NUMBER("messages.warnings.invalid-id"),
INVALID_CLICK_TYPE("messages.warnings.invalid-click-type"),
NO_COMMANDS("messages.warnings.no-commands"),
INVALID_ARGUMENTS("messages.warnings.invalid-arguments"),
INVALID_PERMISSION("messages.warnings.invalid-permission"),
CONSOLE_NOT_ALLOWED("messages.warnings.console-not-allowed"),
NO_PERMISSION("messages.warnings.no-permission"),
WRONG_USAGE("messages.warnings.wrong-usage"),
NEW_VERSION("messages.warnings.new-version"),
DOWNLOAD_AT("messages.warnings.download-at"),
/**
* NPCS
*/
ON_COOLDOWN("messages.npc.on-cooldown"),
ONE_TIME_CLICK("messages.npc.one-time-click"),
PAY_CONFIRM("messages.npc.pay-confirm"),
PAY_CANCELED("messages.npc.pay-canceled"),
PAY_NO_MONEY("messages.npc.pay-no-money"),
PAY_COMPLETED("messages.npc.pay-completed"),
MESSAGE_DISPLAY("messages.npc.message-display"),
/**
* Help
*/
HELP_VERSION("messages.help.version"),
HELP_INFO("messages.help.info"),
HELP_EXAMPLE("messages.help.example"),
HELP_DESCRIPTION_ADD("messages.help.description-add"),
HELP_DESCRIPTION_COOLDOWN("messages.help.description-cooldown"),
HELP_DESCRIPTION_PRICE("messages.help.description-price"),
HELP_DESCRIPTION_LIST("messages.help.description-list"),
HELP_DESCRIPTION_EDIT("messages.help.description-edit"),
HELP_DESCRIPTION_REMOVE("messages.help.description-remove"),
HELP_DESCRIPTION_RELOAD("messages.help.description-reload"),
/**
* Time format
*/
SECONDS("messages.time-format.seconds"),
MINUTES("messages.time-format.minutes"),
HOURS("messages.time-format.hours"),
DAYS("messages.time-format.days");
private String path;
Messages(String path) {
this.path = path;
}
public String getPath() {
return path;
}
}

View File

@ -1,228 +1,38 @@
/*
CitizensCMD - Add-on for Citizens
Copyright (C) 2018 Mateus Moreira
<p>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
<p>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<p>
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
<p>
A special thanks to @ExtendedClip for letting me use and modify this class from PlaceholderAPI
*/
/*
A special thanks to @ExtendedClip for letting me use and modify this class from PlaceholderAPI
*/
package me.mattstudios.citizenscmd.utility;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.paths.Path;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TimeUtil {
private static String dayFormat;
private static String dayPlural;
private static String hourFormat;
private static String hourPlural;
private static String minuteFormat;
private static String minutePlural;
private static String secondFormat;
private static String secondPlural;
private int days;
private int hours;
private int minutes;
private int seconds;
/**
* Gets formatted time from seconds
*
* @param seconds The time in seconds to be converted
* @return String with the time like "2d 2h 2m 2s"
*/
public static String getFormattedTime(CitizensCMD plugin, long seconds, DisplayFormat format) {
private static final int SECONDS_IN_MINUTE = 60;
private static final int SECONDS_IN_HOUR = 60 * SECONDS_IN_MINUTE;
private static final int SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR;
String[] messagesString = new String[4];
messagesString[0] = plugin.getLang().getMessage(Path.SECONDS);
messagesString[1] = plugin.getLang().getMessage(Path.MINUTES);
messagesString[2] = plugin.getLang().getMessage(Path.HOURS);
messagesString[3] = plugin.getLang().getMessage(Path.DAYS);
public TimeUtil(long seconds) {
days = (int) (seconds / SECONDS_IN_DAY);
seconds = seconds - (days * SECONDS_IN_DAY);
hours = (int) (seconds / SECONDS_IN_HOUR);
seconds = seconds - (hours * SECONDS_IN_HOUR);
minutes = (int) (seconds / SECONDS_IN_MINUTE);
this.seconds = (int) (seconds - (minutes * SECONDS_IN_MINUTE));
}
String[] shorts = new String[4];
String[] mediums = new String[4];
String[] fulls = new String[4];
public int getDays() {
return days;
}
Pattern pattern = Pattern.compile("\\[([^]]*)], \\[([^]]*)], \\[([^]]*)]");
for (int i = 0; i < messagesString.length; i++) {
Matcher matcher = pattern.matcher(messagesString[i]);
if (matcher.find()) {
shorts[i] = matcher.group(1);
mediums[i] = matcher.group(2);
fulls[i] = matcher.group(3);
}
}
public int getHours() {
return hours;
}
switch (format) {
case MEDIUM:
String[] mediumsAfter = new String[4];
String[] mediumsPlurals = new String[4];
Pattern patternMediums = Pattern.compile("([^]]*)\\(([^]]*)\\)");
for (int i = 0; i < mediums.length; i++) {
if (mediums[i].contains("(") && mediums[i].contains(")")) {
Matcher matcher = patternMediums.matcher(mediums[i]);
if (matcher.find()) {
mediumsAfter[i] = matcher.group(1);
mediumsPlurals[i] = matcher.group(2);
}
} else {
mediumsAfter[i] = mediums[i];
mediumsPlurals[i] = "";
}
}
dayFormat = " " + mediumsAfter[3];
dayPlural = mediumsPlurals[3];
hourFormat = " " + mediumsAfter[2];
hourPlural = mediumsPlurals[2];
minuteFormat = " " + mediumsAfter[1];
minutePlural = mediumsPlurals[1];
secondFormat = " " + mediumsAfter[0];
secondPlural = mediumsPlurals[0];
break;
case FULL:
String[] fullsAfter = new String[4];
String[] fullsPlurals = new String[4];
Pattern patternFulls = Pattern.compile("([^]]*)\\(([^]]*)\\)");
for (int i = 0; i < fulls.length; i++) {
if (fulls[i].contains("(") && fulls[i].contains(")")) {
Matcher matcher = patternFulls.matcher(fulls[i]);
if (matcher.find()) {
fullsAfter[i] = matcher.group(1);
fullsPlurals[i] = matcher.group(2);
}
} else {
fullsAfter[i] = fulls[i];
fullsPlurals[i] = "";
}
}
dayFormat = " " + fullsAfter[3];
dayPlural = fullsPlurals[3];
hourFormat = " " + fullsAfter[2];
hourPlural = fullsPlurals[2];
minuteFormat = " " + fullsAfter[1];
minutePlural = fullsPlurals[1];
secondFormat = " " + fullsAfter[0];
secondPlural = fullsPlurals[0];
break;
default:
dayFormat = shorts[3];
hourFormat = shorts[2];
minuteFormat = shorts[1];
secondFormat = shorts[0];
break;
}
if (seconds < 60) {
if (seconds == 1 && !format.equals(DisplayFormat.SHORT))
return seconds + secondFormat;
return seconds + secondFormat + secondPlural;
}
long minutes = TimeUnit.SECONDS.toMinutes(seconds);
long secondsLeft = seconds - TimeUnit.MINUTES.toSeconds(minutes);
if (minutes < 60) {
if (minutes == 1 && !format.equals(DisplayFormat.SHORT)) {
if (secondsLeft > 0) {
if (secondsLeft == 1)
return minutes + minuteFormat + " " + secondsLeft + secondFormat;
return minutes + minuteFormat + " " + secondsLeft + secondFormat + secondPlural;
} else
return minutes + minuteFormat;
} else {
if (secondsLeft > 0) {
if (secondsLeft == 1 && !format.equals(DisplayFormat.SHORT))
return minutes + minuteFormat + minutePlural + " " + secondsLeft + secondFormat;
return minutes + minuteFormat + minutePlural + " " + secondsLeft + secondFormat + secondPlural;
} else
return minutes + minuteFormat + minutePlural;
}
}
if (minutes < 1440) {
long hours = TimeUnit.MINUTES.toHours(minutes);
String time;
if (hours == 1 && !format.equals(DisplayFormat.SHORT))
time = hours + hourFormat;
else
time = hours + hourFormat + hourPlural;
long leftOver = minutes - TimeUnit.HOURS.toMinutes(hours);
if (leftOver >= 1) {
if (leftOver == 1 && !format.equals(DisplayFormat.SHORT))
time += " " + leftOver + minuteFormat;
else
time += " " + leftOver + minuteFormat + minutePlural;
}
if (secondsLeft > 0)
if (secondsLeft == 1 && !format.equals(DisplayFormat.SHORT))
time += " " + secondsLeft + secondFormat;
else
time += " " + secondsLeft + secondFormat + secondPlural;
return time;
}
long days = TimeUnit.MINUTES.toDays(minutes);
String time;
if (days == 1 && !format.equals(DisplayFormat.SHORT))
time = days + dayFormat;
else
time = days + dayFormat + dayPlural;
long leftOver = minutes - TimeUnit.DAYS.toMinutes(days);
if (leftOver >= 1) {
if (leftOver < 60) {
if (leftOver == 1 && !format.equals(DisplayFormat.SHORT))
time += " " + leftOver + minuteFormat;
else
time += " " + leftOver + minuteFormat + minutePlural;
} else {
long hours = TimeUnit.MINUTES.toHours(leftOver);
if (hours == 1 && !format.equals(DisplayFormat.SHORT))
time += " " + hours + hourFormat;
else
time += " " + hours + hourFormat + hourPlural;
long minsLeft = leftOver - TimeUnit.HOURS.toMinutes(hours);
if (minsLeft == 1 && !format.equals(DisplayFormat.SHORT))
time += " " + minsLeft + minuteFormat;
else
time += " " + minsLeft + minuteFormat + minutePlural;
}
}
if (secondsLeft > 0) {
if (secondsLeft == 1 && !format.equals(DisplayFormat.SHORT))
time += " " + secondsLeft + secondFormat;
else
time += " " + secondsLeft + secondFormat + secondPlural;
}
return time;
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
}

View File

@ -20,12 +20,13 @@ package me.mattstudios.citizenscmd.utility;
import me.clip.placeholderapi.PlaceholderAPI;
import me.mattstudios.citizenscmd.CitizensCMD;
import me.mattstudios.citizenscmd.utility.paths.Path;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.io.ByteArrayOutputStream;
@ -33,6 +34,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -59,7 +61,7 @@ public class Util {
if (CitizensAPI.getDefaultNPCSelector().getSelected(player) != null) return false;
player.sendMessage(color(HEADER));
player.sendMessage(plugin.getLang().getMessage(Path.NO_NPC));
player.sendMessage(plugin.getLang().getMessage(Messages.NO_NPC));
return true;
}
@ -83,13 +85,48 @@ public class Util {
return CitizensAPI.getDefaultNPCSelector().getSelected(player).getId();
}
/**
* Checks whether or not it should check for updates
*
* @return Returns true if CheckUpdates is true on the config and false if not
*/
public static boolean upCheck(CitizensCMD plugin) {
return plugin.getConfig().getBoolean("check-updates");
public static void setUpMetrics(Metrics metrics, FileConfiguration config) {
System.out.println("here");
metrics.addCustomChart(new Metrics.SimplePie("lang", () -> {
switch (Objects.requireNonNull(config.getString("lang", "en")).toLowerCase()) {
case "en":
return "English";
case "bg":
return "Bulgarian";
case "fr":
return "French";
case "no":
return "Norwegian";
case "pt":
return "Portuguese";
case "Ro":
return "Romanian";
case "ch":
return "Chinese";
default:
return "Other";
}
}));
metrics.addCustomChart(new Metrics.SimplePie("cooldown_display", () -> {
switch (Objects.requireNonNull(config.getString("cooldown-time-display", "MEDIUM")).toLowerCase()) {
case "FULL":
return "Full";
case "SMALL":
return "Small";
default:
return "Medium";
}
}));
}
/**
@ -202,7 +239,7 @@ public class Util {
getScheduler().runTaskLater(plugin, () -> {
String finalMessage;
if (commands.get(finalI).contains("{display}")) {
String tmpStr = commands.get(finalI).replace("{display}", plugin.getLang().getMessage(Path.MESSAGE_DISPLAY));
String tmpStr = commands.get(finalI).replace("{display}", plugin.getLang().getMessage(Messages.MESSAGE_DISPLAY));
finalMessage = tmpStr.replace("{name}", npc.getFullName());
} else
finalMessage = commands.get(finalI);
@ -231,6 +268,166 @@ public class Util {
}
}
/**
* Gets formatted time from seconds
*
* @param seconds The time in seconds to be converted
* @return String with the time like "2d 2h 2m 2s"
*/
public static String getFormattedTime(CitizensCMD plugin, long seconds, DisplayFormat format) {
String dayPlural = "";
String hourPlural = "";
String minutePlural = "";
String secondPlural = "";
TimeUtil timeUtil = new TimeUtil(seconds);
String[] messagesString = new String[4];
messagesString[0] = plugin.getLang().getMessage(Messages.SECONDS);
messagesString[1] = plugin.getLang().getMessage(Messages.MINUTES);
messagesString[2] = plugin.getLang().getMessage(Messages.HOURS);
messagesString[3] = plugin.getLang().getMessage(Messages.DAYS);
String[] shorts = new String[4];
String[] mediums = new String[4];
String[] fulls = new String[4];
Pattern pattern = Pattern.compile("\\[([^]]*)], \\[([^]]*)], \\[([^]]*)]");
for (int i = 0; i < messagesString.length; i++) {
Matcher matcher = pattern.matcher(messagesString[i]);
if (matcher.find()) {
shorts[i] = matcher.group(1);
mediums[i] = matcher.group(2);
fulls[i] = matcher.group(3);
}
}
String dayFormat;
String hourFormat;
String minuteFormat;
String secondFormat;
switch (format) {
case MEDIUM:
String[] mediumsAfter = new String[4];
String[] mediumsPlurals = new String[4];
Pattern patternMediums = Pattern.compile("([^]]*)\\(([^]]*)\\)");
for (int i = 0; i < mediums.length; i++) {
if (mediums[i].contains("(") && mediums[i].contains(")")) {
Matcher matcher = patternMediums.matcher(mediums[i]);
if (matcher.find()) {
mediumsAfter[i] = matcher.group(1);
mediumsPlurals[i] = matcher.group(2);
}
} else {
mediumsAfter[i] = mediums[i];
mediumsPlurals[i] = "";
}
}
dayFormat = " " + mediumsAfter[3];
dayPlural = mediumsPlurals[3];
hourFormat = " " + mediumsAfter[2];
hourPlural = mediumsPlurals[2];
minuteFormat = " " + mediumsAfter[1];
minutePlural = mediumsPlurals[1];
secondFormat = " " + mediumsAfter[0];
secondPlural = mediumsPlurals[0];
break;
case FULL:
String[] fullsAfter = new String[4];
String[] fullsPlurals = new String[4];
Pattern patternFulls = Pattern.compile("([^]]*)\\(([^]]*)\\)");
for (int i = 0; i < fulls.length; i++) {
if (fulls[i].contains("(") && fulls[i].contains(")")) {
Matcher matcher = patternFulls.matcher(fulls[i]);
if (matcher.find()) {
fullsAfter[i] = matcher.group(1);
fullsPlurals[i] = matcher.group(2);
}
} else {
fullsAfter[i] = fulls[i];
fullsPlurals[i] = "";
}
}
dayFormat = " " + fullsAfter[3];
dayPlural = fullsPlurals[3];
hourFormat = " " + fullsAfter[2];
hourPlural = fullsPlurals[2];
minuteFormat = " " + fullsAfter[1];
minutePlural = fullsPlurals[1];
secondFormat = " " + fullsAfter[0];
secondPlural = fullsPlurals[0];
break;
default:
dayFormat = shorts[3];
hourFormat = shorts[2];
minuteFormat = shorts[1];
secondFormat = shorts[0];
break;
}
StringBuilder stringBuilder = new StringBuilder();
if (timeUtil.getDays() != 0) {
if (format != DisplayFormat.SHORT) {
if (timeUtil.getDays() == 1) {
stringBuilder.append(timeUtil.getDays()).append(dayFormat).append(" ");
} else {
stringBuilder.append(timeUtil.getDays()).append(dayFormat).append(dayPlural).append(" ");
}
} else {
stringBuilder.append(timeUtil.getDays()).append(dayFormat).append(" ");
}
}
if (timeUtil.getHours() != 0 || timeUtil.getDays() != 0) {
if (format != DisplayFormat.SHORT) {
if (timeUtil.getHours() == 1) {
stringBuilder.append(timeUtil.getHours()).append(hourFormat).append(" ");
} else {
stringBuilder.append(timeUtil.getHours()).append(hourFormat).append(hourPlural).append(" ");
}
} else {
stringBuilder.append(timeUtil.getHours()).append(hourFormat).append(" ");
}
}
if (timeUtil.getMinutes() != 0 || timeUtil.getHours() != 0) {
if (format != DisplayFormat.SHORT) {
if (timeUtil.getMinutes() == 1) {
stringBuilder.append(timeUtil.getMinutes()).append(minuteFormat).append(" ");
} else {
stringBuilder.append(timeUtil.getMinutes()).append(minuteFormat).append(minutePlural).append(" ");
}
} else {
stringBuilder.append(timeUtil.getMinutes()).append(minuteFormat).append(" ");
}
}
if (timeUtil.getSeconds() != 0 || timeUtil.getMinutes() != 0) {
if (format != DisplayFormat.SHORT) {
if (timeUtil.getSeconds() == 1) {
stringBuilder.append(timeUtil.getSeconds()).append(secondFormat);
} else {
stringBuilder.append(timeUtil.getSeconds()).append(secondFormat).append(secondPlural);
}
} else {
stringBuilder.append(timeUtil.getSeconds()).append(secondFormat);
}
}
return stringBuilder.toString();
}
private static boolean soundExists(String soundName) {
for (Sound sound : Sound.values()) {
if (sound.name().equalsIgnoreCase(soundName)) return true;

View File

@ -1,116 +0,0 @@
/*
CitizensCMD - Add-on for Citizens
Copyright (C) 2018 Mateus Moreira
<p>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
<p>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<p>
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package me.mattstudios.citizenscmd.utility.paths;
public class Path {
/**
* START UP
*/
private static final String MAIN_PATH_STARTUP = "messages.start-up.";
public static final String USING_LANGUAGE = MAIN_PATH_STARTUP + "using-language";
public static final String STARTUP_NEW_VERSION = MAIN_PATH_STARTUP + "new-version";
public static final String PAPI_AVAILABLE = MAIN_PATH_STARTUP + "papi-available";
public static final String VAULT_AVAILABLE = MAIN_PATH_STARTUP + "vault-available";
/**
* COMMANDS
*/
private static final String MAIN_PATH_COMMANDS = "messages.commands.";
public static final String NPC_ADDED = MAIN_PATH_COMMANDS + "npc-add-command-added";
public static final String NPC_ADD_FAIL = MAIN_PATH_COMMANDS + "npc-add-command-failed";
public static final String NPC_ADD_DELAY_FAIL = MAIN_PATH_COMMANDS + "npc-add-command-delay-failed";
public static final String NPC_COOLDOWN_SET = MAIN_PATH_COMMANDS + "npc-cooldown-set";
public static final String NPC_COOLDOWN_SET_ERROR = MAIN_PATH_COMMANDS + "npc-cooldown-error";
public static final String NPC_PRICE_SET = MAIN_PATH_COMMANDS + "npc-price-set";
public static final String LIST_COUNT_RIGHT = MAIN_PATH_COMMANDS + "list-commands-counter-right";
public static final String LIST_COUNT_LEFT = MAIN_PATH_COMMANDS + "list-commands-counter-left";
public static final String LIST_TOOLTIP = MAIN_PATH_COMMANDS + "list-tooltip";
public static final String LIST_COOLDOWN = MAIN_PATH_COMMANDS + "list-cooldown";
public static final String LIST_PRICE = MAIN_PATH_COMMANDS + "list-price";
public static final String RELOAD = MAIN_PATH_COMMANDS + "reload-command";
public static final String REMOVED_COMMAND = MAIN_PATH_COMMANDS + "removed-command";
public static final String EDITED_COMMAND = MAIN_PATH_COMMANDS + "edit-command";
public static final String PERMISSION_SET = MAIN_PATH_COMMANDS + "set-permission";
public static final String PERMISSION_REMOVED = MAIN_PATH_COMMANDS + "remove-permission";
/**
* WARNINGS
*/
private static final String MAIN_PATH_WARNINGS = "messages.warnings.";
public static final String NO_NPC = MAIN_PATH_WARNINGS + "no-npc-selected";
public static final String INVALID_COOLDOWN = MAIN_PATH_WARNINGS + "invalid-cooldown";
public static final String INVALID_PRICE = MAIN_PATH_WARNINGS + "invalid-price";
public static final String INVALID_ID_NUMBER = MAIN_PATH_WARNINGS + "invalid-id";
public static final String INVALID_CLICK_TYPE = MAIN_PATH_WARNINGS + "invalid-click-type";
public static final String NO_COMMANDS = MAIN_PATH_WARNINGS + "no-commands";
public static final String INVALID_ARGUMENTS = MAIN_PATH_WARNINGS + "invalid-arguments";
public static final String INVALID_PERMISSION = MAIN_PATH_WARNINGS + "invalid-permission";
public static final String CONSOLE_NOT_ALLOWED = MAIN_PATH_WARNINGS + "console-not-allowed";
public static final String NO_PERMISSION = MAIN_PATH_WARNINGS + "no-permission";
public static final String WRONG_USAGE = MAIN_PATH_WARNINGS + "wrong-usage";
public static final String NEW_VERSION = MAIN_PATH_WARNINGS + "new-version";
public static final String DOWNLOAD_AT = MAIN_PATH_WARNINGS + "download-at";
/**
* NPCS
*/
private static final String MAIN_PATH_NPCS = "messages.npc.";
public static final String ON_COOLDOWN = MAIN_PATH_NPCS + "on-cooldown";
public static final String ONE_TIME_CLICK = MAIN_PATH_NPCS + "one-time-click";
public static final String PAY_CONFIRM = MAIN_PATH_NPCS + "pay-confirm";
public static final String PAY_CANCELED = MAIN_PATH_NPCS + "pay-canceled";
public static final String PAY_NO_MONEY = MAIN_PATH_NPCS + "pay-no-money";
public static final String PAY_COMPLETED = MAIN_PATH_NPCS + "pay-completed";
public static final String MESSAGE_DISPLAY = MAIN_PATH_NPCS + "message-display";
/**
* Help
*/
private static final String MAIN_PATH_HELP = "messages.help.";
public static final String HELP_VERSION = MAIN_PATH_HELP + "version";
public static final String HELP_INFO = MAIN_PATH_HELP + "info";
public static final String HELP_EXAMPLE = MAIN_PATH_HELP + "example";
public static final String HELP_DESCRIPTION_ADD = MAIN_PATH_HELP + "description-add";
public static final String HELP_DESCRIPTION_COOLDOWN = MAIN_PATH_HELP + "description-cooldown";
public static final String HELP_DESCRIPTION_PRICE = MAIN_PATH_HELP + "description-price";
public static final String HELP_DESCRIPTION_LIST = MAIN_PATH_HELP + "description-list";
public static final String HELP_DESCRIPTION_EDIT = MAIN_PATH_HELP + "description-edit";
public static final String HELP_DESCRIPTION_REMOVE = MAIN_PATH_HELP + "description-remove";
public static final String HELP_DESCRIPTION_RELOAD = MAIN_PATH_HELP + "description-reload";
public static final String HELP_DESCRIPTION_SOUND = MAIN_PATH_HELP + "description-sound";
/**
* Time format
*/
private static final String MAIN_PATH_TIME_FORMAT = "messages.time-format.";
public static final String SECONDS = MAIN_PATH_TIME_FORMAT + "seconds";
public static final String MINUTES = MAIN_PATH_TIME_FORMAT + "minutes";
public static final String HOURS = MAIN_PATH_TIME_FORMAT + "hours";
public static final String DAYS = MAIN_PATH_TIME_FORMAT + "days";
}

View File

@ -31,8 +31,7 @@ messages:
remove-permission: "&aPermission removed successfully!"
warnings:
no-npc-selected: "&cYou must have an NPC selected to execute that command!"
invalid-cooldown: "&cThe cooldown must be a number!"
invalid-price: "&cThe price must be a number!"
invalid-number: "&cThe value entered is not a number!"
invalid-id: "&cPlease introduce a valid command ID!"
invalid-click-type: "&cYou must select either left or right!"
no-commands: "&cThere is no more commands in this NPC!"
@ -62,7 +61,7 @@ messages:
description-edit: "&7Edits a specific command or permission."
description-remove: "&7Removes a command from the NPC."
description-reload: "&7Reloads all the files."
description-sound: "&7Adds a sound to an NPC."
# IMPORTANT!!
# DO NOT CHANGE THIS PATTERN, KEEP THE "[]," CHANGE ONLY THE TEXT INSIDE, O "()" contém os PLURALS
time-format: