11package pro .cloudnode .smp .bankaccounts ;
22
33import net .kyori .adventure .text .Component ;
4- import net .kyori .adventure .text .format .TextDecoration ;
54import 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 ;
85import org .bukkit .Material ;
96import org .bukkit .NamespacedKey ;
107import org .bukkit .OfflinePlayer ;
2421import java .time .LocalDateTime ;
2522import java .time .ZoneOffset ;
2623import java .util .ArrayList ;
27- import java .util .HashMap ;
2824import java .util .List ;
29- import java .util .Map ;
3025import java .util .Objects ;
3126import java .util .Optional ;
3227import 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 */
0 commit comments