Java tutorial
package net.chunkyhosting.Roe.ChunkyTransactions.api; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.logging.Level; import org.json.JSONArray; import org.json.JSONObject; import net.chunkyhosting.Roe.ChunkyTransactions.ChunkyTransactions; import net.chunkyhosting.Roe.ChunkyTransactions.api.requests.internal.IPv4Request; import net.chunkyhosting.Roe.ChunkyTransactions.api.requests.internal.LicenseRequest; import net.chunkyhosting.Roe.ChunkyTransactions.exceptions.*; public class License { private String key; private LicenseType type; private String firstName; private String lastName; private String email; private String ign; private String validIP; private int validPort; private String path; private boolean valid; private String expire; private String mac; public License(String key) { try { this.setKey(key); ChunkyTransactions.getInstance().getServer().getScheduler().scheduleSyncRepeatingTask( ChunkyTransactions.getInstance(), new LicenseThread(), 5 * 20L, 5 * 20L); } catch (InvalidLicenseKeyException e) { ChunkyTransactions.getInstance().getMessage().spitError(e); } } public boolean validateLicense() { try { JSONObject response; if (ChunkyTransactions.getInstance().getServer().getIp().equalsIgnoreCase("")) { response = new IPv4Request().perform(); ChunkyTransactions.getInstance().getAPI().setPublicIP(response.getString("IP").trim()); } else { ChunkyTransactions.getInstance().getAPI() .setPublicIP(ChunkyTransactions.getInstance().getServer().getIp()); } response = new LicenseRequest().perform(); this.setFirstName(response.getString("firstName")); this.setLastName(response.getString("lastName")); this.setIgn(response.getString("ign")); this.setEmail(response.getString("email")); try { JSONObject serverData = this.getServer(response.getJSONArray("servers")); try { if (serverData.getBoolean("reissue")) { this.reissue(); return true; } if (!this.checkServerIP(serverData.getString("ip"))) { return false; } if (!this.checkServerPort(serverData.getInt("port"))) { return false; } if (!this.checkPluginLocation(serverData.getString("valid"))) { return false; } if (!this.checkType(serverData.getString("type"))) { return false; } if (!this.checkMac(serverData.getString("mac"))) { return false; } this.setExpire(serverData.getString("expire")); return true; } catch (InvalidServerException e) { ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } } catch (InvalidAPIKeyException e) { ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } } catch (NullRequestException e) { ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } } public boolean checkServerIP(String ip) throws InvalidServerException { if (ChunkyTransactions.getInstance().getAPI().getPublicIP().equalsIgnoreCase(ip)) { this.setValidIP(ip); return true; } throw new InvalidServerException(); } public boolean checkServerPort(int port) throws InvalidServerException { if (ChunkyTransactions.getInstance().getServer().getPort() == port) { this.setValidPort(port); return true; } throw new InvalidServerException(); } public boolean checkPluginLocation(String licenseLocation) throws InvalidServerException { String pluginLocation = ChunkyTransactions.class.getProtectionDomain().getCodeSource().getLocation() .getPath(); if (pluginLocation.equalsIgnoreCase(licenseLocation.replaceAll("\\/", "/"))) { this.setPath(licenseLocation.replaceAll("\\/", "/")); return true; } throw new InvalidServerException(); } public JSONObject getServer(JSONArray array) throws InvalidAPIKeyException { for (int i = 0; i < array.length(); i++) { JSONObject serverData = array.getJSONObject(i); if (serverData.getString("API") .equalsIgnoreCase(ChunkyTransactions.getInstance().getAPI().getAPIKey())) { return serverData; } } throw new InvalidAPIKeyException(); } public boolean checkType(String type) { this.setType(LicenseType.valueOf(type)); switch (this.getType()) { case FREE: ChunkyTransactions.getInstance().getMessage().log("You're running with a free license"); return true; case LEASED: ChunkyTransactions.getInstance().getMessage().log("You're running with a leased license!"); return true; case LEASED_MINECRAFT: ChunkyTransactions.getInstance().getMessage() .log("You're running with a free license from ChunkyHosting.Net"); return true; case LEASED_NO_BRANDING: ChunkyTransactions.getInstance().getMessage() .log("You're running with a leased license without branding"); return true; case OWNED: ChunkyTransactions.getInstance().getMessage().log("You're running with a owned license!"); return true; case OWNED_NO_BRANDING: ChunkyTransactions.getInstance().getMessage() .log("You're running with a owned license without branding!"); return true; case EXPIRED: ChunkyTransactions.getInstance().getMessage().log(Level.SEVERE, "Your license is expired! Please renew it or get a new license!"); return false; case SUSPENDED: ChunkyTransactions.getInstance().getMessage().log(Level.SEVERE, "Your license have been suspended! Please get a new license!"); return false; } return false; } public boolean checkMac(String mac) { StringBuilder sb = new StringBuilder(); try { NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); byte[] macAddr = network.getHardwareAddress(); for (int i = 0; i < macAddr.length; i++) { sb.append(String.format("%02X%s", macAddr[i], (i < macAddr.length - 1) ? "-" : "")); } ChunkyTransactions.getInstance().getMessage().debug("Your Mac Address: " + sb.toString()); this.setMac(sb.toString()); if (sb.toString().equalsIgnoreCase(mac)) { return true; } } catch (SocketException e) { ChunkyTransactions.getInstance().getMessage().log(Level.SEVERE, "Unable to check your Mac Address"); ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } catch (UnknownHostException e) { ChunkyTransactions.getInstance().getMessage().log(Level.SEVERE, "Unable to check your Mac Address"); ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } return false; } public boolean reissue() { return true; } public String getKey() { return this.key; } private void setKey(String key) throws InvalidLicenseKeyException { if (key.length() != 67) { throw new InvalidLicenseKeyException(); } this.key = key; } public LicenseType getType() { return this.type; } private final void setType(LicenseType type) { this.type = type; } public String getFirstName() { return this.firstName; } private final void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } private final void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getIgn() { return ign; } public void setIgn(String ign) { this.ign = ign; } public String getValidIP() { return this.validIP; } private final void setValidIP(String validIP) { this.validIP = validIP; } public int getValidPort() { return validPort; } private final void setValidPort(int validPort) { this.validPort = validPort; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getExpire() { return expire; } private final void setExpire(String expire) { this.expire = expire; } public boolean isValid() { return valid; } public void setValid(boolean valid) { this.valid = valid; } public String getMac() { return mac; } public void setMac(String mac) { this.mac = mac; } }