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)) {
return;
}
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.BACK_COMMAND);
// Teleport the player
plugin.getTeleportManager().timedTeleport(onlineUser, position)
.thenAccept(result -> plugin.getTeleportManager().finishTeleport(onlineUser, result));
plugin.getTeleportManager().timedTeleport(onlineUser, position, Settings.EconomyAction.RANDOM_TELEPORT)
.thenAccept(result -> plugin.getTeleportManager()
.finishTeleport(onlineUser, result, Settings.EconomyAction.RANDOM_TELEPORT));
},
() -> 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.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.UserData;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.util.Permission;
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 {
protected RtpCommand(@NotNull HuskHomes implementor) {
@@ -28,23 +34,37 @@ public class RtpCommand extends CommandBase implements ConsoleExecutable {
return;
}
// Perform economy check if necessary
if (!plugin.validateEconomyCheck(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT)) {
return;
}
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())
.thenAccept(result -> {
plugin.getTeleportManager().finishTeleport(onlineUser, result);
plugin.performEconomyTransaction(onlineUser, Settings.EconomyAction.RANDOM_TELEPORT);
});
});
CompletableFuture.runAsync(() -> {
// Check the user is not still on /rtp cooldown
final Optional<UserData> userData = plugin.getDatabase().getUserData(onlineUser.uuid).join();
if (userData.isPresent()) {
final Instant currentTime = Instant.now();
if (!currentTime.isAfter(userData.get().rtpCooldown())) {
plugin.getLocales().getLocale("error_rtp_cooldown",
Long.toString(currentTime.until(userData.get().rtpCooldown(), ChronoUnit.MINUTES)))
.ifPresent(onlineUser::sendMessage);
return;
}
}
// 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
@@ -1,6 +1,7 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.messenger.Message;
import net.william278.huskhomes.messenger.MessagePayload;
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
*
* @param onlineUser the {@link OnlineUser} to teleport
* @param position the target {@link Position} to teleport to
* @param onlineUser the {@link OnlineUser} to teleport
* @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
if (currentlyOnWarmup.contains(onlineUser.uuid)) {
return CompletableFuture.supplyAsync(() -> TeleportResult.FAILED_ALREADY_TELEPORTING);
@@ -239,6 +243,11 @@ public class TeleportManager {
return CompletableFuture.supplyAsync(() -> processTeleportWarmup(new TimedTeleport(onlineUser, position, teleportWarmupTime))
.thenApply(teleport -> {
if (!teleport.cancelled) {
for (final Settings.EconomyAction action : economyActions) {
if (!plugin.validateEconomyCheck(onlineUser, action)) {
return TeleportResult.CANCELLED;
}
}
return teleport(onlineUser, position).join();
} else {
return TeleportResult.CANCELLED;
@@ -254,8 +263,11 @@ public class TeleportManager {
*
* @param onlineUser the {@link OnlineUser} to send the teleport completion message to
* @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) {
case COMPLETED_LOCALLY -> plugin.getLocales().getLocale("teleporting_complete")
.ifPresent(onlineUser::sendMessage);
@@ -271,6 +283,13 @@ public class TeleportManager {
case FAILED_INVALID_SERVER -> plugin.getLocales().getLocale("error_invalid_server")
.ifPresent(onlineUser::sendMessage);
}
// Handle economy actions
if (teleportResult.successful) {
for (final Settings.EconomyAction action : economyActions) {
plugin.performEconomyTransaction(onlineUser, action);
}
}
}
/**