From 475169b36a40f7146fc92cc19e2fabf3ad96c751 Mon Sep 17 00:00:00 2001 From: Video Date: Thu, 31 Aug 2023 02:09:07 -0600 Subject: [PATCH 1/2] Adds /whohas command --- .../java/dev/plex/command/impl/WhoHasCMD.java | 53 +++++++++++++++++++ server/src/main/resources/messages.yml | 6 +++ 2 files changed, 59 insertions(+) create mode 100644 server/src/main/java/dev/plex/command/impl/WhoHasCMD.java diff --git a/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java b/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java new file mode 100644 index 0000000..6c68c99 --- /dev/null +++ b/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java @@ -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 = "/ ", 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 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 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(); + } +} diff --git a/server/src/main/resources/messages.yml b/server/src/main/resources/messages.yml index 9c39887..abbcd94 100644 --- a/server/src/main/resources/messages.yml +++ b/server/src/main/resources/messages.yml @@ -204,3 +204,9 @@ removedOwnLoginMessage: "Your login message has been removed." removedOtherLoginMessage: "You removed {0}'s login message." nameRequired: "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: "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: "Players with {0} in their inventory: {1}" +nobodyHasThatMaterial: "No one online has that in their inventory." +# 0 - The attempted material name +materialNotFound: "{0} is not a valid item/block name." From b0c19a14ab419bd92d00ca40d97b016407bf6ff1 Mon Sep 17 00:00:00 2001 From: Video Date: Thu, 31 Aug 2023 02:11:12 -0600 Subject: [PATCH 2/2] Removed unused import --- server/src/main/java/dev/plex/command/impl/WhoHasCMD.java | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java b/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java index 6c68c99..53f0747 100644 --- a/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java +++ b/server/src/main/java/dev/plex/command/impl/WhoHasCMD.java @@ -1,6 +1,5 @@ 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;