Fix economy actions being skippable in some circumstances

This commit is contained in:
William committed 2022-08-20 14:30:49 +01:00
1 parent 08e6569027
commit cda665cc5a
3 files changed
+59 -20

No files matched your search

@@ -20,11 +20,11 @@ public class BackCommand extends CommandBase {
if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.BACK_COMMAND)) { if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.BACK_COMMAND)) {
return; return;
} }
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.BACK_COMMAND);
// Teleport the player // Teleport the player
plugin.getTeleportManager().timedTeleport(onlineUser, position) plugin.getTeleportManager().timedTeleport(onlineUser, position, Settings.EconomyAction.RANDOM_TELEPORT)
.thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result)); .thenAccept(result -> plugin.getTeleportManager()
.finishTeleport(onlineUser, result, Settings.EconomyAction.RANDOM_TELEPORT));
}, },
() -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage))); () -> plugin.getLocales().getLocale("error_no_last_position").ifPresent(onlineUser::sendMessage)));
} }
@@ -3,10 +3,16 @@ package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.UserData;
import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.util.Permission; import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class RtpCommand extends CommandBase implements ConsoleExecutable { public class RtpCommand extends CommandBase implements ConsoleExecutable {
protected RtpCommand(@NotNull HuskHomes implementor) { protected RtpCommand(@NotNull HuskHomes implementor) {
@@ -28,23 +34,37 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
return; return;
} }
// Perform economy check if necessary
if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT)) { if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT)) {
return; return;
} }
plugin.getRtpEngine().getRandomPosition(userPosition) CompletableFuture.runAsync(() -> {
.thenAccept(position -> { // Check the user is not still on /rtp cooldown
if (position.isEmpty()) { final Optional<UserData> userData = plugin.getDatabase().getUserData(onlineUser.uuid).join();
plugin.getLocales().getLocale("error_rtp_randomization_timeout") if (userData.isPresent()) {
.ifPresent(onlineUser::sendMessage); final Instant currentTime = Instant.now();
return; if (!currentTime.isAfter(userData.get().rtpCooldown())) {
} plugin.getLocales().getLocale("error_rtp_cooldown",
plugin.getTeleportManager().timedTeleport(onlineUser, position.get()) Long.toString(currentTime.until(userData.get().rtpCooldown(), ChronoUnit.MINUTES)))
.thenAccept(result -> { .ifPresent(onlineUser::sendMessage);
plugin.getTeleportManager().finishTeleport(onlineUser, result); return;
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT); }
}); }
});
// Get a random position and teleport
plugin.getRtpEngine().getRandomPosition(userPosition)
.thenAccept(position -> {
if (position.isEmpty()) {
plugin.getLocales().getLocale("error_rtp_randomization_timeout")
.ifPresent(onlineUser::sendMessage);
return;
}
plugin.getTeleportManager().timedTeleport(onlineUser, position.get(), Settings.EconomyAction.RANDOM_TELEPORT)
.thenAccept(result -> plugin.getTeleportManager()
.finishTeleport(onlineUser, result, Settings.EconomyAction.RANDOM_TELEPORT)).join();
}).join();
});
} }
@Override @Override
@@ -1,6 +1,7 @@
package net.william278.huskhomes.teleport; package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.messenger.Message; import net.william278.huskhomes.messenger.Message;
import net.william278.huskhomes.messenger.MessagePayload; import net.william278.huskhomes.messenger.MessagePayload;
import net.william278.huskhomes.player.OnlineUser; import net.william278.huskhomes.player.OnlineUser;
@@ -225,10 +226,13 @@ public class TeleportManager {
/** /**
* Teleport a {@link OnlineUser} to a specified {@link Position} after a warmup period * Teleport a {@link OnlineUser} to a specified {@link Position} after a warmup period
* *
* @param onlineUser the {@link OnlineUser} to teleport * @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to * @param position the target {@link Position} to teleport to
* @param economyActions any economy actions to validate before the teleport.
* Note that this will not actually carry out the economy transactions, only validate them
*/ */
public CompletableFuture<TeleportResult> timedTeleport(@NotNull OnlineUser onlineUser, @NotNull Position position) { public CompletableFuture<TeleportResult> timedTeleport(@NotNull OnlineUser onlineUser, @NotNull Position position,
@NotNull Settings.EconomyAction... economyActions) {
// Prevent players starting multiple timed teleports // Prevent players starting multiple timed teleports
if (currentlyOnWarmup.contains(onlineUser.uuid)) { if (currentlyOnWarmup.contains(onlineUser.uuid)) {
return CompletableFuture.supplyAsync(() -> TeleportResult.FAILED_ALREADY_TELEPORTING); return CompletableFuture.supplyAsync(() -> TeleportResult.FAILED_ALREADY_TELEPORTING);
@@ -239,6 +243,11 @@ public class TeleportManager {
return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime)) return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime))
.thenApply(teleport -> { .thenApply(teleport -> {
if (!teleport.cancelled) { if (!teleport.cancelled) {
for (final Settings.EconomyAction action : economyActions) {
if (!plugin.validateEconomyCheck(onlineUser, action)) {
return TeleportResult.CANCELLED;
}
}
return teleport(onlineUser, position).join(); return teleport(onlineUser, position).join();
} else { } else {
return TeleportResult.CANCELLED; return TeleportResult.CANCELLED;
@@ -254,8 +263,11 @@ public class TeleportManager {
* *
* @param onlineUser the {@link OnlineUser} to send the teleport completion message to * @param onlineUser the {@link OnlineUser} to send the teleport completion message to
* @param teleportResult the {@link TeleportResult} to handle * @param teleportResult the {@link TeleportResult} to handle
* @param economyActions any economy actions to complete the transaction of
*/ */
public void finishTeleport(@NotNull OnlineUser onlineUser, @NotNull TeleportResult teleportResult) { public void finishTeleport(@NotNull OnlineUser onlineUser, @NotNull TeleportResult teleportResult,
@NotNull Settings.EconomyAction... economyActions) {
// Display the teleport result message
switch (teleportResult) { switch (teleportResult) {
case COMPLETED_LOCALLY -> plugin.getLocales().getLocale("teleporting_complete") case COMPLETED_LOCALLY -> plugin.getLocales().getLocale("teleporting_complete")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
@@ -271,6 +283,13 @@ public class TeleportManager {
case FAILED_INVALID_SERVER -> plugin.getLocales().getLocale("error_invalid_server") case FAILED_INVALID_SERVER -> plugin.getLocales().getLocale("error_invalid_server")
.ifPresent(onlineUser::sendMessage); .ifPresent(onlineUser::sendMessage);
} }
// Handle economy actions
if (teleportResult.successful) {
for (final Settings.EconomyAction action : economyActions) {
plugin.performEconomyTransaction(onlineUser, action);
}
}
} }
/** /**