Skip to content
Permalink
Browse files

More work towards relocating OnlinePlayer context

This context was conflicting with each other over multiple
ACF modules, so if someone wanted to use multiple ACF's in same jar,
it would clash and not work.

The PR to move these was incomplete and this finishes fixing the
context handlers to support new and old.
  • Loading branch information...
aikar committed Feb 24, 2019
1 parent 5c1d446 commit 17d89ad2168acbb27881795b821daa54eb0163f1
@@ -23,8 +23,7 @@

package co.aikar.commands;

import co.aikar.commands.annotation.Optional;
import co.aikar.commands.contexts.bukkit.OnlinePlayer;
import co.aikar.commands.bukkit.contexts.OnlinePlayer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@@ -40,7 +39,6 @@
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -51,7 +49,11 @@
public BukkitCommandContexts(BukkitCommandManager manager) {
super(manager);

registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.hasAnnotation(Optional.class)));
registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional()));
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;
});
registerContext(OnlinePlayer[].class, (c) -> {
BukkitCommandIssuer issuer = c.getIssuer();
final String search = c.popFirstArg();
@@ -92,7 +94,7 @@ public BukkitCommandContexts(BukkitCommandManager manager) {
});
registerIssuerAwareContext(CommandSender.class, BukkitCommandExecutionContext::getSender);
registerIssuerAwareContext(Player.class, (c) -> {
boolean isOptional = c.hasAnnotation(Optional.class);
boolean isOptional = c.isOptional();
CommandSender sender = c.getSender();
boolean isPlayerSender = sender instanceof Player;
if (!c.hasFlag("other")) {
@@ -21,7 +21,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package co.aikar.commands.contexts.bukkit;
package co.aikar.commands.bukkit.contexts;

import org.bukkit.entity.Player;

@@ -53,8 +53,6 @@ public int hashCode() {

@Override
public String toString() {
return "OnlinePlayer{" +
"player=" + player +
'}';
return "OnlinePlayer{player=" + player + '}';
}
}
@@ -26,10 +26,10 @@
import org.bukkit.entity.Player;

/**
* @deprecated Use {@link co.aikar.commands.contexts.bukkit.OnlinePlayer instead}
* @deprecated Use {@link co.aikar.commands.bukkit.contexts.OnlinePlayer instead}
*/
@Deprecated
public class OnlinePlayer extends co.aikar.commands.contexts.bukkit.OnlinePlayer {
public class OnlinePlayer extends co.aikar.commands.bukkit.contexts.OnlinePlayer {
public OnlinePlayer(Player player) {
super(player);
}
@@ -25,10 +25,11 @@


import co.aikar.commands.annotation.Optional;
import co.aikar.commands.contexts.OnlineProxiedPlayer;
import co.aikar.commands.bungee.contexts.OnlinePlayer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import org.jetbrains.annotations.Nullable;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -37,15 +38,10 @@

BungeeCommandContexts(CommandManager manager) {
super(manager);
registerContext(OnlineProxiedPlayer.class, (c) -> {
ProxiedPlayer proxiedPlayer = ACFBungeeUtil.findPlayerSmart(c.getIssuer(), c.popFirstArg());
if (proxiedPlayer == null) {
if (c.hasAnnotation(Optional.class)) {
return null;
}
throw new InvalidCommandArgument(false);
}
return new OnlineProxiedPlayer(proxiedPlayer);
registerContext(OnlinePlayer.class, this::getOnlinePlayer);
registerContext(co.aikar.commands.contexts.OnlineProxiedPlayer.class, c -> {
OnlinePlayer onlinePlayer = getOnlinePlayer(c);
return onlinePlayer != null ? new co.aikar.commands.contexts.OnlineProxiedPlayer(onlinePlayer.getPlayer()) : null;
});
registerIssuerAwareContext(CommandSender.class, BungeeCommandExecutionContext::getSender);
registerIssuerAwareContext(ProxiedPlayer.class, (c) -> {
@@ -80,4 +76,16 @@
return match;
});
}

@Nullable
private co.aikar.commands.contexts.OnlineProxiedPlayer getOnlinePlayer(BungeeCommandExecutionContext c) throws InvalidCommandArgument {
ProxiedPlayer proxiedPlayer = ACFBungeeUtil.findPlayerSmart(c.getIssuer(), c.popFirstArg());
if (proxiedPlayer == null) {
if (c.hasAnnotation(Optional.class)) {
return null;
}
throw new InvalidCommandArgument(false);
}
return new co.aikar.commands.contexts.OnlineProxiedPlayer(proxiedPlayer);
}
}
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package co.aikar.commands.bungee.contexts;

import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.util.Objects;

public class OnlinePlayer {

public final ProxiedPlayer player;

public OnlinePlayer(ProxiedPlayer player) {
this.player = player;
}

public ProxiedPlayer getPlayer() {
return player;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OnlinePlayer that = (OnlinePlayer) o;
return Objects.equals(player, that.player);
}

@Override
public int hashCode() {
return Objects.hash(player);
}

@Override
public String toString() {
return "OnlinePlayer{player=" + player + '}';
}
}
@@ -1,61 +1,14 @@
/*
* Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package co.aikar.commands.contexts;

import co.aikar.commands.bungee.contexts.OnlinePlayer;
import net.md_5.bungee.api.connection.ProxiedPlayer;

import java.util.Objects;

public class OnlineProxiedPlayer {

public final ProxiedPlayer player;

/**
* @deprecated Use {@link OnlinePlayer}
*/
@Deprecated
public class OnlineProxiedPlayer extends OnlinePlayer {
public OnlineProxiedPlayer(ProxiedPlayer player) {
this.player = player;
}

public ProxiedPlayer getPlayer(){
return player;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OnlineProxiedPlayer that = (OnlineProxiedPlayer) o;
return Objects.equals(player, that.player);
}

@Override
public int hashCode() {
return Objects.hash(player);
}

@Override
public String toString() {
return "OnlineProxiedPlayer{" +
"proxiedPlayer=" + player +
'}';
super(player);
}
}
@@ -33,7 +33,7 @@
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Values;
import co.aikar.commands.contexts.OnlinePlayer;
import co.aikar.commands.bukkit.contexts.OnlinePlayer;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -24,7 +24,7 @@
package co.aikar.commands;

import co.aikar.commands.contexts.CommandResultSupplier;
import co.aikar.commands.contexts.sponge.OnlinePlayer;
import co.aikar.commands.sponge.contexts.OnlinePlayer;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
@@ -50,6 +50,10 @@ public SpongeCommandContexts(final SpongeCommandManager manager) {

registerIssuerOnlyContext(CommandResultSupplier.class, c -> new CommandResultSupplier());
registerContext(OnlinePlayer.class, c -> getOnlinePlayer(c.getIssuer(), c.popFirstArg(), c.isOptional()));
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;
});
registerContext(User.class, c -> {
String name = c.popFirstArg();
// try online players first
@@ -163,7 +167,6 @@ public SpongeCommandContexts(final SpongeCommandManager manager) {
@Nullable
OnlinePlayer getOnlinePlayer(SpongeCommandIssuer issuer, String lookup, boolean allowMissing) throws InvalidCommandArgument {
Player player = ACFSpongeUtil.findPlayerSmart(issuer, lookup);
//noinspection Duplicates
if (player == null) {
if (allowMissing) {
return null;
@@ -26,10 +26,10 @@
import org.spongepowered.api.entity.living.player.Player;

/**
* @deprecated Use {@link co.aikar.commands.contexts.sponge.OnlinePlayer instead}
* @deprecated Use {@link co.aikar.commands.sponge.contexts.OnlinePlayer instead}
*/
@Deprecated
public class OnlinePlayer extends co.aikar.commands.contexts.sponge.OnlinePlayer {
public class OnlinePlayer extends co.aikar.commands.sponge.contexts.OnlinePlayer {
public OnlinePlayer(Player player) {
super(player);
}
@@ -21,7 +21,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package co.aikar.commands.contexts.sponge;
package co.aikar.commands.sponge.contexts;

import org.spongepowered.api.entity.living.player.Player;

@@ -53,8 +53,6 @@ public int hashCode() {

@Override
public String toString() {
return "OnlinePlayer{" +
"player=" + player +
'}';
return "OnlinePlayer{player=" + player + '}';
}
}
@@ -30,30 +30,25 @@
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;

import co.aikar.commands.annotation.Optional;
import co.aikar.commands.contexts.velocity.OnlinePlayer;
import co.aikar.commands.velocity.contexts.OnlinePlayer;
import net.kyori.text.format.TextColor;
import net.kyori.text.format.TextDecoration;
import net.kyori.text.format.TextFormat;
import org.jetbrains.annotations.Nullable;

public class VelocityCommandContexts extends CommandContexts<VelocityCommandExecutionContext> {

VelocityCommandContexts(ProxyServer server, CommandManager manager) {
super(manager);
registerContext(OnlinePlayer.class, (c) -> {
Player proxiedPlayer = ACFVelocityUtil.findPlayerSmart(server, c.getIssuer(), c.popFirstArg());
if (proxiedPlayer == null) {
if (c.hasAnnotation(Optional.class)) {
return null;
}
throw new InvalidCommandArgument(false);
}
return new OnlinePlayer(proxiedPlayer);
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;
});
registerIssuerAwareContext(CommandSource.class, VelocityCommandExecutionContext::getSender);
registerIssuerAwareContext(Player.class, (c) -> {
Player proxiedPlayer = c.getSender() instanceof Player ? (Player) c.getSender() : null;
if (proxiedPlayer == null && !c.hasAnnotation(Optional.class)) {
if (proxiedPlayer == null && !c.isOptional()) {
throw new InvalidCommandArgument(MessageKeys.NOT_ALLOWED_ON_CONSOLE, false);
}
return proxiedPlayer;
@@ -82,4 +77,16 @@
return match;
});
}

@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);
}
}
@@ -26,10 +26,10 @@
import com.velocitypowered.api.proxy.Player;

/**
* @deprecated Use {@link co.aikar.commands.contexts.velocity.OnlinePlayer instead}
* @deprecated Use {@link co.aikar.commands.velocity.contexts.OnlinePlayer instead}
*/
@Deprecated
public class OnlinePlayer extends co.aikar.commands.contexts.velocity.OnlinePlayer {
public class OnlinePlayer extends co.aikar.commands.velocity.contexts.OnlinePlayer {
public OnlinePlayer(Player player) {
super(player);
}
Oops, something went wrong.

0 comments on commit 17d89ad

Please sign in to comment.
You can’t perform that action at this time.