refactor: Rename success and failure consumer methods

This commit is contained in:
Ben Woo 2023-04-13 11:57:48 +08:00
parent 302c22f690
commit ef14a398c3
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 16 additions and 16 deletions

View File

@ -92,8 +92,8 @@ public class NewMVPlayerListener implements InjectableListener {
worldEntryCheckerProvider.forWorld(player, world)
.canStayInWorld()
.success(() -> oneTickLater(() -> handleGameModeAndFlight(player, world)))
.failure(() -> {
.onSuccess(() -> oneTickLater(() -> handleGameModeAndFlight(player, world)))
.onFailure(() -> {
player.sendMessage("[MV] - Sorry you can't be in this world anymore!");
oneTickLater(() -> player.teleport(spawnLocation));
});
@ -175,13 +175,13 @@ public class NewMVPlayerListener implements InjectableListener {
}
ResultGroup worldEntryResult = worldEntryCheckerProvider.forWorld(player, toWorld).canEnterWorld(fromWorld)
.success(() -> {
.onSuccess(() -> {
Logging.fine("Player '%s' is allowed to use portals to enter world '%s'.", player.getName(), toWorld.getName());
if (!config.isUsingCustomPortalSearch()) {
event.setSearchRadius(config.getCustomPortalSearchRadius());
}
})
.failure(() -> {
.onFailure(() -> {
event.setCancelled(true);
Logging.fine("Player '%s' is not allowed to use portals to enter world '%s'.", player.getName(), toWorld.getName());
//TODO send player reason for failure
@ -230,12 +230,12 @@ public class NewMVPlayerListener implements InjectableListener {
ResultGroup worldEntryResult = worldEntryCheckerProvider.forWorld(teleportee, toWorld)
.canEnterWorld(fromWorld.getOrNull())
.success(() -> Logging.fine("MV-Core is allowing '%s' to go to '%s'.", teleportee.getName(), toWorld.getName()))
.successWithReason(EntryFeeResult.Success.ENOUGH_MONEY, () -> {
.onSuccess(() -> Logging.fine("MV-Core is allowing '%s' to go to '%s'.", teleportee.getName(), toWorld.getName()))
.onSuccessReason(EntryFeeResult.Success.ENOUGH_MONEY, () -> {
economist.payEntryFee((Player) teleporter, toWorld);
//TODO send payment message
})
.failure(() -> {
.onFailure(() -> {
event.setCancelled(true);
Logging.fine("MV-Core is denying '%s' from going to '%s'.", teleportee.getName(), toWorld.getName());
//TODO send player reason for failure

View File

@ -20,28 +20,28 @@ public sealed interface Result<S extends SuccessReason, F extends FailureReason>
F failureReason();
default Result<S, F> success(Consumer<S> consumer) {
default Result<S, F> onSuccess(Consumer<S> consumer) {
if (this.isSuccess()) {
consumer.accept(this.successReason());
}
return this;
}
default Result<S, F> failure(Consumer<F> consumer) {
default Result<S, F> onFailure(Consumer<F> consumer) {
if (this.isFailure()) {
consumer.accept(this.failureReason());
}
return this;
}
default Result<S, F> successWithReason(S successReason, Consumer<S> consumer) {
default Result<S, F> onSuccessReason(S successReason, Consumer<S> consumer) {
if (this.isSuccess() && this.successReason() == successReason) {
consumer.accept(this.successReason());
}
return this;
}
default Result<S, F> failureWithReason(F failureReason, Consumer<F> consumer) {
default Result<S, F> onFailureReason(F failureReason, Consumer<F> consumer) {
if (this.isFailure() && this.failureReason() == failureReason) {
consumer.accept(this.failureReason());
}

View File

@ -35,31 +35,31 @@ public class ResultGroup {
return !isSuccess;
}
public ResultGroup success(Runnable successRunnable) {
public ResultGroup onSuccess(Runnable successRunnable) {
if (isSuccess) {
successRunnable.run();
}
return this;
}
public ResultGroup failure(Runnable failureRunnable) {
public ResultGroup onFailure(Runnable failureRunnable) {
if (isFailure()) {
failureRunnable.run();
}
return this;
}
public <S extends SuccessReason> ResultGroup successWithReason(Class<S> successReasonClass, Consumer<S> successConsumer) {
public <S extends SuccessReason> ResultGroup onSuccessReason(Class<S> successReasonClass, Consumer<S> successConsumer) {
getSuccessReason(successReasonClass).peek(successConsumer);
return this;
}
public <F extends FailureReason> ResultGroup failureWithReason(Class<F> failureReasonClass, Consumer<F> failureConsumer) {
public <F extends FailureReason> ResultGroup onFailureReason(Class<F> failureReasonClass, Consumer<F> failureConsumer) {
getFailureReason(failureReasonClass).peek(failureConsumer);
return this;
}
public <S extends SuccessReason> ResultGroup successWithReason(S successReason, Runnable successRunnable) {
public <S extends SuccessReason> ResultGroup onSuccessReason(S successReason, Runnable successRunnable) {
getSuccessReason(successReason.getClass()).filter(successReason::equals).peek(reason -> successRunnable.run());
return this;
}