CommandSetBlock .java

net.minecraft.command.server.CommandSetBlock (class): members: addTabCompletionOptions, getRequiredPermissionLevel, getCommandUsage, getCommandName.

net.minecraft.command.serverclass157 linesserverextends CommandBase5.4 KB

API Surface (5)

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

String literals (9)

"setblock"
"commands.setblock.usage"
"commands.setblock.outOfWorld"
"commands.setblock.tagError"
"destroy"
"commands.setblock.success"
"keep"
"commands.setblock.noChange"
"replace"

Full Source

CommandSetBlock.java157 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.command.server;
2
3
import java.util.List;
4
import net.minecraft.block.Block;
5
import net.minecraft.block.state.IBlockState;
6
import net.minecraft.command.CommandBase;
7
import net.minecraft.command.CommandException;
8
import net.minecraft.command.CommandResultStats;
9
import net.minecraft.command.ICommandSender;
10
import net.minecraft.command.WrongUsageException;
11
import net.minecraft.init.Blocks;
12
import net.minecraft.inventory.IInventory;
13
import net.minecraft.nbt.JsonToNBT;
14
import net.minecraft.nbt.NBTException;
15
import net.minecraft.nbt.NBTTagCompound;
16
import net.minecraft.tileentity.TileEntity;
17
import net.minecraft.util.BlockPos;
18
import net.minecraft.world.World;
19
20
public class CommandSetBlock extends CommandBase
21
{
22
    /**
23
     * Gets the name of the command
24
     */
25
    public String getCommandName()
26
    {
27
        return "setblock";
28
    }
29
30
    /**
31
     * Return the required permission level for this command.
32
     */
33
    public int getRequiredPermissionLevel()
34
    {
35
        return 2;
36
    }
37
38
    /**
39
     * Gets the usage string for the command.
40
     */
41
    public String getCommandUsage(ICommandSender sender)
42
    {
43
        return "commands.setblock.usage";
44
    }
45
46
    /**
47
     * Callback when the command is invoked
48
     */
49
    public void processCommand(ICommandSender sender, String[] args) throws CommandException
50
    {
51
        if (args.length < 4)
52
        {
53
            throw new WrongUsageException("commands.setblock.usage", new Object[0]);
54
        }
55
        else
56
        {
57
            sender.setCommandStat(CommandResultStats.Type.AFFECTED_BLOCKS, 0);
58
            BlockPos blockpos = parseBlockPos(sender, args, 0, false);
59
            Block block = CommandBase.getBlockByText(sender, args[3]);
60
            int i = 0;
61
62
            if (args.length >= 5)
63
            {
64
                i = parseInt(args[4], 0, 15);
65
            }
66
67
            World world = sender.getEntityWorld();
68
69
            if (!world.isBlockLoaded(blockpos))
70
            {
71
                throw new CommandException("commands.setblock.outOfWorld", new Object[0]);
72
            }
73
            else
74
            {
75
                NBTTagCompound nbttagcompound = new NBTTagCompound();
76
                boolean flag = false;
77
78
                if (args.length >= 7 && block.hasTileEntity())
79
                {
80
                    String s = getChatComponentFromNthArg(sender, args, 6).getUnformattedText();
81
82
                    try
83
                    {
84
                        nbttagcompound = JsonToNBT.getTagFromJson(s);
85
                        flag = true;
86
                    }
87
                    catch (NBTException nbtexception)
88
                    {
89
                        throw new CommandException("commands.setblock.tagError", new Object[] {nbtexception.getMessage()});
90
                    }
91
                }
92
93
                if (args.length >= 6)
94
                {
95
                    if (args[5].equals("destroy"))
96
                    {
97
                        world.destroyBlock(blockpos, true);
98
99
                        if (block == Blocks.air)
100
                        {
101
                            notifyOperators(sender, this, "commands.setblock.success", new Object[0]);
102
                            return;
103
                        }
104
                    }
105
                    else if (args[5].equals("keep") && !world.isAirBlock(blockpos))
106
                    {
107
                        throw new CommandException("commands.setblock.noChange", new Object[0]);
108
                    }
109
                }
110
111
                TileEntity tileentity1 = world.getTileEntity(blockpos);
112
113
                if (tileentity1 != null)
114
                {
115
                    if (tileentity1 instanceof IInventory)
116
                    {
117
                        ((IInventory)tileentity1).clear();
118
                    }
119
120
                    world.setBlockState(blockpos, Blocks.air.getDefaultState(), block == Blocks.air ? 2 : 4);
121
                }
122
123
                IBlockState iblockstate = block.getStateFromMeta(i);
124
125
                if (!world.setBlockState(blockpos, iblockstate, 2))
126
                {
127
                    throw new CommandException("commands.setblock.noChange", new Object[0]);
128
                }
129
                else
130
                {
131
                    if (flag)
132
                    {
133
                        TileEntity tileentity = world.getTileEntity(blockpos);
134
135
                        if (tileentity != null)
136
                        {
137
                            nbttagcompound.setInteger("x", blockpos.getX());
138
                            nbttagcompound.setInteger("y", blockpos.getY());
139
                            nbttagcompound.setInteger("z", blockpos.getZ());
140
                            tileentity.readFromNBT(nbttagcompound);
141
                        }
142
                    }
143
144
                    world.notifyNeighborsRespectDebug(blockpos, iblockstate.getBlock());
145
                    sender.setCommandStat(CommandResultStats.Type.AFFECTED_BLOCKS, 1);
146
                    notifyOperators(sender, this, "commands.setblock.success", new Object[0]);
147
                }
148
            }
149
        }
150
    }
151
152
    public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos)
153
    {
154
        return args.length > 0 && args.length <= 3 ? func_175771_a(args, 0, pos) : (args.length == 4 ? getListOfStringsMatchingLastWord(args, Block.blockRegistry.getKeys()) : (args.length == 6 ? getListOfStringsMatchingLastWord(args, new String[] {"replace", "destroy", "keep"}): null));
155
    }
156
}
157

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