CommandReplaceItem .java

net.minecraft.command.CommandReplaceItem (class, 254 lines): members: SHORTCUTS, getUsernames, isUsernameIndex, addTabCompletionOptions, getCommandUsage.

net.minecraft.commandclass254 linescommonextends CommandBase8.3 KB

API Surface (7)

String getCommandName()
int getRequiredPermissionLevel()
String getCommandUsage(ICommandSender sender)
void processCommand(ICommandSender sender, String[] args)
List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
String[] getUsernames()
boolean isUsernameIndex(String[] args, int index)

Fields & Constants (1)

SHORTCUTS

String literals (25)

"replaceitem"
"commands.replaceitem.usage"
"entity"
"block"
"commands.replaceitem.block.usage"
"commands.replaceitem.entity.usage"
"commands.replaceitem.tagError"
"commands.replaceitem.noContainer"
"commands.replaceitem.failed"
"commands.replaceitem.success"
"commands.generic.parameter.invalid"
"slot.container."
"slot.hotbar."
"slot.inventory."
"slot.enderchest."
"slot.villager."
"slot.horse."
"slot.weapon"
"slot.armor.head"
"slot.armor.chest"
"slot.armor.legs"
"slot.armor.feet"
"slot.horse.saddle"
"slot.horse.armor"

Full Source

CommandReplaceItem.java254 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.command;
2
3
import com.google.common.collect.Maps;
4
import java.util.List;
5
import java.util.Map;
6
import net.minecraft.block.Block;
7
import net.minecraft.entity.Entity;
8
import net.minecraft.entity.player.EntityPlayer;
9
import net.minecraft.init.Blocks;
10
import net.minecraft.inventory.IInventory;
11
import net.minecraft.item.Item;
12
import net.minecraft.item.ItemStack;
13
import net.minecraft.nbt.JsonToNBT;
14
import net.minecraft.nbt.NBTException;
15
import net.minecraft.server.MinecraftServer;
16
import net.minecraft.tileentity.TileEntity;
17
import net.minecraft.util.BlockPos;
18
import net.minecraft.world.World;
19
20
public class CommandReplaceItem extends CommandBase
21
{
22
    private static final Map<String, Integer> SHORTCUTS = Maps.<String, Integer>newHashMap();
23
24
    /**
25
     * Gets the name of the command
26
     */
27
    public String getCommandName()
28
    {
29
        return "replaceitem";
30
    }
31
32
    /**
33
     * Return the required permission level for this command.
34
     */
35
    public int getRequiredPermissionLevel()
36
    {
37
        return 2;
38
    }
39
40
    /**
41
     * Gets the usage string for the command.
42
     */
43
    public String getCommandUsage(ICommandSender sender)
44
    {
45
        return "commands.replaceitem.usage";
46
    }
47
48
    /**
49
     * Callback when the command is invoked
50
     */
51
    public void processCommand(ICommandSender sender, String[] args) throws CommandException
52
    {
53
        if (args.length < 1)
54
        {
55
            throw new WrongUsageException("commands.replaceitem.usage", new Object[0]);
56
        }
57
        else
58
        {
59
            boolean flag;
60
61
            if (args[0].equals("entity"))
62
            {
63
                flag = false;
64
            }
65
            else
66
            {
67
                if (!args[0].equals("block"))
68
                {
69
                    throw new WrongUsageException("commands.replaceitem.usage", new Object[0]);
70
                }
71
72
                flag = true;
73
            }
74
75
            int i;
76
77
            if (flag)
78
            {
79
                if (args.length < 6)
80
                {
81
                    throw new WrongUsageException("commands.replaceitem.block.usage", new Object[0]);
82
                }
83
84
                i = 4;
85
            }
86
            else
87
            {
88
                if (args.length < 4)
89
                {
90
                    throw new WrongUsageException("commands.replaceitem.entity.usage", new Object[0]);
91
                }
92
93
                i = 2;
94
            }
95
96
            int j = this.getSlotForShortcut(args[i++]);
97
            Item item;
98
99
            try
100
            {
101
                item = getItemByText(sender, args[i]);
102
            }
103
            catch (NumberInvalidException numberinvalidexception)
104
            {
105
                if (Block.getBlockFromName(args[i]) != Blocks.air)
106
                {
107
                    throw numberinvalidexception;
108
                }
109
110
                item = null;
111
            }
112
113
            ++i;
114
            int k = args.length > i ? parseInt(args[i++], 1, 64) : 1;
115
            int l = args.length > i ? parseInt(args[i++]) : 0;
116
            ItemStack itemstack = new ItemStack(item, k, l);
117
118
            if (args.length > i)
119
            {
120
                String s = getChatComponentFromNthArg(sender, args, i).getUnformattedText();
121
122
                try
123
                {
124
                    itemstack.setTagCompound(JsonToNBT.getTagFromJson(s));
125
                }
126
                catch (NBTException nbtexception)
127
                {
128
                    throw new CommandException("commands.replaceitem.tagError", new Object[] {nbtexception.getMessage()});
129
                }
130
            }
131
132
            if (itemstack.getItem() == null)
133
            {
134
                itemstack = null;
135
            }
136
137
            if (flag)
138
            {
139
                sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 0);
140
                BlockPos blockpos = parseBlockPos(sender, args, 1, false);
141
                World world = sender.getEntityWorld();
142
                TileEntity tileentity = world.getTileEntity(blockpos);
143
144
                if (tileentity == null || !(tileentity instanceof IInventory))
145
                {
146
                    throw new CommandException("commands.replaceitem.noContainer", new Object[] {Integer.valueOf(blockpos.getX()), Integer.valueOf(blockpos.getY()), Integer.valueOf(blockpos.getZ())});
147
                }
148
149
                IInventory iinventory = (IInventory)tileentity;
150
151
                if (j >= 0 && j < iinventory.getSizeInventory())
152
                {
153
                    iinventory.setInventorySlotContents(j, itemstack);
154
                }
155
            }
156
            else
157
            {
158
                Entity entity = getEntity(sender, args[1]);
159
                sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 0);
160
161
                if (entity instanceof EntityPlayer)
162
                {
163
                    ((EntityPlayer)entity).inventoryContainer.detectAndSendChanges();
164
                }
165
166
                if (!entity.replaceItemInInventory(j, itemstack))
167
                {
168
                    throw new CommandException("commands.replaceitem.failed", new Object[] {Integer.valueOf(j), Integer.valueOf(k), itemstack == null ? "Air" : itemstack.getChatComponent()});
169
                }
170
171
                if (entity instanceof EntityPlayer)
172
                {
173
                    ((EntityPlayer)entity).inventoryContainer.detectAndSendChanges();
174
                }
175
            }
176
177
            sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, k);
178
            notifyOperators(sender, this, "commands.replaceitem.success", new Object[] {Integer.valueOf(j), Integer.valueOf(k), itemstack == null ? "Air" : itemstack.getChatComponent()});
179
        }
180
    }
181
182
    private int getSlotForShortcut(String shortcut) throws CommandException
183
    {
184
        if (!SHORTCUTS.containsKey(shortcut))
185
        {
186
            throw new CommandException("commands.generic.parameter.invalid", new Object[] {shortcut});
187
        }
188
        else
189
        {
190
            return ((Integer)SHORTCUTS.get(shortcut)).intValue();
191
        }
192
    }
193
194
    public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
195
    {
196
        return args.length == 1 ? getListOfStringsMatchingLastWord(args, new String[] {"entity", "block"}): (args.length == 2 && args[0].equals("entity") ? getListOfStringsMatchingLastWord(args, this.getUsernames()) : (args.length >= 2 && args.length <= 4 && args[0].equals("block") ? func_175771_a(args, 1, pos) : ((args.length != 3 || !args[0].equals("entity")) && (args.length != 5 || !args[0].equals("block")) ? ((args.length != 4 || !args[0].equals("entity")) && (args.length != 6 || !args[0].equals("block")) ? null : getListOfStringsMatchingLastWord(args, Item.itemRegistry.getKeys())) : getListOfStringsMatchingLastWord(args, SHORTCUTS.keySet()))));
197
    }
198
199
    protected String[] getUsernames()
200
    {
201
        return MinecraftServer.getServer().getAllUsernames();
202
    }
203
204
    /**
205
     * Return whether the specified command parameter index is a username parameter.
206
     */
207
    public boolean isUsernameIndex(String[] args, int index)
208
    {
209
        return args.length > 0 && args[0].equals("entity") && index == 1;
210
    }
211
212
    static
213
    {
214
        for (int i = 0; i < 54; ++i)
215
        {
216
            SHORTCUTS.put("slot.container." + i, Integer.valueOf(i));
217
        }
218
219
        for (int j = 0; j < 9; ++j)
220
        {
221
            SHORTCUTS.put("slot.hotbar." + j, Integer.valueOf(j));
222
        }
223
224
        for (int k = 0; k < 27; ++k)
225
        {
226
            SHORTCUTS.put("slot.inventory." + k, Integer.valueOf(9 + k));
227
        }
228
229
        for (int l = 0; l < 27; ++l)
230
        {
231
            SHORTCUTS.put("slot.enderchest." + l, Integer.valueOf(200 + l));
232
        }
233
234
        for (int i1 = 0; i1 < 8; ++i1)
235
        {
236
            SHORTCUTS.put("slot.villager." + i1, Integer.valueOf(300 + i1));
237
        }
238
239
        for (int j1 = 0; j1 < 15; ++j1)
240
        {
241
            SHORTCUTS.put("slot.horse." + j1, Integer.valueOf(500 + j1));
242
        }
243
244
        SHORTCUTS.put("slot.weapon", Integer.valueOf(99));
245
        SHORTCUTS.put("slot.armor.head", Integer.valueOf(103));
246
        SHORTCUTS.put("slot.armor.chest", Integer.valueOf(102));
247
        SHORTCUTS.put("slot.armor.legs", Integer.valueOf(101));
248
        SHORTCUTS.put("slot.armor.feet", Integer.valueOf(100));
249
        SHORTCUTS.put("slot.horse.saddle", Integer.valueOf(400));
250
        SHORTCUTS.put("slot.horse.armor", Integer.valueOf(401));
251
        SHORTCUTS.put("slot.horse.chest", Integer.valueOf(499));
252
    }
253
}
254

Deobfuscated MCP 1.8.9 source of CommandReplaceItem from Marcelektro/MavenMCP-1.8.9 (net/minecraft/command/CommandReplaceItem.java), stored verbatim.