Java tutorial
/* * 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 net.chris54721.infinitycubed.utils.Utils; import net.openmcauthenticator.OpenMCAuthenticator; import net.openmcauthenticator.exceptions.AuthenticationUnavailableException; import net.openmcauthenticator.exceptions.RequestException; import net.openmcauthenticator.responses.AuthenticationResponse; import net.openmcauthenticator.responses.RefreshResponse; import org.apache.commons.io.IOUtils; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.Serializable; import java.net.URL; import java.util.LinkedHashMap; import java.util.Map; import java.util.UUID; public class Account implements Serializable { private UUID uuid; private String username; private String nickname; private Image skin; private Image skinHead; private boolean authenticated; private Exception authException; private String accessToken; private String clientToken; public Account(String username, String password) { this.username = username; this.authenticate(password); } public Account(String uuid, String username, String nickname, String accessToken, String clientToken) { this.uuid = UUID.fromString(uuid); this.username = username; this.nickname = nickname; this.accessToken = accessToken; this.clientToken = clientToken; } public UUID getUUID() { return uuid; } public String getUsername() { return username; } public String getNickname() { return nickname; } public Image getSkin() { return skin; } public Image getSkinHead() { return skinHead; } public boolean isAuthenticated() { return authenticated; } public Exception getAuthException() { return authException; } public String getAccessToken() { return accessToken; } public String getClientToken() { return clientToken; } private boolean authenticate(String password) { try { AuthenticationResponse authResponse = OpenMCAuthenticator.authenticate(getUsername(), password); this.accessToken = authResponse.getAccessToken(); this.clientToken = authResponse.getClientToken(); this.uuid = authResponse.getSelectedProfile().getUUID(); this.nickname = authResponse.getSelectedProfile().getName(); this.authenticated = true; this.store(); this.fetchSkin(true); return true; } catch (RequestException e) { this.authenticated = false; this.authException = e; LogHelper.error("Couldn't login with username and password", e); return false; } catch (AuthenticationUnavailableException x) { this.authenticated = false; this.authException = x; LogHelper.error("Couldn't login with username and password", x); return false; } } private boolean refresh() { try { RefreshResponse refreshResponse = OpenMCAuthenticator.refresh(accessToken, clientToken); this.accessToken = refreshResponse.getAccessToken(); this.clientToken = refreshResponse.getClientToken(); this.uuid = refreshResponse.getSelectedProfile().getUUID(); this.authenticated = true; this.store(); this.fetchSkin(true); return true; } catch (RequestException e) { LogHelper.error("Couldn't refresh access token", e); this.authenticated = false; this.authException = e; return false; } catch (AuthenticationUnavailableException x) { LogHelper.error("Couldn't refresh access token", x); this.authenticated = false; this.authException = x; return false; } } private boolean validateToken() { try { OpenMCAuthenticator.validate(accessToken, clientToken); this.authenticated = true; return true; } catch (RequestException e) { LogHelper.error("Couldn't validate access token", e); this.authenticated = false; this.authException = e; return false; } catch (AuthenticationUnavailableException x) { LogHelper.error("Couldn't validate access token", x); this.authenticated = false; this.authException = x; return false; } } public void fetchSkin(boolean update) { if (nickname != null) { LogHelper.info("Fetching skin for player " + getNickname()); BufferedImage skin = Utils.toBufferedImage(Utils.getImageResource("steve")); FileInputStream skinStream = null; boolean fetch = true; try { File skinFile = Resources.getFile(Resources.ResourceType.SKIN, getNickname() + ".png"); if (update) { URL skinURL = Resources.getUrl(Resources.ResourceType.SKIN, getNickname() + ".png"); Downloadable skinDownloadable = new Downloadable(skinURL, skinFile); if (!skinDownloadable.download()) fetch = false; } else if (!skinFile.isFile()) fetch = false; if (fetch) { skinStream = new FileInputStream(skinFile); skin = ImageIO.read(skinStream); BufferedImage head = skin.getSubimage(8, 8, 8, 8); BufferedImage mask = skin.getSubimage(40, 8, 8, 8); skin = new BufferedImage(8, 8, BufferedImage.TYPE_INT_ARGB); Graphics resultG = skin.getGraphics(); resultG.drawImage(head, 0, 0, null); resultG.drawImage(mask, 0, 0, null); } this.skin = skin; this.skinHead = skin.getScaledInstance(32, 32, Image.SCALE_DEFAULT); } catch (Exception e) { LogHelper.error("Couldn't fetch player skin", e); } finally { if (skinStream != null) IOUtils.closeQuietly(skinStream); } } } public void store() { try { File file = new File(Reference.ACCOUNTS_FOLDER, nickname + ".json"); if (file.exists()) file.delete(); Map<String, Object> data = new LinkedHashMap<String, Object>(); data.put("uuid", getUUID()); data.put("username", getUsername()); data.put("nickname", getNickname()); data.put("accessToken", getAccessToken()); data.put("clientToken", getClientToken()); String json = Reference.DEFAULT_GSON.toJson(data); Files.write(json, file, Charsets.UTF_8); } catch (Exception e) { LogHelper.error("Couldn't store profile data", e); } } }