    001package net.minecraft.command;
    002
    003import java.util.List;
    004import net.minecraft.server.MinecraftServer;
    005import net.minecraft.world.GameRules;
    006
    007public class CommandGameRule extends CommandBase
    008{
    009    public String getCommandName()
    010    {
    011        return "gamerule";
    012    }
    013
    014    /**
    015     * Return the required permission level for this command.
    016     */
    017    public int getRequiredPermissionLevel()
    018    {
    019        return 2;
    020    }
    021
    022    public String getCommandUsage(ICommandSender par1ICommandSender)
    023    {
    024        return par1ICommandSender.translateString("commands.gamerule.usage", new Object[0]);
    025    }
    026
    027    public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    028    {
    029        String s;
    030
    031        if (par2ArrayOfStr.length == 2)
    032        {
    033            s = par2ArrayOfStr[0];
    034            String s1 = par2ArrayOfStr[1];
    035            GameRules gamerules = this.getGameRules();
    036
    037            if (gamerules.hasRule(s))
    038            {
    039                gamerules.setOrCreateGameRule(s, s1);
    040                notifyAdmins(par1ICommandSender, "commands.gamerule.success", new Object[0]);
    041            }
    042            else
    043            {
    044                notifyAdmins(par1ICommandSender, "commands.gamerule.norule", new Object[] {s});
    045            }
    046        }
    047        else if (par2ArrayOfStr.length == 1)
    048        {
    049            s = par2ArrayOfStr[0];
    050            GameRules gamerules1 = this.getGameRules();
    051
    052            if (gamerules1.hasRule(s))
    053            {
    054                String s2 = gamerules1.getGameRuleStringValue(s);
    055                par1ICommandSender.sendChatToPlayer(s + " = " + s2);
    056            }
    057            else
    058            {
    059                notifyAdmins(par1ICommandSender, "commands.gamerule.norule", new Object[] {s});
    060            }
    061        }
    062        else if (par2ArrayOfStr.length == 0)
    063        {
    064            GameRules gamerules2 = this.getGameRules();
    065            par1ICommandSender.sendChatToPlayer(joinNiceString(gamerules2.getRules()));
    066        }
    067        else
    068        {
    069            throw new WrongUsageException("commands.gamerule.usage", new Object[0]);
    070        }
    071    }
    072
    073    /**
    074     * Adds the strings available in this command to the given list of tab completion options.
    075     */
    076    public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    077    {
    078        return par2ArrayOfStr.length == 1 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, this.getGameRules().getRules()) : (par2ArrayOfStr.length == 2 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, new String[] {"true", "false"}): null);
    079    }
    080
    081    /**
    082     * Return the game rule set this command should be able to manipulate.
    083     */
    084    private GameRules getGameRules()
    085    {
    086        return MinecraftServer.getServer().worldServerForDimension(0).getGameRules();
    087    }
    088}