Moved to Maven

This commit is contained in:
JKetelaar
2015-09-13 22:51:13 +02:00
parent a3bf8d0d0d
commit 52836a3e9a
303 changed files with 56775 additions and 35 deletions
@@ -0,0 +1,17 @@
package org.parabot.environment.api.interfaces;
import java.awt.Graphics;
/**
*
* @author Everel
*
*/
public interface Paintable {
/**
* @param g
*/
public void paint(Graphics g);
}
@@ -0,0 +1,68 @@
package org.parabot.environment.api.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author JKetelaar
*/
public class FileUtil {
public static String getChecksum(File file){
if (file.isFile()) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] getChecksumBytes(File file) {
if (file.isFile()) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] dataBytes = new byte[1024];
int nread;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
}
return md.digest();
}
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
}
return null;
}
}
@@ -0,0 +1,17 @@
package org.parabot.environment.api.utils;
/**
* A simple class to filter things out of an collection
*
* @author Everel
*
* @param <F>
*/
public interface Filter<F> {
/**
* Determines if this object should be accepted
* @param f
* @return <b>true</b> to include this object, otherwise <b>false</b> to exclude.
*/
public boolean accept(F f);
}
@@ -0,0 +1,46 @@
package org.parabot.environment.api.utils;
import java.math.BigInteger;
/**
*
* Helper class for calculating setters for clients that uses multipliers
*
* @author Everel
*
*/
public class Multipliers {
/**
*
* @param multiplier
* the multiplier
* @param set
* the value you want to set
* @return the correct setter value
*/
public static int getIntSetter(int multiplier, int set) {
int bits = 32;
BigInteger quotient = new BigInteger(Integer.toString(multiplier));
BigInteger shift = BigInteger.ONE.shiftLeft(bits);
int value = quotient.modInverse(shift).intValue();
return value * set;
}
/**
*
* @param multiplier
* the multiplier
* @param set
* the value you want to set
* @return the correct setter value
*/
public static long getLongSetter(long multiplier, long set) {
int bits = 64;
BigInteger quotient = new BigInteger(Long.toString(multiplier));
BigInteger shift = BigInteger.ONE.shiftLeft(bits);
long value = quotient.modInverse(shift).longValue();
return value * set;
}
}
@@ -0,0 +1,132 @@
package org.parabot.environment.api.utils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Properties;
/**
* @author JKetelaar
*/
public class PBPreferences {
private static AccountManager manager;
private Properties properties;
private int scriptID;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
PBPreferences.manager = manager;
}
};
public PBPreferences(int scriptID) {
this.scriptID = scriptID;
this.updateSettings();
}
private void updateSettings() {
properties = new Properties();
try {
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/" + scriptID,
"apikey=" + manager.getAccount().getApi())
);
JSONArray resultArray;
if ((resultArray = ((JSONArray) result.get("result"))) != null) {
for(Object rObject : resultArray) {
JSONObject resultObject = (JSONObject) rObject;
for (Object map : resultObject.entrySet()) {
Map.Entry<?, ?> pairs = (Map.Entry<?, ?>) map;
properties.put(pairs.getKey(), pairs.getValue());
}
}
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
/**
* Change a setting
* @param key
* @param value
*/
public void adjustSettings(Object key, Object value) {
this.addSetting(key, value);
}
/**
* Get a setting value
* @param key
* @return
*/
public Object getSetting(Object key){
return this.properties.get(key);
}
/**
* Get a setting value as string
* @param key
* @return
*/
public String getSetting(String key){
return this.properties.getProperty(key);
}
/**
* Remove a setting
* @param key
*/
public void removeSetting(Object key){
try {
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/set/",
"apikey=" + manager.getAccount().getApi() +
"&key=" + URLEncoder.encode(String.valueOf(key), "UTF-8") +
"&script=" + String.valueOf(scriptID)
)
);
if ((boolean)result.get("result")){
this.properties.remove(key);
}
} catch (ParseException | MalformedURLException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* Add a setting
* @param key
* @param value
*/
public void addSetting(Object key, Object value){
try {
JSONObject result = (JSONObject) WebUtil.getJsonParser().parse(
WebUtil.getContents("http://bdn.parabot.org/api/v2/user/preferences/set/",
"apikey=" + manager.getAccount().getApi() +
"&key=" + URLEncoder.encode(String.valueOf(key), "UTF-8") +
"&value=" + URLEncoder.encode(String.valueOf(value), "UTF-8") +
"&script=" + String.valueOf(scriptID)
)
);
if ((boolean)result.get("result")){
this.properties.put(key, value);
}
} catch (ParseException | MalformedURLException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,28 @@
package org.parabot.environment.api.utils;
/**
*
* A random class is used for generating random numbers
*
* @author Everel
*
*/
public class Random {
private final static java.util.Random RANDOM = new java.util.Random();
/**
* Randomizes a number between minimum and maximum
*
* @param min
* @param max
* @return randomized number
*/
public static int between(final int min, final int max) {
try {
return min + (max == min ? 0 : RANDOM.nextInt(max - min));
} catch (Exception e) {
return min + (max - min);
}
}
}
@@ -0,0 +1,30 @@
package org.parabot.environment.api.utils;
/**
*
* @author mkyong
*
*/
public class StringUtils {
public static String convertHexToString(String hex) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for (int i = 0; i < hex.length() - 1; i += 2) {
// grab the hex in pairs
String output = hex.substring(i, (i + 2));
// convert hex to decimal
int decimal = Integer.parseInt(output, 16);
// convert the decimal to character
sb.append((char) decimal);
temp.append(decimal);
}
return sb.toString();
}
}
@@ -0,0 +1,68 @@
package org.parabot.environment.api.utils;
import org.parabot.environment.scripts.framework.SleepCondition;
/**
*
* Holds various Time utilities
*
* @author Everel
*
*/
public final class Time {
/**
* Sleeps for a given amount of time
* @param ms
*/
public static void sleep(final int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
*
* @param minumum
* @param maximum
*/
public static void sleep(final int minumum, final int maximum) {
try {
Thread.sleep(Random.between(minumum, maximum));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Sleeps until the SleepCondition is valid.
*
* @param conn
* the condition.
* @param timeout
* the time in miliseconds before it stops sleeping.
* @return whether it ran successfully without timing out.
*/
public static boolean sleep(SleepCondition conn, int timeout) {
long start = System.currentTimeMillis();
while (!conn.isValid()) {
if (start + timeout < System.currentTimeMillis()) {
return false;
}
Time.sleep(50);
}
return true;
}
/**
* Gets current time in milliseconds
* @return time in ms
*/
public static long get() {
return System.currentTimeMillis();
}
}
@@ -0,0 +1,120 @@
package org.parabot.environment.api.utils;
/**
*
* A simple timer class
*
* @author Everel, Parameter
*
*/
public class Timer {
private long start;
private long end;
/**
* Timer Constructor
*
* @param start
*/
public Timer(long end) {
start = System.currentTimeMillis();
this.end = System.currentTimeMillis() + end;
}
/**
* Timer Constructor
*/
public Timer() {
this(0);
}
/**
* Determines the remaining time left.
*
* @return the remaining time.
*/
public long getRemaining() {
return end - System.currentTimeMillis();
}
/**
* Determines if the end time has been reached, does not mean it stopped
* running.
*/
public boolean isFinished() {
return System.currentTimeMillis() > end;
}
/**
* Stops and resets the timer
*/
public void restart() {
stop();
reset();
}
/**
* Resets the timer if stopped
*/
public void reset() {
if (start == 0) {
start = System.currentTimeMillis();
}
}
/**
* Resets the timer
*/
public void stop() {
end = (end - start) + System.currentTimeMillis();
start = 0;
}
/**
* Determines if timer is running
*
* @return <b>true</b> if timer is running
*/
public boolean isRunning() {
return start != 0;
}
/**
* Gets the run time in long millis.
*
* @return the elapsed time.
*/
public long getElapsedTime() {
return System.currentTimeMillis() - start;
}
/**
* Calculates hourly gains based on given variable
*
* @param gained
* variable
* @return hourly gains
*/
public int getPerHour(final int gained) {
return (int) ((gained) * 3600000D / (System.currentTimeMillis() - start));
}
/**
* Generates string based on HH:MM:SS
*
* @return String
*/
@Override
public String toString() {
StringBuilder b = new StringBuilder();
long elapsed = getElapsedTime();
int second = (int) (elapsed / 1000 % 60);
int minute = (int) (elapsed / 60000 % 60);
int hour = (int) (elapsed / 3600000 % 60);
b.append(hour < 10 ? "0" : "").append(hour).append(":");
b.append(minute < 10 ? "0" : "").append(minute).append(":");
b.append(second < 10 ? "0" : "").append(second);
return new String(b);
}
}
@@ -0,0 +1,311 @@
package org.parabot.environment.api.utils;
import org.json.simple.parser.JSONParser;
import org.parabot.core.io.ProgressListener;
import org.parabot.core.io.SizeInputStream;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
/**
*
* A WebUtil class fetches data from an URL
*
* @author Everel
*
*/
public class WebUtil {
private static JSONParser jsonParser;
private static String agent = "Mozilla/5.0 (Wind0ws NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
/**
* Agent to set at a URL connection
*
* @param userAgent
*/
public static void setUserAgent(final String userAgent) {
agent = userAgent;
}
/**
* Gets useragent
*
* @return useragent
*/
public static String getUserAgent() {
return agent;
}
/**
* Fetches content of a page
*
* @param location
* @return contents of page
* @throws MalformedURLException
*/
public static String getContents(final String location)
throws MalformedURLException {
return getContents(new URL(location));
}
public static String getContents(final String location, String parameters) throws MalformedURLException {
return getContents(new URL(location), parameters);
}
/**
* Get contents from URL
*
* @param url
* @return page contents
*/
public static String getContents(final URL url) {
return getContents(getConnection(url));
}
public static String getContents(final URL url, final String parameters) {
return getContents(getConnection(url), parameters);
}
/**
* Gets contents from URLConnection
*
* @param urlConnection
* @return page contents
*/
public static String getContents(URLConnection urlConnection) {
try {
final BufferedReader in = getReader(urlConnection);
final StringBuilder builder = new StringBuilder();
String line;
if (in != null) {
while ((line = in.readLine()) != null) {
builder.append(line);
}
in.close();
}
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static String getContents(URLConnection urlConnection, String parameters) {
try {
urlConnection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(parameters);
wr.flush();
wr.close();
final BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
final StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
builder.append(line);
}
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
/**
* Gets buffered reader from string url
*
* @param url
* @return bufferedreader
*/
public static BufferedReader getReader(final String url) {
try {
return getReader(new URL(url));
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
/**
* Gets BufferedReader from URL
*
* @param url
* @return BufferedReader from URL
*/
public static BufferedReader getReader(final URL url) {
return getReader(getConnection(url));
}
public static BufferedReader getReader(final URLConnection urlConnection) {
try {
return new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
/**
* Gets inputstream from url
*
* @param url
* @return inputstream from url
*/
public static InputStream getInputStream(final URL url) {
final URLConnection con = getConnection(url);
try {
return con.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Opens a connection
*
* @param url
* @return URLConnection to URL
*/
public static URLConnection getConnection(final URL url) {
try {
final URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", agent);
return con;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static BufferedReader getReader(final URL url, String username, String password) {
try {
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
return new BufferedReader(new InputStreamReader(connection.getInputStream()));
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static URLConnection getConnection(final URL url, String username, String password) {
try {
String data = URLEncoder.encode("username", "UTF-8") + "=" + username;
data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + password;
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
return connection;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
/**
* Downloads a file on the internet
* @param url
* @param destination
* @param listener
*/
public static void downloadFile(final URL url, final File destination,
final ProgressListener listener) {
try {
final URLConnection connection = getConnection(url);
int size = connection.getContentLength();
SizeInputStream sizeInputStream = new SizeInputStream(
connection.getInputStream(), size, listener);
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
FileOutputStream fileOut = new FileOutputStream(destination);
try {
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fileOut.write(data, 0, count);
}
} finally {
if (in != null)
in.close();
if (fileOut != null)
fileOut.close();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Downloads a file on the internet
* @param url
* @param destination
* @param listener
*/
public static void downloadFile(final URL url, final File destination,
final ProgressListener listener, String username, String password) {
try {
final URLConnection connection = getConnection(url, username, password);
int size = connection.getContentLength();
SizeInputStream sizeInputStream = new SizeInputStream(
connection.getInputStream(), size, listener);
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
FileOutputStream fileOut = new FileOutputStream(destination);
try {
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fileOut.write(data, 0, count);
}
} finally {
if (in != null)
in.close();
if (fileOut != null)
fileOut.close();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Converts file format to url format
* @param file
* @return url to file
*/
public static URL toURL(File file) {
try {
return file.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
public static JSONParser getJsonParser() {
if (jsonParser == null){
jsonParser = new JSONParser();
}
return jsonParser;
}
}