Skip to content

Commit 460a22d

Browse files
authored
Fix POS creating with async command (#100)
2 parents c94e7e0 + 16c7bb1 commit 460a22d

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
import java.util.Objects;
4242
import java.util.Optional;
4343
import java.util.UUID;
44+
import java.util.concurrent.Callable;
45+
import java.util.concurrent.Future;
46+
import java.util.concurrent.TimeUnit;
4447
import java.util.logging.Level;
4548

4649
public final class BankAccounts extends JavaPlugin {
@@ -383,6 +386,32 @@ public static Optional<String> checkForUpdates() {
383386
return Optional.empty();
384387
}
385388

389+
/**
390+
* Run a task on the main thread
391+
* @param task The task to run
392+
* @param timeout Task timeout in SECONDS. Set to 0 to disable timeout
393+
*/
394+
public static <T> @NotNull Optional<T> runOnMain(@NotNull Callable<T> task, final long timeout) {
395+
final @NotNull BankAccounts plugin = BankAccounts.getInstance();
396+
final @NotNull Future<T> future = plugin.getServer().getScheduler().callSyncMethod(plugin, task);
397+
try {
398+
if (timeout == 0) return Optional.of(future.get());
399+
return Optional.of(future.get(timeout, TimeUnit.SECONDS));
400+
}
401+
catch (final @NotNull Exception e) {
402+
plugin.getLogger().log(Level.WARNING, "Failed to run task on main thread", e);
403+
}
404+
return Optional.empty();
405+
}
406+
407+
/**
408+
* Run a task on the main thread (without timeout)
409+
* @param task The task to run
410+
*/
411+
public static <T> @NotNull Optional<T> runOnMain(@NotNull Callable<T> task) {
412+
return runOnMain(task, 0);
413+
}
414+
386415
public static final class Key {
387416
public final static @NotNull NamespacedKey INSTRUMENT_ACCOUNT = namespacedKey("instrument-account");
388417
public final static @NotNull NamespacedKey POS_OWNER_GUI = namespacedKey("pos-owner-gui");

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ public int invoicePerPage() {
694694
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.player-never-joined")));
695695
}
696696

697+
// messages.errors.async-failed
698+
public @NotNull Component messagesErrorsAsyncFailed() {
699+
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.async-failed")));
700+
}
701+
697702
// messages.balance
698703
public @NotNull Component messagesBalance(final @NotNull Account account) {
699704
return MiniMessage.miniMessage().deserialize(

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

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

3+
import net.kyori.adventure.audience.Audience;
34
import net.kyori.adventure.text.Component;
45
import net.kyori.adventure.text.minimessage.MiniMessage;
56
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
@@ -16,38 +17,38 @@ public abstract class Command implements CommandExecutor, TabCompleter {
1617
/**
1718
* Send message to sender.
1819
*
19-
* @param sender Command sender.
20-
* @param message Message to send.
20+
* @param audience Message recipient
21+
* @param message Message to send.
2122
* @return Always true.
2223
*/
23-
public static boolean sendMessage(final @NotNull CommandSender sender, final @NotNull Component message) {
24-
sender.sendMessage(message);
24+
public static boolean sendMessage(final @NotNull Audience audience, final @NotNull Component message) {
25+
audience.sendMessage(message);
2526
return true;
2627
}
2728

2829
/**
2930
* Send message to sender.
3031
*
31-
* @param sender Command sender.
32+
* @param audience Message recipient
3233
* @param message Message to send.
3334
* @param placeholders Placeholders to replace.
3435
* @return Always true.
3536
*/
36-
public static boolean sendMessage(final @NotNull CommandSender sender, final @NotNull String message, final @NotNull TagResolver @NotNull ... placeholders) {
37-
sendMessage(sender, MiniMessage.miniMessage().deserialize(message, placeholders));
37+
public static boolean sendMessage(final @NotNull Audience audience, final @NotNull String message, final @NotNull TagResolver @NotNull ... placeholders) {
38+
sendMessage(audience, MiniMessage.miniMessage().deserialize(message, placeholders));
3839
return true;
3940
}
4041

4142
/**
4243
* Send command usage to sender.
4344
*
44-
* @param sender Command sender.
45+
* @param audience Message recipient
4546
* @param label Command label.
4647
* @param arguments Command arguments.
4748
* @return Always true.
4849
*/
49-
protected static boolean sendUsage(final @NotNull CommandSender sender, final @NotNull String label, final @NotNull String arguments) {
50-
return sendMessage(sender, BankAccounts.getInstance().config().messagesCommandUsage(label, arguments));
50+
protected static boolean sendUsage(final @NotNull Audience audience, final @NotNull String label, final @NotNull String arguments) {
51+
return sendMessage(audience, BankAccounts.getInstance().config().messagesCommandUsage(label, arguments));
5152
}
5253

5354
/**

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
@@ -350,6 +350,8 @@ messages:
350350
invoice-cannot-send: "<red>(!) You cannot send this invoice to that player because they don't have permission to view it.</red>"
351351
# Player has never played on this server
352352
player-never-joined: "<red>(!) This player has never joined this server.</red>"
353+
# Asynchronous code failed. Detailed info is outputted in the console
354+
async-failed: "<red>(!) The request failed. See the console for details.</red>"
353355

354356
# Account balance
355357
# Available placeholders:

0 commit comments

Comments
 (0)