Skip to content

Commit

Permalink
Fix invitations not being handled properly
Browse files Browse the repository at this point in the history
Closes #489.
  • Loading branch information
TheE committed Oct 9, 2023
1 parent b695eb1 commit 71f8283
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@
import io.github.mywarp.mywarp.command.parametric.annotation.Modifiable;
import io.github.mywarp.mywarp.command.parametric.provider.exception.ArgumentAuthorizationException;
import io.github.mywarp.mywarp.command.parametric.provider.exception.NoSuchPlayerException;
import io.github.mywarp.mywarp.command.util.CommandUtil;
import io.github.mywarp.mywarp.command.util.ExceedsInitiatorLimitException;
import io.github.mywarp.mywarp.command.util.ExceedsLimitException;
import io.github.mywarp.mywarp.command.util.NoSuchWorldException;
import io.github.mywarp.mywarp.command.util.ProfilePlayerMatcher;
import io.github.mywarp.mywarp.command.util.UnknownException;
import io.github.mywarp.mywarp.command.util.UserViewableException;
import io.github.mywarp.mywarp.platform.Actor;
import io.github.mywarp.mywarp.platform.Game;
import io.github.mywarp.mywarp.platform.LocalPlayer;
import io.github.mywarp.mywarp.platform.PlayerNameResolver;
import io.github.mywarp.mywarp.platform.Profile;
import io.github.mywarp.mywarp.command.util.*;
import io.github.mywarp.mywarp.platform.*;
import io.github.mywarp.mywarp.service.economy.FeeType;
import io.github.mywarp.mywarp.service.limit.LimitService;
import io.github.mywarp.mywarp.util.Message;
Expand All @@ -50,10 +40,11 @@
import io.github.mywarp.mywarp.util.playermatcher.UuidPlayerMatcher;
import io.github.mywarp.mywarp.warp.Warp;
import io.github.mywarp.mywarp.warp.Warp.Type;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nullable;

/**
* Bundles commands that involve social interaction with other players.
Expand Down Expand Up @@ -91,12 +82,7 @@ private static int parse(PlayerMatcher invitation) {
}

private static String toName(PlayerMatcher invitation) {
if (invitation instanceof ProfilePlayerMatcher) {
return ((ProfilePlayerMatcher) invitation).getProfile().getNameOrId();
}
if (invitation instanceof UuidPlayerMatcher) {
// This should not happen as the InvitationProvider always provides ProfilePlayerMatchers when a
// UuidPlayerMatcher is required.
return ((UuidPlayerMatcher) invitation).getCriteria().toString();
}
if (invitation instanceof GroupPlayerMatcher) {
Expand Down Expand Up @@ -246,24 +232,25 @@ public void publicize(Actor actor, @Switch('f') boolean ignoreLimits, @Modifiabl
@Command(aliases = {"invite"}, desc = "invite.description", help = "invite.help")
@Require("mywarp.cmd.invite")
@Billable(FeeType.INVITE)
public void invite(Actor actor, @Require("mywarp.cmd.invite.group") CompletableFuture<PlayerMatcher> invitationFuture,
@Modifiable Warp warp) {
public void invite(Actor actor, @Require("mywarp.cmd.invite.group") CompletableFuture<Invitation> invitationFuture,
@Modifiable Warp warp) {
nameResolver.getByUniqueId(warp.getCreator()).thenApply(Profile::getNameOrId)
.thenAcceptBothAsync(invitationFuture, (creatorName, invitation) -> {
if (!warp.hasInvitation(invitation)) {
if (invitation instanceof UuidPlayerMatcher && warp
.isCreator(((UuidPlayerMatcher) invitation).getCriteria())) {
actor.sendError(msg.getString("invite.is-creator", creatorName));
}
warp.addInvitation(invitation);

actor
.sendMessage(msg.getString("invite.successful", parse(invitation), toName(invitation), warp.getName()));
if (warp.isType(Type.PUBLIC)) {
actor.sendMessage(Message.of(Style.INFO, msg.getString("invite.public", warp.getName())));
}
} else {
actor.sendError(msg.getString("invite.already-invited", parse(invitation), toName(invitation)));
.thenAcceptBothAsync(invitationFuture, (creatorName, invitation) -> {
PlayerMatcher matcher = invitation.getMatcher();
if (!warp.hasInvitation(matcher)) {
if (matcher instanceof UuidPlayerMatcher && warp
.isCreator(((UuidPlayerMatcher) matcher).getCriteria())) {
actor.sendError(msg.getString("invite.is-creator", creatorName));
}
warp.addInvitation(matcher);

actor
.sendMessage(msg.getString("invite.successful", parse(matcher), invitation.getIdentifier(), warp.getName()));
if (warp.isType(Type.PUBLIC)) {
actor.sendMessage(Message.of(Style.INFO, msg.getString("invite.public", warp.getName())));
}
} else {
actor.sendError(msg.getString("invite.already-invited", parse(matcher), invitation.getIdentifier()));
}
}, game.getExecutor()).exceptionally((ex) -> {

Expand All @@ -282,20 +269,21 @@ public void invite(Actor actor, @Require("mywarp.cmd.invite.group") CompletableF
@Require("mywarp.cmd.uninvite")
@Billable(FeeType.UNINVITE)
public void uninvite(Actor actor,
@Require("mywarp.cmd.uninvite.group") CompletableFuture<PlayerMatcher> invitationFuture,
@Modifiable Warp warp) {
@Require("mywarp.cmd.uninvite.group") CompletableFuture<Invitation> invitationFuture,
@Modifiable Warp warp) {
nameResolver.getByUniqueId(warp.getCreator()).thenApply(Profile::getNameOrId)
.thenAcceptBothAsync(invitationFuture, (creatorName, invitation) -> {
if (warp.hasInvitation(invitation)) {
warp.removeInvitation(invitation);
PlayerMatcher matcher = invitation.getMatcher();
if (warp.hasInvitation(matcher)) {
warp.removeInvitation(matcher);

actor.sendMessage(
msg.getString("uninvite.successful", parse(invitation), toName(invitation), warp.getName()));
msg.getString("uninvite.successful", parse(matcher), invitation.getIdentifier(), warp.getName()));
if (warp.isType(Type.PUBLIC)) {
actor.sendMessage(Message.of(Style.INFO, msg.getString("uninvite.public", warp.getName())));
}
} else {
actor.sendError(msg.getString("uninvite.not-invited", parse(invitation), toName(invitation)));
actor.sendError(msg.getString("uninvite.not-invited", parse(matcher), invitation.getIdentifier()));
}
}, game.getExecutor()).exceptionally((ex) -> {
UserViewableException userViewableException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import io.github.mywarp.mywarp.command.parametric.annotation.Usable;
import io.github.mywarp.mywarp.command.parametric.annotation.Viewable;
import io.github.mywarp.mywarp.command.parametric.annotation.WarpName;
import io.github.mywarp.mywarp.command.util.Invitation;
import io.github.mywarp.mywarp.platform.*;
import io.github.mywarp.mywarp.util.playermatcher.PlayerMatcher;
import io.github.mywarp.mywarp.warp.Warp;
import io.github.mywarp.mywarp.warp.WarpManager;
import io.github.mywarp.mywarp.warp.authorization.AuthorizationResolver;
Expand Down Expand Up @@ -108,7 +108,7 @@ Predicate<Warp> isValid(AuthorizationResolver resolver, Actor actor) {
})).toProvider(new WarpComparatorProvider());

//invitations
bind(key(new TypeCapture<CompletableFuture<PlayerMatcher>>() {
bind(key(new TypeCapture<CompletableFuture<Invitation>>() {
})).toProvider(new InvitationProvider(platform.getPlayerNameResolver()));

//configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import com.sk89q.intake.Require;
import com.sk89q.intake.argument.ArgumentException;
import com.sk89q.intake.argument.CommandArgs;
import io.github.mywarp.mywarp.command.util.ProfilePlayerMatcher;
import io.github.mywarp.mywarp.command.util.Invitation;
import io.github.mywarp.mywarp.platform.PlayerNameResolver;
import io.github.mywarp.mywarp.util.playermatcher.GroupPlayerMatcher;
import io.github.mywarp.mywarp.util.playermatcher.PlayerMatcher;
import io.github.mywarp.mywarp.util.playermatcher.UuidPlayerMatcher;

import java.lang.annotation.Annotation;
import java.util.List;
Expand All @@ -35,7 +36,7 @@
/**
* Provides {@link PlayerMatcher} instances.
*/
class InvitationProvider extends AbstractProvider<CompletableFuture<PlayerMatcher>> {
class InvitationProvider extends AbstractProvider<CompletableFuture<Invitation>> {

private final PlayerNameResolver resolver;

Expand All @@ -44,8 +45,8 @@ class InvitationProvider extends AbstractProvider<CompletableFuture<PlayerMatche
}

@Override
public CompletableFuture<PlayerMatcher> get(CommandArgs arguments, List<? extends Annotation> modifiers)
throws ArgumentException {
public CompletableFuture<Invitation> get(CommandArgs arguments, List<? extends Annotation> modifiers)
throws ArgumentException {
String argument = arguments.next();

if (argument.charAt(1) == ':') {
Expand All @@ -60,14 +61,14 @@ public CompletableFuture<PlayerMatcher> get(CommandArgs arguments, List<? extend
if (require.isPresent()) {
ProviderUtil.checkPermissions(ProviderUtil.actor(arguments.getNamespace()), require.get().value());
}
return CompletableFuture.completedFuture(new GroupPlayerMatcher(realArgument));
return CompletableFuture.completedFuture(new Invitation(new GroupPlayerMatcher(realArgument), realArgument));
//unique identifier
case 'u':
return resolver.getByUniqueId(ProviderUtil.parseUuid(realArgument)).thenApply(ProfilePlayerMatcher::new);
return resolver.getByUniqueId(ProviderUtil.parseUuid(realArgument)).thenApply(profile -> new Invitation(new UuidPlayerMatcher(profile.getUuid()), profile.getNameOrId()));
default: //fall-through
}
}

return resolver.getByName(argument).thenApply(ProfilePlayerMatcher::new);
return resolver.getByName(argument).thenApply(profile -> new Invitation(new UuidPlayerMatcher(profile.getUuid()), profile.getNameOrId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,46 @@

package io.github.mywarp.mywarp.command.util;

import io.github.mywarp.mywarp.platform.Profile;
import io.github.mywarp.mywarp.util.playermatcher.PlayerMatcher;
import io.github.mywarp.mywarp.util.playermatcher.UuidPlayerMatcher;

/**
* Matches players using a Profile's unique identifier.
*
* <p>Unless a Profile is already available, {@link UuidPlayerMatcher} should be used instead.</p>
*/
public class ProfilePlayerMatcher extends UuidPlayerMatcher {
public class Invitation {

private final Profile profile;
private final PlayerMatcher matcher;
private final String identifier;


/**
* Creates a new invitation
*
* @param matcher the invitation's PlayerMatcher
* @param identifier the invitation's given name as entered by the player
*/
public Invitation(PlayerMatcher matcher, String identifier) {
this.matcher = matcher;
this.identifier = identifier;
}

/**
* Creates an instance that will match the player who has the unique identifer of the given profile.
* Gets the matcher.
*
* @param profile the profile
* @return the matcher
*/
public ProfilePlayerMatcher(Profile profile) {
super(profile.getUuid());
this.profile = profile;
public PlayerMatcher getMatcher() {
return matcher;
}

/**
* Gets the Profile of the player who this matcher matches.
* Gets the given name.
*
* @return the profile
* @return the given name
*/
public Profile getProfile() {
return profile;
public String getIdentifier() {
return identifier;
}
}

0 comments on commit 71f8283

Please sign in to comment.