| 1 | package net.minecraft.command;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 5 | import net.minecraft.item.Item;
|
| 6 | import net.minecraft.nbt.JsonToNBT;
|
| 7 | import net.minecraft.nbt.NBTException;
|
| 8 | import net.minecraft.nbt.NBTTagCompound;
|
| 9 | import net.minecraft.server.MinecraftServer;
|
| 10 | import net.minecraft.util.BlockPos;
|
| 11 | import net.minecraft.util.ChatComponentTranslation;
|
| 12 |
|
| 13 | public class CommandClearInventory extends CommandBase
|
| 14 | {
|
| 15 | /**
|
| 16 | * Gets the name of the command
|
| 17 | */
|
| 18 | public String getCommandName()
|
| 19 | {
|
| 20 | return "clear";
|
| 21 | }
|
| 22 |
|
| 23 | /**
|
| 24 | * Gets the usage string for the command.
|
| 25 | */
|
| 26 | public String getCommandUsage(ICommandSender sender)
|
| 27 | {
|
| 28 | return "commands.clear.usage";
|
| 29 | }
|
| 30 |
|
| 31 | /**
|
| 32 | * Return the required permission level for this command.
|
| 33 | */
|
| 34 | public int getRequiredPermissionLevel()
|
| 35 | {
|
| 36 | return 2;
|
| 37 | }
|
| 38 |
|
| 39 | /**
|
| 40 | * Callback when the command is invoked
|
| 41 | */
|
| 42 | public void processCommand(ICommandSender sender, String[] args) throws CommandException
|
| 43 | {
|
| 44 | EntityPlayerMP entityplayermp = args.length == 0 ? getCommandSenderAsPlayer(sender) : getPlayer(sender, args[0]);
|
| 45 | Item item = args.length >= 2 ? getItemByText(sender, args[1]) : null;
|
| 46 | int i = args.length >= 3 ? parseInt(args[2], -1) : -1;
|
| 47 | int j = args.length >= 4 ? parseInt(args[3], -1) : -1;
|
| 48 | NBTTagCompound nbttagcompound = null;
|
| 49 |
|
| 50 | if (args.length >= 5)
|
| 51 | {
|
| 52 | try
|
| 53 | {
|
| 54 | nbttagcompound = JsonToNBT.getTagFromJson(buildString(args, 4));
|
| 55 | }
|
| 56 | catch (NBTException nbtexception)
|
| 57 | {
|
| 58 | throw new CommandException("commands.clear.tagError", new Object[] {nbtexception.getMessage()});
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | if (args.length >= 2 && item == null)
|
| 63 | {
|
| 64 | throw new CommandException("commands.clear.failure", new Object[] {entityplayermp.getName()});
|
| 65 | }
|
| 66 | else
|
| 67 | {
|
| 68 | int k = entityplayermp.inventory.clearMatchingItems(item, i, j, nbttagcompound);
|
| 69 | entityplayermp.inventoryContainer.detectAndSendChanges();
|
| 70 |
|
| 71 | if (!entityplayermp.capabilities.isCreativeMode)
|
| 72 | {
|
| 73 | entityplayermp.updateHeldItem();
|
| 74 | }
|
| 75 |
|
| 76 | sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, k);
|
| 77 |
|
| 78 | if (k == 0)
|
| 79 | {
|
| 80 | throw new CommandException("commands.clear.failure", new Object[] {entityplayermp.getName()});
|
| 81 | }
|
| 82 | else
|
| 83 | {
|
| 84 | if (j == 0)
|
| 85 | {
|
| 86 | sender.addChatMessage(new ChatComponentTranslation("commands.clear.testing", new Object[] {entityplayermp.getName(), Integer.valueOf(k)}));
|
| 87 | }
|
| 88 | else
|
| 89 | {
|
| 90 | notifyOperators(sender, this, "commands.clear.success", new Object[] {entityplayermp.getName(), Integer.valueOf(k)});
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
|
| 97 | {
|
| 98 | return args.length == 1 ? getListOfStringsMatchingLastWord(args, this.func_147209_d()) : (args.length == 2 ? getListOfStringsMatchingLastWord(args, Item.itemRegistry.getKeys()) : null);
|
| 99 | }
|
| 100 |
|
| 101 | protected String[] func_147209_d()
|
| 102 | {
|
| 103 | return MinecraftServer.getServer().getAllUsernames();
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * Return whether the specified command parameter index is a username parameter.
|
| 108 | */
|
| 109 | public boolean isUsernameIndex(String[] args, int index)
|
| 110 | {
|
| 111 | return index == 0;
|
| 112 | }
|
| 113 | }
|
| 114 |
|