Fix isOptional usage in onlinePlayer context resolvers

Null should not be returned when something is provided as an argument
This commit is contained in:
chickeneer
2022-01-15 13:45:55 -06:00
parent 61904737e0
commit 153a636fa1
3 changed files with 13 additions and 18 deletions
@@ -34,7 +34,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Contract;
import java.util.HashSet;
import java.util.Set;
@@ -51,10 +51,10 @@ public class BukkitCommandContexts extends CommandContexts<BukkitCommandExecutio
public BukkitCommandContexts(BukkitCommandManager manager) {
super(manager);
registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional()));
registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), false));
registerContext(co.aikar.commands.contexts.OnlinePlayer.class, c -> {
OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional());
return onlinePlayer != null ? new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer()) : null;
OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), c.popFirstArg(), false);
return new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer());
});
registerContext(OnlinePlayer[].class, (c) -> {
BukkitCommandIssuer issuer = c.getIssuer();
@@ -125,8 +125,8 @@ public class BukkitCommandContexts extends CommandContexts<BukkitCommandExecutio
throw new InvalidCommandArgument();
}
OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), arg, isOptional);
return onlinePlayer != null ? onlinePlayer.getPlayer() : null;
OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), arg, false);
return onlinePlayer.getPlayer();
}
});
registerContext(OfflinePlayer.class, c -> {
@@ -249,7 +249,7 @@ public class BukkitCommandContexts extends CommandContexts<BukkitCommandExecutio
}
}
@Nullable
@Contract("_,_,false -> !null")
OnlinePlayer getOnlinePlayer(BukkitCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument {
Player player = ACFBukkitUtil.findPlayerSmart(issuer, lookup);
//noinspection Duplicates
@@ -25,7 +25,7 @@ package co.aikar.commands;
import co.aikar.commands.contexts.CommandResultSupplier;
import co.aikar.commands.sponge.contexts.OnlinePlayer;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Contract;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
@@ -49,10 +49,10 @@ public class SpongeCommandContexts extends CommandContexts<SpongeCommandExecutio
super(manager);
registerIssuerOnlyContext(CommandResultSupplier.class, c -> new CommandResultSupplier());
registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional()));
registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), false));
registerContext(co.aikar.commands.contexts.OnlinePlayer.class, c -> {
OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional());
return onlinePlayer != null ? new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer()) : null;
OnlinePlayer onlinePlayer = getOnlinePlayer(c.getIssuer(), c.popFirstArg(), false);
return new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer());
});
registerContext(User.class, c -> {
String name = c.popFirstArg();
@@ -164,7 +164,7 @@ public class SpongeCommandContexts extends CommandContexts<SpongeCommandExecutio
});
}
@Nullable
@Contract("_,_,false -> !null")
OnlinePlayer getOnlinePlayer(SpongeCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument {
Player player = ACFSpongeUtil.findPlayerSmart(issuer, lookup);
if (player == null) {
@@ -32,7 +32,6 @@ import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.format.TextFormat;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.Nullable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -44,7 +43,7 @@ public class VelocityCommandContexts extends CommandContexts<VelocityCommandExec
registerContext(OnlinePlayer.class, (c) -> getOnlinePlayer(server, c));
registerContext(co.aikar.commands.contexts.OnlinePlayer.class, c -> {
OnlinePlayer onlinePlayer = getOnlinePlayer(server, c);
return onlinePlayer != null ? new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer()) : null;
return new co.aikar.commands.contexts.OnlinePlayer(onlinePlayer.getPlayer());
});
registerIssuerAwareContext(CommandSource.class, VelocityCommandExecutionContext::getSender);
registerIssuerAwareContext(Player.class, (c) -> {
@@ -79,13 +78,9 @@ public class VelocityCommandContexts extends CommandContexts<VelocityCommandExec
});
}
@Nullable
private OnlinePlayer getOnlinePlayer(ProxyServer server, VelocityCommandExecutionContext c) throws InvalidCommandArgument {
Player proxiedPlayer = ACFVelocityUtil.findPlayerSmart(server, c.getIssuer(), c.popFirstArg());
if (proxiedPlayer == null) {
if (c.isOptional()) {
return null;
}
throw new InvalidCommandArgument(false);
}
return new OnlinePlayer(proxiedPlayer);