Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customisation for account type string and material name translation #32

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/main/java/pro/cloudnode/smp/bankaccounts/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
return MiniMessage.miniMessage().deserialize(string,
Placeholder.unparsed("account", this.name == null ? (this.type == Type.PERSONAL && this.owner.getName() != null ? this.owner.getName() : this.id) : this.name),
Placeholder.parsed("account-id", this.id),
Placeholder.parsed("account-type", this.type.name),
Placeholder.parsed("account-type", this.type.getName()),
Placeholder.parsed("account-owner", this.owner.getUniqueId().equals(BankAccounts.getConsoleOfflinePlayer().getUniqueId()) ? "<i>the server</i>" : this.owner.getName() == null ? "<i>unknown player</i>" : this.owner.getName()),
Formatter.date("date", LocalDateTime.now(ZoneOffset.UTC))
).decoration(TextDecoration.ITALIC, false);
Expand Down Expand Up @@ -345,7 +345,7 @@ public static String placeholdersString(@NotNull String string, HashMap<String,
String prefix = name.isEmpty() ? "" : name + "-";
string = string.replace("<" + prefix + "account>", account.name == null ? (account.type == Account.Type.PERSONAL && account.owner.getName() != null ? account.owner.getName() : account.id) : account.name)
.replace("<" + prefix + "account-id>", account.id)
.replace("<" + prefix + "account-type>", account.type.name)
.replace("<" + prefix + "account-type>", account.type.getName())
.replace("<" + prefix + "account-owner>", account.owner.getUniqueId().equals(BankAccounts.getConsoleOfflinePlayer().getUniqueId()) ? "<i>the server</i>" : account.owner.getName() == null ? "<i>unknown player</i>" : account.owner.getName())
.replace("<" + prefix + "balance>", account.balance == null ? "∞" : account.balance.toPlainString())
.replace("<" + prefix + "balance-formatted>", BankAccounts.formatCurrency(account.balance))
Expand Down Expand Up @@ -392,19 +392,17 @@ public enum Type {
/**
* Personal, individual, private account
*/
PERSONAL("Personal"),
PERSONAL,
/**
* Account owned by a company or other corporate entity
*/
BUSINESS("Business");
BUSINESS;

/**
* User-friendly name
* Get type name (as set in config)
*/
public final @NotNull String name;

Type(final @NotNull String name) {
this.name = name;
public @NotNull String getName() {
return Objects.requireNonNull(BankAccounts.getInstance().getConfig().getString("messages.types." + getType(this)));
}

/**
Expand All @@ -426,9 +424,8 @@ public static int getType(final @NotNull Type type) {
}

public static @NotNull Optional<@NotNull Type> fromString(final @NotNull String name) {
for (final @NotNull Type type : Type.values()) {
if (type.name.equalsIgnoreCase(name)) return Optional.of(type);
}
for (final @NotNull Type type : Type.values())
if (type.name().equalsIgnoreCase(name)) return Optional.of(type);
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ public static boolean create(final @NotNull CommandSender sender, final @NotNull
int limit = BankAccounts.getInstance().getConfig()
.getInt("account-limits." + Account.Type.getType(optionalType.get()));
if (limit != -1 && accounts.length >= limit)
return sendMessage(sender, BankConfig.MESSAGES_ERRORS_MAX_ACCOUNTS, Placeholder.unparsed("type", optionalType.get().name), Placeholder.unparsed("limit", String.valueOf(BankAccounts
.getInstance().getConfig()
.getInt("account-limits." + Account.Type.getType(optionalType.get())))));
return sendMessage(sender, BankConfig.MESSAGES_ERRORS_MAX_ACCOUNTS, Placeholder.unparsed("type", optionalType
.get().getName()), Placeholder.unparsed("limit", String.valueOf(BankAccounts.getInstance()
.getConfig().getInt("account-limits." + Account.Type.getType(optionalType.get())))));
}

final @NotNull Account account = new Account(target, optionalType.get(), null, BigDecimal.ZERO, false);
Expand Down Expand Up @@ -460,8 +460,8 @@ public static boolean transfer(final @NotNull CommandSender sender, final @NotNu
return sendMessage(sender, Account.placeholders(Objects.requireNonNull(BankAccounts.getInstance()
.getConfig().getString(BankConfig.MESSAGES_ERRORS_INSUFFICIENT_FUNDS.getKey())), from.get()));

@Nullable String description = args.length > 3 ? String.join(" ", Arrays.copyOfRange(argsCopy, 3, argsCopy.length))
.trim() : null;
@Nullable String description = args.length > 3 ? String
.join(" ", Arrays.copyOfRange(argsCopy, 3, argsCopy.length)).trim() : null;
if (description != null && description.length() > 64) description = description.substring(0, 64);

if (description != null && (description.contains("<") || description.contains(">")))
Expand Down Expand Up @@ -576,10 +576,10 @@ else if (args.length < 1)
.findFirst().orElse(null);

if (!sender.hasPermission("bank.instrument.create.bypass")) {
if (item == null)
return sendMessage(sender, BankConfig.MESSAGES_ERRORS_INSTRUMENT_REQUIRES_ITEM, Placeholder.unparsed("material", Objects
.requireNonNull(BankAccounts.getInstance().getConfig()
.getString(BankConfig.INSTRUMENTS_MATERIAL.getKey())).toLowerCase()));
if (item == null) return sendMessage(sender, Objects
.requireNonNull(BankAccounts.getInstance().getConfig()
.getString(BankConfig.MESSAGES_ERRORS_INSTRUMENT_REQUIRES_ITEM.getKey()))
.replace("<material-key>", material.translationKey()), Placeholder.unparsed("material", material.name()));
else {
final @NotNull ItemStack clone = item.clone();
clone.setAmount(1);
Expand Down
13 changes: 11 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ messages:
# - <arguments> - The command usage arguments (e.g. "send <from> <to> <amount> [description]")
command-usage: "<yellow>(!) Usage:</yellow> <white>/<command> <arguments></white>"

# Account types
types:
# Personal account
0: Personal
# Business account
1: Business

# Errors
errors:
# You have no accounts
Expand Down Expand Up @@ -268,8 +275,10 @@ messages:
# Player not found
player-not-found: "<red>(!) Player not found.</red>"
# You must have a specific item to convert to an instrument
# Placeholder: <material> - the item type that is required
instrument-requires-item: "<red>(!) You must have <gray>x1 <material></gray> in your inventory to create a bank card from.</red>"
# Placeholders:
# <material> - the item type that is required (the item ID)
# <material-key> - item translation key, can be used together with <lang:<material-key>>
instrument-requires-item: "<red>(!) You must have <gray>x1 <lang:<material-key>></gray> in your inventory to create a bank card from.</red>"
# Target player's inventory is full
# Placeholder: <player> - the target player's name
target-inventory-full: "<red>(!) The inventory of <gray><player></gray> is full.</red>"
Expand Down