001 package org.bukkit.command.defaults; 002 003 import com.google.common.collect.ImmutableList; 004 import org.apache.commons.lang.Validate; 005 import org.bukkit.Bukkit; 006 import org.bukkit.ChatColor; 007 import org.bukkit.Material; 008 import org.bukkit.command.Command; 009 import org.bukkit.command.CommandSender; 010 import org.bukkit.entity.Player; 011 import org.bukkit.util.StringUtil; 012 013 import java.util.ArrayList; 014 import java.util.Collections; 015 import java.util.List; 016 017 public class ClearCommand extends VanillaCommand { 018 private static List<String> materials; 019 static { 020 ArrayList<String> materialList = new ArrayList<String>(); 021 for (Material material : Material.values()) { 022 materialList.add(material.name()); 023 } 024 Collections.sort(materialList); 025 materials = ImmutableList.copyOf(materialList); 026 } 027 028 public ClearCommand() { 029 super("clear"); 030 this.description = "Clears the player's inventory. Can specify item and data filters too."; 031 this.usageMessage = "/clear <player> [item] [data]"; 032 this.setPermission("bukkit.command.clear"); 033 } 034 035 @Override 036 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 037 if (!testPermission(sender)) return true; 038 039 Player player = null; 040 if (args.length > 0) { 041 player = Bukkit.getPlayer(args[0]); 042 } else if (sender instanceof Player) { 043 player = (Player) sender; 044 } 045 046 if (player != null) { 047 int id; 048 049 if (args.length > 1 && !(args[1].equals("-1"))) { 050 Material material = Material.matchMaterial(args[1]); 051 if (material == null) { 052 sender.sendMessage(ChatColor.RED + "There's no item called " + args[1]); 053 return false; 054 } 055 056 id = material.getId(); 057 } else { 058 id = -1; 059 } 060 061 int data = args.length >= 3 ? getInteger(sender, args[2], 0) : -1; 062 int count = player.getInventory().clear(id, data); 063 064 Command.broadcastCommandMessage(sender, "Cleared the inventory of " + player.getDisplayName() + ", removing " + count + " items"); 065 } else if (args.length == 0) { 066 sender.sendMessage(ChatColor.RED + "Please provide a player!"); 067 } else { 068 sender.sendMessage(ChatColor.RED + "Can't find player " + args[0]); 069 } 070 071 return true; 072 } 073 074 @Override 075 public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { 076 Validate.notNull(sender, "Sender cannot be null"); 077 Validate.notNull(args, "Arguments cannot be null"); 078 Validate.notNull(alias, "Alias cannot be null"); 079 080 if (args.length == 1) { 081 return super.tabComplete(sender, alias, args); 082 } 083 if (args.length == 2) { 084 final String arg = args[1]; 085 final List<String> materials = ClearCommand.materials; 086 List<String> completion = null; 087 088 final int size = materials.size(); 089 int i = Collections.binarySearch(materials, arg, String.CASE_INSENSITIVE_ORDER); 090 091 if (i < 0) { 092 // Insertion (start) index 093 i = -1 - i; 094 } 095 096 for ( ; i < size; i++) { 097 String material = materials.get(i); 098 if (StringUtil.startsWithIgnoreCase(material, arg)) { 099 if (completion == null) { 100 completion = new ArrayList<String>(); 101 } 102 completion.add(material); 103 } else { 104 break; 105 } 106 } 107 108 if (completion != null) { 109 return completion; 110 } 111 } 112 return ImmutableList.of(); 113 } 114 }