Adds /whohas command

This commit is contained in:
Video 2023-08-31 02:09:07 -06:00
parent bb019abd89
commit 475169b36a
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package dev.plex.command.impl;
import com.destroystokyo.paper.MaterialSetTag;
import com.google.common.collect.ImmutableList;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
@CommandPermissions(permission = "plex.whohas")
@CommandParameters(name = "whohas", description = "Returns a list of players with a specific item in their inventory.", usage = "/<command> <material>", aliases = "wh")
public class WhoHasCMD extends PlexCommand
{
@Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args)
{
if (args.length == 0)
{
return usage();
}
final Material material = Material.getMaterial(args[0].toUpperCase());
if (material == null)
{
return messageComponent("materialNotFound", args[0]);
}
final List<TextComponent> players = Bukkit.getOnlinePlayers().stream().filter(player ->
player.getInventory().contains(material)).map(player -> Component.text(player.getName())).toList();
return players.isEmpty() ? messageComponent("nobodyHasThatMaterial") :
messageComponent("playersWithMaterial", Component.text(material.name()),
Component.join(JoinConfiguration.commas(true), players));
}
@Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
return args.length == 1 && silentCheckPermission(sender, "plex.whohas") ? Arrays.stream(Material.values()).map(Enum::name).toList() : ImmutableList.of();
}
}

View File

@ -204,3 +204,9 @@ removedOwnLoginMessage: "<gray>Your login message has been removed."
removedOtherLoginMessage: "<gray>You removed {0}'s login message."
nameRequired: "<red>Policy requires that you must state your player name in your login message. You can either do this by inserting your name or %player%."
rankRequired: "<red>Policy requires that you must state your rank in your login message. You can do this by using %rank% in your login message."
# 0 - The material name
# 1 - The players who have the material in their inventory
playersWithMaterial: "<gray>Players with {0} in their inventory: {1}"
nobodyHasThatMaterial: "<gray>No one online has that in their inventory."
# 0 - The attempted material name
materialNotFound: "<red>{0} is not a valid item/block name."