TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_whohas.java

98 lines
2.8 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2013-07-04 21:32:11 +00:00
import java.util.ArrayList;
2019-11-03 21:40:05 +00:00
import java.util.Arrays;
import java.util.Collections;
2013-07-04 21:32:11 +00:00
import java.util.List;
import me.totalfreedom.totalfreedommod.rank.Rank;
import org.apache.commons.lang.StringUtils;
2013-07-04 21:32:11 +00:00
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = Rank.OP, source = SourceType.BOTH)
@CommandParameters(description = "See who has an item and optionally clear the specified item.", usage = "/<command> <item> [clear]", aliases = "wh")
public class Command_whohas extends FreedomCommand
2013-07-04 21:32:11 +00:00
{
2013-07-04 21:32:11 +00:00
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
2013-07-04 21:32:11 +00:00
{
if (args.length < 1)
2013-07-04 21:32:11 +00:00
{
return false;
2013-07-04 21:32:11 +00:00
}
final boolean doClear = args.length >= 2 && "clear".equalsIgnoreCase(args[1]);
final String materialName = args[0];
Material material = Material.matchMaterial(materialName);
2013-07-04 21:32:11 +00:00
if (material == null)
{
2018-07-25 02:08:29 +00:00
msg("Invalid item: " + materialName, ChatColor.RED);
return true;
2013-07-04 21:32:11 +00:00
}
final List<String> players = new ArrayList<>();
2013-07-04 21:32:11 +00:00
for (final Player player : server.getOnlinePlayers())
2013-07-04 21:32:11 +00:00
{
2020-04-14 06:40:22 +00:00
if (!plugin.al.isAdmin(sender) && plugin.al.vanished.contains(player))
{
continue;
}
2013-08-14 14:01:42 +00:00
if (player.getInventory().contains(material))
2013-07-04 21:32:11 +00:00
{
2013-08-14 14:01:42 +00:00
players.add(player.getName());
2019-11-03 21:40:05 +00:00
if (plugin.al.isAdmin(sender))
2013-07-04 21:32:11 +00:00
{
2019-11-03 21:40:05 +00:00
if (doClear && !plugin.al.isAdmin(player))
{
player.getInventory().remove(material);
}
2013-07-04 21:32:11 +00:00
}
}
}
if (players.isEmpty())
{
msg("There are no players with that item");
2013-07-04 21:32:11 +00:00
}
else
{
msg("Players with item " + material.name() + ": " + StringUtils.join(players, ", "));
2013-07-04 21:32:11 +00:00
}
return true;
}
2019-11-03 21:40:05 +00:00
public static List<String> getAllMaterials()
{
List<String> names = new ArrayList<>();
for (Material material : Material.values())
{
names.add(material.name());
}
return names;
}
@Override
public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{
if (args.length == 1)
{
return getAllMaterials();
}
if (args.length == 2 && plugin.al.isAdmin(sender))
{
return Arrays.asList("clear");
}
return Collections.emptyList();
}
2013-07-04 21:32:11 +00:00
}