net.chris54721.infinitycubed.data.ForgeVersion.java Source code

Java tutorial

Introduction

Here is the source code for net.chris54721.infinitycubed.data.ForgeVersion.java

Source

/*
 * This file is part of the InfinityCubed Launcher source code.
 * Copyright (C) 2014 InfinityCubed Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.chris54721.infinitycubed.data;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import net.chris54721.infinitycubed.utils.LogHelper;
import net.chris54721.infinitycubed.utils.Reference;
import net.chris54721.infinitycubed.utils.Resources;

import java.io.File;
import java.net.URL;

public class ForgeVersion {

    private String id;
    private String arguments;
    private Library[] libraries;

    public ForgeVersion(String id, String arguments, Library[] libraries) {
        this.id = id;
        this.arguments = arguments;
        this.libraries = libraries;
    }

    public String getId() {
        return id;
    }

    public String getArguments() {
        return arguments;
    }

    public Library[] getLibraries() {
        return libraries;
    }

    public static ForgeVersion fetch(String version) {
        try {
            URL jsonUrl = Resources.getUrl(Resources.ResourceType.VERSION, version + ".json");
            File jsonFile = Resources.getFile(Resources.ResourceType.VERSION, version + ".json");
            Downloadable versionJson = new Downloadable(jsonUrl, jsonFile);
            versionJson.setForce(false);
            if (versionJson.download()) {
                String json = Files.toString(jsonFile, Charsets.UTF_8);
                return Reference.DEFAULT_GSON.fromJson(json, ForgeVersion.class);
            } else
                return null;
        } catch (Exception e) {
            LogHelper.error("Failed fetching JSON for Forge " + version, e);
            return null;
        }
    }

}