fridgegameinstaller.MCJsonConf.java Source code

Java tutorial

Introduction

Here is the source code for fridgegameinstaller.MCJsonConf.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fridgegameinstaller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author leon
 */
public class MCJsonConf {

    public static void getJson(String path, int mb) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://api.fridgegame.com/mcconf/Fridgegame.json");

        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            System.out.println(httpGet.toString());
            System.out.println(response1.getStatusLine());
            BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
            String a;
            String output = "";
            while ((a = br.readLine()) != null) {
                output += a + "\n";
            }
            System.out.println(output);
            try {

                JSONObject json = new JSONObject(output);
                String mcArgs = json.getString("minecraftArguments");
                String regex = "(-Xmx[^ ]*)";
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(mcArgs);
                String newArgs = m.replaceAll("-Xmx" + mb + "M");
                json.put("minecraftArguments", newArgs);
                FileWriter file = new FileWriter(path);

                try {
                    file.write(json.toString());

                } catch (IOException e) {
                    e.printStackTrace();

                } finally {
                    file.flush();
                    file.close();
                }

            } catch (JSONException e) {

            }
        } finally {
            response1.close();
        }

    }

}