Skip to content

Commit cbaa08a

Browse files
committed
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-of-a-bank-account
2 parents 52a6a7e + 93ea9fd commit cbaa08a

File tree

9 files changed

+61
-35
lines changed

9 files changed

+61
-35
lines changed

.github/workflows/maven.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
name: Java CI with Maven
1010

11+
permissions:
12+
contents: write
13+
1114
on:
1215
push:
1316
branches: [ "master" ]

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<plugin>
3131
<groupId>org.apache.maven.plugins</groupId>
3232
<artifactId>maven-shade-plugin</artifactId>
33-
<version>3.5.1</version>
33+
<version>3.5.2</version>
3434
<executions>
3535
<execution>
3636
<phase>package</phase>
@@ -82,7 +82,7 @@
8282
<dependency>
8383
<groupId>org.mariadb.jdbc</groupId>
8484
<artifactId>mariadb-java-client</artifactId>
85-
<version>3.3.2</version>
85+
<version>3.3.3</version>
8686
</dependency>
8787
<dependency>
8888
<groupId>me.clip</groupId>

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
import java.util.Objects;
4343
import java.util.Optional;
4444
import java.util.UUID;
45-
import java.util.function.Supplier;
45+
import java.util.concurrent.Callable;
46+
import java.util.concurrent.Future;
47+
import java.util.concurrent.TimeUnit;
4648
import java.util.logging.Level;
4749

4850
public final class BankAccounts extends JavaPlugin {
@@ -395,6 +397,32 @@ public static Optional<String> checkForUpdates() {
395397
return Optional.empty();
396398
}
397399

400+
/**
401+
* Run a task on the main thread
402+
* @param task The task to run
403+
* @param timeout Task timeout in SECONDS. Set to 0 to disable timeout
404+
*/
405+
public static <T> @NotNull Optional<T> runOnMain(@NotNull Callable<T> task, final long timeout) {
406+
final @NotNull BankAccounts plugin = BankAccounts.getInstance();
407+
final @NotNull Future<T> future = plugin.getServer().getScheduler().callSyncMethod(plugin, task);
408+
try {
409+
if (timeout == 0) return Optional.of(future.get());
410+
return Optional.of(future.get(timeout, TimeUnit.SECONDS));
411+
}
412+
catch (final @NotNull Exception e) {
413+
plugin.getLogger().log(Level.WARNING, "Failed to run task on main thread", e);
414+
}
415+
return Optional.empty();
416+
}
417+
418+
/**
419+
* Run a task on the main thread (without timeout)
420+
* @param task The task to run
421+
*/
422+
public static <T> @NotNull Optional<T> runOnMain(@NotNull Callable<T> task) {
423+
return runOnMain(task, 0);
424+
}
425+
398426
public static final class Key {
399427
public final static @NotNull NamespacedKey INSTRUMENT_ACCOUNT = namespacedKey("instrument-account");
400428
public final static @NotNull NamespacedKey POS_OWNER_GUI = namespacedKey("pos-owner-gui");

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,11 @@ public int invoicePerPage() {
752752
return Objects.requireNonNull(config.getString("messages.errors.change-owner-accept-failed"));
753753
}
754754

755+
// messages.errors.async-failed
756+
public @NotNull Component messagesErrorsAsyncFailed() {
757+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.async-failed")));
758+
}
759+
755760
// messages.balance
756761
public @NotNull Component messagesBalance(final @NotNull Account account) {
757762
return MiniMessage.miniMessage().deserialize(
@@ -894,12 +899,10 @@ public int invoicePerPage() {
894899
.replace("<to-balance-short>", BankAccounts.formatCurrencyShort(to.balance))
895900
.replace("<amount>", amount.toPlainString())
896901
.replace("<amount-formatted>", BankAccounts.formatCurrency(amount))
897-
.replace("<amount-short>", BankAccounts.formatCurrencyShort(amount)),
902+
.replace("<amount-short>", BankAccounts.formatCurrencyShort(amount))
903+
.replace("<confirm-command>", "/bank transfer --confirm " + from.id + " " + to.id + " " + amount.toPlainString() + (description == null ? "" : " " + description)),
898904
Placeholder.component("description", description == null ? MiniMessage.miniMessage().deserialize("<gray><i>no description</i>") : Component.text(description))
899-
).replaceText(configurer -> {
900-
configurer.matchLiteral("<confirm-command>");
901-
configurer.replacement(Component.text("/bank transfer --confirm " + from.id + " " + to.id + " " + amount.toPlainString() + (description == null ? "" : " " + description)));
902-
});
905+
);
903906
}
904907

905908
// messages.transfer-sent
@@ -1143,13 +1146,9 @@ public int invoicePerPage() {
11431146
Objects.requireNonNull(config.getString("messages.baltop.header"))
11441147
.replace("<category>", category)
11451148
.replace("<page>", String.valueOf(page))
1146-
).replaceText(configurer -> {
1147-
configurer.matchLiteral("<cmd-prev>");
1148-
configurer.replacement(cmdPrev);
1149-
}).replaceText(configurer -> {
1150-
configurer.matchLiteral("<cmd-next>");
1151-
configurer.replacement(cmdNext);
1152-
});
1149+
.replace("<cmd-prev>", cmdPrev)
1150+
.replace("<cmd-next>", cmdNext)
1151+
);
11531152
}
11541153

11551154
// messages.baltop.entry

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public static boolean sendMessage(final @Nullable Audience audience, final @NotN
5151
/**
5252
* Send command usage to sender.
5353
*
54-
* @param sender Command sender.
54+
* @param audience Message recipient
5555
* @param label Command label.
5656
* @param arguments Command arguments.
5757
* @return Always true.
5858
*/
59-
protected static boolean sendUsage(final @NotNull CommandSender sender, final @NotNull String label, final @NotNull String arguments) {
60-
return sendMessage(sender, BankAccounts.getInstance().config().messagesCommandUsage(label, arguments));
59+
protected static boolean sendUsage(final @NotNull Audience audience, final @NotNull String label, final @NotNull String arguments) {
60+
return sendMessage(audience, BankAccounts.getInstance().config().messagesCommandUsage(label, arguments));
6161
}
6262

6363
/**

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Optional;
3131
import java.util.UUID;
3232
import java.util.logging.Level;
33-
import java.util.stream.IntStream;
3433
import java.util.zip.CRC32;
3534

3635
public final class POS {
@@ -374,26 +373,18 @@ public static void openBuyGui(final @NotNull Player player, final @NotNull Chest
374373
return Arrays.stream(items).sorted(Comparator.comparing(ItemStack::translationKey)).sorted(Comparator.comparing(ItemStack::getAmount)).map(POS::checksum).toArray(String[]::new);
375374
}
376375

377-
/**
378-
* Verify checksum
379-
*
380-
* @param item The item to verify
381-
* @param checksum The checksum to verify against
382-
*/
383-
public static boolean verifyChecksum(final @NotNull ItemStack item, final @NotNull String checksum) {
384-
return checksum(item).equals(checksum);
385-
}
386-
387376
/**
388377
* Verify checksums
389378
*
390379
* @param items The items to verify
391380
* @param checksums The checksums to verify against
392381
*/
393382
public static boolean verifyChecksum(final @NotNull ItemStack @NotNull [] items, final @NotNull String @NotNull [] checksums) {
394-
if (items.length != checksums.length)
395-
throw new IllegalArgumentException("The number of items and checksums must be the same.");
396-
397-
return IntStream.range(0, items.length).allMatch(i -> verifyChecksum(items[i], checksums[i]));
383+
if (items.length != checksums.length) return false;
384+
final @NotNull String @NotNull [] itemChecksums = checksum(items);
385+
return Arrays.equals(
386+
Arrays.stream(itemChecksums).sorted().toArray(String[]::new),
387+
Arrays.stream(checksums).sorted().toArray(String[]::new)
388+
);
398389
}
399390
}

src/main/java/pro/cloudnode/smp/bankaccounts/commands/POSCommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pro.cloudnode.smp.bankaccounts.commands;
22

33
import org.bukkit.block.Block;
4+
import org.bukkit.block.BlockState;
45
import org.bukkit.block.Chest;
56
import org.bukkit.command.CommandSender;
67
import org.bukkit.entity.Player;
@@ -65,7 +66,9 @@ public boolean execute(final @NotNull CommandSender sender, final @NotNull Strin
6566
final @Nullable Block target = player.getTargetBlockExact(5);
6667
if (target == null) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsBlockTooFar());
6768

68-
if (!(target.getState() instanceof final @NotNull Chest chest))
69+
final @NotNull Optional<@NotNull BlockState> block = BankAccounts.runOnMain(target::getState, 5);
70+
if (block.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAsyncFailed());
71+
if (!(block.get() instanceof final @NotNull Chest chest))
6972
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosNotChest());
7073
if (chest.getInventory() instanceof DoubleChestInventory)
7174
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosDoubleChest());

src/main/java/pro/cloudnode/smp/bankaccounts/events/Join.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void onPlayerJoin(final @NotNull PlayerJoinEvent event) {
2626
}
2727
}));
2828
if (player.hasPermission(Permissions.NOTIFY_UPDATE)) {
29-
BankAccounts.getInstance().getServer().getScheduler().runTaskLater(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion -> {
29+
BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion -> {
3030
player.sendMessage(BankAccounts.getInstance().config().messagesUpdateAvailable(latestVersion));
3131
}), 20L);
3232
}

src/main/resources/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ messages:
361361
invoice-cannot-send: "<red>(!) You cannot send this invoice to that player because they don't have permission to view it.</red>"
362362
# Player has never played on this server
363363
player-never-joined: "<red>(!) This player has never joined this server.</red>"
364+
# Asynchronous code failed. Detailed info is outputted in the console
365+
async-failed: "<red>(!) The request failed. See the console for details.</red>"
364366
# Account balance minimum to change owner not satisfied (placeholders same as balance)
365367
# Additional placeholders:
366368
# <required-balance> - required min balance, example: 123456.78

0 commit comments

Comments
 (0)