Skip to content

Commit 577bcde

Browse files
authored
Config refactor (#89)
2 parents d740f91 + 46f4ab8 commit 577bcde

File tree

13 files changed

+731
-629
lines changed

13 files changed

+731
-629
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/Account.java

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package pro.cloudnode.smp.bankaccounts;
22

33
import net.kyori.adventure.text.Component;
4-
import net.kyori.adventure.text.format.TextDecoration;
54
import net.kyori.adventure.text.minimessage.MiniMessage;
6-
import net.kyori.adventure.text.minimessage.tag.resolver.Formatter;
7-
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
85
import org.bukkit.Material;
96
import org.bukkit.NamespacedKey;
107
import org.bukkit.OfflinePlayer;
@@ -24,9 +21,7 @@
2421
import java.time.LocalDateTime;
2522
import java.time.ZoneOffset;
2623
import java.util.ArrayList;
27-
import java.util.HashMap;
2824
import java.util.List;
29-
import java.util.Map;
3025
import java.util.Objects;
3126
import java.util.Optional;
3227
import java.util.UUID;
@@ -110,6 +105,18 @@ public Account(final @NotNull ResultSet rs) throws @NotNull SQLException {
110105
);
111106
}
112107

108+
public final @NotNull String name() {
109+
return this.name == null ? (this.type == Type.PERSONAL && this.owner.getName() != null ? this.owner.getName() : this.id) : this.name;
110+
}
111+
112+
public final @NotNull Component ownerName() {
113+
return this.owner.getUniqueId().equals(BankAccounts.getConsoleOfflinePlayer().getUniqueId()) ? MiniMessage.miniMessage().deserialize("<i>the server</i>") : this.owner.getName() == null ? MiniMessage.miniMessage().deserialize("<i>unknown player</i>") : Component.text(this.owner.getName());
114+
}
115+
116+
public final @NotNull String ownerNameUnparsed() {
117+
return this.owner.getUniqueId().equals(BankAccounts.getConsoleOfflinePlayer().getUniqueId()) ? "<i>the server</i>" : this.owner.getName() == null ? "<i>unknown player</i>" : this.owner.getName();
118+
}
119+
113120
/**
114121
* Update account balance
115122
* @param diff Balance difference (positive or negative)
@@ -157,15 +164,11 @@ public final boolean hasFunds(final @NotNull BigDecimal amount) {
157164
final @NotNull Material material = BankAccounts.getInstance().config().instrumentsMaterial();
158165
final @NotNull ItemStack instrument = new ItemStack(material);
159166

160-
final @NotNull String name = BankAccounts.getInstance().config().instrumentsName();
161-
final @NotNull List<@NotNull String> lore = BankAccounts.getInstance().config().instrumentsLore();
162-
final boolean glint = BankAccounts.getInstance().config().instrumentsGlintEnabled();
163-
164167
final @NotNull ItemMeta meta = instrument.getItemMeta();
165-
meta.displayName(this.instrumentPlaceholders(name));
166-
meta.lore(lore.stream().map(this::instrumentPlaceholders).toList());
168+
meta.displayName(BankAccounts.getInstance().config().instrumentsName(this, LocalDateTime.now(ZoneOffset.UTC)));
169+
meta.lore(BankAccounts.getInstance().config().instrumentsLore(this, LocalDateTime.now(ZoneOffset.UTC)));
167170

168-
if (glint) {
171+
if (BankAccounts.getInstance().config().instrumentsGlintEnabled()) {
169172
final @NotNull Enchantment enchantment = BankAccounts.getInstance().config().instrumentsGlintEnchantment();
170173
meta.addEnchant(enchantment, 1, true);
171174
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
@@ -188,20 +191,6 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
188191
return item.getItemMeta().getPersistentDataContainer().has(BankAccounts.Key.INSTRUMENT_ACCOUNT, PersistentDataType.STRING);
189192
}
190193

191-
/**
192-
* Set placeholders for instrument
193-
* @param string String to set placeholders in
194-
*/
195-
public final @NotNull Component instrumentPlaceholders (final @NotNull String string) {
196-
return MiniMessage.miniMessage().deserialize(string,
197-
Placeholder.unparsed("account", this.name == null ? (this.type == Type.PERSONAL && this.owner.getName() != null ? this.owner.getName() : this.id) : this.name),
198-
Placeholder.parsed("account-id", this.id),
199-
Placeholder.parsed("account-type", this.type.getName()),
200-
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()),
201-
Formatter.date("date", LocalDateTime.now(ZoneOffset.UTC))
202-
).decoration(TextDecoration.ITALIC, false);
203-
}
204-
205194
/**
206195
* Get account by ID
207196
* @param id Account ID
@@ -248,7 +237,7 @@ public static boolean isInstrument(final @NotNull ItemStack item) {
248237
return accounts.toArray(new Account[0]);
249238
}
250239
catch (final @NotNull Exception e) {
251-
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get accounts for: " + owner.getUniqueId().toString() + " (" + owner.getName() + "), type = " + (type == null ? "all" : type.name()), e);
240+
BankAccounts.getInstance().getLogger().log(Level.SEVERE, "Could not get accounts for: " + owner.getUniqueId() + " (" + owner.getName() + "), type = " + (type == null ? "all" : type.name()), e);
252241
return new Account[0];
253242
}
254243
}
@@ -377,58 +366,6 @@ public void delete() {
377366
}
378367
}
379368

380-
/**
381-
* Account placeholders
382-
* @param string String to deserialize with MiniMessage and apply placeholders to
383-
* @param accounts Accounts to apply placeholders to
384-
*/
385-
public static String placeholdersString(@NotNull String string, HashMap<String, @NotNull Account> accounts) {
386-
for (Map.Entry<String, Account> entry : accounts.entrySet()) {
387-
String name = entry.getKey();
388-
Account account = entry.getValue();
389-
String prefix = name.isEmpty() ? "" : name + "-";
390-
string = string.replace("<" + prefix + "account>", account.name == null ? (account.type == Account.Type.PERSONAL && account.owner.getName() != null ? account.owner.getName() : account.id) : account.name)
391-
.replace("<" + prefix + "account-id>", account.id)
392-
.replace("<" + prefix + "account-type>", account.type.getName())
393-
.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())
394-
.replace("<" + prefix + "balance>", account.balance == null ? "∞" : account.balance.toPlainString())
395-
.replace("<" + prefix + "balance-formatted>", BankAccounts.formatCurrency(account.balance))
396-
.replace("<" + prefix + "balance-short>", BankAccounts.formatCurrencyShort(account.balance));
397-
}
398-
return string;
399-
}
400-
401-
/**
402-
* Account placeholders
403-
* @param string String to deserialize with MiniMessage and apply placeholders to
404-
* @param accounts Accounts to apply placeholders to
405-
*/
406-
public static Component placeholders(@NotNull String string, HashMap<String, Account> accounts) {
407-
return MiniMessage.miniMessage().deserialize(placeholdersString(string, accounts));
408-
}
409-
410-
/**
411-
* Account placeholders
412-
* @param string String to deserialize with MiniMessage and apply placeholders to
413-
* @param account Account to apply placeholders to
414-
*/
415-
public static Component placeholders(@NotNull String string, Account account) {
416-
return placeholders(string, new HashMap<>() {{
417-
put("", account);
418-
}});
419-
}
420-
421-
/**
422-
* Account placeholders
423-
* @param string String to deserialize with MiniMessage and apply placeholders to
424-
* @param account Account to apply placeholders to
425-
*/
426-
public static String placeholdersString(@NotNull String string, Account account) {
427-
return placeholdersString(string, new HashMap<>() {{
428-
put("", account);
429-
}});
430-
}
431-
432369
/**
433370
* Bank account type
434371
*/

src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import org.bukkit.command.CommandSender;
1212
import org.bukkit.command.PluginCommand;
1313
import org.bukkit.event.Listener;
14-
import org.bukkit.inventory.Inventory;
15-
import org.bukkit.inventory.InventoryHolder;
16-
import org.bukkit.inventory.ItemStack;
1714
import org.bukkit.plugin.java.JavaPlugin;
1815
import org.bukkit.scheduler.BukkitTask;
1916
import org.jetbrains.annotations.NotNull;
@@ -38,7 +35,6 @@
3835
import java.sql.Connection;
3936
import java.sql.PreparedStatement;
4037
import java.sql.SQLException;
41-
import java.text.DecimalFormat;
4238
import java.util.Arrays;
4339
import java.util.HashMap;
4440
import java.util.Map;
@@ -149,13 +145,11 @@ public static void reload() {
149145
getInstance().setupDbSource();
150146
getInstance().initDbWrapper();
151147
createServerAccount();
152-
getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> {
153-
checkForUpdates().ifPresent(latestVersion -> {
154-
getInstance().getLogger().warning("An update is available: " + latestVersion);
155-
getInstance().getLogger().warning("Please update to the latest version to benefit from bug fixes, security patches, new features and support.");
156-
getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion);
157-
});
158-
});
148+
getInstance().getServer().getScheduler().runTaskAsynchronously(getInstance(), () -> checkForUpdates().ifPresent(latestVersion -> {
149+
getInstance().getLogger().warning("An update is available: " + latestVersion);
150+
getInstance().getLogger().warning("Please update to the latest version to benefit from bug fixes, security patches, new features and support.");
151+
getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion);
152+
}));
159153
getInstance().startInterestTimer();
160154
}
161155

@@ -245,12 +239,7 @@ private void startInterestTimer() {
245239
private void interestPayment(final @NotNull Account account, final @NotNull BigDecimal amount, final double rate, final @NotNull Account serverAccount) {
246240
if (account.balance == null) return;
247241
if (account.id.equals(serverAccount.id)) return;
248-
final @NotNull String description = this.config().interestDescription(account.type)
249-
.replace("<rate>", String.valueOf(rate))
250-
.replace("<rate-formatted>", new DecimalFormat("#.##").format(rate) + "%")
251-
.replace("<balance>", account.balance.toPlainString())
252-
.replace("<balance-formatted>", BankAccounts.formatCurrency(account.balance))
253-
.replace("<balance-short>", BankAccounts.formatCurrencyShort(account.balance));
242+
final @NotNull String description = this.config().interestDescription(account.type, rate, account);
254243

255244
try {
256245
// interest paid to the bank
@@ -283,8 +272,7 @@ private void interestPayment(final @NotNull Account account, final @NotNull BigD
283272
*/
284273
public static String formatCurrency(final @Nullable BigDecimal amount) {
285274
if (amount == null) return getCurrencySymbol() + "∞";
286-
final @Nullable String format = getInstance().config().currencyFormat();
287-
return (amount.compareTo(BigDecimal.ZERO) < 0 ? "<red>-" : "") + getCurrencySymbol() + new DecimalFormat(format).format(amount.abs().setScale(2, RoundingMode.HALF_UP)) + (amount.compareTo(BigDecimal.ZERO) < 0 ? "</red>" : "");
275+
return (amount.compareTo(BigDecimal.ZERO) < 0 ? "<red>-" : "") + getCurrencySymbol() + getInstance().config().currencyFormat().format(amount.abs().setScale(2, RoundingMode.HALF_UP)) + (amount.compareTo(BigDecimal.ZERO) < 0 ? "</red>" : "");
288276
}
289277

290278
/**
@@ -394,32 +382,6 @@ public static Optional<String> checkForUpdates() {
394382
return Optional.empty();
395383
}
396384

397-
/**
398-
* Check if an inventory can fit items
399-
*
400-
* @param inventory The inventory that you want to hold the items
401-
* @param items The items to check if they can fit in the inventory
402-
* @return A HashMap containing items that didn't fit.
403-
*/
404-
public static @NotNull HashMap<@NotNull Integer, @NotNull ItemStack> canFit(final @NotNull Inventory inventory, final @NotNull ItemStack... items) {
405-
final @NotNull Inventory inv = getInstance().getServer().createInventory(null, inventory.getSize());
406-
inv.setContents(inventory.getContents());
407-
final @NotNull HashMap<@NotNull Integer, @NotNull ItemStack> didNotFit = inv.addItem(items);
408-
inv.close();
409-
return didNotFit;
410-
}
411-
412-
/**
413-
* Check if entity's inventory can fit items
414-
*
415-
* @param entity The entity that you want to hold the items
416-
* @param items The items to check if they can fit in the inventory
417-
* @return A HashMap containing items that didn't fit.
418-
*/
419-
public static @NotNull HashMap<@NotNull Integer, @NotNull ItemStack> canFit(final @NotNull InventoryHolder entity, final @NotNull ItemStack... items) {
420-
return canFit(entity.getInventory(), items);
421-
}
422-
423385
public static final class Key {
424386
public final static @NotNull NamespacedKey INSTRUMENT_ACCOUNT = namespacedKey("instrument-account");
425387
public final static @NotNull NamespacedKey POS_OWNER_GUI = namespacedKey("pos-owner-gui");

0 commit comments

Comments
 (0)