New methods in AbstractPlayerManager + new suggestions suppliers in PaperBrigadierCommand

This commit is contained in:
Marc Baloup 2022-12-13 15:17:51 +01:00
parent 33f4c1550c
commit 49024bf3f8
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 43 additions and 0 deletions

View File

@ -26,6 +26,9 @@ import fr.pandacube.lib.paper.reflect.wrapper.minecraft.commands.Vec3Argument;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.BlockPos; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.core.BlockPos;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerPlayer; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.server.ServerPlayer;
import fr.pandacube.lib.paper.reflect.wrapper.paper.PaperAdventure; import fr.pandacube.lib.paper.reflect.wrapper.paper.PaperAdventure;
import fr.pandacube.lib.players.standalone.AbstractOffPlayer;
import fr.pandacube.lib.players.standalone.AbstractOnlinePlayer;
import fr.pandacube.lib.players.standalone.AbstractPlayerManager;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import fr.pandacube.lib.util.Log; import fr.pandacube.lib.util.Log;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -52,6 +55,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream;
/** /**
* Abstract class to hold a command to be integrated into a Paper server vanilla command dispatcher. * Abstract class to hold a command to be integrated into a Paper server vanilla command dispatcher.
@ -409,6 +413,20 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<BukkitBriga
} }
/**
* A suggestions supplier that suggests the names of the currently connected players (that the command sender can see).
*/
public static final SuggestionsSupplier<CommandSender> TAB_PLAYER_CURRENT_SERVER = (sender, ti, token, a) -> {
@SuppressWarnings("unchecked")
AbstractPlayerManager<AbstractOnlinePlayer, AbstractOffPlayer> pm = (AbstractPlayerManager<AbstractOnlinePlayer, AbstractOffPlayer>) AbstractPlayerManager.getInstance();
Stream<String> plStream;
if (pm == null)
plStream = Bukkit.getOnlinePlayers().stream().filter(p -> !(sender instanceof Player senderP) || senderP.canSee(p)).map(Player::getName);
else
plStream = pm.getNamesOnlyVisible(sender instanceof Player senderP ? pm.getOffline(senderP.getUniqueId()) : null).stream();
return SuggestionsSupplier.collectFilteredStream(plStream, token);
};
/** /**
* A suggestions supplier that suggests the names of the worlds currently loaded on this server. * A suggestions supplier that suggests the names of the worlds currently loaded on this server.
*/ */

View File

@ -117,8 +117,33 @@ public abstract class AbstractPlayerManager<OP extends AbstractOnlinePlayer, OF
return new ArrayList<>(onlinePlayers.values()); return new ArrayList<>(onlinePlayers.values());
} }
/**
* Get all the players that the provided player can see.
* The default implementation returns all the players.
* Concrete subclasses should override this method, especially
* on Paper server, using the {@code Player.canSee(Player)} API.
* @return the players that the provided player can see.
*/
public List<OP> getOnlyVisibleFor(OF viewer) {
return getAll();
}
/**
* Get all the players that the provided player can see.
* The default implementation returns all the players.
* Concrete subclasses should override this method, especially
* on Paper server, using the {@code Player.canSee(Player)} API.
* @return the players that the provided player can see.
*/
public List<String> getNamesOnlyVisible(OF viewer) {
return getOnlyVisibleFor(viewer).stream()
.map(AbstractOnlinePlayer::getName)
.toList();
}
/** /**
* Returns an instance of {@link AbstractOffPlayer} corresponding to a player with the provided {@link UUID}. * Returns an instance of {@link AbstractOffPlayer} corresponding to a player with the provided {@link UUID}.
*
* @param p the UUID of the player. * @param p the UUID of the player.
* @return an instance of {@link AbstractOffPlayer}. It can be a new instance, an {@link AbstractOnlinePlayer} * @return an instance of {@link AbstractOffPlayer}. It can be a new instance, an {@link AbstractOnlinePlayer}
* instance if the player is online, or a cached instance of {@link AbstractOffPlayer}. * instance if the player is online, or a cached instance of {@link AbstractOffPlayer}.