Skip to content

Commit

Permalink
Merge branch 'master' into 65-add-the-ability-to-transfer-ownership-o…
Browse files Browse the repository at this point in the history
…f-a-bank-account
  • Loading branch information
zefir-git committed Feb 28, 2024
2 parents 52a6a7e + 93ea9fd commit 968e5eb
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

name: Java CI with Maven

permissions:
contents: write

on:
push:
branches: [ "master" ]
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.3.2</version>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>me.clip</groupId>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

public final class BankAccounts extends JavaPlugin {
Expand Down Expand Up @@ -395,6 +398,32 @@ public static Optional<String> checkForUpdates() {
return Optional.empty();
}

/**
* Run a task on the main thread
* @param task The task to run
* @param timeout Task timeout in SECONDS. Set to 0 to disable timeout
*/
public static <T> @NotNull Optional<T> runOnMain(@NotNull Callable<T> task, final long timeout) {
final @NotNull BankAccounts plugin = BankAccounts.getInstance();
final @NotNull Future<T> future = plugin.getServer().getScheduler().callSyncMethod(plugin, task);
try {
if (timeout == 0) return Optional.of(future.get());
return Optional.of(future.get(timeout, TimeUnit.SECONDS));
}
catch (final @NotNull Exception e) {
plugin.getLogger().log(Level.WARNING, "Failed to run task on main thread", e);
}
return Optional.empty();
}

/**
* Run a task on the main thread (without timeout)
* @param task The task to run
*/
public static <T> @NotNull Optional<T> runOnMain(@NotNull Callable<T> task) {
return runOnMain(task, 0);
}

public static final class Key {
public final static @NotNull NamespacedKey INSTRUMENT_ACCOUNT = namespacedKey("instrument-account");
public final static @NotNull NamespacedKey POS_OWNER_GUI = namespacedKey("pos-owner-gui");
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/pro/cloudnode/smp/bankaccounts/BankConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ public int invoicePerPage() {
return Objects.requireNonNull(config.getString("messages.errors.change-owner-accept-failed"));
}

// messages.errors.async-failed
public @NotNull Component messagesErrorsAsyncFailed() {
return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(config.getString("messages.errors.async-failed")));
}

// messages.balance
public @NotNull Component messagesBalance(final @NotNull Account account) {
return MiniMessage.miniMessage().deserialize(
Expand Down Expand Up @@ -894,12 +899,10 @@ public int invoicePerPage() {
.replace("<to-balance-short>", BankAccounts.formatCurrencyShort(to.balance))
.replace("<amount>", amount.toPlainString())
.replace("<amount-formatted>", BankAccounts.formatCurrency(amount))
.replace("<amount-short>", BankAccounts.formatCurrencyShort(amount)),
.replace("<amount-short>", BankAccounts.formatCurrencyShort(amount))
.replace("<confirm-command>", "/bank transfer --confirm " + from.id + " " + to.id + " " + amount.toPlainString() + (description == null ? "" : " " + description)),
Placeholder.component("description", description == null ? MiniMessage.miniMessage().deserialize("<gray><i>no description</i>") : Component.text(description))
).replaceText(configurer -> {
configurer.matchLiteral("<confirm-command>");
configurer.replacement(Component.text("/bank transfer --confirm " + from.id + " " + to.id + " " + amount.toPlainString() + (description == null ? "" : " " + description)));
});
);
}

// messages.transfer-sent
Expand Down Expand Up @@ -1143,13 +1146,9 @@ public int invoicePerPage() {
Objects.requireNonNull(config.getString("messages.baltop.header"))
.replace("<category>", category)
.replace("<page>", String.valueOf(page))
).replaceText(configurer -> {
configurer.matchLiteral("<cmd-prev>");
configurer.replacement(cmdPrev);
}).replaceText(configurer -> {
configurer.matchLiteral("<cmd-next>");
configurer.replacement(cmdNext);
});
.replace("<cmd-prev>", cmdPrev)
.replace("<cmd-next>", cmdNext)
);
}

// messages.baltop.entry
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/pro/cloudnode/smp/bankaccounts/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public static boolean sendMessage(final @Nullable Audience audience, final @NotN
/**
* Send command usage to sender.
*
* @param sender Command sender.
* @param audience Message recipient
* @param label Command label.
* @param arguments Command arguments.
* @return Always true.
*/
protected static boolean sendUsage(final @NotNull CommandSender sender, final @NotNull String label, final @NotNull String arguments) {
return sendMessage(sender, BankAccounts.getInstance().config().messagesCommandUsage(label, arguments));
protected static boolean sendUsage(final @NotNull Audience audience, final @NotNull String label, final @NotNull String arguments) {
return sendMessage(audience, BankAccounts.getInstance().config().messagesCommandUsage(label, arguments));
}

/**
Expand Down
21 changes: 6 additions & 15 deletions src/main/java/pro/cloudnode/smp/bankaccounts/POS.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;
import java.util.stream.IntStream;
import java.util.zip.CRC32;

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

/**
* Verify checksum
*
* @param item The item to verify
* @param checksum The checksum to verify against
*/
public static boolean verifyChecksum(final @NotNull ItemStack item, final @NotNull String checksum) {
return checksum(item).equals(checksum);
}

/**
* Verify checksums
*
* @param items The items to verify
* @param checksums The checksums to verify against
*/
public static boolean verifyChecksum(final @NotNull ItemStack @NotNull [] items, final @NotNull String @NotNull [] checksums) {
if (items.length != checksums.length)
throw new IllegalArgumentException("The number of items and checksums must be the same.");

return IntStream.range(0, items.length).allMatch(i -> verifyChecksum(items[i], checksums[i]));
if (items.length != checksums.length) return false;
final @NotNull String @NotNull [] itemChecksums = checksum(items);
return Arrays.equals(
Arrays.stream(itemChecksums).sorted().toArray(String[]::new),
Arrays.stream(checksums).sorted().toArray(String[]::new)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pro.cloudnode.smp.bankaccounts.commands;

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

if (!(target.getState() instanceof final @NotNull Chest chest))
final @NotNull Optional<@NotNull BlockState> block = BankAccounts.runOnMain(target::getState, 5);
if (block.isEmpty()) return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsAsyncFailed());
if (!(block.get() instanceof final @NotNull Chest chest))
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosNotChest());
if (chest.getInventory() instanceof DoubleChestInventory)
return sendMessage(sender, BankAccounts.getInstance().config().messagesErrorsPosDoubleChest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void onPlayerJoin(final @NotNull PlayerJoinEvent event) {
}
}));
if (player.hasPermission(Permissions.NOTIFY_UPDATE)) {
BankAccounts.getInstance().getServer().getScheduler().runTaskLater(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion -> {
BankAccounts.getInstance().getServer().getScheduler().runTaskLaterAsynchronously(BankAccounts.getInstance(), () -> BankAccounts.checkForUpdates().ifPresent(latestVersion -> {
player.sendMessage(BankAccounts.getInstance().config().messagesUpdateAvailable(latestVersion));
}), 20L);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ messages:
invoice-cannot-send: "<red>(!) You cannot send this invoice to that player because they don't have permission to view it.</red>"
# Player has never played on this server
player-never-joined: "<red>(!) This player has never joined this server.</red>"
# Asynchronous code failed. Detailed info is outputted in the console
async-failed: "<red>(!) The request failed. See the console for details.</red>"
# Account balance minimum to change owner not satisfied (placeholders same as balance)
# Additional placeholders:
# <required-balance> - required min balance, example: 123456.78
Expand Down

0 comments on commit 968e5eb

Please sign in to comment.