mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-15 07:05:28 +01:00
refactor: Rename CheckResult to Result
This commit is contained in:
parent
ad47ab2340
commit
8df371c90c
@ -24,7 +24,7 @@ import com.onarandombox.MultiverseCore.inject.InjectableListener;
|
||||
import com.onarandombox.MultiverseCore.teleportation.TeleportQueue;
|
||||
import com.onarandombox.MultiverseCore.utils.MVPermissions;
|
||||
import com.onarandombox.MultiverseCore.utils.PermissionTools;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.CheckResultChain;
|
||||
import com.onarandombox.MultiverseCore.utils.result.ResultChain;
|
||||
import com.onarandombox.MultiverseCore.world.entrycheck.EntryFeeResult;
|
||||
import com.onarandombox.MultiverseCore.world.entrycheck.WorldEntryCheckerProvider;
|
||||
import jakarta.inject.Inject;
|
||||
@ -235,7 +235,7 @@ public class MVPlayerListener implements InjectableListener {
|
||||
return;
|
||||
}
|
||||
|
||||
CheckResultChain entryResult = worldEntryCheckerProvider.forSender(teleporter).canEnterWorld(fromWorld, toWorld)
|
||||
ResultChain entryResult = worldEntryCheckerProvider.forSender(teleporter).canEnterWorld(fromWorld, toWorld)
|
||||
.onSuccessReason(EntryFeeResult.Success.class, reason -> {
|
||||
if (reason == EntryFeeResult.Success.ENOUGH_MONEY) {
|
||||
economist.payEntryFee((Player) teleporter, toWorld);
|
||||
@ -306,7 +306,7 @@ public class MVPlayerListener implements InjectableListener {
|
||||
return;
|
||||
}
|
||||
|
||||
CheckResultChain entryResult = worldEntryCheckerProvider.forSender(event.getPlayer()).canEnterWorld(fromWorld, toWorld)
|
||||
ResultChain entryResult = worldEntryCheckerProvider.forSender(event.getPlayer()).canEnterWorld(fromWorld, toWorld)
|
||||
.onFailure(results -> {
|
||||
event.setCancelled(true);
|
||||
getCommandManager().getCommandIssuer(event.getPlayer()).sendError(results.getLastResultMessage());
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.onarandombox.MultiverseCore.utils.checkresult;
|
||||
package com.onarandombox.MultiverseCore.utils.result;
|
||||
|
||||
import co.aikar.locales.MessageKey;
|
||||
import co.aikar.locales.MessageKeyProvider;
|
@ -1,21 +1,18 @@
|
||||
package com.onarandombox.MultiverseCore.utils.checkresult;
|
||||
package com.onarandombox.MultiverseCore.utils.result;
|
||||
|
||||
import co.aikar.commands.CommandIssuer;
|
||||
import com.onarandombox.MultiverseCore.commandtools.PluginLocales;
|
||||
import com.onarandombox.MultiverseCore.utils.message.Message;
|
||||
import com.onarandombox.MultiverseCore.utils.message.MessageReplacement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public sealed interface CheckResult<S extends SuccessReason, F extends FailureReason> permits CheckResult.Success, CheckResult.Failure {
|
||||
static <F extends FailureReason, S extends SuccessReason> CheckResult<S, F> success(S successReason, MessageReplacement...replacements) {
|
||||
public sealed interface Result<S extends SuccessReason, F extends FailureReason> permits Result.Success, Result.Failure {
|
||||
static <F extends FailureReason, S extends SuccessReason> Result<S, F> success(S successReason, MessageReplacement...replacements) {
|
||||
return new Success<>(successReason, replacements);
|
||||
}
|
||||
|
||||
static <F extends FailureReason, S extends SuccessReason> CheckResult<S, F> failure(F failureReason, MessageReplacement...replacements) {
|
||||
static <F extends FailureReason, S extends SuccessReason> Result<S, F> failure(F failureReason, MessageReplacement...replacements) {
|
||||
return new Failure<>(failureReason, replacements);
|
||||
}
|
||||
|
||||
@ -29,35 +26,35 @@ public sealed interface CheckResult<S extends SuccessReason, F extends FailureRe
|
||||
|
||||
@NotNull Message getReasonMessage();
|
||||
|
||||
default CheckResult<S, F> onSuccess(Consumer<S> consumer) {
|
||||
default Result<S, F> onSuccess(Consumer<S> consumer) {
|
||||
if (this.isSuccess()) {
|
||||
consumer.accept(this.getSuccessReason());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default CheckResult<S, F> onFailure(Consumer<F> consumer) {
|
||||
default Result<S, F> onFailure(Consumer<F> consumer) {
|
||||
if (this.isFailure()) {
|
||||
consumer.accept(this.getFailureReason());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default CheckResult<S, F> onSuccessReason(S successReason, Consumer<S> consumer) {
|
||||
default Result<S, F> onSuccessReason(S successReason, Consumer<S> consumer) {
|
||||
if (this.isSuccess() && this.getSuccessReason() == successReason) {
|
||||
consumer.accept(this.getSuccessReason());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default CheckResult<S, F> onFailureReason(F failureReason, Consumer<F> consumer) {
|
||||
default Result<S, F> onFailureReason(F failureReason, Consumer<F> consumer) {
|
||||
if (this.isFailure() && this.getFailureReason() == failureReason) {
|
||||
consumer.accept(this.getFailureReason());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
final class Success<F extends FailureReason, S extends SuccessReason> implements CheckResult<S, F> {
|
||||
final class Success<F extends FailureReason, S extends SuccessReason> implements Result<S, F> {
|
||||
private final S successReason;
|
||||
private final MessageReplacement[] replacements;
|
||||
|
||||
@ -99,7 +96,7 @@ public sealed interface CheckResult<S extends SuccessReason, F extends FailureRe
|
||||
}
|
||||
}
|
||||
|
||||
final class Failure<S extends SuccessReason, F extends FailureReason> implements CheckResult<S, F> {
|
||||
final class Failure<S extends SuccessReason, F extends FailureReason> implements Result<S, F> {
|
||||
private final F failureReason;
|
||||
private final MessageReplacement[] replacements;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.onarandombox.MultiverseCore.utils.checkresult;
|
||||
package com.onarandombox.MultiverseCore.utils.result;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.onarandombox.MultiverseCore.utils.message.Message;
|
||||
@ -11,7 +11,7 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CheckResultChain {
|
||||
public class ResultChain {
|
||||
public static Builder builder() {
|
||||
return new Builder(true);
|
||||
}
|
||||
@ -21,9 +21,9 @@ public class CheckResultChain {
|
||||
}
|
||||
|
||||
private final boolean isSuccess;
|
||||
private final List<CheckResult<?, ?>> results;
|
||||
private final List<Result<?, ?>> results;
|
||||
|
||||
CheckResultChain(boolean isSuccess, List<CheckResult<?, ?>> results) {
|
||||
ResultChain(boolean isSuccess, List<Result<?, ?>> results) {
|
||||
this.isSuccess = isSuccess;
|
||||
this.results = results;
|
||||
}
|
||||
@ -36,45 +36,45 @@ public class CheckResultChain {
|
||||
return !isSuccess;
|
||||
}
|
||||
|
||||
public CheckResultChain onSuccess(Runnable successRunnable) {
|
||||
public ResultChain onSuccess(Runnable successRunnable) {
|
||||
if (isSuccess) {
|
||||
successRunnable.run();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CheckResultChain onFailure(Runnable failureRunnable) {
|
||||
public ResultChain onFailure(Runnable failureRunnable) {
|
||||
if (isFailure()) {
|
||||
failureRunnable.run();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CheckResultChain onSuccess(Consumer<CheckResultChain> successRunnable) {
|
||||
public ResultChain onSuccess(Consumer<ResultChain> successRunnable) {
|
||||
if (isSuccess) {
|
||||
successRunnable.accept(this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public CheckResultChain onFailure(Consumer<CheckResultChain> failureRunnable) {
|
||||
public ResultChain onFailure(Consumer<ResultChain> failureRunnable) {
|
||||
if (isFailure()) {
|
||||
failureRunnable.accept(this);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public <S extends SuccessReason> CheckResultChain onSuccessReason(Class<S> successReasonClass, Consumer<S> successConsumer) {
|
||||
public <S extends SuccessReason> ResultChain onSuccessReason(Class<S> successReasonClass, Consumer<S> successConsumer) {
|
||||
getSuccessReason(successReasonClass).peek(successConsumer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public <F extends FailureReason> CheckResultChain onFailureReason(Class<F> failureReasonClass, Consumer<F> failureConsumer) {
|
||||
public <F extends FailureReason> ResultChain onFailureReason(Class<F> failureReasonClass, Consumer<F> failureConsumer) {
|
||||
getFailureReason(failureReasonClass).peek(failureConsumer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public <S extends SuccessReason> CheckResultChain onSuccessReason(S successReason, Runnable successRunnable) {
|
||||
public <S extends SuccessReason> ResultChain onSuccessReason(S successReason, Runnable successRunnable) {
|
||||
getSuccessReason(successReason.getClass()).filter(successReason::equals).peek(reason -> successRunnable.run());
|
||||
return this;
|
||||
}
|
||||
@ -84,7 +84,7 @@ public class CheckResultChain {
|
||||
return Option.none();
|
||||
}
|
||||
return Option.ofOptional(results.stream()
|
||||
.map(CheckResult::getSuccessReason)
|
||||
.map(Result::getSuccessReason)
|
||||
.filter(successReasonClass::isInstance)
|
||||
.map(successReasonClass::cast)
|
||||
.findFirst());
|
||||
@ -95,7 +95,7 @@ public class CheckResultChain {
|
||||
return Option.none();
|
||||
}
|
||||
return Option.ofOptional(results.stream()
|
||||
.map(CheckResult::getFailureReason)
|
||||
.map(Result::getFailureReason)
|
||||
.filter(failureReasonClass::isInstance)
|
||||
.map(failureReasonClass::cast)
|
||||
.findFirst());
|
||||
@ -115,7 +115,7 @@ public class CheckResultChain {
|
||||
|
||||
public static class Builder {
|
||||
private final boolean stopOnFailure;
|
||||
private final List<CheckResult<?, ?>> results;
|
||||
private final List<Result<?, ?>> results;
|
||||
|
||||
private boolean isSuccess = true;
|
||||
|
||||
@ -124,11 +124,11 @@ public class CheckResultChain {
|
||||
this.results = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Builder then(Supplier<CheckResult<?, ?>> resultSupplier) {
|
||||
public Builder then(Supplier<Result<?, ?>> resultSupplier) {
|
||||
if (!isSuccess && stopOnFailure) {
|
||||
return this;
|
||||
}
|
||||
CheckResult<?, ?> result = resultSupplier.get();
|
||||
Result<?, ?> result = resultSupplier.get();
|
||||
if (result.isFailure()) {
|
||||
isSuccess = false;
|
||||
}
|
||||
@ -136,8 +136,8 @@ public class CheckResultChain {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CheckResultChain build() {
|
||||
return new CheckResultChain(isSuccess, results);
|
||||
public ResultChain build() {
|
||||
return new ResultChain(isSuccess, results);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.onarandombox.MultiverseCore.utils.checkresult;
|
||||
package com.onarandombox.MultiverseCore.utils.result;
|
||||
|
||||
import co.aikar.locales.MessageKey;
|
||||
import co.aikar.locales.MessageKeyProvider;
|
@ -3,8 +3,8 @@ package com.onarandombox.MultiverseCore.world.entrycheck;
|
||||
import co.aikar.locales.MessageKey;
|
||||
import co.aikar.locales.MessageKeyProvider;
|
||||
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.SuccessReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.SuccessReason;
|
||||
|
||||
public class BlacklistResult {
|
||||
public enum Success implements SuccessReason {
|
||||
|
@ -4,8 +4,8 @@ package com.onarandombox.MultiverseCore.world.entrycheck;
|
||||
import co.aikar.locales.MessageKey;
|
||||
import co.aikar.locales.MessageKeyProvider;
|
||||
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.SuccessReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.SuccessReason;
|
||||
|
||||
public class EntryFeeResult {
|
||||
public enum Success implements SuccessReason {
|
||||
|
@ -3,8 +3,8 @@ package com.onarandombox.MultiverseCore.world.entrycheck;
|
||||
import co.aikar.locales.MessageKey;
|
||||
import co.aikar.locales.MessageKeyProvider;
|
||||
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.SuccessReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.SuccessReason;
|
||||
|
||||
public class PlayerLimitResult {
|
||||
public enum Success implements SuccessReason {
|
||||
|
@ -3,8 +3,8 @@ package com.onarandombox.MultiverseCore.world.entrycheck;
|
||||
import co.aikar.locales.MessageKey;
|
||||
import co.aikar.locales.MessageKeyProvider;
|
||||
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.SuccessReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.FailureReason;
|
||||
import com.onarandombox.MultiverseCore.utils.result.SuccessReason;
|
||||
|
||||
public class WorldAccessResult {
|
||||
public enum Success implements SuccessReason {
|
||||
|
@ -4,9 +4,8 @@ import com.onarandombox.MultiverseCore.api.MVWorld;
|
||||
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
|
||||
import com.onarandombox.MultiverseCore.economy.MVEconomist;
|
||||
import com.onarandombox.MultiverseCore.permissions.CorePermissionsChecker;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.CheckResult;
|
||||
import com.onarandombox.MultiverseCore.utils.checkresult.CheckResultChain;
|
||||
import com.onarandombox.MultiverseCore.utils.message.MessageReplacement;
|
||||
import com.onarandombox.MultiverseCore.utils.result.Result;
|
||||
import com.onarandombox.MultiverseCore.utils.result.ResultChain;
|
||||
import com.onarandombox.MultiverseCore.world.configuration.EntryFee;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.BlockCommandSender;
|
||||
@ -37,12 +36,12 @@ public class WorldEntryChecker {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public CheckResultChain canStayInWorld(@NotNull MVWorld world) {
|
||||
public ResultChain canStayInWorld(@NotNull MVWorld world) {
|
||||
return canEnterWorld(null, world);
|
||||
}
|
||||
|
||||
public CheckResultChain canEnterWorld(@Nullable MVWorld fromWorld, @NotNull MVWorld toWorld) {
|
||||
return CheckResultChain.builder()
|
||||
public ResultChain canEnterWorld(@Nullable MVWorld fromWorld, @NotNull MVWorld toWorld) {
|
||||
return ResultChain.builder()
|
||||
.then(() -> canAccessWorld(toWorld))
|
||||
.then(() -> isWithinPlayerLimit(toWorld))
|
||||
.then(() -> isNotBlacklisted(fromWorld, toWorld))
|
||||
@ -50,54 +49,54 @@ public class WorldEntryChecker {
|
||||
.build();
|
||||
}
|
||||
|
||||
public CheckResult<WorldAccessResult.Success, WorldAccessResult.Failure> canAccessWorld(@NotNull MVWorld world) {
|
||||
public Result<WorldAccessResult.Success, WorldAccessResult.Failure> canAccessWorld(@NotNull MVWorld world) {
|
||||
if (!config.getEnforceAccess()) {
|
||||
return CheckResult.success(WorldAccessResult.Success.NO_ENFORCE_WORLD_ACCESS);
|
||||
return Result.success(WorldAccessResult.Success.NO_ENFORCE_WORLD_ACCESS);
|
||||
}
|
||||
return permissionsChecker.hasWorldAccessPermission(this.sender, world)
|
||||
? CheckResult.success(WorldAccessResult.Success.HAS_WORLD_ACCESS)
|
||||
: CheckResult.failure(WorldAccessResult.Failure.NO_WORLD_ACCESS);
|
||||
? Result.success(WorldAccessResult.Success.HAS_WORLD_ACCESS)
|
||||
: Result.failure(WorldAccessResult.Failure.NO_WORLD_ACCESS);
|
||||
}
|
||||
|
||||
public CheckResult<PlayerLimitResult.Success, PlayerLimitResult.Failure> isWithinPlayerLimit(@NotNull MVWorld world) {
|
||||
public Result<PlayerLimitResult.Success, PlayerLimitResult.Failure> isWithinPlayerLimit(@NotNull MVWorld world) {
|
||||
final int playerLimit = world.getPlayerLimit();
|
||||
if (playerLimit <= -1) {
|
||||
return CheckResult.success(PlayerLimitResult.Success.NO_PLAYERLIMIT);
|
||||
return Result.success(PlayerLimitResult.Success.NO_PLAYERLIMIT);
|
||||
}
|
||||
if (permissionsChecker.hasPlayerLimitBypassPermission(sender, world)) {
|
||||
return CheckResult.success(PlayerLimitResult.Success.BYPASS_PLAYERLIMIT);
|
||||
return Result.success(PlayerLimitResult.Success.BYPASS_PLAYERLIMIT);
|
||||
}
|
||||
return playerLimit > world.getCBWorld().getPlayers().size()
|
||||
? CheckResult.success(PlayerLimitResult.Success.WITHIN_PLAYERLIMIT)
|
||||
: CheckResult.failure(PlayerLimitResult.Failure.EXCEED_PLAYERLIMIT);
|
||||
? Result.success(PlayerLimitResult.Success.WITHIN_PLAYERLIMIT)
|
||||
: Result.failure(PlayerLimitResult.Failure.EXCEED_PLAYERLIMIT);
|
||||
}
|
||||
|
||||
public CheckResult<BlacklistResult.Success, BlacklistResult.Failure> isNotBlacklisted(@Nullable MVWorld fromWorld, @NotNull MVWorld toWorld) {
|
||||
public Result<BlacklistResult.Success, BlacklistResult.Failure> isNotBlacklisted(@Nullable MVWorld fromWorld, @NotNull MVWorld toWorld) {
|
||||
if (fromWorld == null) {
|
||||
return CheckResult.success(BlacklistResult.Success.UNKNOWN_FROM_WORLD);
|
||||
return Result.success(BlacklistResult.Success.UNKNOWN_FROM_WORLD);
|
||||
}
|
||||
return toWorld.getWorldBlacklist().contains(fromWorld.getName())
|
||||
? CheckResult.failure(BlacklistResult.Failure.BLACKLISTED, replace("{world}").with(fromWorld.getAlias()))
|
||||
: CheckResult.success(BlacklistResult.Success.NOT_BLACKLISTED);
|
||||
? Result.failure(BlacklistResult.Failure.BLACKLISTED, replace("{world}").with(fromWorld.getAlias()))
|
||||
: Result.success(BlacklistResult.Success.NOT_BLACKLISTED);
|
||||
}
|
||||
|
||||
public CheckResult<EntryFeeResult.Success, EntryFeeResult.Failure> canPayEntryFee(MVWorld world) {
|
||||
public Result<EntryFeeResult.Success, EntryFeeResult.Failure> canPayEntryFee(MVWorld world) {
|
||||
double price = world.getPrice();
|
||||
Material currency = world.getCurrency();
|
||||
if (price == 0D && (currency == null || currency == EntryFee.DISABLED_MATERIAL)) {
|
||||
return CheckResult.success(EntryFeeResult.Success.FREE_ENTRY);
|
||||
return Result.success(EntryFeeResult.Success.FREE_ENTRY);
|
||||
}
|
||||
if (sender instanceof ConsoleCommandSender || sender instanceof BlockCommandSender) {
|
||||
return CheckResult.success(EntryFeeResult.Success.CONSOLE_OR_BLOCK_COMMAND_SENDER);
|
||||
return Result.success(EntryFeeResult.Success.CONSOLE_OR_BLOCK_COMMAND_SENDER);
|
||||
}
|
||||
if (permissionsChecker.hasWorldExemptPermission(sender, world)) {
|
||||
return CheckResult.success(EntryFeeResult.Success.EXEMPT_FROM_ENTRY_FEE);
|
||||
return Result.success(EntryFeeResult.Success.EXEMPT_FROM_ENTRY_FEE);
|
||||
}
|
||||
if (!(sender instanceof Player player)) {
|
||||
return CheckResult.failure(EntryFeeResult.Failure.CANNOT_PAY_ENTRY_FEE);
|
||||
return Result.failure(EntryFeeResult.Failure.CANNOT_PAY_ENTRY_FEE);
|
||||
}
|
||||
return economist.isPlayerWealthyEnough(player, price, currency)
|
||||
? CheckResult.success(EntryFeeResult.Success.ENOUGH_MONEY)
|
||||
: CheckResult.failure(EntryFeeResult.Failure.NOT_ENOUGH_MONEY, replace("{amount}").with("$##")); //TODO
|
||||
? Result.success(EntryFeeResult.Success.ENOUGH_MONEY)
|
||||
: Result.failure(EntryFeeResult.Failure.NOT_ENOUGH_MONEY, replace("{amount}").with("$##")); //TODO
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user