me.heyimblake.HiveMCRank.Utils.WebConnector.java Source code

Java tutorial

Introduction

Here is the source code for me.heyimblake.HiveMCRank.Utils.WebConnector.java

Source

package me.heyimblake.HiveMCRank.Utils;

import org.bukkit.Bukkit;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.net.URL;
import java.net.URLConnection;
import java.util.UUID;
import java.util.logging.Level;

/**
 * @author heyimblake
 *         <p>
 *         This file is part of HiveMCRank.
 *         Copyright (C) 2015  heyimblake
 *         <p>
 *         This program is free software; you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation.
 *         <p>
 *         This program is distributed in the hope that it will be useful,
 *         but WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *         GNU General Public License for more details.
 *         <p>
 *         You should have received a copy of the GNU General Public License along
 *         with this program; if not, see http://www.gnu.org/licenses/.
 */

public class WebConnector {

    private static WebConnector ourInstance = new WebConnector();

    private WebConnector() {
    }

    public static WebConnector getInstance() {
        return ourInstance;
    }

    /**
     * Returns the Hive Name of the rank of a supplied UUID. Regular Member is returned if null.
     *
     * @param uuid the UUID of the player
     * @return Hive Name of the rank of the UUID
     * @throws Exception thrown if the UUID has never been on TheHive or TheHive's API is not available. Default Rank is returned.
     */
    public String getHiveRank(final UUID uuid) throws Exception {
        String rankfromapi;
        final String uuidNoDash = uuid.toString().replace("-", "");
        try {
            final String API_URL = "http://api.hivemc.com/v1/player/";
            final URLConnection connection = new URL(API_URL + uuidNoDash).openConnection();
            Bukkit.getServer().getLogger().log(Level.INFO, "Connection opened to " + API_URL + uuidNoDash);
            connection.addRequestProperty("User-Agent", "Mozilla/5.0");
            final JSONTokener tokener = new JSONTokener(connection.getInputStream());
            final JSONObject root = new JSONObject(tokener);
            if (root.getString("rankName") == null) {
                rankfromapi = "Regular Member";
            } else {
                rankfromapi = root.getString("rankName");
            }
        } catch (Exception e) {
            Bukkit.getServer().getLogger().log(Level.SEVERE,
                    "Exception occurred while trying to get the HiveMC rank of " + uuid);
            Bukkit.getServer().getLogger().log(Level.SEVERE,
                    "Chances are that the user has never logged onto TheHive before. Their rank has been set to regular for now.");
            rankfromapi = "Regular Member";
        }
        return rankfromapi;
    }
}