CommandEnchant .java

net.minecraft.command.CommandEnchant (class, 143 lines): members: getListOfPlayers, isUsernameIndex, addTabCompletionOptions, getRequiredPermissionLevel.

net.minecraft.commandclass143 linescommonextends CommandBase4.7 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[] getListOfPlayers()
boolean isUsernameIndex(String[] args, int index)

String literals (7)

"enchant"
"commands.enchant.usage"
"commands.enchant.noItem"
"commands.enchant.notFound"
"commands.enchant.cantEnchant"
"commands.enchant.cantCombine"
"commands.enchant.success"

Full Source

CommandEnchant.java143 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.command;
2
3
import java.util.List;
4
import net.minecraft.enchantment.Enchantment;
5
import net.minecraft.entity.player.EntityPlayer;
6
import net.minecraft.item.ItemStack;
7
import net.minecraft.nbt.NBTTagList;
8
import net.minecraft.server.MinecraftServer;
9
import net.minecraft.util.BlockPos;
10
11
public class CommandEnchant extends CommandBase
12
{
13
    /**
14
     * Gets the name of the command
15
     */
16
    public String getCommandName()
17
    {
18
        return "enchant";
19
    }
20
21
    /**
22
     * Return the required permission level for this command.
23
     */
24
    public int getRequiredPermissionLevel()
25
    {
26
        return 2;
27
    }
28
29
    /**
30
     * Gets the usage string for the command.
31
     */
32
    public String getCommandUsage(ICommandSender sender)
33
    {
34
        return "commands.enchant.usage";
35
    }
36
37
    /**
38
     * Callback when the command is invoked
39
     */
40
    public void processCommand(ICommandSender sender, String[] args) throws CommandException
41
    {
42
        if (args.length < 2)
43
        {
44
            throw new WrongUsageException("commands.enchant.usage", new Object[0]);
45
        }
46
        else
47
        {
48
            EntityPlayer entityplayer = getPlayer(sender, args[0]);
49
            sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 0);
50
            int i;
51
52
            try
53
            {
54
                i = parseInt(args[1], 0);
55
            }
56
            catch (NumberInvalidException numberinvalidexception)
57
            {
58
                Enchantment enchantment = Enchantment.getEnchantmentByLocation(args[1]);
59
60
                if (enchantment == null)
61
                {
62
                    throw numberinvalidexception;
63
                }
64
65
                i = enchantment.effectId;
66
            }
67
68
            int j = 1;
69
            ItemStack itemstack = entityplayer.getCurrentEquippedItem();
70
71
            if (itemstack == null)
72
            {
73
                throw new CommandException("commands.enchant.noItem", new Object[0]);
74
            }
75
            else
76
            {
77
                Enchantment enchantment1 = Enchantment.getEnchantmentById(i);
78
79
                if (enchantment1 == null)
80
                {
81
                    throw new NumberInvalidException("commands.enchant.notFound", new Object[] {Integer.valueOf(i)});
82
                }
83
                else if (!enchantment1.canApply(itemstack))
84
                {
85
                    throw new CommandException("commands.enchant.cantEnchant", new Object[0]);
86
                }
87
                else
88
                {
89
                    if (args.length >= 3)
90
                    {
91
                        j = parseInt(args[2], enchantment1.getMinLevel(), enchantment1.getMaxLevel());
92
                    }
93
94
                    if (itemstack.hasTagCompound())
95
                    {
96
                        NBTTagList nbttaglist = itemstack.getEnchantmentTagList();
97
98
                        if (nbttaglist != null)
99
                        {
100
                            for (int k = 0; k < nbttaglist.tagCount(); ++k)
101
                            {
102
                                int l = nbttaglist.getCompoundTagAt(k).getShort("id");
103
104
                                if (Enchantment.getEnchantmentById(l) != null)
105
                                {
106
                                    Enchantment enchantment2 = Enchantment.getEnchantmentById(l);
107
108
                                    if (!enchantment2.canApplyTogether(enchantment1))
109
                                    {
110
                                        throw new CommandException("commands.enchant.cantCombine", new Object[] {enchantment1.getTranslatedName(j), enchantment2.getTranslatedName(nbttaglist.getCompoundTagAt(k).getShort("lvl"))});
111
                                    }
112
                                }
113
                            }
114
                        }
115
                    }
116
117
                    itemstack.addEnchantment(enchantment1, j);
118
                    notifyOperators(sender, this, "commands.enchant.success", new Object[0]);
119
                    sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 1);
120
                }
121
            }
122
        }
123
    }
124
125
    public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
126
    {
127
        return args.length == 1 ? getListOfStringsMatchingLastWord(args, this.getListOfPlayers()) : (args.length == 2 ? getListOfStringsMatchingLastWord(args, Enchantment.func_181077_c()) : null);
128
    }
129
130
    protected String[] getListOfPlayers()
131
    {
132
        return MinecraftServer.getServer().getAllUsernames();
133
    }
134
135
    /**
136
     * Return whether the specified command parameter index is a username parameter.
137
     */
138
    public boolean isUsernameIndex(String[] args, int index)
139
    {
140
        return index == 0;
141
    }
142
}
143

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